telos docs
Search…
2.7: Inline Actions to External Contracts
Previously, we sent an inline action to an action that was defined in the contract. In this part of the tutorial, we'll explore sending actions to an external contract. Since we've already gone over quite a bit of contract authoring, we'll keep this contract extremely simple. We'll author a contract that counts actions written by the contract. This contract has very little real-world use, but will demonstrate inline action calls to an external contract

Step 1: The Addressbook Counter Contract

Navigate to CONTRACTS_DIR if not already there, create a directory called abcounter and then create a abcounter.cpp file
1
cd CONTRACTS_DIR
2
mkdir abcounter
3
touch abcounter.cpp
Copied!
Open the abcounter.cpp file in your favorite editor and paste the following code into the file. This contract is very basic, and for the most part does not cover much that we haven't already covered up until this point. There are a few exceptions though, and they are covered in full below.
1
#include <eosio/eosio.hpp>
2
3
using namespace eosio;
4
5
class [[eosio::contract("abcounter")]] abcounter : public eosio::contract {
6
public:
7
8
abcounter(name receiver, name code, datastream<const char*> ds): contract(receiver, code, ds) {}
9
10
[[eosio::action]]
11
void count(name user, std::string type) {
12
require_auth( name("addressbook"));
13
count_index counts(get_first_receiver(), get_first_receiver().value);
14
auto iterator = counts.find(user.value);
15
16
if (iterator == counts.end()) {
17
counts.emplace("addressbook"_n, [&]( auto& row ) {
18
row.key = user;
19
row.emplaced = (type == "emplace") ? 1 : 0;
20
row.modified = (type == "modify") ? 1 : 0;
21
row.erased = (type == "erase") ? 1 : 0;
22
});
23
}
24
else {
25
counts.modify(iterator, "addressbook"_n, [&]( auto& row ) {
26
if(type == "emplace") { row.emplaced += 1; }
27
if(type == "modify") { row.modified += 1; }
28
if(type == "erase") { row.erased += 1; }
29
});
30
}
31
}
32
33
using count_action = action_wrapper<"count"_n, &abcounter::count>;
34
35
private:
36
struct [[eosio::table]] counter {
37
name key;
38
uint64_t emplaced;
39
uint64_t modified;
40
uint64_t erased;
41
uint64_t primary_key() const { return key.value; }
42
};
43
44
using count_index = eosio::multi_index<"counts"_n, counter>;
45
};
Copied!
The first new concept in the code above is that we are explicitly restricting calls to the one action to a specific account in this contract using require_auth to the addressbook contract, as seen below.
1
//Only the addressbook account/contract can authorize this command.
2
require_auth( name("addressbook"));
Copied!
Previously, a dynamic value was used with require_auth.
Another new concept in the code above, is action wrapper. As shown below the first template parameter is the 'action' we are going to call and the second one should point to the action function
1
using count_action = action_wrapper<"count"_n, &abcounter::count>;
Copied!

Step 2: Create Account for abcounter Contract

Open your terminal and execute the following command to create the abcounter user.
1
cleos create account eosio abcounter YOUR_PUBLIC_KEY
Copied!

Step 3: Compile and Deploy

1
eosio-cpp abcounter.cpp -o abcounter.wasm
Copied!
Finally, deploy the abcounter contract.
1
cleos set contract abcounter CONTRACTS_DIR/abcounter
Copied!

Step 4: Modify addressbook contract to send inline-action to abcounter

Navigate to your addressbook directory now.
1
cd CONTRACTS_DIR/addressbook
Copied!
Open the addressbook.cpp file in your favorite editor if not already open.
In the last part of this series, we went over inline actions to our own contract. This time, we are going to send an inline action to another contract, our new abcounter contract.
Create another helper called increment_counter under the private declaration of the contract as below:
1
void increment_counter(name user, std::string type) {
2
abcounter::count_action count("abcounter"_n, {get_self(), "active"_n});
3
count.send(user, type);
4
}
Copied!
Let's go through the code listing above.
This time we use the action wrapper instead of calling a function. To do that, we firstly initialised the count_action object defined earlier. The first parameter we pass is the callee contract name, in this case abcounter. The second parameter is the permission struct.
  • For the permission, get_self() returns the current addressbook contract. The active permission of addressbook is used.
