在将此问题标记为副本之前,请注意,我没有使用松露:)
所以问题是,每次我向我的智能合同发送交易时,我都会从标题中得到错误。
const express = require('express');
const router = express.Router();
const EthUtil = require('ethereumjs-util');
const wallet = require('ethereumjs-wallet');
const Tx = require('ethereumjs-tx');
const Web3 = require('web3');
const testNetWS = "ws://...:";
const web3 = new Web3(new Web3.providers.WebsocketProvider(testNetWS));
const parameters = require('./parameters.js');
const myContract = new web3.eth.Contract(parameters.getSCABI(), parameters.getSCAddress());
const nonceCounterMap = new Map();
const account = "0x9cc01300194131b04cf297d9a0ebbef0ae011241";
const privateKey = "0x...";
router.get('/sendTheSmartContract', async function(req,res,next){
// nonce
var nonce = await getNewMaxNonce(account);
const nonceHex = web3.utils.toHex(nonce);
// gasLimit
const gasLimit = "0x59a5380"; //"0xE0000000"; // from genesis
// Gas price
var gasPriceInWei;
await web3.eth.getGasPrice()
.then( function(value){
gasPriceInWei = value;
}
);
var gasPriceInWeiHex = web3.utils.toHex(gasPriceInWei);
// Tw-x
var dataTx = myContract.methods.saveString('Test').encodeABI();
var rawTx = {
"nonce": nonceHex,
"gasPrice": 0,
"gasLimit": gasLimit,
"to": parameters.getSCAddress(),
"data": dataTx,
"value": '0x01',
"chainId": 71242
}
var tx = new Tx(rawTx);
tx.sign( EthUtil.toBuffer(privateKey) );
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('transactionHash', async function(hash){ console.log("Transaction hash step , DONE : " + hash); })
.on('confirmation', async function(conf){ console.log("Confirmation step , DONE : " + JSON.stringify(conf)); })
.on('receipt', async function(result){ console.log("Receipt step , DONE : " + JSON.stringify(result)); })
.on('error', async function(err){ console.log("Sending signed transaction operation , STATUS : FAILED !!");console.error(err); });
});
});我正在使用NodeJS & ExpressJS。下面列出了我从上面的代码片段导入的版本:
"ethereumjs-tx": "^1.3.7",
"ethereumjs-util": "^6.1.0",
"ethereumjs-wallet": "^0.6.3",
"express": "~4.16.1",
"morgan": "~1.9.1",如果我设置:
"gasPrice": gasPriceInWeiHex我会发现另一个错误:
{"code":-32000,"message":"Gas price not 0"}无论如何,当我发送这个事务时,它被记录在仲裁块链仪表板上,我可以看到事务,但不知何故,在"transactionHash“事件(例如:.on('transactionHash', )之后,我得到了:
Error: Number can only safely store up to 53 bits我想是来自rawTx对象,但我不知道我错过了什么.我发现Quorum正在计算以纳秒为单位的块时间戳,但我不知道如何检查这是原因,还是可能导致它。
你认为如何?
发布于 2019-08-13 14:38:44
问题不是代码,而是web3版本,在我更新到web3 2.0.0 Alpha 1之后
sudo npm install web3@2.0.0-alpha.1一切正常。
PS :旧的web3版本是: web3 1.0.0 beta 52
发布于 2021-09-20 19:23:14
强烈建议使用3.x
因此,我通过升级到3.x来解决这个问题。
npm i web3@3.0.0-rc.5
https://stackoverflow.com/questions/57393050
复制相似问题