Contract-Development
如何估算呼叫智能合約方法的成本?
在乙太坊測試網上成功部署此智能合約後
https://testnet.etherscan.io/address/0x27c042342C9ba937214117e11A4970A6145034cB
是否可以計算呼叫智能合約的給定方法需要多少氣體?
閱讀如何在沒有任何輸入參數的情況下估計函式的氣體?以及使用契約的費用是多少?估計Gas的限制是什麼?它的估計什麼時候會嚴重錯誤?沒有像預期的那樣幫助我…
這是在 geth 控制台命令行上實例化智能合約的方法:
// creation of contract object var aContract = web3.eth.contract([ { "constant": true, "inputs": [], "name": "queryNumEscrows", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "type": "function" }, { "constant": true, "inputs": [ { "name": "escrowId", "type": "uint256" } ], "name": "queryStatus", "outputs": [ { "name": "status", "type": "uint256", "value": "0" } ], "type": "function" }, { "constant": false, "inputs": [ { "name": "seller", "type": "address" }, { "name": "thirdParty", "type": "address" } ], "name": "start1", "outputs": [ { "name": "escrowId", "type": "uint256" } ], "type": "function" }, { "constant": false, "inputs": [ { "name": "escrowId", "type": "uint256" } ], "name": "revoke", "outputs": [ { "name": "amount", "type": "uint256" } ], "type": "function" }, { "constant": false, "inputs": [ { "name": "escrowId", "type": "uint256" } ], "name": "release", "outputs": [ { "name": "amount", "type": "uint256" } ], "type": "function" }, { "constant": false, "inputs": [ { "name": "seller", "type": "address" }, { "name": "thirdParty", "type": "address" } ], "name": "start", "outputs": [ { "name": "", "type": "uint256" } ], "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "type": "function" } ]); // initiate contract for an address var sc = aContract.at('0x27c042342C9ba937214117e11A4970A6145034cB'); // view code eth.getCode(sc.address);
現在,我想將此交易發送到智能合約,但我想知道它需要多少 gas:
sc.start.sendTransaction("0x90e8682b63d7922a3e942d4bbd4c88095634a17b", "0x47978a69f410d0f61850c92acdb0d4c464d70937", {from:"0x3b877e80b5c0b29d88f3768ed4292b35fdd93a9d", value:"0x3b9aca00"});
為避免此消息:
"Warning! Error encountered during contract execution [Out of gas]".
我想應該使用以下 json-rpc 方法:
https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_estimategas
我在文件中讀到:
When calling SCs => data: DATA - (optional) Hash of the method signature and encoded parameters.
但是不知道如何在數據欄位中編碼方法名稱、參數和 ABI。任何的想法?
或者有沒有其他方法可以解決這個問題?
謝謝。
文件在細節上很簡陋,我花了很長時間才在您的連結的幫助下找出正確的方法,無論如何,這對我有用:
在 Geth 上測試:1.6.7 -stable、web3.js:0.20.2 Parity/v1.7.2 和 Kovan 測試網。
我將使用最小令牌作為範例:
pragma solidity ^0.4.0; contract minimalToken { mapping (address => uint256) public balanceOf; function minimalToken(uint256 initialSupply) { balanceOf[msg.sender] = initialSupply; } function transfer(address _to, uint256 _value) { require(balanceOf[msg.sender] >= _value); require(balanceOf[_to] + _value >= balanceOf[_to]); balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; } }
我將重點介紹使用 2 個參數(地址、金額)進行傳輸的傳輸方法:
var contract = web3.eth.contract(contractABI).at("0x8caaa1f263ff14d0276ff1a1a6ed15c51159d6e0"); var receiverAddress = '0x00Ce6C92856A657979E7728005DBc9acD002Eb09'; var callData = contract.transfer.getData(receiverAddress, 2000); var gasEstimate = web3.eth.estimateGas({ from: web3.eth.coinbase, to: "0x8caaa1f263ff14d0276ff1a1a6ed15c51159d6e0", data: callData }); var gasPrice = web3.eth.gasPrice; console.log('gas Price: ' + gasPrice); console.log('Estimated Transaction gas: ' + gasEstimate); // gas Price: 21000000000 // Estimated Transaction gas: 34207
讓我失望的一件事是,由於我不包括交易中的**from:**欄位,我不斷收到交易錯誤。
希望這可以幫助。