Overview
This guide provides instructions which show you how to create an action which requires the user to pay for the resources needed to store data in a Key-Value Table
(kv table
).
Use the method put
defined by the eosio::kv::table
type to accomplish this task and provide as the second parameter the account name which is the payer for the resources needed.
Before you begin
Make sure you have the following prerequisites in place:
- An EOSIO development environment, for details consult the Get Started Guide
- A smart contract named
smrtcontract
- A user defined type,
struct
orclass
, which defines the data stored in the map, namedperson
- A
kv table
data type,struct
orclass
, which inheritseosio::kv::table
, and stores objects of typeperson
, namedaddress_table
- A primary index is defined for the
kv table
for theperson::account_name
data member
Refer to the following reference implementation for your starting point:
smartcontract.hpp file
struct person {
eosio::name account_name;
std::string first_name;
std::string last_name;
std::string personal_id;
};
class [[eosio::contract]] smrtcontract : public contract {
struct [[eosio::table]] address_table : eosio::kv::table<person, "kvaddrbook"_n> {
index<name> account_name_uidx {
name{"accname"_n},
&person::account_name };
address_table(name contract_name) {
init(contract_name, account_name_uidx)
}
};
public:
using contract::contract;
};
Procedure
Complete the following steps to allow the payer
account, to be the payer for the resources needed to store a person object in the kv table
:
- Create a new action
upsert
in your smart contact class, which takes as input parameters anaccount name, a first name, a last name, a personal id
which define a person data, and anaccount name
for the payer. - In the
upsert
action access the instance ofaddress_table
belonging to this contract by declaring a local variable ofaddress_table
type and pass the contract name as paramter. - Call the
put
method of theaddress_table
and pass to it a newly createdperson
object based on the action’s input parameters and the payer account name.
Refer to the following reference implementation to allow a specific account name to be the payer for the resources needed to store a person object in the kv table
:
smartcontract.hpp file
class [[eosio::contract]] smrtcontract : public contract {
struct [[eosio::table]] address_table : eosio::kv::table<person, "kvaddrbook"_n> {
index<name> account_name_uidx {
name{"accname"_n},
&person::account_name };
address_table(name contract_name) {
init(contract_name, account_name_uidx)
}
};
public:
using contract::contract;
// creates if not exists, or updates if already exists, a person
[[eosio::action]]
void upsert(eosio::name account_name,
string first_name,
string last_name,
string country,
string personal_id,
eosio::name payer);
using upsert_action = action_wrapper<"upsert"_n, &smrtcontract::upsert>;
};
smartcontract.cpp file
// creates if not exists, or updates if already exists, a person
[[eosio::action]]
void smrtcontract::upsert(
eosio::name account_name,
string first_name,
string last_name,
string personal_id,
eosio::name payer) {
address_table addresses{"kvaddrbook"_n};
// upsert into kv table
addresses.put({account_name, first_name, last_name, personal_id}, payer);
}
Summary
In conclusion, the above instructions show how to create an action which requires the user to pay for the resources needed to store data in a Key-Value Table
(kv table
).
Next Steps
- Check if the newly inserted
person
actually exists in the table. To accomplish this task, use theexists()
function of any index defined for the table. - Retrieve the newly inserted or updated
person
from the table. To accomplish this task, use thefind()
function of any index defined for the table. - Delete the newly created or updated
person
from the table. To accomplish this task, use theerase()
function of thekv table
.