8 Inline Action to External Contract
This section of the guide provides instructions for sending actions to an external contract. The examples of the execution of instructions are shown in the contract, where the performed actions are kept.
8.1 Addressbook counter address contract
Log into CONTRACTS_DIR and create a directory there with the name abcounter, as well as the file abcounter.cpp.
cd CONTRACTS_DIR
mkdir abcounter
cd abcounter
touch abcounter.cppOpen the abcounter.cpp file and write the following program text into it:
#include <eosiolib/eosio.hpp>
using namespace eosio;
class [[eosio::contract]] abcounter : public eosio::contract {
private:
struct [[eosio::table]] counter {
name key;
uint64_t emplaced;
uint64_t modified;
uint64_t erased;
uint64_t primary_key() const {
return key.value;
}
};
using count_index = eosio::multi_index<"counter"_n, counter>;
public:
using contract::contract;
abcounter(name receiver, name code, datastream<const char*> ds):
contract(receiver, code, ds) {}
[[eosio::action]]
void count(name user, std::string type) {
require_auth( name("addressbook"));
count_index counts(name(_code), _code.value);
auto iterator = counts.find(user.value);
if (iterator == counts.end()) {
counts.emplace("addressbook"_n, [&]( auto& row ) {
row.key = user;
row.emplaced = (type == "emplace") ? 1 : 0;
row.modified = (type == "modify") ? 1 : 0;
row.erased = (type == "erase") ? 1 : 0;
});
}
else {
counts.modify(iterator, "addressbook"_n, [&]( auto& row ) {
if(type == "emplace") { row.emplaced += 1; }
if(type == "modify") { row.modified += 1; }
if(type == "erase") { row.erased += 1; }
});
}
}
};
EOSIO_DISPATCH( abcounter, (count));The peculiarity of this text is that it has a restriction on calls to an action for the contract account. For restriction, use require_auth for the addressbook contract. Only the addressbook contract account is authorized to perform require_auth. The count action is not sent by the user, but by the addressbook contract.
8.2 Create an account for abcounter contract
8.3 Compile and install the abcounter contract
8.4 Modify the contract addressbook to send inline actions to a new abcounter contract
Open the file addressbook.cpp and create in it another helper named increment_counter in the private part of the contract.
The action body contains:
выдача разрешения уровня
active.Для разрешения функцияget_self()возвращает текущий контрактaddressbook;the
abcounteraccount name of the contract;the
countaction to call;data, username and line type.
Add calls to «set», «modify» and «erase» helpers.
As a result, the addressbook.cpp file should look like this:
8.5 Recompile and set the addressbook contract
Since the changes should not affect the ABI, you do not need to re-edit the ABI file (make sure that this file has not been updated).
8.6 Perform testing of sending an action from an addressbook contract to an abcounter contract
Before testing, make sure that the abcounter and addressbook contracts are set.
8.6.1 Verify that a notification is sent to the abcounter contract, as well as a change in the entry in the addressbook contract table by executing:
The action is considered successful if the output contains the following information:
8.6.2 Check the appearance of a row in the table for the alice user.
The action is considered successful if the result is the following:
8.6.3 Check that the upsert method modifies the record.
The actions are considered successfully completed if the resulting output contains the following information:
8.6.4 Verify the liquidation of the alice user entry from the table by running:
The action is considered to be successfully completed if the resulting output contains the following information:
8.6.5 Test the ability to manipulate abcounter contract data by executing:
The abcounter contract table should contain the following information:
Last updated