我正在使用express.js
,web3.js v1.0.0-beta.34
和nodeJS v9.11.2
在smartcontract
Kovan
上执行testnet
。在Ropsten上,另一个smartcontract
也可以使用相同的方法。这是我收到的错误:
交易执行错误。在Object.ErrorResponse (... / node_modules / web3-core-helpers / src / errors.js:29:16)在 ... / node_modules / web3-core-requestmanager / src / index.js:140:36 at XMLHttpRequest.request.onreadystatechange (... / node_modules / web3-providers-http / src / index.js:77:13)在 XMLHttpRequestEventTarget.dispatchEvent (... / node_modules / xhr2 / lib / xhr2.js:64:18)在 XMLHttpRequest._setReadyState (... / node_modules / xhr2 / lib / xhr2.js:354:12)在 XMLHttpRequest._onHttpResponseEnd (... / node_modules / xhr2 / lib / xhr2.js:509:12)在 传入消息。 (... / node_modules / xhr2 / lib / xhr2.js:469:24) 在endReadableNT的IncomingMessage.emit(events.js:185:15) (_stream_visible.js:1106:12)在process._tickCallback (internal / process / next_tick.js:178:19)
这是我的节点代码。我使用一个GET
请求来执行它:
const Tx = require('ethereumjs-tx');
var Web3 = require('web3');
const express = require('express');
const app = express();
app.use(express.json());
var isStatus;
app.get('/api/updateStatus', function(req, res) {
isStatus = req.query.isStatus;
if (isStatus) {
updateStatus(isStatus, function(err, data) {
if (!err) {
res.send(data);
}
else {
res.send(err);
}});
}
else {
res.send('ERROR: Please use all query variables!');
}
});
function updateStatus(_isStatus, callback) {
var web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/api_key'));
const contractAddress = '0xd30b60A2f9...';
var contractABI = new web3.eth.Contract(
[...abi...],
contractAddress);
web3.eth.defaultAccount = "0x002D189c2...";
const account = '0x002D189c25...';
const privateKey = Buffer.from('240462d5665...', 'hex');
const contractFunction = contractABI.methods.updateStatus(Number(_isStatus));
const functionAbi = contractFunction.encodeABI();
let estimatedGas;
let nonce;
contractFunction.estimateGas(function(error, gasAmount) {
if(!error) {
console.log('Estimated Gas : ' + gasAmount);
estimatedGas = gasAmount + 10000;
console.log('New Gas: ' + estimatedGas);
web3.eth.getTransactionCount(account).then(_nonce => {
nonce = _nonce.toString(16);
console.log("Nonce: " + nonce);
const txParams = {
gasPrice: estimatedGas,
gasLimit: 5000000,
to: contractAddress,
data: functionAbi,
from: account,
nonce: '0x' + nonce
};
const tx = new Tx(txParams);
tx.sign(privateKey);
const serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', receipt => {
callback(receipt);
});
});
}
else {
callback(error);
}
});
}
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
如果我在remix
内或在Parity
上使用Kovan
来执行智能合约,它本身就会起作用。 function
看起来像这样:
function updateStatus(uint _isStatus) public onlyOwner{
emit LogStatusUpdate(msg.sender, block.number, _isStatus);
if (_isStatus <= box.mynumber) {
uint getMore = box.maxStatus - _isStatus;
uint total = getMore * box.pricePerLitre;
createOrder(getMore, totalUSD);
}