EOSIO Config handler for contracts

EOSIO Configuration handler pluggable into any contract

A simple way to handle configuration values for EOSIO contracts that fits any pre-existing contract code.

Check the code at eosio-config repo

How it works

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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:

1
2
3
4
5
6
7
[[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…

Share