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?
Asked
Active
Viewed 3,021 times
1 Answers
9
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).
Edmund Edgar
- 16,897
- 1
- 29
- 58
-
How would you call the transferBulk? What would you need to put in the data of the transaction? Also how could I make this work with an already deployed contract? Could I pay you to write me a contract? Email me at info@ohni.io – James Jan 09 '18 at 23:24
-
there is a lot of things that can go wrong with this approach. For example, if a ERC20 has custom logic which addresses can/cannot send txs, the JavaScript side has to dry-run and pre-validate all addresses. I'd recommend trying https://multisender.app because it does all the heavy lifting for you. – rstormsf Apr 29 '21 at 21:01