In my contract, I'd like to be able to check how much EOS an account has. For that, I'd write some C++ code like this:
token::accounts the_table("eosio.token"_n, "useraccount1"_n.value);
auto it = the_table.begin();
auto how_much = it -> balance;
Here, I'd use the type token::accounts
from the eosio.token
contract.
The problem is that, in the official eosio.token
contract, this type is private, so my code above does not compile.
So, instead of just an #include ".../eosio.token.hpp"
, now I copy the type declaration to the code of my contract, like this:
struct [[eosio::table]] account {
asset balance;
uint64_t primary_key() const {return balance.symbol.code().raw();}
};
typedef eosio::multi_index <"accounts"_n, account> accounts;
Copying and modifying the data structures, just to be able to access them, is bad coding practice. But maybe I'm using a wrong approach here. So my question here is two-fold:
- Why are the types in the official code private?
- How to better see from within a contract how much EOS an account has?