3

What are the best practices for setting a minimum deposit amount for a contract?

2

You can store the minimum value in a singleton via a public method that requires authentication, use the asset struct for your method, and assert that the amount exceeds your minimum value.

Example:

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

using eosio::contract;
using eosio::asset;
using eosio::singleton;
using eosio::print;

class min_tokens : public contract {
    public:
        min_tokens(account_name n) : contract(n), tokens_store(_self, _self) {}

        void min(account_name account, uint64_t min) {
            require_auth(_self);
            require_auth(account);
            tokens_store.set(min, _self);
        }

        void something(account_name account, asset tokens ) {
            require_auth(account);
            _requirements(tokens);
            print("Success!");
        }

    private:
        singleton<N(tokens), uint64_t> tokens_store;

        void _requirements(const asset& tokens) {
            eosio_assert(tokens.amount >= tokens_store.get(), "You need to send more tokens");
        }
};

EOSIO_ABI(min_tokens, (something)(min))
2
  • Is void min not guaranteed to assert unless _self is equal to account? If not how would the permissions look like for executing that command? – Phillip Hamnett - EOS42 Aug 27 '18 at 11:58
  • @PhillipHamnett-EOS42 my thinking was that _self can let account use their active permission with cleos set account permission <contract_creator> active <json> in case others besides the creator should be allowed to set a minimum threshold too – confused00 Aug 27 '18 at 12:23

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.