1

In the old version, I can get self balance by these code:
auto token = eosio::token(N(eosio.token)); const asset balance = token.get_balance(_self, symbol_type(EOS_SYMBOL).name());

But when I upgrade repo to 1.3.2 eosio-cpp, and using eosio.token.hpp, I don't even know how to initial the class, because the constructor has been removed.

So how to write the code above in higher version?

2 Answers 2

2

I've solved it ;-)

action

// START balance get
ACTION balance(name user) {
    const symbol sym(symbol_code("EOS"), 4);
    const auto my_balance = eosio::token::get_balance("eosio.token"_n, get_self(), sym.code());
    print("my balance is ", my_balance);
}
// END balance get

eosio.token.hpp

#pragma once

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

#include <string>

namespace eosiosystem {
class system_contract;
}

namespace eosio {

using std::string;

CONTRACT token: public contract {
public:
    using contract::contract;

    ACTION create(name issuer, asset maximum_supply);

    ACTION issue(name to, asset quantity, string memo);

    ACTION retire(asset quantity, string memo);

    ACTION transfer(name from, name to, asset quantity, string memo);

    ACTION open(name owner, const symbol& symbol, name ram_payer);

    ACTION close(name owner, const symbol& symbol);

    static asset get_supply(name token_contract_account, symbol_code sym_code) {
        stats statstable(token_contract_account, sym_code.raw());
        const auto& st = statstable.get(sym_code.raw());
        return st.supply;
    }

    static asset get_balance(name token_contract_account, name owner, symbol_code sym_code) {
        accounts accountstable(token_contract_account, owner.value);
        const auto& ac = accountstable.get(sym_code.raw());
        return ac.balance;
    }

private:
    TABLE account {
        asset balance;

        uint64_t primary_key() const {
            return balance.symbol.code().raw();
        }
    };

    TABLE currency_stats {
        asset supply;
        asset max_supply;
        name issuer;

        uint64_t primary_key() const {
            return supply.symbol.code().raw();
        }
    };

    typedef eosio::multi_index<"accounts"_n, account> accounts;
    typedef eosio::multi_index<"stat"_n, currency_stats> stats;

    void sub_balance(name owner, asset value);
    void add_balance(name owner, asset value, name ram_payer);
};

} /// namespace eosio
1

ok, i got it, just like this
asset pool_eos = eosio::token::get_balance("eosio.token"_n,get_self(), symbol_code("EOS"));

1
  • it seems not right.........
    – liu Rick
    Nov 6, 2018 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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