繁体   English   中英

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

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

所以基本上我正在尝试编写一个应用程序,该应用程序能够在管理员从仪表板“批准”交易后向多个地址发送指定数量的令牌或以太币。

智能合约代码 -

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);
            }
        }
    }

}

与批量交易交互的前端 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)
         })
    }

为了与智能合约交互,我使用 useDapp 连接到钱包,而 web3.js 仅用于此 batchTransfer function。当我尝试执行批量转账时,会出现此消息。

在此处输入图像描述

这里有什么问题?

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

问题不是传递string类型,而是传递一个数字。 所以很可能你在这里传递了错误的论点:

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

[...transactionsToSend]参数导致错误

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM