Using echo chain interface
Is now in the development stage.

Using echo chain interface

There is a set of classes and functions in chain.hpp and contract_base.hpp for the interaction with the Echo chain.

chain interface includes:

Get balance

Member of class account.
You can get balance of account or contract in needed assets.
Parameters
Option
Description
Example
string id
triplet id of account or contract
1.2.5646 or 1.11.165
string asset_id
triplet asset id
1.3.0
Return
int64_t - requested balance.
Example
1
int64_t my_get_balance(string id, string asset_id)
2
{
3
account other(id);
4
return other.get_balance(asset_id);
5
}
Copied!

Transfer

Member of class contract_base.
You can transfer assets from contract to the recipient.
Parameters
Option
Description
Example
string to
triplet id of account
1.2.5646
string asset_id
triplet asset id
1.3.0
int64_t amount
transferred amount
100
Return
void
Example
1
void my_transfer(string to, string asset_id, int64_t amount)
2
{
3
transfer(to, asset_id, amount);
4
}
Copied!

Get property of objects from echo chain

Member of class object.
You can get property of object from echo chain.
Parameters
Option
Description
Example
string id
triplet id of object
1.2.15
string request
name of property
echorand_key
Return
string - value of requested property.
Example
1
string my_get_property(string id, string request)
2
{
3
object other(id);
4
return other.get_property(request);
5
}
Copied!

Get asset id for current contract call

Member of class contract_base.
You can get asset id with which the contract was called.
Return
uint64_t - asset id with which the contract was called.
Example
1
uint64_t my_get_call_asset_id()
2
{
3
return get_call_asset_id();
4
}
Copied!

Get block hash by number

Static member of class chain, in the namespace x86_64_contract.
You can get block hash by number.
Parameters
Option
Description
Example
uint32_t number
number of block
15445
Return
string - block hash.
Example
1
string my_get_block_hash(uint32_t number)
2
{
3
return chain::get_block_hash(number);
4
}
Copied!

Get current head block author

Member of class contract_base.
You can get the author of current block.
Return
string - block author id.
Example
1
string my_get_block_author()
2
{
3
return get_block_author();
4
}
Copied!

Get current gas limit

Member of class contract_base.
You can get current gas limit.
Return
uint64_t int - current gas limit in 10^-8^ ECHO.
Example
1
uint64_t int my_get_gas_limit()
2
{
3
return get_gas_limit();
4
}
Copied!

Get current head block number

Member of class contract_base.
You can get current head block number.
Return
uint32_t - block number.
Example
1
uint32_t my_get_block_number()
2
{
3
return get_block_number();
4
}
Copied!

Get current head block timestamp

Member of class contract_base.
You can get current head block timestamp.
Return
uint32_t - block timestamp in seconds.
Example
1
uint32_t my_get_block_timestamp()
2
{
3
return get_block_timestamp();
4
}
Copied!

Get send value for current contract call

Member of class contract_base.
You can get send value for current contract call.
Return
int64_t - amount of send.
Example
1
int64_t my_get_send_value()
2
{
3
return get_send_value();
4
}
Copied!

Get sender for current contract call

Member of class contract_base.
You can get sender for current contract call.
Return
string - triplet id of sender.
Example
1
string my_get_sender()
2
{
3
return get_sender();
4
}
Copied!

Get origin sender for current contract call

Member of class contract_base.
You can get origin sender for current contract call.
Return
string - triplet id of sender.
Example
1
string my_get_origin_sender()
2
{
3
return get_origin_sender();
4
}
Copied!

Get called contract id

Static member of class chain, in the namespace x86_64_contract.
You can get called contract id.
Return
uint32_t - id of contract.
Example
1
uint32_t my_get_contract_id()
2
{
3
return chain::get_contract_id();
4
}
Copied!

Get called full contract id

Member of class contract_base.
You can get full contract id.
Return
string - triplet id of contract.
Example
1
string my_get_full_contract_id()
2
{
3
return get_full_contract_id();
4
}
Copied!

Get contract variable from another x86-64 contract

