Eos: How to reach another contract table in my smart contract ?

Created on 8 Jun 2018  路  8Comments  路  Source: EOSIO/eos

Is there any way to reach (get rows) of another contract table in my own contract ?

Support

All 8 comments

It seems that the sample contracts don't have the 'get rows' sample.

Look at how system contract reads the token supply.

So , first.
you should make some struct in your smartcontract.

struct difftable{
    int id;
    float value;
   EOSLIB_SERIALIZE(difftable, (id)(value))
}
typedef multi_index<N(difftable), difftable> difftable_t;

second, you should read it as

name difacc = "differentacc"_n;
name difscope = "differentacc"_n;
auto difftbd = difftable_t(difacc , difscope );

@rommzestz

Thank you for this code. It works well on many tables that I have tried it on.

However, I have a question, I am trying to use it on newdexpublic with their exchangepair table.
The table is always empty. This is the code I'm using. Is there something I'm missing?

```//structure for ndx_symbol
struct ndx_symbol{
name contract;
symbol sym;
};

//exchangepair from newdexpublic
TABLE exchange_pair {
uint64_t pair_id;
uint8_t price_precision;
uint8_t status;
ndx_symbol base_symbol;
ndx_symbol quote_symbol;
name manager;
time_point_sec list_time;
string pair_symbol;
float_t current_price;
uint64_t base_currency_id;
uint64_t quote_currency_id;
uint8_t pair_free;
uint64_t ext1;
uint64_t ext2;
string extstr;
uint64_t primary_key() const { return pair_id; } //this is the primary key of the table
};

//https://github.com/EOSIO/eos/issues/3954
//rommzestz code below
typedef multi_index exchangepair_t;

name difacc = "newdexpublic"_n;
name difscope = "newdexpublic"_n;

//https://github.com/EOSIO/eosio.cdt/issues/159
// diffscope.value is second parameter
exchangepair_t _newdexpublic = exchangepair_t(difacc , difscope.value );
```

Any help would be much appreciated.

struct ndx_symbol{
name contract;
symbol sym;
};

//exchangepair from newdexpublic
TABLE exchange_pair {
uint64_t pair_id;
uint8_t price_precision;
uint8_t status;
ndx_symbol base_symbol;
ndx_symbol quote_symbol;
name manager;
time_point_sec list_time;
string pair_symbol;
float_t current_price;
uint64_t base_currency_id;
uint64_t quote_currency_id;
uint8_t pair_free;
uint64_t ext1;
uint64_t ext2;
string extstr;
uint64_t primary_key() const { return pair_id; }
EOSLIB_SERIALIZE( exchange_pair, (pair_id)(price_precision)(status)(base_symbol)(quote_symbol)(manager)(list_time)(pair_symbol)(current_price)(base_currency_id)(quote_currency_id)(pair_free)(ext1)(ext2)(extstr) )
};

typedef eosio::multi_index< "exchangepair"_n, exchange_pair> exchangepair_t;

exchangepair_t _newdexpublic("newdexpublic"_n , "newdexpublic"_n.value );

auto pair = _newdexpublic.find( pair_id );

@rommzestz
Thank you for the help. Unfortunately this did not work for me.
I have used similar code for other contracts and it works.

It seems there is something unusual about the newdexpublic tables that prevents it from returning information.

@rommzestz
Thank you for the help. Unfortunately this did not work for me.
I have used similar code for other contracts and it works.

It seems there is something unusual about the newdexpublic tables that prevents it from returning information.

i think not.
So, basic. One smart contract have table.
Another any smart contract can be read this table.
Any another.
i dont have time for check it.

@rommzestz
I got the code to work. I had to adjust the locations of the variable declaration in order for it to work.

//this line declares the variable _newdexpublic and it should be inside the action definition (cpp file)
exchangepair_t _newdexpublic("newdexpublic"_n , "newdexpublic"_n.value );

//this creates the variable pair with the correct datatype . This line is in the action (cpp file).
auto pair = _newdexpublic.find( pair_id );

It is interesting as I have used the same type of code with another contract.

Look at the Jungle EOS network (https://jungle.bloks.io)
eosmicroloan and techupdloans
can read each other's contract table. In those contracts the table variables are declared in the HPP file, then used in the action.

However, for newdexpublic and other contracts it appears I define the structure in the header file.
Then create the table variable in the action and it can query the table correctly.

The contract now works correctly for our purposes. Thank you for your help @rommzestz .

Was this page helpful?
0 / 5 - 0 ratings