0

THE STRUCTURE OF MY PROJECT

I am working in mycontract.cpp, which imports myheader.hpp


WHAT I WANT

I want to create a table in myheader.hpp, for a generic contract, "IN THEORY" as follows:

struct [[eosio::table, eosio::contract(_self)]] test {
  ...
}

WHAT IS NOT WORKING

  1. eosio::contract(_self), in the WHAT I WANT section, is not a valid code.
  2. in what used to be the standard way of defining a table, the table section in the ABI is not generated by the new compiler eosio-cpp.

In fact, if I create a table like the following:

struct [[eosio::table]] test {
  ...
}

the code compiles; but, the table does not appear in mycontract's tables section in the ABI.


WHAT IS WORKING

If I create a table in myheader.hpp, for a specific contract, as follows:

struct [[eosio::table, eosio::contract("mycontract")]] test {
  ...
}

the code compiles; and, the table appears in mycontract's table section in the ABI.

  • Maybe this will help: github.com/EOSIO/eos/issues/6015 Could you show how you're typdefing the Multi Index container? It's now like this: typedef eosio::multi_index< "accounts"_n, account > accounts; This may also be a good resource to look at: developers.eos.io/eosio-home/docs/data-persistence – Nat Nov 27 '18 at 17:37
  • I am doing like this: typedef eosio::multi_index<name("test"), test> t_test. Btw, I will check, thanks for sharing. But I need to define a general table in the .hpp file, .hpp which includes also the table typedef. – Riccardo Persiani Nov 27 '18 at 17:40
0

It really depends on what you are going to achieve.

If you are just trying to reuse the old code in a hurry, I would suggest:

SOLUTION 1

header.hpp

struct [[ eosio::table]] test {
  uint64_t prim_key;
  uint64_t timestamp;

  auto primary_key() const { return prim_key; }
};

one.cpp

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

using namespace eosio;

CONTRACT one : public contract {
private:
  #include "../header.hpp"
  typedef multi_index<name("one"), test> test_table;

public:
  using contract::contract;
  ACTION hi(name user) { print_f("Hello % from one", user); }
};

EOSIO_DISPATCH(one, (hi))

If you think it is ugly and want to spend some effort extract an macro, try:

SOLUTION 2

header.hpp

#include <eosiolib/eosio.hpp>

struct [[ eosio::table, eosio::contract(__MY__CONTRACT__)]] test {
  uint64_t prim_key;
  uint64_t timestamp;

  auto primary_key() const { return prim_key; }
};

one.cpp

#define __MY__CONTRACT__ "one"
#include "../header.hpp"
#include <eosiolib/asset.hpp>
#include <eosiolib/eosio.hpp>

using namespace eosio;

CONTRACT one : public contract {
  private:
    typedef multi_index<name("one"), test> test_table;

  public:
    using contract::contract;
    ACTION hi(name user) { print_f("Hello % from one", user); }
};

EOSIO_DISPATCH(one, (hi))

The last solution may not fit your needs as you need the table have same name with contract. However, we use it in our own projects so I would like show it here:

SOLUTION 3

header.hpp

#include <eosiolib/eosio.hpp>

struct test {
  uint64_t prim_key;
  uint64_t timestamp;

  auto primary_key() const { return prim_key; }
};

one.cpp

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

using namespace eosio;

CONTRACT one : public contract {
  private:
    TABLE tableone : test{};
    typedef multi_index<name("one"), test> test_table;

  public:
    using contract::contract;
    ACTION hi(name user) { print_f("Hello % from one", user); }
};

EOSIO_DISPATCH(one, (hi))

Conclusion

This can be also done by many other macro tricks and you can pick which ever you like.

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.