AntelopeIO / cdt

Contract Development Toolkit (CDT) is a suite of tools to facilitate C/C++ development of contracts for Antelope blockchains
Other
30 stars 30 forks source link

[question] Is the precedence of e.g. [[eosio::on_notify("eosio.token/*::transfer")]] well defined? #244

Closed FrontierPsychiatrist closed 12 months ago

FrontierPsychiatrist commented 12 months ago

If I have a smart contract that defines two event listeners for transfer notifications - one for all smart contracts and one for eosio.token, is it guaranteed that the more specific one gets matched first?

CONTRACT test :: public eosio::contract {
public:
  [[eosio::on_notify("*::transfer")]]
  void on_any_transfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo);

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

When receiving a token from eosio.token, will alyways on_specific_transfer be called?

ericpassmore commented 12 months ago

reposted to telegram

ericpassmore commented 12 months ago

Its safest to do everything in the general handler.

FrontierPsychiatrist commented 12 months ago

But in the general handler I can't extract the token contract?

ericpassmore commented 12 months ago

Best to jump on telegram and post questions in the developer forum https://t.me/antelopedevs

FrontierPsychiatrist commented 10 months ago

To add on this, in case anyone finds this in the future: The way to handle different token smart contracts in a wildcard receiver is to use get_code (deprecated now) resp. get_first_receiver of eosio::contract.

CONTRACT example : public eosio::contract {
  public:
    [[eosio::on_notify("*::transfer")]]
    void on_transfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo);
}

void example::on_transfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo) {
  if (to != get_self()) {
    // Only handle incoming transfer notifications
    return;
  }
  if (get_first_receiver() == "eosio.token"_n) {
    // Got EOS, WAX or whatever - do something with it
  } else if (get_first_receiver() == "othertoken"_n) {
    // Got some other token - do something with it
  }
}