I want to implement dispatcher in a smart contract. I research about this and understand theoretically but I am not getting practically. Exactly why the dispatcher is to be used? The basic thing I got that we need an apply action handler which checks code and receiver. But I want to know the exact need and some useful links for simple understanding if possible. Also any tutorials. How to implement dispatcher in smart contract? Thank you.
1 Answer
Usually, you don't need to implement dispatcher by yourself. [[eosio::action]]
and [[eosio::on_notify]]
will generate a proper action dispatcher automatically. There were some security accidents by writing incomplete action dispatcher. If you don't know what you are doing exactly, it's better to rely on automatic generation of action dispatcher by eosio-cpp
.
Basically, EOSIO calls apply()
function by default for every action. You can consider it like main()
in normal program. It is an entry point of executing an action. apply()
function instantiates your contract class (You should've written classes inheriting from eosio::contract
), and deserializes name
and data
fields in action. name
indicates the name which this action wants to call like transfer
in eosio::token
and data
are arguments like (from, to, quantity, memo)
needed to be passed to function. By these information, apply()
will call a proper function and pass arguments to it.
Refer to this to understand how generated apply()
looks like.
-
Okay got your point. So, even If I don't implement dispatcher separately, there is no concern of security. But still, why dispatchers are used by others then?– VarshCommented Jun 17, 2020 at 9:10
-
Because earlier
eosio-cpp
didn't support automatic generation ofapply()
. If code that is written recently has manual action dispatcher, its writer may not know the latest changes of eosio.cdt. :)– conr2dCommented Jun 17, 2020 at 9:54 -