2

Modifier payable triggers an action when an address sends ether to a contract.

For example:

function () public payable {
      require(msg.data.length == 0);
      DoSomething();
    }

Is this action also triggered when the msg.sender sends tokens? My question is if it's possible to trigger actions through payable when an address send tokens too, or it's necessary another modifier or a more complex workaround to achieve this?

1
  • there is normaly a deposit function for token instead which you can put your DoSomehting()
    – Badr Bellaj
    Jul 1, 2018 at 12:43

2 Answers 2

0

To be able to handle receiving tokens, you can make your class inherit from the ERC223Receiver standard:

contract ERC223Receiver
{
    function tokenFallback(address _from, uint _value, bytes _data);
}

contract YourContract is ERC223Receiver
{
    function tokenFallback(address _from, uint _value, bytes _data)
    {
        // Handle receiving tokens
    }
}

I have not tested this myself.

Source: https://github.com/ethereum/EIPs/issues/744

7
  • Thanks @Jesse , looks like a viable answer. But where do I define which token I'm receiving. There should be a place to define the token address? Dec 23, 2017 at 11:54
  • 1
    When tokenFallback is called by the token contract itself, msg.sender will be the address of the token contract. You should probably throw if tokenFallback is not called by a trusted token contract.
    – Jesbus
    Dec 23, 2017 at 12:06
  • 1
    Also be sure to look into the adoption of ERC223. I'm not sure many tokens are built yet with this standard.
    – user19510
    Dec 23, 2017 at 13:02
  • 1
    @MoisésBriseñoEstrello No, that's not what it means. Whenever a token holder sends tokens to your contract, they are interacting with your contract with the token contract acting as intermediary. When you send tokens, you are really just calling the transfer function in a token contract. The token contract in turn will call tokenFallback in your contract. The fact that the call to tokenFallback came from the token contract proves that real tokens were transfered. The call chain is like this: USER --> TokenContract.transfer(..) --> YourContract.tokenFallBack(...)
    – Jesbus
    Dec 23, 2017 at 14:32
  • 1
    @MoisésBriseñoEstrello The receiving contract needs to have a function tokenFallback(...) if it wants to run some code when it receives tokens. The token contract needs to call tokenFallback(...) inside its function transfer(...)
    – Jesbus
    Dec 23, 2017 at 14:48
0

Modifier payable allows function to receive ether. Function in your example is a default payable fallback function which will be called every time somebody send ether to your contract.

You can also implement any other function as payable so this function can accept Ether.

Payable only allows function to accept ether and it is not in any way related to token transfers.

Sending tokens is just a transfer function in ERC20 standard. By default it will only change balance within the token. So if you send 1 token from A to B the only change will be that:

token.balances[A] = token.balannces[A] - 1
token.balances[B] = token.balances[B] + 1

To be able to do what you want you have two options.

  1. Implement your tokens so they call tokenFallback function when transfer is called. Please have in mind that sending token must call tokenFallback function in the receiving contract. Default Open Zeppelin ERC20 token doesn't not do it. You will have to make a modification to it.

  2. Write a javascript or python code using web3 library which will listen for transfer events from your token and call some function in the receiving contract when transfer is realized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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