A simple way to handle configuration values for EOSIO contracts and tests built with eoslime
The only thing needed is to plug the config header file. Then on your contract constructor load the singleton config data and on the destructor save it.
#include "config.hpp"
class [[eosio::contract]] profile: public eosio::contract {
config_singleton config; // the configuration singleton, do not use directly
config_data cfg;
...
public:
profile(eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds) :
contract(receiver, code, ds),
config(receiver, receiver.value)
{
// load config
cfg = config.get_or_create(receiver);
}
~profile() {
// save configuration
config.set(cfg, eosio::same_payer);
}
[[eosio::action]]
void ...
Then you just need to add the action to configure your contract... Please refer to the included test contract for a complete working example, like:
[[eosio::action]]
void configure(eosio::name key, std::string value) {
// TODO: define who has access to perform configuration, ideally a msig account
// require_auth(controlling_account);
cfg.set(key, value);
}
Throughout the code all configuration is accessible through the cfg member variable...
-
src/ - source directory that contains
config.hppmain project code file -
contracts/ - test contract directory
-
tests/ - tests specs
-
scripts/ - helper scripts for running nodeos locally
$ npm install -g eoslime$ eoslime compile$ ./scripts/nodeos.shNOTE: Please customize the script to your local development needs. This might be made easier in the future with configuration and a better script...
$ eoslime test