Skip to content

Instantly share code, notes, and snippets.

@selevit
Created February 19, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selevit/56948be46a2292c32674ced5060d4208 to your computer and use it in GitHub Desktop.
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.
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;
}
}
@selevit
Copy link
Author

selevit commented Feb 19, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment