通过 web3 将区块链交易部署到 Kovan 测试网需要比实际使用 web3 更多的资金



我最近将我的智能合约从Ropsten(仅更改Infura节点(迁移到Kovan,我遇到的第一件事是错误:

部署事务出错 错误:返回的错误:不足 基金。您尝试发送交易的帐户没有 足够的资金。需要 559650000000000000000 并得到: 4747259100000000000。

我拥有的资金是 4.7 ETH,所以远远超过所需的交易。所以我从Kovan Waterucet获得了更多的以太币,并再次推动交易,结果它只需要0.0160552以太币。我有点困惑,这种人为的要求从何而来,因为gasPrice和gasLimit都小得多。现在,通过具有高于 5.5 ETH 的余额,该问题已半解决,但我想知道在迁移到主网之前消除它的原因。我在 NodeJS 中用于部署事务的代码如下所示:

function deploying_transaction(event, callback){
console.log("Data raw", event.dataContractCallRaw)
web3.eth.getGasPrice(function(err,gasPriceWei){
if (err){
console.log("Error by getting Gas price", err)
callback(err)
}else {
console.log("gasPrice", gasPriceWei)
web3.eth.getBlock("latest", function(err,block){
if(err){
console.log("Error by getting gas limit", err)
callback(err)
} else {
console.log("Block Gas Limit", block.gasLimit)
web3.eth.getTransactionCount(event.addressSender,function(err,result){
if (!err){
var rawTx = {
nonce: web3.utils.toHex(result),
to: event.addressContract,
gasPrice: web3.utils.toHex(web3.utils.toWei('700','gwei')), // gasPriceWei in the future we can use gasPrice wei, but it is fucked up for now
gasLimit: web3.utils.toHex(block.gasLimit - 5000), 
value: 0,
data: event.dataContractCallRaw
}
console.log("rawTx", rawTx)
web3.eth.accounts.signTransaction(rawTx, event.privateKeySigner).then(signed => {
web3.eth.sendSignedTransaction(signed.rawTransaction, function(err, response, receipt){
if(err){
callback(err)
} else {
console.log("Response from network", response)
callback(null,response)
}
})
});
}else{
console.log('Error in getting transaction count ' + JSON.stringify(err));
callback(err)
}
});
}
});
}
})
}

我发现降低gasLimit会使对资金的需求更小。

最新更新