How to describe a table outside of a contract-class?

78 Views Asked by At

As we know from the documentation, a table must be initialized as shown below:

class [[eosio::contract]] helloworld: public eosio::contract
{
public:
   helloworld(eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds)
   : contract(receiver, code, ds),
     my_table(receiver, receiver.value) { }

   ///////////////

   struct [[eosio::table]] testtable_j
   {
      name owner;
      std::string data;
      uint64_t primary_key() const { return owner.value; }
   };
   using testtable_t = eosio::multi_index<"testtable"_n, testtable_j>;
   testtable_t my_table;
}

But how can I describe my table for using its type without binding to 'helloworld'? This case doesn't work (abi not generated):

struct [[eosio::table]] testtable_j
{
      name owner;
      std::string data;
      uint64_t primary_key() const { return owner.value; }
};
using testtable_t = eosio::multi_index<"testtable"_n, testtable_j>;
   
class [[eosio::contract]] helloworld: public eosio::contract
{
public:
   helloworld(eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds)
   : contract(receiver, code, ds),
     my_table(receiver, receiver.value) { }
   
   testtable_t my_table;
}
1

There are 1 best solutions below

0
0101 On

What's needed in this case is an extra parameter referring to the contract's name. As follows:

struct [[eosio::table, eosio::contract("helloworld")]] testtable_j {}