我试图获得通过kovan网络部署的erc20令牌合同的估计气体,如下所示:
var getCode = web3.eth.getCode(tokenAddress)
var transactionObject = {
from: address,
to: tokenAddress,
value: web3.utils.toWei(data.value.toString()),
data: getCode
}
web3.eth.estimateGas(transactionObject)
答复:
Unhandled rejection Error: Returned error: Transaction execution error. at Object.ErrorResponse (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-core-helpers/src/errors.js:29:16) at /Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-core-requestmanager/src/index.js:137:36 at XMLHttpRequest.request.onreadystatechange (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/src/index.js:64:13) at XMLHttpRequestEventTarget.dispatchEvent (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:64:18) at XMLHttpRequest._setReadyState (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:354:12) at XMLHttpRequest._onHttpResponseEnd (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:509:12) at IncomingMessage.<anonymous> (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:469:24) at emitNone (events.js:110:20) at IncomingMessage.emit (events.js:207:7) at endReadableNT (_stream_readable.js:1059:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickDomainCallback (internal/process/next_tick.js:218:9)
发布于 2017-12-15 09:41:56
您的data
几乎肯定是无效的。您正在调用getCode
,这将为您提供契约的字节码,然后在您的事务中发送代码。estimateGas
需要一个有效的事务,因为它实质上运行事务(不向网络的其他部分提交事务)来度量所使用的气体。
如果您有合同的ABI,并且试图估计特定方法调用的气体使用量,您可以这样做:
contract.methods.myMethod(...).estimateGas(function (error, gasAmount) {
...
});
见https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-estimategas。
https://ethereum.stackexchange.com/questions/33486
复制相似问题