-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcurve.sx.cpp
580 lines (474 loc) · 25.4 KB
/
curve.sx.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include <eosio.token/eosio.token.hpp>
#include <sx.utils/utils.hpp>
#include <sx.safemath/safemath.hpp>
#include <sx.rex/rex.hpp>
#include "curve.sx.hpp"
#include "src/actions.cpp"
namespace sx {
/**
* Notify contract when any token transfer notifiers relay contract
*/
[[eosio::on_notify("*::transfer")]]
void curve::on_transfer( const name from, const name to, const asset quantity, const string memo )
{
// authenticate incoming `from` account
require_auth( from );
// tables
curve::config_table _config( get_self(), get_self().value );
curve::pairs_table _pairs( get_self(), get_self().value );
// config
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
const name status = _config.get().status;
check( (status == "ok"_n || status == "withdraw"_n ), "curve::on_transfer: contract is under maintenance");
// ignore transfers
if ( to != get_self() || from == "eosio.ram"_n ) return;
// user input params
const auto parsed_memo = parse_memo( memo );
const extended_asset ext_in = { quantity, get_first_receiver() };
const bool is_liquidity = _pairs.find( quantity.symbol.code().raw() ) != _pairs.end();
// only allow liquidity withdraws to be available
if ( status == "withdraw"_n ) check( is_liquidity, "curve::on_transfer: only accepts liquidity tokens during `withdraw` status");
// add liquidity (memo required => "deposit,<pair_id>")
if ( parsed_memo.action == "deposit"_n ) {
add_liquidity( from, parsed_memo.pair_ids[0], ext_in );
// swap convert (memo required => "swap,<min_return>,<pair_ids>")
} else if ( parsed_memo.action == "swap"_n) {
convert( from, ext_in, parsed_memo.pair_ids, parsed_memo.min_return );
// withdraw liquidity (no memo required)
} else if ( is_liquidity ) {
withdraw_liquidity( from, ext_in );
} else {
check( false, ERROR_INVALID_MEMO );
}
// accounts to be notified via inline action
notify();
}
[[eosio::action]]
void curve::init( const name token_contract )
{
require_auth( get_self() );
curve::config_table _config( get_self(), get_self().value );
auto config = _config.exists() ? _config.get() : curve::config_row{};
// token contract can not be modified once initialized
check( is_account( token_contract ), "curve::init: `token_contract` does not exist");
if ( config.token_contract.value ) check( config.token_contract == token_contract, "curve::init: `token_contract` cannot be modified once initialized");
// set config
config.token_contract = token_contract;
_config.set( config, get_self() );
}
[[eosio::action]]
void curve::reset()
{
require_auth( get_self() );
curve::config_table _config( get_self(), get_self().value );
_config.remove();
}
void curve::convert( const name owner, const extended_asset ext_in, const vector<symbol_code> pair_ids, const int64_t min_return )
{
// execute the trade by updating all involved pools
const extended_asset out = apply_trade( owner, ext_in, pair_ids );
// enforce minimum return (slippage protection)
check(out.quantity.amount != 0 && out.quantity.amount >= min_return, "curve::convert: invalid minimum return");
// transfer amount to owner
transfer( get_self(), owner, out, get_self().to_string() + ": swap token" );
}
extended_asset curve::apply_trade( const name owner, const extended_asset ext_quantity, const vector<symbol_code> pair_ids )
{
curve::pairs_table _pairs( get_self(), get_self().value );
curve::config_table _config( get_self(), get_self().value );
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
auto config = _config.get();
// initial quantities
extended_asset ext_out;
extended_asset ext_in = ext_quantity;
// iterate over each liquidity pool per each `pair_id` provided in swap memo
for ( const symbol_code pair_id : pair_ids ) {
const auto& pairs = _pairs.get( pair_id.raw(), "curve::apply_trade: `pair_id` does not exist");
const bool is_in = pairs.reserve0.quantity.symbol == ext_in.quantity.symbol;
const extended_asset reserve_in = is_in ? pairs.reserve0 : pairs.reserve1;
const extended_asset reserve_out = is_in ? pairs.reserve1 : pairs.reserve0;
// validate input quantity & reserves
check(reserve_in.get_extended_symbol() == ext_in.get_extended_symbol(), "curve::apply_trade: invalid extended symbol");
check(reserve_in.quantity.amount != 0 && reserve_out.quantity.amount != 0, "curve::apply_trade: empty pool reserves");
// calculate out
ext_out = { get_amount_out( ext_in.quantity, pair_id, get_self() ), reserve_out.contract };
// send protocol fees to fee account
const extended_asset protocol_fee = { ext_in.quantity.amount * config.protocol_fee / 10000, ext_in.get_extended_symbol() };
const extended_asset trade_fee = { ext_in.quantity.amount * config.trade_fee / 10000, ext_in.get_extended_symbol() };
const extended_asset fee = protocol_fee + trade_fee;
// modify reserves
_pairs.modify( pairs, get_self(), [&]( auto & row ) {
// calculate last price
const double price = calculate_price( ext_out.quantity, ext_in.quantity );
if ( is_in ) {
row.reserve0.quantity += ext_in.quantity - protocol_fee.quantity;
row.reserve1.quantity -= ext_out.quantity;
row.volume0 += ext_in.quantity;
row.price0_last = price;
} else {
row.reserve1.quantity += ext_in.quantity - protocol_fee.quantity;
row.reserve0.quantity -= ext_out.quantity;
row.volume1 += ext_in.quantity;
row.price1_last = price;
}
row.amplifier = get_amplifier( pair_id, get_self() );
row.virtual_price = calculate_virtual_price( row.reserve0.quantity, row.reserve1.quantity, row.liquidity.quantity );
row.trades += 1;
row.last_updated = current_time_point();
// swap log
curve::swaplog_action swaplog( get_self(), { get_self(), "active"_n });
swaplog.send( pair_id, owner, "swap"_n, ext_in.quantity, ext_out.quantity, fee.quantity, price, row.reserve0.quantity, row.reserve1.quantity );
});
// send protocol fees
if ( protocol_fee.quantity.amount ) transfer( get_self(), config.fee_account, protocol_fee, get_self().to_string() + ": protocol fee");
// swap input as output to prepare for next conversion
ext_in = ext_out;
}
return ext_out;
}
[[eosio::action]]
void curve::deposit( const name owner, const symbol_code pair_id, const optional<int64_t> min_amount )
{
require_auth( owner );
curve::config_table _config( get_self(), get_self().value );
curve::pairs_table _pairs( get_self(), get_self().value );
curve::orders_table _orders( get_self(), pair_id.raw() );
// configs
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
auto config = _config.get();
// get current order & pairs
auto & pair = _pairs.get( pair_id.raw(), "curve::deposit: `pair_id` does not exist");
auto & orders = _orders.get( owner.value, "curve::deposit: no deposits available for this user");
check( orders.quantity0.quantity.amount && orders.quantity1.quantity.amount, "curve::deposit: one of the deposit is empty");
// symbol helpers
const symbol sym0 = pair.reserve0.quantity.symbol;
const symbol sym1 = pair.reserve1.quantity.symbol;
const uint8_t precision_norm = max( sym0.precision(), sym1.precision() );
// calculate total deposits based on reserves: reserves ratio should remain the same
// if reserves empty, fallback to 1
const int128_t reserve0 = pair.reserve0.quantity.amount ? mul_amount(pair.reserve0.quantity.amount, precision_norm, sym0.precision()) : 1;
const int128_t reserve1 = pair.reserve1.quantity.amount ? mul_amount(pair.reserve1.quantity.amount, precision_norm, sym1.precision()) : 1;
const int128_t reserves = reserve0 + reserve1;
// get owner order and calculate payment
const int128_t amount0 = mul_amount(orders.quantity0.quantity.amount, precision_norm, sym0.precision());
const int128_t amount1 = mul_amount(orders.quantity1.quantity.amount, precision_norm, sym1.precision());
const int128_t payment = amount0 + amount1;
// calculate actual amounts to deposit
const int128_t deposit0 = (amount0 * reserves <= reserve0 * payment) ? amount0 : (amount1 * reserve0 / reserve1);
const int128_t deposit1 = (amount0 * reserves <= reserve0 * payment) ? (amount0 * reserve1 / reserve0) : amount1;
// send back excess deposit to owner
if (deposit0 < amount0) {
const int64_t excess_amount = div_amount(static_cast<int64_t>(amount0 - deposit0), precision_norm, sym0.precision());
const extended_asset excess = { excess_amount, pair.reserve0.get_extended_symbol() };
if (excess.quantity.amount) transfer( get_self(), owner, excess, get_self().to_string() + ": excess");
}
if (deposit1 < amount1) {
const int64_t excess_amount = div_amount(static_cast<int64_t>(amount1 - deposit1), precision_norm, sym1.precision());
const extended_asset excess = { excess_amount, pair.reserve1.get_extended_symbol() };
if (excess.quantity.amount) transfer( get_self(), owner, excess, get_self().to_string() + ": excess");
}
// normalize final deposits
const extended_asset ext_deposit0 = { div_amount(deposit0, precision_norm, sym0.precision()), pair.reserve0.get_extended_symbol()};
const extended_asset ext_deposit1 = { div_amount(deposit1, precision_norm, sym1.precision()), pair.reserve1.get_extended_symbol()};
// issue liquidity
const int64_t supply = mul_amount(pair.liquidity.quantity.amount, precision_norm, pair.liquidity.quantity.symbol.precision());
const int64_t issued_amount = rex::issue(deposit0 + deposit1, reserves, supply, 1);
const extended_asset issued = { div_amount(issued_amount, precision_norm, pair.liquidity.quantity.symbol.precision()), pair.liquidity.get_extended_symbol()};
// add liquidity deposits & newly issued liquidity
_pairs.modify(pair, get_self(), [&]( auto & row ) {
row.reserve0 += ext_deposit0;
row.reserve1 += ext_deposit1;
row.liquidity += issued;
// log liquidity change
curve::liquiditylog_action liquiditylog( get_self(), { get_self(), "active"_n });
liquiditylog.send( pair_id, owner, "deposit"_n, issued.quantity, ext_deposit0.quantity, ext_deposit1.quantity, row.liquidity.quantity, row.reserve0.quantity, row.reserve1.quantity );
});
// issue & transfer to owner
issue( issued, get_self().to_string() + ": deposit" );
transfer( get_self(), owner, issued, get_self().to_string() + ": deposit");
// deposit slippage protection
if ( min_amount ) check( issued.quantity.amount >= *min_amount, "curve::deposit: deposit amount must exceed `min_amount`");
// delete any remaining liquidity deposit order
_orders.erase( orders );
}
// returns any remaining orders to owner account
[[eosio::action]]
void curve::cancel( const name owner, const symbol_code pair_id )
{
if ( !has_auth( get_self() )) require_auth( owner );
curve::orders_table _orders( get_self(), pair_id.raw() );
auto & orders = _orders.get( owner.value, "curve::cancel: no deposits for this user in this pool");
if ( orders.quantity0.quantity.amount ) transfer( get_self(), owner, orders.quantity0, get_self().to_string() + ": cancel");
if ( orders.quantity1.quantity.amount ) transfer( get_self(), owner, orders.quantity1, get_self().to_string() + ": cancel");
_orders.erase( orders );
}
[[eosio::action]]
void curve::removepair( const symbol_code pair_id )
{
require_auth( get_self() );
curve::pairs_table _pairs( get_self(), get_self().value );
auto & pair = _pairs.get( pair_id.raw(), "curve::removepair: [pair_id] does not exist");
check( pair.liquidity.quantity.amount == 0, "curve::removepair: liquidity amount must be empty");
_pairs.erase( pair );
}
void curve::withdraw_liquidity( const name owner, const extended_asset value )
{
curve::pairs_table _pairs( get_self(), get_self().value );
// get current pairs
const symbol_code pair_id = value.quantity.symbol.code();
auto & pair = _pairs.get( pair_id.raw(), "curve::withdraw_liquidity: `pair_id` does not exist");
// prevent invalid liquidity token contracts
check(pair.liquidity.get_extended_symbol() == value.get_extended_symbol(), "curve::withdraw_liquidity: invalid extended symbol");
// extended symbols
const extended_symbol ext_sym0 = pair.reserve0.get_extended_symbol();
const extended_symbol ext_sym1 = pair.reserve1.get_extended_symbol();
const symbol sym0 = pair.reserve0.quantity.symbol;
const symbol sym1 = pair.reserve1.quantity.symbol;
const uint8_t precision_norm = max( sym0.precision(), sym1.precision() );
// calculate total deposits based on reserves
const int64_t supply = mul_amount(pair.liquidity.quantity.amount, precision_norm, pair.liquidity.quantity.symbol.precision());
const int128_t reserve0 = pair.reserve0.quantity.amount ? mul_amount(pair.reserve0.quantity.amount, precision_norm, sym0.precision()) : 1;
const int128_t reserve1 = pair.reserve1.quantity.amount ? mul_amount(pair.reserve1.quantity.amount, precision_norm, sym1.precision()) : 1;
const int128_t reserves = reserve0 + reserve1;
// calculate withdraw amounts
const int64_t payment = mul_amount(value.quantity.amount, precision_norm, value.quantity.symbol.precision());
const int64_t retire_amount = rex::retire( payment, reserves, supply );
// get owner order and calculate payment
int64_t amount0 = static_cast<int64_t>( retire_amount * reserve0 / reserves );
int64_t amount1 = static_cast<int64_t>( retire_amount * reserve1 / reserves );
if (amount0 == reserve0 || amount1 == reserve1) { //deal with rounding error on final withdrawal
amount0 = static_cast<int64_t>( reserve0 );
amount1 = static_cast<int64_t>( reserve1 );
}
const extended_asset out0 = { div_amount(amount0, precision_norm, sym0.precision()), ext_sym0 };
const extended_asset out1 = { div_amount(amount1, precision_norm, sym1.precision()), ext_sym1 };
check( out0.quantity.amount || out1.quantity.amount, "curve::withdraw_liquidity: withdraw amount too small");
// add liquidity deposits & newly issued liquidity
_pairs.modify(pair, get_self(), [&]( auto & row ) {
row.reserve0 -= out0;
row.reserve1 -= out1;
row.liquidity -= value;
// log liquidity change
curve::liquiditylog_action liquiditylog( get_self(), { get_self(), "active"_n });
liquiditylog.send( pair_id, owner, "withdraw"_n, value.quantity, -out0.quantity, -out1.quantity, row.liquidity.quantity, row.reserve0.quantity, row.reserve1.quantity );
});
// issue & transfer to owner
retire( value, get_self().to_string() + ": withdraw" );
if ( out0.quantity.amount ) transfer( get_self(), owner, out0, get_self().to_string() + ": withdraw");
if ( out1.quantity.amount ) transfer( get_self(), owner, out1, get_self().to_string() + ": withdraw");
}
void curve::add_liquidity( const name owner, const symbol_code pair_id, const extended_asset value )
{
curve::pairs_table _pairs( get_self(), get_self().value );
curve::orders_table _orders( get_self(), pair_id.raw() );
// get current order & pairs
auto pair = _pairs.get( pair_id.raw(), "curve::add_liquidity: `pair_id` does not exist");
auto itr = _orders.find( owner.value );
// extended symbols
const extended_symbol ext_sym_in = value.get_extended_symbol();
const extended_symbol ext_sym0 = pair.reserve0.get_extended_symbol();
const extended_symbol ext_sym1 = pair.reserve1.get_extended_symbol();
// initialize quantities
auto insert = [&]( auto & row ) {
row.owner = owner;
row.quantity0 = { itr == _orders.end() ? 0 : itr->quantity0.quantity.amount, ext_sym0 };
row.quantity1 = { itr == _orders.end() ? 0 : itr->quantity1.quantity.amount, ext_sym1 };
// add & validate deposit
if ( ext_sym_in == ext_sym0 ) row.quantity0 += value;
else if ( ext_sym_in == ext_sym1 ) row.quantity1 += value;
else check( false, "curve::add_liquidity: invalid extended symbol");
};
// create/modify order
if ( itr == _orders.end() ) _orders.emplace( get_self(), insert );
else _orders.modify( itr, get_self(), insert );
}
// increase/decrease amplifier of given pair id
[[eosio::action]]
void curve::ramp( const symbol_code pair_id, const uint64_t target_amplifier, const int64_t minutes )
{
require_auth( get_self() );
curve::ramp_table _ramp_table( get_self(), get_self().value );
curve::pairs_table _pairs( get_self(), get_self().value );
auto pair = _pairs.get(pair_id.raw(), "curve::ramp: `pair_id` does not exist in `pairs`");
// validation
check( target_amplifier > 0 && target_amplifier <= MAX_AMPLIFIER, "curve::ramp: target amplifier should be within within valid range");
check( minutes > 0, "curve::ramp: minutes should be above 0");
check( minutes * 60 >= MIN_RAMP_TIME, "curve::ramp: minimum ramp timeframe must exceed " + to_string(MIN_RAMP_TIME) + " seconds");
auto insert = [&]( auto & row ) {
row.pair_id = pair_id;
row.start_amplifier = pair.amplifier;
row.target_amplifier = target_amplifier;
row.start_time = current_time_point();
row.end_time = current_time_point() + eosio::minutes(minutes);
};
auto itr = _ramp_table.find(pair_id.raw());
if ( itr == _ramp_table.end() ) _ramp_table.emplace( get_self(), insert );
else _ramp_table.modify( itr, get_self(), insert );
}
[[eosio::action]]
void curve::stopramp( const symbol_code pair_id )
{
require_auth( get_self() );
curve::ramp_table _ramp( get_self(), get_self().value );
auto & ramp = _ramp.get(pair_id.raw(), "curve::stopramp: `pair_id` does not exist in `ramp` table");
_ramp.erase( ramp );
}
[[eosio::action]]
void curve::setfee( const uint8_t trade_fee, const optional<uint8_t> protocol_fee, const optional<name> fee_account )
{
require_auth( get_self() );
// config
curve::config_table _config( get_self(), get_self().value );
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
auto config = _config.get();
// required params
check( trade_fee <= MAX_TRADE_FEE, "curve::setfee: `trade_fee` has exceeded maximum limit");
check( *protocol_fee <= MAX_PROTOCOL_FEE, "curve::setfee: `protocol_fee` has exceeded maximum limit");
// optional params
if ( fee_account->value ) check( is_account( *fee_account ), "curve::setfee: `fee_account` does not exist");
if ( *protocol_fee ) check( fee_account->value, "curve::setfee: must provide `fee_account` if `protocol_fee` is defined");
// set config
config.trade_fee = trade_fee;
config.protocol_fee = *protocol_fee;
config.fee_account = *fee_account;
_config.set( config, get_self() );
}
[[eosio::action]]
void curve::setnotifiers( const vector<name> notifiers )
{
require_auth( get_self() );
for ( const name notifier : notifiers ) {
check( is_account( notifier ), "curve::setnotifiers: `notifier` does not exist");
}
curve::config_table _config( get_self(), get_self().value );
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
auto config = _config.get();
config.notifiers = notifiers;
_config.set( config, get_self() );
}
[[eosio::action]]
void curve::setstatus( const name status )
{
require_auth( get_self() );
curve::config_table _config( get_self(), get_self().value );
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
auto config = _config.get();
config.status = status;
_config.set( config, get_self() );
}
[[eosio::action]]
void curve::createpair( const name creator, const symbol_code pair_id, const extended_symbol reserve0, const extended_symbol reserve1, const uint64_t amplifier )
{
// `creator` must be contract
check( creator == get_self(), "curve::createpair: only contract admin can create pair");
require_auth( creator );
// tables
curve::pairs_table _pairs( get_self(), get_self().value );
curve::config_table _config( get_self(), get_self().value );
check( _config.exists(), ERROR_CONFIG_NOT_EXISTS );
const name token_contract = _config.get().token_contract;
// reserve params
const name contract0 = reserve0.get_contract();
const name contract1 = reserve1.get_contract();
const symbol sym0 = reserve0.get_symbol();
const symbol sym1 = reserve1.get_symbol();
// check reserves
check( is_account( contract0 ), "curve::createpair: reserve0 contract does not exists");
check( is_account( contract1 ), "curve::createpair: reserve1 contract does not exists");
check( token::get_supply( contract0, sym0.code() ).symbol == sym0, "curve::createpair: reserve0 extended symbol mismatch supply" );
check( token::get_supply( contract1, sym1.code() ).symbol == sym1, "curve::createpair: reserve1 extended symbol mismatch supply" );
check( _pairs.find( pair_id.raw() ) == _pairs.end(), "curve::createpair: `pair_id` already exists" );
check( amplifier > 0 && amplifier <= MAX_AMPLIFIER, "curve::createpair: invalid amplifier" );
// create liquidity token
const extended_symbol liquidity = {{ pair_id, max(sym0.precision(), sym1.precision())}, token_contract };
// in case supply already exists
token::stats _stats( token_contract, pair_id.raw() );
auto stats_itr = _stats.find( pair_id.raw() );
// create token if supply does not exist
if ( stats_itr == _stats.end() ) create( liquidity );
// supply must be empty
else check( !stats_itr->supply.amount, "curve::createpair: creating new pair requires existing supply to be zero" );
// create pair
_pairs.emplace( creator, [&]( auto & row ) {
row.id = pair_id;
row.reserve0 = { 0, reserve0 };
row.reserve1 = { 0, reserve1 };
row.liquidity = { 0, liquidity };
row.amplifier = amplifier;
row.volume0 = { 0, sym0 };
row.volume1 = { 0, sym1 };
row.last_updated = current_time_point();
});
}
// calculate reserve amounts relative to supply
double curve::calculate_virtual_price( const asset value0, const asset value1, const asset supply )
{
const uint8_t precision_norm = max( value0.symbol.precision(), value1.symbol.precision() );
const int64_t amount0 = mul_amount( value0.amount, precision_norm, value0.symbol.precision() );
const int64_t amount1 = mul_amount( value1.amount, precision_norm, value1.symbol.precision() );
const int64_t amount2 = mul_amount( supply.amount, precision_norm, supply.symbol.precision() );
return static_cast<double>( safemath::add(amount0, amount1) ) / amount2;
}
// calculate last price per trade
double curve::calculate_price( const asset value0, const asset value1 )
{
const uint8_t precision_norm = max( value0.symbol.precision(), value1.symbol.precision() );
const int64_t amount0 = mul_amount( value0.amount, precision_norm, value0.symbol.precision() );
const int64_t amount1 = mul_amount( value1.amount, precision_norm, value1.symbol.precision() );
return static_cast<double>(amount0) / amount1;
}
// Memo schemas
// ============
// Swap: `swap,<min_return>,<pair_ids>` (ex: "swap,0,SXA" )
// Deposit: `deposit,<pair_id>` (ex: "deposit,SXA")
// Withdrawal: `` (empty)
curve::memo_schema curve::parse_memo( const string memo )
{
if (memo == "") return {};
// split memo into parts
const vector<string> parts = sx::utils::split(memo, ",");
check(parts.size() <= 3, ERROR_INVALID_MEMO );
// memo result
memo_schema result;
result.action = sx::utils::parse_name(parts[0]);
result.min_return = 0;
// swap action
if ( result.action == "swap"_n ) {
result.pair_ids = parse_memo_pair_ids( parts[2] );
check( sx::utils::is_digit( parts[1] ), ERROR_INVALID_MEMO );
result.min_return = std::stoll( parts[1] );
check( result.min_return >= 0, ERROR_INVALID_MEMO );
check( result.pair_ids.size() >= 1, ERROR_INVALID_MEMO );
// deposit action
} else if ( result.action == "deposit"_n ) {
result.pair_ids = parse_memo_pair_ids( parts[1] );
check( result.pair_ids.size() == 1, ERROR_INVALID_MEMO );
}
return result;
}
// Memo schemas
// ============
// Single: `<pair_id>` (ex: "SXA")
// Multiple: `<pair_id>-<pair_id>` (ex: "SXA-SXB")
vector<symbol_code> curve::parse_memo_pair_ids( const string memo )
{
curve::pairs_table _pairs( get_self(), get_self().value );
set<symbol_code> duplicates;
vector<symbol_code> pair_ids;
for ( const string str : sx::utils::split(memo, "-") ) {
const symbol_code symcode = sx::utils::parse_symbol_code( str );
check( symcode.raw(), ERROR_INVALID_MEMO );
check( _pairs.find( symcode.raw() ) != _pairs.end(), "curve::parse_memo_pair_ids: `pair_id` does not exist");
pair_ids.push_back( symcode );
check( !duplicates.count( symcode ), "curve::parse_memo_pair_ids: invalid duplicate `pair_ids`");
duplicates.insert( symcode );
}
return pair_ids;
}
[[eosio::action]]
void curve::calculate( const uint64_t amount, const uint64_t reserve_in, const uint64_t reserve_out, const uint64_t amplifier, const uint64_t fee )
{
const uint64_t out = Curve::get_amount_out( amount, reserve_in, reserve_out, amplifier, fee );
check(false, "current get_amount_out(amount: " + to_string(amount) + ", amp: " + to_string(amplifier) + " ): " + to_string(out) );
}
} // namespace sx