如何解决Web3.eth.abi.encodeFunctionCall 不起作用
我正在创建一个 dapp,它根据用户的输入向用户收取特定数量的 eth。
每当我尝试创建交易时,我都会在 Wei 中指定 Eth 的数量。它抛出一个错误,没有说明为什么它无法完成交易
错误:生成新 NFT 时出错 在 MintNewNFT (Transactions.js:68) 在 Object.onClick (index.js:62)
(第 62 行是 catch 块)
AmountIn 为 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 而不是字符串作为参数。我真的无法解释为什么会发生这种情况,因为其他人可以
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。