Web3js

web3 - EVM 已恢復合約部署

  • June 12, 2019

我想部署一個合約,但我的交易被還原了。這僅適用於 Web3,而適用於 Remix。
我可以保證 ABI 和字節碼是正確的,因為我從 Remix 粘貼了它們,我還調試了事務並且它不會因為錯誤的require語句而被拒絕。由於使用 Web3 時出現問題,我想知道我是否對程式碼做錯了什麼,但我遵循了文件,所以應該沒問題。

web3.eth.personal.unlockAccount("0x57...8c6b", "myPassword", 10000000).then(function() {
   let abi = [...]; // Pasted from Remix
   let bytecode = "0x..."; // Pasted from Remix
   let sender = "0x577...8c6b";

   let newContract = new web3.eth.Contract(abi);
   newContract.options.data = bytecode;

   newContract.deploy({
       args: ""
   }).send({
       from: sender,
       gas: 3000000, 
       gasPrice: '30000000000000'
   }).on('error', (error) => {
       console.log("Error: ", error);
   }).on('transactionHash', (transactionHash) => {
       console.log("TxHash: ", transactionHash);
   }).on('receipt', (receipt) => {
      console.log("Address: ", receipt.contractAddress)
   }).then((newContractInstance) => {
       console.log(newContractInstance);   
   }).catch(function(error) {
       console.log(error);
   });
});

我也嘗試忽略args參數,deploy但結果是一樣的:

Error: Error: Transaction has been reverted by the EVM:
{
 "blockHash": "0x0038dfdb8cfd6504d247c9463ad13a43418c15235c625663be4cffee52fe5d61",
 "blockNumber": 387165,
 "contractAddress": "0xf6c8bA6fe06d7F1266bE90A2618faa9E172c0ee6",
 "cumulativeGasUsed": 3000000,
 "from": "0x577ed03fb404fab176c5ca39543def8fd3308c6b",
 "gasUsed": 3000000,
 "logs": [],
 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
 "status": false,
 "to": null,
 "transactionHash": "0x4e6c1ebb28770bd702d37f41b442a05b66beba3653a8f1c237e6e18be42b707c",
 "transactionIndex": 0
}

通過調試交易,我可以看到它在2097504剩餘氣體時失敗,而 Web3 的輸出表明交易使用了所有氣體。我想了解的是我是否對 Web3 做錯了什麼,因為考慮到我可以通過 Remix 部署它,程式碼應該沒問題。

注意:我使用的是私有鏈。

擺脫這個:

newContract.options.data = bytecode;

並改變這個:

newContract.deploy({args: ""})

對此:

newContract.deploy({data: bytecode, arguments: []})

引用自:https://ethereum.stackexchange.com/questions/71761