Unlike the Adding Inline Actions tutorial, we won't need to specify the action because the action wrapper type incorporates the action when it is defined.
In line 3 we call the action with the data, namely user and type which are required by the abcounter contract.
Now, add the following calls to the helpers in their respective action scopes.
1
//Emplace
2
increment_counter(user, "emplace");
3
//Modify
4
increment_counter(user, "modify");
5
//Erase
6
increment_counter(user, "erase");
Copied!
Now your addressbook.cpp contract should look like this.
1
#include <eosio/eosio.hpp>
2
#include "abcounter.cpp"
3
4
using namespace eosio;
5
6
class [[eosio::contract("addressbook")]] addressbook : public eosio::contract {
7
8
public:
9
10
addressbook(name receiver, name code, datastream<const char*> ds): contract(receiver, code, ds) {}
11
12
[[eosio::action]]
13
void upsert(name user, std::string first_name, std::string last_name,
14
uint64_t age, std::string street, std::string city, std::string state) {
15
require_auth(user);
16
address_index addresses(get_first_receiver(), get_first_receiver().value);
17
auto iterator = addresses.find(user.value);
18
if( iterator == addresses.end() )
19
{
20
addresses.emplace(user, [&]( auto& row ) {
21
row.key = user;
22
row.first_name = first_name;
23
row.last_name = last_name;
24
row.age = age;
25
row.street = street;
26
row.city = city;
27
row.state = state;
28
});
29
send_summary(user, " successfully emplaced record to addressbook");
30
increment_counter(user, "emplace");
31
}
32
else {
33
std::string changes;
34
addresses.modify(iterator, user, [&]( auto& row ) {
35
row.key = user;
36
row.first_name = first_name;
37
row.last_name = last_name;
38
row.age = age;
39
row.street = street;
40
row.city = city;
41
row.state = state;
42
});
43
send_summary(user, " successfully modified record to addressbook");
44
increment_counter(user, "modify");
45
}
46
}
47
48
[[eosio::action]]
49
void erase(name user) {
50
require_auth(user);
51
52
address_index addresses(get_first_receiver(), get_first_receiver().value);
53
54
auto iterator = addresses.find(user.value);
55
check(iterator != addresses.end(), "Record does not exist");
56
addresses.erase(iterator);
57
send_summary(user, " successfully erased record from addressbook");
58
increment_counter(user, "erase");
59
}
60
61
[[eosio::action]]
62
void notify(name user, std::string msg) {
63
require_auth(get_self());
64
require_recipient(user);
65
}
66
67
private:
68
struct [[eosio::table]] person {
69
name key;
70
std::string first_name;
71
std::string last_name;
72
uint64_t age;
73
std::string street;
74
std::string city;
75
std::string state;
76
77
uint64_t primary_key() const { return key.value; }
78
uint64_t get_secondary_1() const { return age;}
79
};
80
81
void send_summary(name user, std::string message) {
82
action(
83
permission_level{get_self(),"active"_n},
84
get_self(),
85
"notify"_n,
86
std::make_tuple(user, name{user}.to_string() + message)
87
).send();
88
};
89
90
void increment_counter(name user, std::string type) {
91
abcounter::count_action count("abcounter"_n, {get_self(), "active"_n});
92
count.send(user, type);
93
}
94
95
typedef eosio::multi_index<"people"_n, person,
96
indexed_by<"byage"_n, const_mem_funuint64_t, &person::get_secondary_1>>
97
> address_index;
98
};
Copied!

Step 5: Recompile and redeploy the addressbook contract