Member of class contract_object.
You can call get storage variable from another x86-64 contract. For the get, you will need to pass the amount by 0, count of gas by 0 and the variable name.
Parameters
Option
Description
Example
string id
triplet id of contract
1.11.165
uint64_t amount
amount, doesn't affect anything
0
uint64_t gas
gas, doesn't affect anything
0
string var_name
variable name
"var_name"
Return
string - result of executing an internal contract call. First 4 bytes(8 characters) - size of output.
Example
1
void call_contract(string id, string var_name, uint64_t amount, uint64_t gas)
2
{
3
contract_object c(id);
4
string var_1 = c.call(amount, gas, var_name);
5
string var_2 = c.call(amount, gas, string("var_name"));
6
}
Copied!

Call x86-64 contract from another x86-64 contract with call line

Member of class contract_object.
You can call another x86-64 contract. For the call, you will need to pass the amount, count of gas to perform and the call line. If you do not want to limit the call to the gas, 0 is transmitted.
Parameters
Option
Description
Example
string id
triplet id of contract
1.11.165
uint64_t amount
amount allocated to the contract, in the smallest units
0 or 1000000
uint64_t gas
gas restriction for internal call, if you set 0, then there will be no limit
0 or 10000000
string call_line
call line
greet() or get_balance(\"1.2.236\", \"1.3.0\")
Return
string - result of executing an internal contract call. First 4 bytes(8 characters) - size of output.
Example
1
string call_contract(string id, string call_line, uint64_t amount, uint64_t gas)
2
{
3
contract_object c(id);
4
return c.call(amount, gas, call_line);
5
}
Copied!

Call x86-64 contract from another x86-64 contract with parameters

Member of class contract_object.
You can call another x86-64 contract. For the call, you will need to pass the amount, count of gas to perform, callee function name and the call params. If you do not want to limit the call to the gas, 0 is transmitted.
Parameters
Option
Description
Example
string id
triplet id of contract
1.11.165
uint64_t amount
amount allocated to the contract, in the smallest units
0 or 1000000
uint64_t int gas
gas restriction for internal call, if you set 0, then there will be no limit
0 or 10000000
string function_name
callee function name
get_balance
args... params
call params
a, true, 637, string("string_param")
Return
string - result of executing an internal contract call. First 4 bytes(8 characters) - size of output.
Example
1
string call_contract(string id, string var_1, unsigned int var_2, uint64_t amount, uint64_t gas)
2
{
3
int var_local = 544;
4
bool var_bool = true;
5
6
contract_object c(id);
7
return c.call(amount, gas, string("my_function"), var_1, var_2, var_local, var_bool, -45);
8
}
Copied!

Echo interface contract example

1
#include "chain.hpp"
2
#include "contract_base.hpp"
3
#include "db_types.hpp"
4
#include "parameters.hpp"
5
#include "string.hpp"
6
7
namespace x86_64_contract {
8
9
class contract : public contract_base
10
{
11
public:
12
void send_to_contract()
13
{
14
_value += get_send_value();
15
}
16
17
bool transfer(string to, string asset_id, uint64_t amount)
18
{
19
string sender = get_origin_sender();
20
uint64_t send_with_call = get_send_value();
21
22
object sender_obj(sender);
23
string sender_name = sender_obj.get_property("name");
24
25
if (sender != _owner && sender_name != "my_second_account")
26
{
27
if (send_with_call < amount)
28
return false;
29
else
30
{
31
transfer(to, asset_id, amount);
32
_value += send_with_call - amount;
33
return true;
34
}
35
}
36
37
if (_value + send_with_call < amount)
38
return false;
39
40
transfer(to, asset_id, amount);
41
_value += send_with_call - amount;
42
return true;
43
}
44
45
void constructor() override
46
{
47
_owner = get_origin_sender();
48
}
49
50
private:
51
string _name { "my_contract" };
52
DB_STRING(_owner);
53
DB_UINT64(_value);
54
};
55
56
MAIN(contract, FUNC(send_to_contract) FUNC(transfer))
57
58
}
Copied!
Last modified 1yr ago