【发布时间】:2021-08-22 18:46:05
【问题描述】:
我正在创建一个 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)
}
}
非常感谢任何关于我可能做错的指示
【问题讨论】:
-
请发布合约函数
mintNewNFT()(包括它的依赖项)在这种情况下,如果可以的话,最好发布一个实时合约的链接(带有经过验证的源代码)......我的猜测是一个失败的require()或assert()条件,MetaMask 没有识别它(可能是因为基于块数据的决策树),而是推荐极高的费用。 -
@PetrHejda 在将 web3.eth.abi.encodeSignature 更改为 web3.eth.abi.encodeFunctionCall 后,它甚至没有在元掩码中提示我任何东西。看起来我需要进行更改,因为它还编码了我的函数所必需的函数参数
标签: javascript ethereum web3 cryptocurrency metamask