简体   繁体   English

Metamask 交易预计会在智能合约 function 上失败,该合约会批量进行以太币转账

[英]Metamask transaction is expected to fail on a Smart Contract function that batches ether transfer

So basically I'm trying to write an application that is able to send multiple addresses specified amount of tokens or ethers, after the admin has "approved" a transaction from the dashboard.所以基本上我正在尝试编写一个应用程序,该应用程序能够在管理员从仪表板“批准”交易后向多个地址发送指定数量的令牌或以太币。

Smart Contract Code -智能合约代码 -

interface ERC20Token {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}



contract SimpleContract {
    address public owner;
    ERC20Token public TetherToken;
    ERC20Token public USDCoin;

    struct Transaction {
        address payable recipient;
        uint amount;
        string coinType;
    }

    constructor() {
        owner = msg.sender;
        TetherToken = ERC20Token(0x97192842006D54AC767D004c96Dd3723194c8AcC);
        USDCoin = ERC20Token(0xFE724a829fdF12F7012365dB98730EEe33742ea2);
    }


    function batchTransaction(Transaction[] memory transactions) public payable {
        for(uint i = 0; i < transactions.length; i++) {
            if(keccak256(abi.encodePacked(transactions[i].coinType)) == keccak256(abi.encodePacked("ETH"))) {
                transactions[i].recipient.transfer(transactions[i].amount);
            } else if (keccak256(abi.encodePacked(transactions[i].coinType)) == keccak256(abi.encodePacked("USDC"))) {
                USDCoin.transferFrom(msg.sender, transactions[i].recipient, transactions[i].amount);
            } else if (keccak256(abi.encodePacked(transactions[i].coinType)) == keccak256(abi.encodePacked("USDT"))) {
                TetherToken.transferFrom(msg.sender, transactions[i].recipient, transactions[i].amount);
            }
        }
    }

}

Front-end React Code that interacts with batch transaction -与批量交易交互的前端 React 代码 -

    async function batchUpdate() {
        let transactionsToSend = [...transactions.filter((transaction) => {
            return checkedIndices?.find((tx) => tx.id === transaction._id)?.isChecked == true
        }).map((tx) => ({
            recipient: tx.address,
            amount: utils.parseEther(tx.amount.toString()),
            coinType: tx.coinType
        }))]

        MadRiverContract.methods.batchTransaction([...transactionsToSend]).send({ from: account }).on('receipt', (receipt) => {
            updateStatusBatch('approved', checkedIndices.filter((tx) => tx.isChecked == true).map((tx) => tx.id), receipt.transactionHash)
         }).catch((error) => {
             alert(error.message)
             console.log(error)
         })
    }

For interacting with the smart contract, I'm using useDapp to connect to the wallet, and web3.js only for this batchTransfer function. When I try to execute the batch transfer, this appears.为了与智能合约交互,我使用 useDapp 连接到钱包,而 web3.js 仅用于此 batchTransfer function。当我尝试执行批量转账时,会出现此消息。

在此处输入图像描述

What's the issue here?这里有什么问题?

I had the same error here: https://ethereum.stackexchange.com/questions/119491/this-transaction-is-expected-to-fail-trying-to-execute-it-is-expected-to-be-exp我在这里有同样的错误: https://ethereum.stackexchange.com/questions/119491/this-transaction-is-expected-to-fail-trying-to-execute-it-is-expected-to-be-exp

The problem was instead of passing string type, I was passing a number.问题不是传递string类型,而是传递一个数字。 So most likely you are passing wrong argument here:所以很可能你在这里传递了错误的论点:

MadRiverContract.methods.batchTransaction([...transactionsToSend])

[...transactionsToSend] argument is causing error [...transactionsToSend]参数导致错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用 Solidity 函数从智能合约返回 Ether? - How to call Solidity Function to return Ether from Smart Contract? 如何在智能合约中将地址连接到午餐 function? | TypeError:无法读取未定义的属性(读取“连接”)ether.js - how can i connect an address to lunch a function in the smart contract ? | TypeError: Cannot read properties of undefined (reading 'connect') ether.js 在 React 中使用 ABI 和 Ether.js 查询智能合约 - Querying Smart Contract using its ABI and Ether.js in React 如何从 React 前端向智能合约发送可变数量的以太币? - How to send variable amount of ether to smart contract from React front end? 安全帽元掩码错误 - “尝试使用无效链 ID 发送原始交易。预期链 ID 为 31337” - Hardhat metamask error - "Trying to send a raw transaction with an invalid chainId. The expected chainId is 31337" 智能合约:如何在 React 中获得 Solidity function 的回报? - Smart contract: How to get return of Solidity function in React? 无法使用 web3.js 从智能合约调用 function - Unable to call function from smart contract using web3.js 似乎无法在 UI 中调用智能合约功能 - Can't seem to call a smart contract function in the UI EthersJS 未正确从 function 调用的智能合约返回值 - EthersJS not properly returning a value from a smart contract on function call React获取Metamask最新交易状态 - React get metamask latest transaction status
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM