-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaergo.hpp
More file actions
181 lines (137 loc) · 7.26 KB
/
aergo.hpp
File metadata and controls
181 lines (137 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
extern "C" {
#include "aergo.h"
}
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Aergo {
aergo *instance;
template<typename Arg>
void add_json_value(stringstream &result, Arg arg){
if (result.rdbuf()->in_avail())
result << ",";
if constexpr (std::is_same_v<Arg, string>) {
result << "\"" << arg << "\"";
} else if constexpr (std::is_same_v<Arg, const char*>) {
result << "\"" << arg << "\"";
} else if constexpr (std::is_same_v<Arg, char*>) {
result << "\"" << arg << "\"";
} else if constexpr (std::is_same_v<Arg, bool>) {
result << std::boolalpha;
result << arg;
result << std::noboolalpha;
} else if constexpr (std::is_same_v<Arg, nullptr_t>) {
result << "null";
} else {
result << arg;
}
}
template<typename... Args>
std::string to_json_array(const Args&... args) {
std::stringstream result;
(add_json_value(result, args), ...);
return "[" + result.str() + "]";
}
public:
// Constructor
Aergo(string host, int port) {
instance = aergo_connect(host.c_str(), port);
}
// Destructor
~Aergo() {
aergo_free(instance);
}
int process_requests(int timeout = 0) {
return aergo_process_requests(instance, timeout);
}
// Accounts
bool check_privkey(aergo_account *account) {
return aergo_check_privkey(instance, account);
}
bool get_account_state(aergo_account *account, char *error) {
return aergo_get_account_state(instance, account, error);
}
// Transfer - synchronous
bool transfer(transaction_receipt *receipt, aergo_account *from_account, string to_account, double value) {
return aergo_transfer(instance, receipt, from_account, to_account.c_str(), value);
}
bool transfer(transaction_receipt *receipt, aergo_account *from_account, string to_account, uint64_t integer, uint64_t decimal) {
return aergo_transfer_int(instance, receipt, from_account, to_account.c_str(), integer, decimal);
}
bool transfer(transaction_receipt *receipt, aergo_account *from_account, string to_account, string value) {
return aergo_transfer_str(instance, receipt, from_account, to_account.c_str(), value.c_str());
}
bool transfer(transaction_receipt *receipt, aergo_account *from_account, string to_account, unsigned char *amount, int len) {
return aergo_transfer_bignum(instance, receipt, from_account, to_account.c_str(), amount, len);
}
// Transfer - asynchronous
bool transfer_async(transaction_receipt_cb cb, void *arg, aergo_account *from_account, string to_account, double value) {
return aergo_transfer_async(instance, cb, arg, from_account, to_account.c_str(), value);
}
bool transfer_async(transaction_receipt_cb cb, void *arg, aergo_account *from_account, string to_account, uint64_t integer, uint64_t decimal) {
return aergo_transfer_int_async(instance, cb, arg, from_account, to_account.c_str(), integer, decimal);
}
bool transfer_async(transaction_receipt_cb cb, void *arg, aergo_account *from_account, string to_account, string value) {
return aergo_transfer_str_async(instance, cb, arg, from_account, to_account.c_str(), value.c_str());
}
bool transfer_async(transaction_receipt_cb cb, void *arg, aergo_account *from_account, string to_account, unsigned char *amount, int len) {
return aergo_transfer_bignum_async(instance, cb, arg, from_account, to_account.c_str(), amount, len);
}
// Call smart contract function - synchronous
bool call_smart_contract_json(transaction_receipt *receipt, aergo_account *account, string contract_address, string function, string args) {
return aergo_call_smart_contract_json(instance, receipt, account, contract_address.c_str(), function.c_str(), args.c_str());
}
template<typename... Args>
bool call_smart_contract(transaction_receipt *receipt, aergo_account *account, string contract_address, string function, Args... args) {
string json_args = to_json_array(args...);
return aergo_call_smart_contract_json(instance, receipt, account, contract_address.c_str(), function.c_str(), json_args.c_str());
}
// Call smart contract function - asynchronous
bool call_smart_contract_json_async(transaction_receipt_cb cb, void *arg, aergo_account *account, string contract_address, string function, string args) {
return aergo_call_smart_contract_json_async(instance, cb, arg, account, contract_address.c_str(), function.c_str(), args.c_str());
}
template<typename... Args>
bool call_smart_contract_async(transaction_receipt_cb cb, void *arg, aergo_account *account, string contract_address, string function, Args... args) {
string json_args = to_json_array(args...);
return aergo_call_smart_contract_json_async(instance, cb, arg, account, contract_address.c_str(), function.c_str(), json_args.c_str());
}
// MultiCall - synchronous
bool multicall(transaction_receipt *receipt, aergo_account *account, string payload) {
return aergo_multicall(instance, receipt, account, payload.c_str());
}
// MultiCall - asynchronous
bool multicall_async(transaction_receipt_cb cb, void *arg, aergo_account *account, string payload) {
return aergo_multicall_async(instance, cb, arg, account, payload.c_str());
}
// Query smart contract - synchronous
bool query_smart_contract_json(char *result, int resultlen, string contract_address, string function, string args) {
return aergo_query_smart_contract_json(instance, result, resultlen, contract_address.c_str(), function.c_str(), args.c_str());
}
template<typename... Args>
bool query_smart_contract(char *result, int resultlen, string contract_address, string function, Args... args) {
string json_args = to_json_array(args...);
return aergo_query_smart_contract_json(instance, result, resultlen, contract_address.c_str(), function.c_str(), json_args.c_str());
}
// Query smart contract - asynchronous
bool query_smart_contract_json_async(query_smart_contract_cb cb, void *arg, string contract_address, string function, string args) {
return aergo_query_smart_contract_json_async(instance, cb, arg, contract_address.c_str(), function.c_str(), args.c_str());
}
template<typename... Args>
bool query_smart_contract_async(query_smart_contract_cb cb, void *arg, string contract_address, string function, Args... args) {
string json_args = to_json_array(args...);
return aergo_query_smart_contract_json_async(instance, cb, arg, contract_address.c_str(), function.c_str(), json_args.c_str());
}
// Query smart contract state variable - synchronous
bool query_smart_contract_state_variable(char *result, int resultlen, string contract_address, string state_var) {
return aergo_query_smart_contract_state_variable(instance, result, resultlen, contract_address.c_str(), state_var.c_str());
}
// Query smart contract state variable - asynchronous
bool query_smart_contract_state_variable_async(query_state_var_cb cb, void *arg, string contract_address, string state_var) {
return aergo_query_smart_contract_state_variable_async(instance, cb, arg, contract_address.c_str(), state_var.c_str());
}
// Smart contract events
bool contract_events_subscribe(string contract_address, string event_name, contract_event_cb cb, void *arg) {
return aergo_contract_events_subscribe(instance, contract_address.c_str(), event_name.c_str(), cb, arg);
}
};