Ethereum Wallet DApp – Grouping Multiple ERC20 Tokens into One Transaction

erc-20ethereum-wallet-dapp

Specifically, if I wanted to send multiple different tokens to a smart contract, but it was important to the contracts function that all tokens arrive at the same time, could I put;
20 Token A
45 Token B
7.5 Token C
in a single transaction, or easily be able to create packages of different tokens to send out together and remain together when sent back and forth?

Best Answer

You can do this by creating a contract and having the contract interact with the various token contracts.

One method would be to create your contract, then transfer ownership of your balance on each token to the contract. Untested example (for illustrative purposes only, don't use this as is):

pragma solidity ^0.4.6;

contract ERC20API {
    function transfer(address _to, uint256 _value) returns (bool success);
}

contract ProxyContract {

   address owner;

   function ProxyContract() {
      owner = msg.sender;
   }

   // Simple transfer function, just wraps the normal transfer
   function transfer(address token, address _to, uint256 _value) {
      if (owner != msg.sender) throw;
      ERC20API(token).transfer(_to, _value);
   }

   // The same as the simple transfer function
   // But for multiple transfer instructions
   function transferBulk(address[] tokens, address[] _tos, uint256[] _values) {
      if (owner != msg.sender) throw;
      for(uint256 i=0; i<tokens.length; i++) {
         // If one fails, revert the tx, including previous transfers
         if (!ERC20API(tokens[i]).transfer(_tos[i], _values[i])) throw;
      }
   }

}

Another method would be to leave the ownership of the tokens under your own account, and call approve() against each token to give your proxy contract permission to move them. Then where the above code calls transfer(_to, _value) in the proxy contract, you'd instead call transferFrom(msg.sender, _to, _value).

Related Topic