7

I'm working on a contract that will run an action when receiving tokens. The following code does not run when the account receives EOS or JUNGLE tokens on the Jungle Testnet.

#include "./test.hpp"

...

void test::init(name username)
{
    require_auth(_self);
}

void test::ontransfer(name from, name to, asset quantity, string memo)
{
    check(true == false, "sorry not sorry");
}
#include <string>

#include <eosio/eosio.hpp>
#include <eosio/asset.hpp>

#define EOS_SYMBOL symbol("EOS", 4)

CONTRACT test : public eosio::contract
{
    using contract::contract;

  public:
    struct init
    {
        init(){};
        eosio::name name;
        EOSLIB_SERIALIZE(init, (name))
    };

    ACTION init(eosio::name username);

    [[eosio::on_notify("eosio.token::transfer")]]
    void ontransfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo);

     // https://eosio.stackexchange.com/questions/4381/why-is-eosioon-notifyeosio-tokentransfer-not-working 
    using transfer_action = eosio::action_wrapper<eosio::name("transfer"), &test::ontransfer>;

};

EOSIO_DISPATCH(test, (init))

How can I debug this? Is this a problem of v1.6.1? Is there something wrong with my code?

3
  • Does this code work on the main net?
    – Matei Radu
    Commented Jun 11, 2019 at 11:56
  • I have not tested in mainnet yet, that would be useful. I reverted back to the old way of doing it, by defining the apply function. It would be useful as to why this code doesn't work on at least jungle.
    – marijn
    Commented Jun 12, 2019 at 8:36
  • With CDT 1.6.1, the on_notify attribute does not work when you explicitly use EOSIO_DISPATCH, so try to remove the dispatcher as it is no longer mandatory with CDT 1.6.
    – Matei Radu
    Commented Jun 12, 2019 at 10:07

1 Answer 1

7

Using the EOSIO_DISPATCH macros disables the automatic inclusion of actions and on_notifys, and assumes that you will deal with them manually.

To fix this, you have to remove the EOSIO_DISPATCH macro from your header file.

An additional note, as of eosio.cdt 1.6 there is a bug when using on_notify with a wildcard for the contract name, this can be fixed with a hack where a on_notify is added with a given contract name. For example:

Replace:

[[eosio::on_notify("*::transfer")]] void ontransfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo);

with:

[[eosio::on_notify("*::transfer")]] void ontransfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo);
[[eosio::on_notify("eosio.token::transfer")]] void dummytansfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo){ontransfer(from,to,quantity,memo);} // This is a hack, otherwise the ontransfer function won't work

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.