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
cd CONTRACTS_DIR
mkdir abcounter
touch abcounter.cpp
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.
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.
//Only the addressbook account/contract can authorize this command.require_auth( name("addressbook"));
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
using count_action = action_wrapper<"count"_n, &abcounter::count>;
Step 2: Create Account for abcounter Contract
Open your terminal and execute the following command to create the abcounter user.
infra-cli set contract abcounter CONTRACTS_DIR/abcounter
Step 4: Modify addressbook contract to send inline-action to abcounter
Navigate to your addressbook directory now.
cd CONTRACTS_DIR/addressbook
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:
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.
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.
Test each of the actions and check the counter. There's already a row for alice, so upsert shouldmodify the record.
infra-cli push action addressbook upsert '["alice", "alice", "liddell", 21,"1 there we go", "wonderland", "amsterdam"]' -p alice@active
executed transaction: c819ffeade670e3b44a40f09cf4462384d6359b5e44dd211f4367ac6d3ccbc70 152 bytes 909 us
# addressbook <= addressbook::upsert {"user":"alice","first_name":"alice","last_name":"liddell","street":"1 coming down","city":"normalla...
# addressbook <= addressbook::notify {"user":"alice","msg":"alice successfully emplaced record to addressbook"}
>> Notified
# alice <= addressbook::notify {"user":"alice","msg":"alice successfully emplaced record to addressbook"}
# abcounter <= abcounter::count {"user":"alice","type":"emplace"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
executed transaction: aa82577cb1efecf7f2871eac062913218385f6ab2597eaf31a4c0d25ef1bd7df 104 bytes 973 us
# addressbook <= addressbook::erase {"user":"alice"}
>> Erased
# addressbook <= addressbook::notify {"user":"alice","msg":"alice successfully erased record from addressbook"}
>> Notified
# alice <= addressbook::notify {"user":"alice","msg":"alice successfully erased record from addressbook"}
# abcounter <= abcounter::count {"user":"alice","type":"erase"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
Toaster:addressbook sandwich$
Next, we'll test if we can manipulate the data in abcounter contract by calling it directly.
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.