Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use EOS multi-index table

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use EOS multi-index table, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In order to have an in-depth and clear understanding of the multi-index table, the final .cpp file section will be discussed in further detail. Note that the complete .cpp file can be found at the bottom of the page.

Glossary

Code: refers to the account_name that has announced a smart contract.

The range of data involved in scope:account_name.

Table_name: the name of the table stored in memory.

The structure to be stored by code decomposition

The data to be stored in a multi-index table is a limit_order structure. The primary_key (), get_expiration (), get_price () functions are used to return the table. The returned table is sorted according to the function called.

Struct limit_order {uint64_t id; uint128_t price; uint64_t expiration; account_name owner; auto primary_key () const {return id;} uint64_t get_expiration () const {return expiration;} uint128_t get_price () const {return price;} EOSLIB_SERIALIZE (limit_order, (id) (price) (expiration) (owner))}; create a multi-index table auto payer = ilm.get_account ();

Payer is a variable that holds the account. It adds billing elements to the multi-index table and modifies elements that are already in the multi-index table.

... eosio::multi_index

< N( orders ), limit_order, ... N(orders)是多索引表的名称,limit_order是要存储在表中的数据。 ... indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration>

>,.

Indexed_by

< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration>

> defines the indexing method of multi-index tables. N (byexp) is the name of this index. Const_mem_fun indicates the data type being queried, the variable type of limit_order is uint64_t, and you will use the get_expiration function to get the variable.

... Indexed_by

< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price>

>.

Indexed_by

< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price>

> defines the indexing method of multi-index tables. N (byprice) is the name of this index. Const_mem_fun indicates the data type being queried, the variable type of limit_order is uint128_t, and you will use the get_price function to get the variable.

Orders (N (limitorders), N (limitorders))

Orders is a multi-index table.

Auto payer = ilm.get_account (); print ("Creating multi index table 'orders'.\ n"); eosio::multi_index

< N( orders ), limit_order, indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration>

>, indexed_by

< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price>

> > orders (N (limitorders), N (limitorders)); add multiple index tables

Next, add two limit_order to the orders table. Note that payer is the "bill" account that is modifying the orders table.

Orders.emplace (payer, [&] (auto& o) {o.id = 1; o.expiration = 300; o.owner = N (dan);}); auto order2 = orders.emplace (payer, [&] (auto& o) {o.id = 2; o.expiration = 200; o.owner = N (thomas);}); sort by primary key

The default orders table is sorted by primary key.

Print ("Items sorted by primary key:\ n"); for (const auto& item: orders) {print ("ID=", item.id, ", expiration=", item.expiration, ", owner=", name {item.owner}, "\ n");} sort by second index expiration

The orders table is sorted by expiration and assigned to expidx.

Auto expidx = orders.get_index (); print ("Items sorted by expiration:\ n"); for (const auto& item: expidx) {print ("ID=", item.id, ", expiration=", item.expiration, ", owner=", name {item.owner}, "\ n");} sort by second index price

The orders table is sorted by price and assigned to oridx.

Auto pridx = orders.get_index (); print ("Items sorted by price:\ n"); for (const auto& item: pridx) {print ("ID=", item.id, ", expiration=", item.expiration, ", owner=", name {item.owner}, "\ n");} modify an input value

Next, the entry for "ID=2" is modified. Note that payer is the "bill" account that is modifying the orders table.

Print ("Modifying expiration of order with ID=2 to 400.\ n"); orders.modify (order2, payer, [&] (auto& o) {o.expiration = 400;}); get a minimum value auto lower = expidx.lower_bound; print ("First order with an expiration of at least 100 has ID=", lower- > id, "and expiration=", lower- > get_expiration (), "\ n"); complete .cpp file # include # include # include using namespace eosio Namespace limit_order_table {struct limit_order {uint64_t id; uint128_t price; uint64_t expiration; account_name owner; auto primary_key () const {return id;} uint64_t get_expiration () const {return expiration;} uint128_t get_price () const {return price } EOSLIB_SERIALIZE (limit_order, (id) (price) (expiration) (owner))}; class limit_order_table {public: ACTION (N (limitorders), issue_limit_order) {EOSLIB_SERIALIZE (issue_limit_order)}; static void on (const issue_limit_order& ilm) {auto payer = ilm.get_account () Print ("Creating multi index table 'orders'.\ n"); eosio::multi_index

< N( orders ), limit_order, indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration>

>, indexed_by

< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price>

> orders (N (limitorders), N (limitorders)); orders.emplace (payer, [&] (auto& o) {o.id = 1; o.expiration = 300; o.owner = N (dan);}) Auto order2 = orders.emplace (payer, [&] (auto& o) {o.id = 2; o.expiration = 200; o.owner = N (thomas);}); print ("Items sorted by primary key:\ n") For (const auto& item: orders) {print ("ID=", item.id, ", expiration=", item.expiration, ", owner=", name {item.owner}, "\ n");} auto expidx = orders.get_index (); print ("Items sorted by expiration:\ n") For (const auto& item: expidx) {print ("ID=", item.id, ", expiration=", item.expiration, ", owner=", name {item.owner}, "\ n");} auto pridx = orders.get_index (); print ("Items sorted by price:\ n") For (const auto& item: pridx) {print ("ID=", item.id, ", expiration=", item.expiration, ", owner=", name {item.owner}, "\ n");} print ("Modifying expiration of order with ID=2 to 400.\ n") Orders.modify (order2, payer, [&] (auto& o) {o.expiration = 400;}); auto lower = expidx.lower_bound; print ("First order with an expiration of at least 100 has ID=", lower- > id, "and expiration=", lower- > get_expiration (), "\ n");} } / limit_order_tablenamespace limit_order_table {extern "C" {/ The apply method implements the dispatch of events to this contract void apply (uint64_t code, uint64_t action) {require_auth (code); eosio_assert (eosio::dispatch

< limit_order_table, limit_order_table::issue_limit_order >

(code, action), "Could not dispatch");} delete the table

The table cannot be deleted directly, but after all rows are deleted, the table is automatically deleted.

On how to use EOS multi-index table to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report