Recompile the addressbook.cpp contract, we don't need to regenerate the ABI, because none of our changes have affected the ABI. Note here we include the abcounter contract folder with the -I option.
1
eosio-cpp -o addressbook.wasm addressbook.cpp -I ../abcounter/
Copied!
Redeploy the contract
1
cleos set contract addressbook CONTRACTS_DIR/addressbook
Copied!

Step 6: Test It.

Now that we have the abcounter deployed and addressbook redeployed, we're ready for some testing.
1
cleos push action addressbook upsert '["alice", "alice", "liddell", 19, "123 drink me way", "wonderland", "amsterdam"]' -p [email protected]
Copied!
1
executed transaction: cc46f20da7fc431124e418ecff90aa882d9ca017a703da78477b381a0246eaf7 152 bytes 1493 us
2
# addressbook <= addressbook::upsert {"user":"alice","first_name":"alice","last_name":"liddell","street":"123 drink me way","city":"wonde...
3
# addressbook <= addressbook::notify {"user":"alice","msg":"alice successfully modified record in addressbook"}
4
# alice <= addressbook::notify {"user":"alice","msg":"alice successfully modified record in addressbook"}
5
# abcounter <= abcounter::count {"user":"alice","type":"modify"}
Copied!
As you can see, the counter was successfully notified. Let's check the table now.
1
cleos get table abcounter abcounter counts --lower alice --limit 1
Copied!
1
{
2
"rows": [{
3
"key": "alice",
4
"emplaced": 1,
5
"modified": 0,
6
"erased": 0
7
}
8
],
9
"more": false
10
}
Copied!
Test each of the actions and check the counter. There's already a row for alice, so upsert should modify the record.
1
cleos push action addressbook upsert '["alice", "alice", "liddell", 21,"1 there we go", "wonderland", "amsterdam"]' -p [email protected]
Copied!
1
executed transaction: c819ffeade670e3b44a40f09cf4462384d6359b5e44dd211f4367ac6d3ccbc70 152 bytes 909 us
2
# addressbook <= addressbook::upsert {"user":"alice","first_name":"alice","last_name":"liddell","street":"1 coming down","city":"normalla...
3
# addressbook <= addressbook::notify {"user":"alice","msg":"alice successfully emplaced record to addressbook"}
4
>> Notified
5
# alice <= addressbook::notify {"user":"alice","msg":"alice successfully emplaced record to addressbook"}
6
# abcounter <= abcounter::count {"user":"alice","type":"emplace"}
7
warning: transaction executed locally, but may not be confirmed by the network yet ]
Copied!
To erase:
1
cleos push action addressbook erase '["alice"]' -p [email protected]
Copied!
1
executed transaction: aa82577cb1efecf7f2871eac062913218385f6ab2597eaf31a4c0d25ef1bd7df 104 bytes 973 us
2
# addressbook <= addressbook::erase {"user":"alice"}
3
>> Erased
4
# addressbook <= addressbook::notify {"user":"alice","msg":"alice successfully erased record from addressbook"}
5
>> Notified
6
# alice <= addressbook::notify {"user":"alice","msg":"alice successfully erased record from addressbook"}
7
# abcounter <= abcounter::count {"user":"alice","type":"erase"}
8
warning: transaction executed locally, but may not be confirmed by the network yet ]
9
Toaster:addressbook sandwich$
Copied!
Next, we'll test if we can manipulate the data in abcounter contract by calling it directly.
1
cleos push action abcounter count '["alice", "erase"]' -p [email protected]
Copied!
Checking the table in abcounter we'll see the following:
1
cleos get table abcounter abcounter counts --lower alice
Copied!
1
{
2
"rows": [{
3
"key": "alice",
4
"emplaced": 1,
5
"modified": 1,
6
"erased": 1
7
}
8
],
9
"more": false
10
}
Copied!
Wonderful! Since we require_auth for name("addressbook"), only the addressbook contract can successfully execute this action, the call by alice to fudge the numbers had no affect on the table.

