Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access to action name as part of contract #101

Open
spoonincode opened this issue Jan 29, 2023 · 1 comment
Open

access to action name as part of contract #101

spoonincode opened this issue Jan 29, 2023 · 1 comment
Labels

Comments

@spoonincode
Copy link
Member

Consider a basic contract for illustrative purposes (may not be syntactically correct)

CONTRACT mycontract : public contract {
public:
   using contract::contract;

   [[eosio::action]] void coolstuff() {
      // does cool stuff
   }
   [[eosio::action]] void coolstuff2() {
      // 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]] void init(uint64_t howcool2be) {
      check(!_config.exists(), "contract already initialized");

      _config.set(howcool2be, get_self());
   }

   void assert_inited() {
      check(_config.exists(), "contract not initialized");
   }

   [[eosio::action]] void coolstuff() {
      assert_inited();
      // does cool stuff
   }
   [[eosio::action]] void coolstuff2() {
      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<const char*> 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]] void init(uint64_t howcool2be) {
      _config.set(howcool2be, get_self());
   }

   [[eosio::action]] void coolstuff() {
      // does cool stuff
   }
   [[eosio::action]] void coolstuff2() {
      // does even cooler stuff
   }

   eosio::singleton<"config"_n, uint64_t> _config{get_self(), get_self().value};
};
@stephenpdeos
Copy link
Member

Labeling as more-info, requires more breakdown before actionable.

@ericpassmore ericpassmore added this to the CDT 5.0.0-rc1 milestone Aug 11, 2023
@ericpassmore ericpassmore modified the milestones: CDT 4.1.0-rc1, Future Milestone Aug 24, 2023
@arhag arhag removed this from the Future Milestone milestone Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

5 participants