简体   繁体   English

如何在智能合约中使用不同的代币进行批量交易?

[英]how to do a bulk transaction with different tokens in a smart contract?

I have this smart contract I found on github that is able to send multiple transactions but they all should be for the same token.我有这个在 github 上找到的智能合约,它能够发送多个交易,但它们都应该是同一个令牌。 but what I want is a smart contract that sends multiple transactions for different tokens like the picture I added, if anyone knows how let me know or send a link to a similar smart contract or project.但我想要的是一个智能合约,它为不同的代币发送多个交易,就像我添加的图片一样,如果有人知道如何让我知道或发送指向类似智能合约或项目的链接。 在此处输入图像描述

You can invoke the transfer() (sending tokens from your contract) or transferFrom() (sending tokens from other address, need to have prior approval) functions on multiple token contracts.您可以在多个代币合约上调用transfer() (从您的合约发送代币)或transferFrom() (从其他地址发送代币,需要事先获得批准)函数。

Assuming that from address has approved MyContract address to operate their tokens (both token1 and token2 ), the example below transfers both token1 and token2 within one transaction.假设from address 已批准MyContract地址来操作其代币( token1token2 ),下面的示例在一次交易中同时传输token1token2

pragma solidity ^0.8;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract MyContract {
    function batchTransfer(address token1, address token2, address from, address to, uint256 amount) public {
        bool success1 = IERC20(token1).transferFrom(from, to, amount);
        bool success2 = IERC20(token2).transferFrom(from, to, amount);
    }
}

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

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