You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider a basic contract for illustrative purposes (may not be syntactically correct)
CONTRACT mycontract : public contract {
public:using contract::contract;
[[eosio::action]] voidcoolstuff() {
// does cool stuff
}
[[eosio::action]] voidcoolstuff2() {
// does even cooler stuff
}
};
Let's say I want to add an init action, and then enforce that the init action be called once and only once. I might do something like
CONTRACT mycontract : public contract {
public:using contract::contract;
[[eosio::action]] voidinit(uint64_t howcool2be) {
check(!_config.exists(), "contract already initialized");
_config.set(howcool2be, get_self());
}
voidassert_inited() {
check(_config.exists(), "contract not initialized");
}
[[eosio::action]] voidcoolstuff() {
assert_inited();
// does cool stuff
}
[[eosio::action]] voidcoolstuff2() {
assert_inited();
// does even cooler stuff
}
eosio::singleton<"config"_n, uint64_t> _config{get_self(), get_self().value};
};
What's unfortunate about the code above is the need to remember to place assert_inited() in every non-init action. (for a rule that states "enforce that the init action be called once and only once" I don't believe one should manually need to sprinkle stuff like this all over)
What would be interesting is if the contract had access to the action name in its ctor. That would allow a common location to perform contract start up checks inclusive of behavior of the action name. It would allow the code above to be simplified rather substantially,
CONTRACT mycontract : public contract {
public:mycontract(name self, name first_receiver, datastream<constchar*> ds, name action_name) :
contract(self, first_receiver, ds) {
check(_config.exists() ^ action_name == "init"_n, "contract must be inited once and only once");
}
[[eosio::action]] voidinit(uint64_t howcool2be) {
_config.set(howcool2be, get_self());
}
[[eosio::action]] voidcoolstuff() {
// does cool stuff
}
[[eosio::action]] voidcoolstuff2() {
// does even cooler stuff
}
eosio::singleton<"config"_n, uint64_t> _config{get_self(), get_self().value};
};
The text was updated successfully, but these errors were encountered:
Consider a basic contract for illustrative purposes (may not be syntactically correct)
Let's say I want to add an init action, and then enforce that the init action be called once and only once. I might do something like
What's unfortunate about the code above is the need to remember to place
assert_inited()
in every non-init action. (for a rule that states "enforce that the init action be called once and only once" I don't believe one should manually need to sprinkle stuff like this all over)What would be interesting is if the
contract
had access to the action name in its ctor. That would allow a common location to perform contract start up checks inclusive of behavior of the action name. It would allow the code above to be simplified rather substantially,The text was updated successfully, but these errors were encountered: