This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheosio.token_tests.cpp
422 lines (322 loc) · 14.4 KB
/
eosio.token_tests.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>
// #include "eosio.system_tester.hpp"
#include "contracts.hpp"
#include "Runtime/Runtime.h"
#include <fc/variant_object.hpp>
using namespace eosio::testing;
using namespace eosio;
using namespace eosio::chain;
using namespace eosio::testing;
using namespace fc;
using namespace std;
using mvo = fc::mutable_variant_object;
class eosio_token_tester : public tester {
public:
eosio_token_tester() {
produce_blocks( 2 );
create_accounts( { "alice"_n, "bob"_n, "carol"_n, "eosio.token"_n } );
produce_blocks( 2 );
set_code( "eosio.token"_n, contracts::token_wasm() );
set_abi( "eosio.token"_n, contracts::token_abi().data() );
produce_blocks();
const auto& accnt = control->db().get<account_object,by_name>( "eosio.token"_n );
abi_def abi;
BOOST_REQUIRE_EQUAL(abi_serializer::to_abi(accnt.abi, abi), true);
abi_ser.set_abi(abi, abi_serializer::create_yield_function(abi_serializer_max_time));
}
action_result push_action( const account_name& signer, const action_name &name, const variant_object &data ) {
string action_type_name = abi_ser.get_action_type(name);
action act;
act.account = "eosio.token"_n;
act.name = name;
act.data = abi_ser.variant_to_binary( action_type_name, data, abi_serializer::create_yield_function(abi_serializer_max_time) );
return base_tester::push_action( std::move(act), signer.to_uint64_t() );
}
fc::variant get_stats( const string& symbolname )
{
auto symb = eosio::chain::symbol::from_string(symbolname);
auto symbol_code = symb.to_symbol_code().value;
vector<char> data = get_row_by_account( "eosio.token"_n, name(symbol_code), "stat"_n, account_name(symbol_code) );
return data.empty() ? fc::variant() : abi_ser.binary_to_variant( "currency_stats", data, abi_serializer::create_yield_function(abi_serializer_max_time) );
}
fc::variant get_account( account_name acc, const string& symbolname)
{
auto symb = eosio::chain::symbol::from_string(symbolname);
auto symbol_code = symb.to_symbol_code().value;
vector<char> data = get_row_by_account( "eosio.token"_n, acc, "accounts"_n, account_name(symbol_code) );
return data.empty() ? fc::variant() : abi_ser.binary_to_variant( "account", data, abi_serializer::create_yield_function(abi_serializer_max_time) );
}
action_result create( account_name issuer,
asset maximum_supply ) {
return push_action( "eosio.token"_n, "create"_n, mvo()
( "issuer", issuer)
( "maximum_supply", maximum_supply)
);
}
action_result issue( account_name issuer, asset quantity, string memo ) {
return push_action( issuer, "issue"_n, mvo()
( "to", issuer)
( "quantity", quantity)
( "memo", memo)
);
}
action_result retire( account_name issuer, asset quantity, string memo ) {
return push_action( issuer, "retire"_n, mvo()
( "quantity", quantity)
( "memo", memo)
);
}
action_result transfer( account_name from,
account_name to,
asset quantity,
string memo ) {
return push_action( from, "transfer"_n, mvo()
( "from", from)
( "to", to)
( "quantity", quantity)
( "memo", memo)
);
}
action_result open( account_name owner,
const string& symbolname,
account_name ram_payer ) {
return push_action( ram_payer, "open"_n, mvo()
( "owner", owner )
( "symbol", symbolname )
( "ram_payer", ram_payer )
);
}
action_result close( account_name owner,
const string& symbolname ) {
return push_action( owner, "close"_n, mvo()
( "owner", owner )
( "symbol", "0,CERO" )
);
}
abi_serializer abi_ser;
};
BOOST_AUTO_TEST_SUITE(eosio_token_tests)
BOOST_FIXTURE_TEST_CASE( create_tests, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1000.000 TKN"));
auto stats = get_stats("3,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "0.000 TKN")
("max_supply", "1000.000 TKN")
("issuer", "alice")
);
produce_blocks(1);
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( create_negative_max_supply, eosio_token_tester ) try {
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "max-supply must be positive" ),
create( "alice"_n, asset::from_string("-1000.000 TKN"))
);
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( symbol_already_exists, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("100 TKN"));
auto stats = get_stats("0,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "0 TKN")
("max_supply", "100 TKN")
("issuer", "alice")
);
produce_blocks(1);
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "token with symbol already exists" ),
create( "alice"_n, asset::from_string("100 TKN"))
);
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( create_max_supply, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("4611686018427387903 TKN"));
auto stats = get_stats("0,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "0 TKN")
("max_supply", "4611686018427387903 TKN")
("issuer", "alice")
);
produce_blocks(1);
asset max(10, symbol(SY(0, NKT)));
share_type amount = 4611686018427387904;
static_assert(sizeof(share_type) <= sizeof(asset), "asset changed so test is no longer valid");
static_assert(std::is_trivially_copyable<asset>::value, "asset is not trivially copyable");
memcpy(&max, &amount, sizeof(share_type)); // hack in an invalid amount
BOOST_CHECK_EXCEPTION( create( "alice"_n, max) , asset_type_exception, [](const asset_type_exception& e) {
return expect_assert_message(e, "magnitude of asset amount must be less than 2^62");
});
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( create_max_decimals, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1.000000000000000000 TKN"));
auto stats = get_stats("18,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "0.000000000000000000 TKN")
("max_supply", "1.000000000000000000 TKN")
("issuer", "alice")
);
produce_blocks(1);
asset max(10, symbol(SY(0, NKT)));
//1.0000000000000000000 => 0x8ac7230489e80000L
share_type amount = 0x8ac7230489e80000L;
static_assert(sizeof(share_type) <= sizeof(asset), "asset changed so test is no longer valid");
static_assert(std::is_trivially_copyable<asset>::value, "asset is not trivially copyable");
memcpy(&max, &amount, sizeof(share_type)); // hack in an invalid amount
BOOST_CHECK_EXCEPTION( create( "alice"_n, max) , asset_type_exception, [](const asset_type_exception& e) {
return expect_assert_message(e, "magnitude of asset amount must be less than 2^62");
});
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( issue_tests, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1000.000 TKN"));
produce_blocks(1);
issue( "alice"_n, asset::from_string("500.000 TKN"), "hola" );
auto stats = get_stats("3,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "500.000 TKN")
("max_supply", "1000.000 TKN")
("issuer", "alice")
);
auto alice_balance = get_account("alice"_n, "3,TKN");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "500.000 TKN")
);
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "quantity exceeds available supply" ),
issue( "alice"_n, asset::from_string("500.001 TKN"), "hola" )
);
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "must issue positive quantity" ),
issue( "alice"_n, asset::from_string("-1.000 TKN"), "hola" )
);
BOOST_REQUIRE_EQUAL( success(),
issue( "alice"_n, asset::from_string("1.000 TKN"), "hola" )
);
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( retire_tests, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1000.000 TKN"));
produce_blocks(1);
BOOST_REQUIRE_EQUAL( success(), issue( "alice"_n, asset::from_string("500.000 TKN"), "hola" ) );
auto stats = get_stats("3,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "500.000 TKN")
("max_supply", "1000.000 TKN")
("issuer", "alice")
);
auto alice_balance = get_account("alice"_n, "3,TKN");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "500.000 TKN")
);
BOOST_REQUIRE_EQUAL( success(), retire( "alice"_n, asset::from_string("200.000 TKN"), "hola" ) );
stats = get_stats("3,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "300.000 TKN")
("max_supply", "1000.000 TKN")
("issuer", "alice")
);
alice_balance = get_account("alice"_n, "3,TKN");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "300.000 TKN")
);
//should fail to retire more than current supply
BOOST_REQUIRE_EQUAL( wasm_assert_msg("overdrawn balance"), retire( "alice"_n, asset::from_string("500.000 TKN"), "hola" ) );
BOOST_REQUIRE_EQUAL( success(), transfer( "alice"_n, "bob"_n, asset::from_string("200.000 TKN"), "hola" ) );
//should fail to retire since tokens are not on the issuer's balance
BOOST_REQUIRE_EQUAL( wasm_assert_msg("overdrawn balance"), retire( "alice"_n, asset::from_string("300.000 TKN"), "hola" ) );
//transfer tokens back
BOOST_REQUIRE_EQUAL( success(), transfer( "bob"_n, "alice"_n, asset::from_string("200.000 TKN"), "hola" ) );
BOOST_REQUIRE_EQUAL( success(), retire( "alice"_n, asset::from_string("300.000 TKN"), "hola" ) );
stats = get_stats("3,TKN");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "0.000 TKN")
("max_supply", "1000.000 TKN")
("issuer", "alice")
);
alice_balance = get_account("alice"_n, "3,TKN");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "0.000 TKN")
);
//trying to retire tokens with zero supply
BOOST_REQUIRE_EQUAL( wasm_assert_msg("overdrawn balance"), retire( "alice"_n, asset::from_string("1.000 TKN"), "hola" ) );
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( transfer_tests, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1000 CERO"));
produce_blocks(1);
issue( "alice"_n, asset::from_string("1000 CERO"), "hola" );
auto stats = get_stats("0,CERO");
REQUIRE_MATCHING_OBJECT( stats, mvo()
("supply", "1000 CERO")
("max_supply", "1000 CERO")
("issuer", "alice")
);
auto alice_balance = get_account("alice"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "1000 CERO")
);
transfer( "alice"_n, "bob"_n, asset::from_string("300 CERO"), "hola" );
alice_balance = get_account("alice"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "700 CERO")
("frozen", 0)
("whitelist", 1)
);
auto bob_balance = get_account("bob"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( bob_balance, mvo()
("balance", "300 CERO")
("frozen", 0)
("whitelist", 1)
);
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "overdrawn balance" ),
transfer( "alice"_n, "bob"_n, asset::from_string("701 CERO"), "hola" )
);
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "must transfer positive quantity" ),
transfer( "alice"_n, "bob"_n, asset::from_string("-1000 CERO"), "hola" )
);
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( open_tests, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1000 CERO"));
auto alice_balance = get_account("alice"_n, "0,CERO");
BOOST_REQUIRE_EQUAL(true, alice_balance.is_null() );
BOOST_REQUIRE_EQUAL( wasm_assert_msg("tokens can only be issued to issuer account"),
push_action( "alice"_n, "issue"_n, mvo()
( "to", "bob")
( "quantity", asset::from_string("1000 CERO") )
( "memo", "") ) );
BOOST_REQUIRE_EQUAL( success(), issue( "alice"_n, asset::from_string("1000 CERO"), "issue" ) );
alice_balance = get_account("alice"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "1000 CERO")
);
auto bob_balance = get_account("bob"_n, "0,CERO");
BOOST_REQUIRE_EQUAL(true, bob_balance.is_null() );
BOOST_REQUIRE_EQUAL( wasm_assert_msg("owner account does not exist"),
open( "nonexistent"_n, "0,CERO", "alice"_n ) );
BOOST_REQUIRE_EQUAL( success(),
open( "bob"_n, "0,CERO", "alice"_n ) );
bob_balance = get_account("bob"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( bob_balance, mvo()
("balance", "0 CERO")
);
BOOST_REQUIRE_EQUAL( success(), transfer( "alice"_n, "bob"_n, asset::from_string("200 CERO"), "hola" ) );
bob_balance = get_account("bob"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( bob_balance, mvo()
("balance", "200 CERO")
);
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "symbol does not exist" ),
open( "carol"_n, "0,INVALID", "alice"_n ) );
BOOST_REQUIRE_EQUAL( wasm_assert_msg( "symbol precision mismatch" ),
open( "carol"_n, "1,CERO", "alice"_n ) );
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE( close_tests, eosio_token_tester ) try {
auto token = create( "alice"_n, asset::from_string("1000 CERO"));
auto alice_balance = get_account("alice"_n, "0,CERO");
BOOST_REQUIRE_EQUAL(true, alice_balance.is_null() );
BOOST_REQUIRE_EQUAL( success(), issue( "alice"_n, asset::from_string("1000 CERO"), "hola" ) );
alice_balance = get_account("alice"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "1000 CERO")
);
BOOST_REQUIRE_EQUAL( success(), transfer( "alice"_n, "bob"_n, asset::from_string("1000 CERO"), "hola" ) );
alice_balance = get_account("alice"_n, "0,CERO");
REQUIRE_MATCHING_OBJECT( alice_balance, mvo()
("balance", "0 CERO")
);
BOOST_REQUIRE_EQUAL( success(), close( "alice"_n, "0,CERO" ) );
alice_balance = get_account("alice"_n, "0,CERO");
BOOST_REQUIRE_EQUAL(true, alice_balance.is_null() );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_SUITE_END()