7 Adding Inline Actions
This section provides instructions for creating inline embedded calls (inline) for actions
. Actions may be called by another action of the same contract. Examples of these instructions are shown on the addressbook address book contract, the creation of which is described in Section 5 of this manual.
7.1 Add cyber.code for permission to action
In order for the action in the address book contract to trigger another action, the contract account must be given permission to do so. To do this, add cyber.code
by executing:
The cyber.code
authority allows the contract to perform embedded calls to actions.
7.2 Send notification
Open the addressbook.cpp
contract and create an additional action that will behave like a transaction that receives a notification. To do this, you can create a helper function in the address book class.
This function will only accept the name and string.
7.3 Create a copy of the transaction and send it to the sender as a notification
Create a copy of the transaction in which the action was loaded and send this copy to the user to be accepted. To do this, you can use the require_recipient
method. Calling require_recipient
adds an account to the require_recipient
set and ensures that these accounts receive notification of the action being performed. The notification is similar to sending accounts to an "exact copy" of the action.
To exclude the possibility of the unauthorized calling of this function by a third-party user, it is necessary to require authorization to perform the action. At the same time authorization must be provided by the contract itself. To do this, add the get_self()
method to authorize yourself.
If an attempt is made to call this function, someone other than the contract itself will generate an exception.
7.4 Notify helper function about sending called action
Since an operation can be called more than once using the built-in invocation, it is necessary to create an auxiliary function to reuse the code in order to reduce duplication.
Inside this helper you need to create an action and send it.
7.5 Create a constructor actionр
7.5.1 Modify an addressbook
contract to send receipts to the user each time the contract is invoked. It is necessary to take into account a case when a record is absent from the table.
Save this object as notification
for action.
The following parameters are required in the action:
permission_level
permission_level
structure;contract to call (initialized using the type
eosio::name
);action (initialized using the type
eosio::name
);data passed to the action.
7.5.2 Initialize the permission structure.
In the contract, the permission must be authorized by the account of the active
contract with get_self()
. To use the built-in «active» credentials, it is necessary that the contract provides active credentials to the cyber.code
code.
7.5.3 Enable get_self()
.
Since the called action is placed in the contract directly, you must use get_self()
. It will also work in the address book \"addressbook\" _n
. Using get_self()
is the most acceptable option, since the contract doesn’t work if it’s deployed under a different account name.
7.5.4 Enable the _n
operator.
The notify
action was previously defined to invoke this inline action. Therefore, it is necessary to use the _n
operator.
7.5.5 Define the data to be transferred to the action.
The notify
function takes two parameters, a name and a string. The constructor action expects data as a bytes
type, so you must use the make_tuple
method, available through the std C++
library. The sent data are determined by the order of parameters received by the called action.
Pass the user
variable, specified as the upsert()
action parameter. Combine the string containing the user name and include message
to send the notify
notification to the action.
7.5.6 Send an action using the send
method.
7.6 Call the auxiliary function and enter the appropriate message
At this point, the helper function is defined and can be called. There are three events when a new notify
helper can be triggered:
After the contract added a new entry:
send_summary (user, «the address book entry was added successfully»)
.After the contract changed an existing entry:
send_summary (user, «the entry in the address book has been changed successfully»)
.After the contract deleted an existing entry:
send_summary (user, «the entry from the address book was successfully deleted»)
.
7.7 Make a change to the macro EOSIO_DISPATCH
As the notify
action was added to the contract, you need to update the EOSIO_DISPATCH macro to enable this action to be available for external execution (for example, an external transaction, a call to an operation (method, action). This ensures that the notify
action is not excluded by the eosio.cdt
optimizer.
The addressbook
contract will be:
7.8 Recompile and edit the ABI file
Recompile the contract with the --abigen
flag, since the contract has been modified to affect ABI.
The contracts for EOSIO are available for the update so this contract ‘addressbook’ can be transferred with the changes.
As a result of the command execution, the following information should appear:
7.9 Testing
In the previous section, the alice
user's address book entry was deleted during the testing phase, therefore, when calling upsert
, the built-in action is run.
Execute:
As a result of the command execution, the following information should appear:
The text (at the bottom of the output) reports that the address book notifies (addressbook::notify
) the user alice
about the transaction.
The cleos get actions
command can be used to view the actions related to the alice
user.
The result of the command will be the following information:
Last updated