2

Looks like they changed the constructor for singletons or I am doing this incorrectly. Can anyone advise?

#include <eosiolib/eosio.hpp>
#include <eosiolib/singleton.hpp>

using namespace std;
using namespace eosio;


class simple: public contract {
public:
    simple(account_name self) : contract(self) {}

    void init(account_name key) {
        require_auth(_self);
        require_auth(key);

        // THIS DOESN'T WORK!
        AppSettings(N(simple)).set(AppConfig{key});
    }

private:
    struct AppConfig {
        account_name key;

        account_name primary_key() const { return key; }
        EOSLIB_SERIALIZE(AppConfig, (key))
    };


    typedef singleton<N(simple), N(appconfig),
                      N(simple), AppConfig   >   AppSettings;
};

EOSIO_ABI(simple, (init))

I am getting this in return:

simple.cpp:8:9: error: no matching conversion for functional-style cast from 'uint64_t'
      (aka 'unsigned long long') to 'AppSettings' (aka 'singleton< ::eosio::string_to_name("simple"),
      ::eosio::string_to_name("appconfig"), ::eosio::string_to_name("simple"), simple::AppConfig>')
        AppSettings(N(simple)).set(AppConfig{key});
  • Figured it out, the syntax should just be: AppSettings().set(AppConfig{key}); No arguments needed. Is there a way to query this from cleos? – Magic Man Xpress May 16 '18 at 0:45
1

I am not sure what you are trying to do , so I just made it compile for you. template<uint64_t SingletonName, typename T> class singleton first parameter is N(simple) e.g. singleton name , second is a C++ type this singleton holds

Singleton constructor Say if scope and code is the same and same as owner of a contract then use contract::_self or simply _self because contract is a base class.

/**
      * Construct a new singleton object given the table's owner and the scope
      * 
      * @brief Construct a new singleton object
      * @param code - The table's owner
      * @param scope - The scope of the table
      */
     singleton( account_name code, scope_name scope ) : _t( code, scope ) {}

singleton.set() function

/**
          * Set new value to the singleton table
          * 
          * @brief Set new value to the singleton table
          * 
          * @param value - New value to be set
          * @param bill_to_account - Account to pay for the new value
          */
         void set( const T& value, account_name bill_to_account ) {

I am not sure if this is what you are looking for but I made your example compile

#include <eosiolib/eosio.hpp>
#include <eosiolib/singleton.hpp>

using namespace std;
using namespace eosio;


class simple: public contract {
public:
    simple(account_name self) : contract(self) {}

    void init(account_name key) {
        require_auth(_self);
        require_auth(key);

        // THIS WORKS!!!!!!
        AppSettings{_self, _self}.set(AppConfig{key}, N(simple));
    }

private:
    struct AppConfig {
        account_name key;

        account_name primary_key() const { return key; }
        EOSLIB_SERIALIZE(AppConfig, (key))
    };


    typedef singleton<N(simple), AppConfig>   AppSettings;
};
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.