Solidity

乙太坊智能合約中的函式呼叫錯誤

  • April 24, 2020

我正在嘗試呼叫乙太坊智能合約功能,但它給了我錯誤。它在 remix JVM 環境中執行良好,但在 web3 提供程序中拋出錯誤。我只是想呼叫一個函式,但它向我顯示了不同的合約地址,就好像它創建了新合約一樣。契約有問題嗎?

錯誤 :-

"Transaction has been reverted by the EVM:
{
 "blockHash": "0xac0800a862fb3f6864f2fadc354baa47b11279095ce1c1e130af5176c97140df",
 "blockNumber": 6369858,
 "contractAddress": "0xb7fcf989EDB79DAd87990DF85412DF116AC7406E",
 "cumulativeGasUsed": 2100000,
 "from": "0xf61343ef22bbccc7221dcda85c5a69219ea00c2b",
 "gasUsed": 2100000,
 "logs": [],
 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
 "status": false,
 "to": null,
 "transactionHash": "0xab70d64929785cb3e718c78238575c3d21e9f4d4f5e481d26ca4069f269ae9d9",
 "transactionIndex": 0
}"

web3 程式碼:-

const NODE_ADDRESS = config.web3Provider;
   const PRIVATE_KEY = reqdata.signerPriKey;

   const sourceCode = fs.readFileSync('path_to_contract', 'utf8').toString();
   const compiledCode = compiler.compile(sourceCode, 1).contracts[':PropertyContract']
   const abi = JSON.parse(compiledCode.interface);

   async function send(web3, account, transaction) {
       while (true) {
           try {
               const options = {
                   data: transaction.encodeABI(),
                   //gas: await transaction.estimateGas({ from: account.address }),
                   gas: 2100000,
                   gasPrice: 10000000000,
               };
               const signed = await web3.eth.accounts.signTransaction(options, account.privateKey);
               const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
               return receipt;
           }
           catch (error) {
               return error
           }
       }
   }

   async function run() {
       try {
           const web3 = new Web3(NODE_ADDRESS);
           const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
           const contract = new web3.eth.Contract(abi, reqdata.contractAddr);
           const transaction = contract.methods.signAgreement(reqdata.signerPubKey);
           const receipt = await send(web3, account, transaction);
           console.log(JSON.stringify(receipt, null, 4));
           if (web3.currentProvider.constructor.name == "WebsocketProvider")
               web3.currentProvider.connection.close();
           if (receipt) {
               next(null, receipt)
           }
       } catch (error) {
           next(error, null)
       }  
   }
   run();

options傳遞給signTransaction函式的對像中,添加以下內容:

to: transaction._parent._address

請注意,如果此(您已屏蔽)失敗:

await transaction.estimateGas({ from: account.address })

那麼實際的交易也會失敗。

所以掩蓋它是沒有意義的。您應該調查您的功能恢復的原因。

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