0

This question already has an answer here:

I will make an example of what kind of interaction i'm asking for:

  • Sender send X quantity of token A to smart contract 1
  • Smart contract 1 elaborate the information and send it to smart contract 2
  • Smart contract 2 send the right quantity of token B to the original sender

is it possible to make this kind of interactions? thanks for your help

marked as duplicate by Eli Drion, ivicaa, Achala Dissanayake, Ismael, Asone Apr 2 '18 at 9:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I don't think this is a duplicate because sending tokens is a different use case compared to executing functions – cleanunicorn Apr 1 '18 at 20:09
0

Detecting the transfer of token A to contract 1

What you ask is possible, but the difficulty lies in detecting that contract A has transferred it's tokens to contract 1.

There are several possibilities depending upon the capabilities of contract A.

The easy way

If A is an ERC223 contract, then it's really easy.

Simply implement the tokenFallback function in your contract 1 and when you get the callback, verify the msg.sender is contract A, then call contract 2 to transfer the required amount of B tokens to the from address.

Example:

function tokenFallback(address _from, uint _value, bytes _data) external {
    require(msg.sender == a_token);
    b_token.transfer(_from, _value);
}

The hard way

Sometimes life is not easy.

In this case you may have to get the holder of token A to approve contract 1 to transfer tokens on their behalf, then call contract 1, which will transfer the A token and issue the B tokens.

Example:

function crossTransfer(uint _value) external {
    // This requires the sender has previously approved this transfer.
    a_token.transferFrom(msg.sender, this, _value);
    b_token.transfer(_from, _value);
}

Another possibility:

If you control all the contracts, a possibility may be to use something similar to this:

Three token transfer between each other

  • ok so it's possible to do it, that's a key point for me thank you very much! – Raffozor Apr 2 '18 at 9:26
0

You can make smart contracts interact with each other but sending tokens is a different thing because you are interacting with the smart contract token, not with the token that you need.

Let's say you have this situation

  • smart contract token ==> A
  • smart contract 1 ==> B
  • smart contract 2 ==> C

B sends token to C effectively means that B calls a function of A. And there will be no code that runs from C

Not the answer you're looking for? Browse other questions tagged or ask your own question.