通过web3将区块链交易部署到Kovan testnet需要的资金远远超过使用web3的实际资金

问题描述 投票:1回答:1

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

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

我拥有的资金是4.7 ETH,所以不仅仅需要交易。因此,我从Kovan Faucet获得了更多以太,并再次推动了这项交易,事实证明它只需要0.0160552以太。我有点困惑,这个人工要求来自哪里,因为gasPrice和gasLimit都要小得多。这个问题现在通过比5.5 ETH更高的平衡来解决,但我想知道在迁移到mainnet之前消除它的原因。我在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)
                        }
                    });
                }

            });
        }

    })
}
node.js blockchain web3
1个回答
0
投票

我发现降低gasLimit使得对资金的要求变小,但另一个问题出现了,为什么KOVAN testnet在这方面的表现与ROPSTEN不同?

© www.soinside.com 2019 - 2024. All rights reserved.