通过web3将区块链交易部署到Kovan testnet所需的资金比使用web3实际需要的资金多得多

时间:2018-09-12 13:57:23

标签: node.js blockchain web3

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

  

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

我的资金是4.7 ETH,所以远远超出了交易所需。因此,我从Kovan Faucet那里获得了更多的以太币,并再次推动了该交易,结果证明它只需要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)
                        }
                    });
                }

            });
        }

    })
}

1 个答案:

答案 0 :(得分:0)

我发现降低gasLimit会使资金需求变小,但另一个问题出现了,为什么KOVAN测试网在这方面与ROPSTEN有所不同?