Solidity
基本費用超過氣體限制,使用編碼建構子部署時出錯
我有以下簡單的小契約
pragma solidity 0.4.24; contract MyContract { uint256 public totalSupply ; mapping( address => uint256) public balances ; address public owner; constructor(address _wallet) public payable { totalSupply = 6; owner = _wallet; } function () external payable{ buyToken(); } function buyToken() public payable { require(totalSupply >= (msg.value/1000000000000000000)*2); balances[msg.sender] += (msg.value/1000000000000000000)*2; totalSupply -=(msg.value/1000000000000000000)*2; } function getTotalSupply()public view returns (uint256 ){ return totalSupply; } function setTotalSupply(uint256 newSupply) public { require(msg.sender==owner); totalSupply = newSupply; } }
我想使用它的 abi、bin(從 solc 編譯器版本 0.4.24 生成的 json 文件中提取)和編碼的建構子參數來部署上述合約。它給出了以下錯誤;
base fee exceeds gas limit
這是我的程式碼:
async function dep(0) { var params; const contract = new web3.eth.Contract(myAbi[0]); if(constructorParams[0]=='') { params = '0x' + myBin[0] } else { params = '0x' + myBin[0]+constructorParams[0] } try { await web3.eth.sendTransaction( {from:account1, data: params }, function(err, receipt) {console.log(receipt)}); } catch (error) { console.log(" ERROR !", error); } }
您的查詢可能需要的一些值;
我正在使用 Nodejs、Javascript 和 Ganache GUI,gaslimit:100000000,Gas 價格:20000000000。這是一些值
constructorParams[0] : 0000000000000000000000000000000000000000000000000000000000000000 myAbi[0] : [ { constant: true, inputs: [], name: 'totalSupply', outputs: [ [Object] ], payable: false, stateMutability: 'view', type: 'function', signature: '0x18160ddd' }, { constant: true, inputs: [ [Object] ], name: 'balances', outputs: [ [Object] ], payable: false, stateMutability: 'view', type: 'function', signature: '0x27e235e3' }, { constant: true, inputs: [], name: 'owner', outputs: [ [Object] ], payable: false, stateMutability: 'view', type: 'function', signature: '0x8da5cb5b' }, { constant: false, inputs: [], name: 'buyToken', outputs: [], payable: true, stateMutability: 'payable', type: 'function', signature: '0xa4821719' }, { constant: true, inputs: [], name: 'getTotalSupply', outputs: [ [Object] ], payable: false, stateMutability: 'view', type: 'function', signature: '0xc4e41b22' }, { constant: false, inputs: [ [Object] ], name: 'setTotalSupply', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function', signature: '0xf7ea7a3d' }, { inputs: [ [Object] ], payable: true, stateMutability: 'payable', type: 'constructor', constant: undefined }, { payable: true, stateMutability: 'payable', type: 'fallback', constant: undefined } ] myBin: 60806040526040516020806103cc83398101806040528101908080519060200190929190505050600660008190555080600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505061034d8061007f6000396000f300608060405260043610610078576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806318160ddd1461008257806327e235e3146100ad5780638da5cb5b14610104578063a48217191461015b578063c4e41b2214610165578063f7ea7a3d14610190575b6100806101bd565b005b34801561008e57600080fd5b5061009761026e565b6040518082815260200191505060405180910390f35b3480156100b957600080fd5b506100ee600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610274565b6040518082815260200191505060405180910390f35b34801561011057600080fd5b5061011961028c565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6101636101bd565b005b34801561017157600080fd5b5061017a6102b2565b6040518082815260200191505060405180910390f35b34801561019c57600080fd5b506101bb600480360381019080803590602001909291905050506102bb565b005b6002670de0b6b3a7640000348115156101d257fe5b0402600054101515156101e457600080fd5b6002670de0b6b3a7640000348115156101f957fe5b0402600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055506002670de0b6b3a76400003481151561025c57fe5b04026000808282540392505081905550565b60005481565b60016020528060005260406000206000915090505481565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054905090565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561031757600080fd5b80600081905550505600a165627a7a7230582013a524fedd05746ca684e0f9782f25f213e4ddceb23e1d60bd69d17a076fefea0029
我可以通過以下方式部署您的契約:
async function send(web3, privateKey, params) const options = { data: params, gas: 2000000, }; const signed = await web3.eth.accounts.signTransaction(options, privateKey); const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); console.log(receipt.contractAddress); }
0xc40757e5E9FAaa9D3c7eEAd99d123F8042629B49
如果您有興趣,它部署在Ropsten 上。