Extra Credit: More Verbose Receipts

The following modification sends custom receipts based on changes made, and if no changes are made during a modification, the receipt will reflect this situation.
1
#include <eosio/eosio.hpp>
2
#include "abcounter.cpp"
3
4
using namespace eosio;
5
6
class [[eosio::contract("addressbook")]] addressbook : public eosio::contract {
7
8
public:
9
10
addressbook(name receiver, name code, datastream<const char*> ds): contract(receiver, code, ds) {}
11
12
[[eosio::action]]
13
void upsert(name user, std::string first_name, std::string last_name, uint64_t age, std::string street, std::string city, std::string state) {
14
require_auth(user);
15
16
address_index addresses(get_first_receiver(), get_first_receiver().value);
17
18
auto iterator = addresses.find(user.value);
19
if( iterator == addresses.end() )
20
{
21
addresses.emplace(user, [&]( auto& row ){
22
row.key = user;
23
row.first_name = first_name;
24
row.last_name = last_name;
25
row.age = age;
26
row.street = street;
27
row.city = city;
28
row.state = state;
29
send_summary(user, " successfully emplaced record to addressbook");
30
increment_counter(user, "emplace");
31
});
32
}
33
else {
34
std::string changes;
35
addresses.modify(iterator, user, [&]( auto& row ) {
36
37
if(row.first_name != first_name) {
38
row.first_name = first_name;
39
changes += "first name ";
40
}
41
42
if(row.last_name != last_name) {
43
row.last_name = last_name;
44
changes += "last name ";
45
}
46
47
if(row.age != age) {
48
row.age = age;
49
changes += "age ";
50
}
51
52
if(row.street != street) {
53
row.street = street;
54
changes += "street ";
55
}
56
57
if(row.city != city) {
58
row.city = city;
59
changes += "city ";
60
}
61
62
if(row.state != state) {
63
row.state = state;
64
changes += "state ";
65
}
66
});
67
68
if(changes.length() > 0) {
69
send_summary(user, " successfully modified record in addressbook. Fields changed: " + changes);
70
increment_counter(user, "modify");
71
} else {
72
send_summary(user, " called upsert, but request resulted in no changes.");
73
}
74
}
75
}
76
77
[[eosio::action]]
78
void erase(name user) {
79
require_auth(user);
80
address_index addresses(get_first_receiver(), get_first_receiver().value);
81
auto iterator = addresses.find(user.value);
82
check(iterator != addresses.end(), "Record does not exist");
83
addresses.erase(iterator);
84
send_summary(user, " successfully erased record from addressbook");
85
increment_counter(user, "erase");
86
}
87
88
[[eosio::action]]
89
void notify(name user, std::string msg) {
90
require_auth(get_self());
91
require_recipient(user);
92
}
93
94
private:
95
96
struct [[eosio::table]] person {
97
name key;
98
std::string first_name;
99
std::string last_name;
100
uint64_t age;
101
std::string street;
102
std::string city;
103
std::string state;
104
uint64_t primary_key() const { return key.value; }
105
uint64_t get_secondary_1() const { return age;}
106
};
107
108
void send_summary(name user, std::string message) {
109
action(
110
permission_level{get_self(),"active"_n},
111
get_self(),
112
"notify"_n,
113
std::make_tuple(user, name{user}.to_string() + message)
114
).send();
115
};
116
117
void increment_counter(name user, std::string type) {
118
119
action counter = action(
120
permission_level{get_self(),"active"_n},
121
"abcounter"_n,
122
"count"_n,
123
std::make_tuple(user, type)
124
);
125
126
counter.send();
127
}
128
129
typedef eosio::multi_index<"people"_n, person, indexed_by<"byage"_n, const_mem_funuint64_t, &person::get_secondary_1>>> address_index;
130
};
Copied!

What's Next?

Last modified 1yr ago