Created
February 19, 2020 12:23
-
-
Save selevit/56948be46a2292c32674ced5060d4208 to your computer and use it in GitHub Desktop.
An Ethereum smart contract which transfers many different ERC20 tokens and ETH in one transaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.21 <0.7.0; | |
pragma experimental ABIEncoderV2; | |
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; | |
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"; | |
contract MultiTransfer { | |
struct Destination { | |
address token; | |
uint256 amount; | |
address payable receiver; | |
} | |
function() external payable {} | |
function send(Destination[] memory destinations) public payable returns (bool success) { | |
for (uint256 x = 0; x < destinations.length; x++) { | |
Destination memory dst = destinations[x]; | |
if (dst.token == 0x000000000000000000000000000000000000bEEF) { | |
// Eth transfer | |
dst.receiver.transfer(dst.amount); | |
} else { | |
ERC20 erc20_contract = ERC20(dst.token); | |
assert (erc20_contract.transferFrom(msg.sender, dst.receiver, dst.amount)); | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example of the transaction which transfers both TST1 and TST1 tokens as well as ETH.
All sending ERC20 tokens must be approved to spend by this smart contract. The contract should also have ETH if you want to transfer it too.