-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhello.cpp
250 lines (208 loc) · 8.39 KB
/
hello.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <eosiolib/eosio.hpp>
// #include <eosiolib/print.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/crypto.h>
#include <eosio.token/eosio.token.hpp>
#include <string>
#include <sstream>
using namespace eosio;
// using string;
class l2dex : public eosio::contract {
public:
using contract::contract;
/// @abi table channels
struct channel {
uint64_t id;
asset allowance;
account_name opener;
account_name respondent;
public_key pub_key;
public_key pub_key_resp;
std::string pair;
int64_t openerAmount;
uint64_t n;
time_t t = 0;
bool opener_agreed = false;
uint64_t primary_key() const { return id; }
};
struct account {
asset balance;
uint64_t primary_key()const { return balance.symbol.name(); }
};
typedef eosio::multi_index<N(accounts), account> accounts;
typedef eosio::multi_index<N(channels), channel> channels;
/// @abi action
void open(account_name opener, public_key pub_key, account_name respondent, public_key resp_key, asset quantity, std::string pair)
{
// print for sanity
print("Opening ", opener, " ", respondent, " for ", quantity.symbol.name(), quantity.amount, " - ", pair);
// check input arguments correctness
eosio_assert(quantity.symbol.is_valid(), "invalid symbol name" );
eosio_assert(quantity.amount > 0, "invalid token quantity");
eosio_assert(is_account(opener), "invalid opener!");
eosio_assert(is_account(respondent), "invalid respondent!");
eosio_assert(opener != respondent, "can't open channel to self!");
eosio_assert(!pair.empty(), "invalid pair!");
// check opener authorization to prevent malicious use
require_auth(opener);
require_recipient(opener);
require_recipient(respondent);
// check if the channel between these two accounts in the selected currency already exists
channels channel( _self, N(l2dex.code) );
auto chid = calcChannelId(opener, respondent, quantity, pair);
auto ch2 = channel.find(chid);
eosio_assert(ch2 == channel.end(), "channel already exists!");
// print(" ",hash_source," ");
// print(" ",std::string(reinterpret_cast<char*>(id_hash.hash)).c_str()," ");
// SEND_INLINE_ACTION(eosio::token, get_balance, ("owner", opener)("sym", quantity.symbol.name()));
// INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {payer, N(opener)},
// { opener, N(eosio.ramfee), fee, std::string("") } )
// check that opener has enough money
eosio_assert(enoughMoney(opener, quantity), "not enough money!");
// raise permissions level and transfer tokens (currency/symbol) from opener to this contract
action(
permission_level{ opener, N(active) },
N(eosio.token), N(transfer),
std::make_tuple(opener, _self, quantity, pair)
).send();
// put channel data into contract table
channel.emplace(opener, [&]( auto& a ) {
a.id = chid;
a.allowance = quantity;
a.opener = opener;
a.respondent = respondent;
a.pub_key = pub_key;
a.pair = pair;
a.pub_key_resp = resp_key;
});
// transfer tokens
// sub_balance(opener, quantity);
// add_balance(_self, quantity, opener);
}
/// @abi action
void extend(account_name opener, public_key pub_key, account_name respondent, asset quantity, std::string pair)
{
print("Extending: ", opener, " ", respondent, " with ", quantity, " ", pair);
require_auth(opener);
eosio_assert(enoughMoney(opener, quantity), "not enough money!");
channels channel( _self, N(l2dex.code) );
auto id = calcChannelId(opener, respondent, quantity, pair);
auto ch2 = channel.find(id);
eosio_assert(ch2 != channel.end(), "channel doesn't exist!");
const auto& ch = *ch2;
eosio_assert(ch.allowance.amount < quantity.amount, "not enough quantity to extend!");
channel.modify(ch, 0, [&](auto& x)
{
x.allowance = quantity;
});
}
/// @abi action
void validate(uint64_t chid, int64_t openerAmount, uint64_t n, signature& sign, bool isOpener)
{
channels channel( _self, N(l2dex.code) );
const auto& ch = channel.get(chid, "channel doesn't exist!");
// print("pre-auth");
if (!has_auth(ch.respondent))
require_auth(ch.opener);
// print("post-auth");
public_key key = has_auth(ch.respondent) ? ch.pub_key : ch.pub_key_resp;
// eosio_assert(openerAmount <= ch.allowance.amount, "not enough allowance!");
eosio_assert(openerAmount >= 0, "can't go below zero!");
// print("tak-to vse vrode zaebis");
std::string str = std::to_string(chid) + ";" + std::to_string(n) + ";" + std::to_string(openerAmount);
checksum256 hash;
sha256(const_cast<char*>(str.c_str()), str.length(), &hash);
assert_recover_key(&hash, reinterpret_cast<char *>(sign.data), 66, key.data, 34);
}
/// @abi action
void close(uint64_t chid, int64_t openerAmount, uint64_t n, signature& sign, bool isOpener)
{
validate(chid, openerAmount, n, sign, isOpener);
channels channel( _self, N(l2dex.code) );
const auto& ch = channel.get(chid, "channel doesn't exist!");
eosio_assert(n >= ch.n, "nonce is lower than last tx!");
bool challengeOpen = ch.t > 0;
if (challengeOpen)
{
if (isOpener != ch.opener_agreed)
{
// finalize
finalizeChannel(chid, openerAmount);
}
else
{
if (channelTimedout(ch.t))
{
// single-party finalize on timeout
finalizeChannel(chid, openerAmount);
}
else
{
// discard
eosio_assert(false, "this party already submitted their close request!");
}
}
}
else
{
// open challenge
channel.modify(ch, ch.opener, [&](auto& ch)
{
ch.n = n;
ch.openerAmount = openerAmount;
ch.opener_agreed = isOpener;
ch.t = now();
});
}
}
/// @abi action
void hi( account_name user ) {
print( "Hello, ", name{user} );
}
private:
bool channelTimedout(time_t t)
{
if (!t)
return false;
return (t < (now() - 300));
}
void finalizeChannel(uint64_t chid, int64_t openerAmount)
{
channels channel( _self, N(l2dex.code) );
const auto& ch = channel.get(chid, "channel doesn't exist!");
asset newBalance(openerAmount, ch.allowance.symbol);
// SEND_INLINE_ACTION(eosio::token, transfer, ("from", _self)("to", opener)("quantity", newBalance)("memo", chid));
action(
permission_level{ _self, N(active) },
N(eosio.token), N(transfer),
std::make_tuple(_self, ch.opener, newBalance, std::to_string(chid))
).send();
channel.erase(ch);
}
bool enoughMoney(account_name opener, asset quantity)
{
return getUserBalance(opener, quantity).amount >= quantity.amount;
}
asset getUserBalance(account_name opener, asset quantity)
{
accounts acc( N(eosio.token), opener );
auto balance = acc.get(quantity.symbol.name());
return balance.balance;
}
uint64_t calcChannelId(account_name opener, account_name respondent, asset quantity, std::string pair)
{
auto hash = calcChannelHash(opener, respondent, quantity, pair);
return reinterpret_cast<uint64_t*>(hash.hash)[0];
}
checksum256 calcChannelHash(account_name opener, account_name respondent, asset quantity, std::string pair)
{
std::string hash_source = std::to_string(opener)
+ std::to_string(respondent)
+ std::to_string(quantity.symbol.name())
+ pair;
checksum256 id_hash;
sha256(const_cast<char*>(hash_source.c_str()), hash_source.length(), &id_hash);
return id_hash;
}
};
EOSIO_ABI( l2dex, (hi)(open)(validate)(close)(extend) )