-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathasset.hpp
537 lines (467 loc) · 18.3 KB
/
asset.hpp
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#pragma once
#include "serialize.hpp"
#include "print.hpp"
#include "check.hpp"
#include "symbol.hpp"
#include <tuple>
#include <limits>
namespace eosio {
char* write_decimal( char* begin, char* end, bool dry_run, uint64_t number, uint8_t num_decimal_places, bool negative );
/**
* @defgroup asset Asset
* @ingroup core
* @brief Defines C++ API for managing assets
*/
/**
* Stores information for owner of asset
*
* @ingroup asset
*/
struct asset {
/**
* The amount of the asset
*/
int64_t amount = 0;
/**
* The symbol name of the asset
*/
symbol symbol;
/**
* Maximum amount possible for this asset. It's capped to 2^62 - 1
*/
static constexpr int64_t max_amount = (1LL << 62) - 1;
asset() {}
/**
* Construct a new asset given the symbol name and the amount
*
* @param a - The amount of the asset
* @param s - The name of the symbol
*/
asset( int64_t a, class symbol s )
:amount(a),symbol{s}
{
eosio::check( is_amount_within_range(), "magnitude of asset amount must be less than 2^62" );
eosio::check( symbol.is_valid(), "invalid symbol name" );
}
/**
* Check if the amount doesn't exceed the max amount
*
* @return true - if the amount doesn't exceed the max amount
* @return false - otherwise
*/
bool is_amount_within_range()const { return -max_amount <= amount && amount <= max_amount; }
/**
* Check if the asset is valid. %A valid asset has its amount <= max_amount and its symbol name valid
*
* @return true - if the asset is valid
* @return false - otherwise
*/
bool is_valid()const { return is_amount_within_range() && symbol.is_valid(); }
/**
* Set the amount of the asset
*
* @param a - New amount for the asset
*/
void set_amount( int64_t a ) {
amount = a;
eosio::check( is_amount_within_range(), "magnitude of asset amount must be less than 2^62" );
}
/// @cond OPERATORS
/**
* Unary minus operator
*
* @return asset - New asset with its amount is the negative amount of this asset
*/
asset operator-()const {
asset r = *this;
r.amount = -r.amount;
return r;
}
/**
* Subtraction assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is subtracted by the amount of asset a
*/
asset& operator-=( const asset& a ) {
eosio::check( a.symbol == symbol, "attempt to subtract asset with different symbol" );
amount -= a.amount;
eosio::check( -max_amount <= amount, "subtraction underflow" );
eosio::check( amount <= max_amount, "subtraction overflow" );
return *this;
}
/**
* Addition Assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is added with the amount of asset a
*/
asset& operator+=( const asset& a ) {
eosio::check( a.symbol == symbol, "attempt to add asset with different symbol" );
amount += a.amount;
eosio::check( -max_amount <= amount, "addition underflow" );
eosio::check( amount <= max_amount, "addition overflow" );
return *this;
}
/**
* Addition operator
*
* @param a - The first asset to be added
* @param b - The second asset to be added
* @return asset - New asset as the result of addition
*/
inline friend asset operator+( const asset& a, const asset& b ) {
asset result = a;
result += b;
return result;
}
/**
* Subtraction operator
*
* @param a - The asset to be subtracted
* @param b - The asset used to subtract
* @return asset - New asset as the result of subtraction of a with b
*/
inline friend asset operator-( const asset& a, const asset& b ) {
asset result = a;
result -= b;
return result;
}
/**
* Multiplication assignment operator, with a number
*
* @details Multiplication assignment operator. Multiply the amount of this asset with a number and then assign the value to itself.
* @param a - The multiplier for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is multiplied by a
*/
asset& operator*=( int64_t a ) {
int128_t tmp = (int128_t)amount * (int128_t)a;
eosio::check( tmp <= max_amount, "multiplication overflow" );
eosio::check( tmp >= -max_amount, "multiplication underflow" );
amount = (int64_t)tmp;
return *this;
}
/**
* Multiplication operator, with a number proceeding
*
* @brief Multiplication operator, with a number proceeding
* @param a - The asset to be multiplied
* @param b - The multiplier for the asset's amount
* @return asset - New asset as the result of multiplication
*/
friend asset operator*( const asset& a, int64_t b ) {
asset result = a;
result *= b;
return result;
}
/**
* Multiplication operator, with a number preceeding
*
* @param a - The multiplier for the asset's amount
* @param b - The asset to be multiplied
* @return asset - New asset as the result of multiplication
*/
friend asset operator*( int64_t b, const asset& a ) {
asset result = a;
result *= b;
return result;
}
/**
* @brief Division assignment operator, with a number
*
* @details Division assignment operator. Divide the amount of this asset with a number and then assign the value to itself.
* @param a - The divisor for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is divided by a
*/
asset& operator/=( int64_t a ) {
eosio::check( a != 0, "divide by zero" );
eosio::check( !(amount == std::numeric_limits<int64_t>::min() && a == -1), "signed division overflow" );
amount /= a;
return *this;
}
/**
* Division operator, with a number proceeding
*
* @param a - The asset to be divided
* @param b - The divisor for the asset's amount
* @return asset - New asset as the result of division
*/
friend asset operator/( const asset& a, int64_t b ) {
asset result = a;
result /= b;
return result;
}
/**
* Division operator, with another asset
*
* @param a - The asset which amount acts as the dividend
* @param b - The asset which amount acts as the divisor
* @return int64_t - the resulted amount after the division
* @pre Both asset must have the same symbol
*/
friend int64_t operator/( const asset& a, const asset& b ) {
eosio::check( b.amount != 0, "divide by zero" );
eosio::check( a.symbol == b.symbol, "attempt to divide assets with different symbol" );
return a.amount / b.amount;
}
/**
* Equality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset has the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
friend bool operator==( const asset& a, const asset& b ) {
eosio::check( a.symbol == b.symbol, "comparison of assets with different symbols is not allowed" );
return a.amount == b.amount;
}
/**
* Inequality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset doesn't have the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
friend bool operator!=( const asset& a, const asset& b ) {
return !( a == b);
}
/**
* Less than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
friend bool operator<( const asset& a, const asset& b ) {
eosio::check( a.symbol == b.symbol, "comparison of assets with different symbols is not allowed" );
return a.amount < b.amount;
}
/**
* Less or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
friend bool operator<=( const asset& a, const asset& b ) {
eosio::check( a.symbol == b.symbol, "comparison of assets with different symbols is not allowed" );
return a.amount <= b.amount;
}
/**
* Greater than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
friend bool operator>( const asset& a, const asset& b ) {
eosio::check( a.symbol == b.symbol, "comparison of assets with different symbols is not allowed" );
return a.amount > b.amount;
}
/**
* Greater or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
friend bool operator>=( const asset& a, const asset& b ) {
eosio::check( a.symbol == b.symbol, "comparison of assets with different symbols is not allowed" );
return a.amount >= b.amount;
}
/// @endcond
/**
* Writes the asset as a string to the provided char buffer
*
* @brief Writes the asset as a string to the provided char buffer
* @pre is_valid() == true
* @pre The range [begin, end) must be a valid range of memory to write to.
* @param begin - The start of the char buffer
* @param end - Just past the end of the char buffer
* @param dry_run - If true, do not actually write anything into the range.
* @return char* - Just past the end of the last character that would be written assuming dry_run == false and end was large enough to provide sufficient space. (Meaning only applies if returned pointer >= begin.)
* @post If the output string fits within the range [begin, end) and dry_run == false, the range [begin, returned pointer) contains the string representation of the asset. Nothing is written if dry_run == true or returned pointer > end (insufficient space) or if returned pointer < begin (overflow in calculating desired end).
*/
char* write_as_string( char* begin, char* end, bool dry_run = false )const {
bool negative = (amount < 0);
uint64_t abs_amount = static_cast<uint64_t>(negative ? -amount : amount);
// 0 <= abs_amount <= std::numeric_limits<int64_t>::max() < 10^19 < std::numeric_limits<uint64_t>::max()
uint8_t precision = symbol.precision();
int sufficient_size = std::max(static_cast<int>(precision), 19) + 11;
if( dry_run || (begin + sufficient_size < begin) || (begin + sufficient_size > end) ) {
char* start_of_symbol = write_decimal( begin, end, true, abs_amount, precision, negative ) + 1;
char* actual_end = symbol.code().write_as_string( start_of_symbol, end, true );
if( dry_run || (actual_end < begin) || (actual_end > end) ) return actual_end;
}
char* end_of_number = write_decimal( begin, end, false, abs_amount, precision, negative );
*(end_of_number) = ' ';
return symbol.code().write_as_string( end_of_number + 1, end );
}
/**
* %asset to std::string
*
* @brief %asset to std::string
*/
std::string to_string()const {
int buffer_size = std::max(static_cast<int>(symbol.precision()), 19) + 11;
char buffer[buffer_size];
char* end = write_as_string( buffer, buffer + buffer_size );
check( end <= buffer + buffer_size, "insufficient space in buffer" ); // should never fail
return {buffer, end};
}
/**
* %Print the asset
*
* @brief %Print the asset
*/
void print()const {
int buffer_size = std::max(static_cast<int>(symbol.precision()), 19) + 11;
char buffer[buffer_size];
char* end = write_as_string( buffer, buffer + buffer_size );
check( end <= buffer + buffer_size, "insufficient space in buffer" ); // should never fail
if( buffer < end )
printl( buffer, (end-buffer) );
}
EOSLIB_SERIALIZE( asset, (amount)(symbol) )
};
/**
* Extended asset which stores the information of the owner of the asset
*
* @ingroup asset
*/
struct extended_asset {
/**
* The asset
*/
asset quantity;
/**
* The owner of the asset
*/
name contract;
/**
* Get the extended symbol of the asset
*
* @return extended_symbol - The extended symbol of the asset
*/
extended_symbol get_extended_symbol()const { return extended_symbol{ quantity.symbol, contract }; }
/**
* Default constructor
*/
extended_asset() = default;
/**
* Construct a new extended asset given the amount and extended symbol
*/
extended_asset( int64_t v, extended_symbol s ):quantity(v,s.get_symbol()),contract(s.get_contract()){}
/**
* Construct a new extended asset given the asset and owner name
*/
extended_asset( asset a, name c ):quantity(a),contract(c){}
/**
* %Print the extended asset
*/
void print()const {
quantity.print();
::eosio::print("@", contract);
}
/// @cond OPERATORS
// Unary minus operator
extended_asset operator-()const {
return {-quantity, contract};
}
// Subtraction operator
friend extended_asset operator - ( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return {a.quantity - b.quantity, a.contract};
}
// Addition operator
friend extended_asset operator + ( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return {a.quantity + b.quantity, a.contract};
}
/// Addition operator.
friend extended_asset& operator+=( extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
a.quantity += b.quantity;
return a;
}
/// Subtraction operator.
friend extended_asset& operator-=( extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
a.quantity -= b.quantity;
return a;
}
/// Multiplication operator.
extended_asset& operator*=( int64_t b ) {
quantity *= b;
return *this;
}
/// Multiplication operator.
friend extended_asset operator*( const extended_asset& a, int64_t b ) {
return {a.quantity * b, a.contract};
}
/// Multiplication operator.
friend extended_asset operator*( int64_t a, const extended_asset& b ) {
return {a * b.quantity, b.contract};
}
/// Division operator. Follows format of 'int64_t operator/( const asset& a, const asset& b )'.
friend int64_t operator/( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return a.quantity / b.quantity;
}
/// Division operator.
extended_asset& operator/=( int64_t b ) {
quantity /= b;
return *this;
}
/// Division operator.
friend extended_asset operator/( const extended_asset& a, int64_t b ) {
return {a.quantity / b, a.contract};
}
/// Less than operator
friend bool operator<( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return a.quantity < b.quantity;
}
/// Greater than operator
friend bool operator>( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return a.quantity > b.quantity;
}
/// Equality operator
friend bool operator==( const extended_asset& a, const extended_asset& b ) {
return std::tie(a.quantity, a.contract) == std::tie(b.quantity, b.contract);
}
/// Inequality operator
friend bool operator!=( const extended_asset& a, const extended_asset& b ) {
return std::tie(a.quantity, a.contract) != std::tie(b.quantity, b.contract);
}
/// Less than or equal operator
friend bool operator<=( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return a.quantity <= b.quantity;
}
/// Greater than or equal operator
friend bool operator>=( const extended_asset& a, const extended_asset& b ) {
eosio::check( a.contract == b.contract, "type mismatch" );
return a.quantity >= b.quantity;
}
/// @endcond
EOSLIB_SERIALIZE( extended_asset, (quantity)(contract) )
};
}