How can i set up a listener to handle deposits to my smart contract

Related to this question: How to do an action in a contract to transfer tokens to other user accounts without adding permission in dawn4.0?

  • Check out the demux-js repo. github.com/EOSIO/demux-js "Demux is a backend infrastructure pattern for sourcing blockchain events to deterministically update queryable datastores and trigger side effects. This library serves as a reference implementation of that pattern for use with Node applications."
    – Nat
    Aug 17, 2018 at 12:13

3 Answers 3


I've developed it for monstereos.io - the smart contract is open sourced, so you can see it here: https://github.com/leordev/monstereos/blob/master/contracts/pet/pet.cpp#L197-L234

The above highlighted code has the function to listen to any transfer that the contract receives and add it to an account balance table.

Please be aware of the Extended ABI macro that you will need to add to your code: https://github.com/leordev/monstereos/blob/master/contracts/pet/pet.cpp#L308-L347

The sad part is that if you still want to keep generating ABIs from eosiocpp -g you will need to keep commenting/uncommenting your extended ABI macro.


I wrote the following apply function:

  extern "C" { 
    void apply( uint64_t receiver, uint64_t code, uint64_t action ) { 
      auto self = receiver; 
      if( action == N(onerror)) { 
        /* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ 
        eosio_assert(code == N(eosio), "onerror action's are only valid from the \"eosio\" system account"); 
        eosio::mycontract thiscontract( self ); 
        switch( action ) { 
          case N(transfer):
            eosio_assert(code == N(eosio.token), "mycontract | use eosio.token");
            EOSIO_API( eosio::mycontract, (function1)(function2)(etc...)) 
        /* does not allow destructor of thiscontract to run: eosio_exit(0); */ 


Here you can see that the EOSIO_API allows you to use normal functions such as function1 and function2. However I also have this case N(transfer). Which means that if the contract recieves a transfer, then the following code will be called:

void mycontract::on(eosio::currency::transfer const & transfer)
  if(transfer.from == N(mycontract) return;
    Your code for the transfer goes here

You can refer to the implementation of macro EOSIO_ABI and write the apply function yourself.

An example of this can be found the MonsterEOS contract

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.