In this post, I will talk about inline action. Before I start, I have to mention that I referred to this post a lot.

https://blog.csdn.net/itleaks/article/details/80535318

So what we want here is that we want to call the contract on another account. Here we have two accounts “samhero” and “samtre”. And we load code contract on samhero and target contract on samtre. We will call the function in samtre from samhero.

Let’s start coding!

In the code.cpp we have:

++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class hello : public eosio::contract{
public:
using contract::contract;

/// @abi action
void hi( account_name from, account_name to ){
require_auth(from);
print("hello, from:",name{from},", to: ",name{to});
action(
permission_level{to, N(active)},
N(samtre),N(callme),
std::make_tuple(to)
).send();
}

};

EOSIO_ABI(hello, (hi))

Here the action() is the key that we call the function in samtre.

In the target.cpp we have:

++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class target : public eosio::contract{
public:
using contract::contract;

/// @abi action
void callme( account_name user ){
require_auth(user);
print( "Call me from, ", name{user} );
}
};

EOSIO_ABI(target, (callme))

So here are the commands that we are going to type.

1
2
3
4
5
6
7
8
9
10
sam@hero:~/Documents/eos/contracts$ cleos set contract samhero code/ -p samhero
sam@hero:~/Documents/eos/contracts$ cleos set contract samtre target/ -p samtre
sam@hero:~/Documents/eos/contracts$ cleos set account permission samtre active '{"threshold": 1,"keys": [],"accounts": [{"permission":{"actor":"samhero","permission":"eosio.code"},"weight":1}]}' owner -p samtre@owner

sam@hero:~/Documents/eos/contracts$ cleos push action samhero hi '["samhero","samtre"]' -p samhero
executed transaction: 858fd8523d7c0c746d432146a1eb5aafaf5b0dd830257a642a5609645efe9528 112 bytes 1270 us
# samhero <= samhero::hi {"from":"samhero","to":"samtre"}
>> hello, from:samhero, to: samtre
# samtre <= samtre::callme {"user":"samtre"}
>> Call me from, samtre