Transactions

Web3.eth.abi.encodeFunctionCall 不工作

  • November 12, 2021

我正在創建一個 dapp,它根據使用者的輸入向使用者收取特定數量的 eth。

每當我嘗試創建交易時,我都會在 Wei 中指定 Eth 的數量。它拋出一個錯誤,沒有說明為什麼它不能完成交易

錯誤:在 Object.onClick (index.js:62) 的 MintNewNFT (Transactions.js:68) 上創建新 NFT 時出錯

(第 62 行是 catch 塊)

金額為 0.02166 ETH

這是我的程式碼:

export const MintNewNFT = async (WalletABI,address, network, mediaID, amountIn) => {

 try {
   
   
   //adjust this to take an argument for media id
   
   const web3 = new Web3('https://rinkeby.infura.io/v3/key');
   const weiValue = Web3.utils.toWei(amountIn.toString(), 'ether');
   console.log(weiValue , mediaID);
   
   const transactionParameters = {
     to: WalletABI._address, // Required except during contract publications.
     from: address, // must match user's active address.
     value: weiValue.toString(), 
     data: web3.eth.abi.encodeFunctionCall(    
       {
         "inputs": [
         {
           "internalType": "bytes32",
           "name": "mediaID",
           "type": "bytes32"
         }
       ],
       "name": "mintNewNFT",
       "outputs": [],
       "stateMutability": "payable",
       "type": "function",
       "payable": true
       
     },[mediaID]),
     chainId: `0x${network}`, // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
   };
   
 
   
   // txHash is a hex string
   // As with any RPC call, it may throw an error
   await window.ethereum.request({
     method: 'eth_sendTransaction',
     params: [transactionParameters],
   }).then((result) => {

     // The result varies by by RPC method.
     // For example, this method will return a transaction hash hexadecimal string on success.

     console.log(`Transaction Result ${result}`)


   })
   .catch((error) => {
     // If the request fails, the Promise will reject with an error.

     console.log(`Transaction ERROR :  ${error.message}`)
   });

   

} catch (error) {
   throw Error("Error Minting New NFT", error)
}
}

任何關於我可能做錯的跡象將不勝感激

嗯….我解決了我的問題。原來它失敗了,因為我使用的是 bytes32 而不是參數的字元串。我真的無法解釋為什麼會發生這種情況,以防其他人可以

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