-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmindswap.hpp
81 lines (66 loc) · 2.44 KB
/
mindswap.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
#pragma once
#include <eosio/asset.hpp>
namespace mindswap {
using namespace eosio;
constexpr name id = "mindswap"_n;
constexpr name code = "mindswapswap"_n;
const std::string description = "Mindswap Converter";
/**
* pair
*/
struct [[eosio::table]] stat_row {
asset supply;
asset max_supply;
name issuer;
extended_asset pool1;
extended_asset pool2;
int32_t fee;
name fee_contract;
uint64_t curve;
uint64_t primary_key() const { return supply.symbol.code().raw(); }
};
typedef multi_index< "stat"_n, stat_row > stat;
/**
* ## STATIC `get_amount_out`
*
* Given an input amount of an asset and pair id, returns the calculated return
*
* ### params
*
* - `{asset} in` - input amount
* - `{symbol} out_sym` - out symbol
* - `{string} pair_id` - pair_id
*
* ### example
*
* ```c++
* // Inputs
* const asset in = asset { 10000, "EOS" };
* const symbol out_sym = symbol {"IQ,3"};
* const string pair_id = "EOSIQ"
*
* // Calculation
* const uint64_t amount_out = mindswap::get_amount_out( in, out_sym, pair_id );
* // => 9996
* ```
*/
static asset get_amount_out( const asset in, symbol out_sym, const string& pair_id )
{
check(in.amount > 0, "MindswapLibrary: INSUFFICIENT_INPUT_AMOUNT");
symbol_code liq_symcode {pair_id};
check(liq_symcode.is_valid(), "MindswapLibrary: Invalid pair_id");
mindswap::stat _stat( mindswap::code, liq_symcode.raw() );
auto row = _stat.get(liq_symcode.raw(), "MindswapLibrary: Invalid Mindswap symbol");
if(row.pool1.quantity.symbol != in.symbol) std::swap(row.pool1, row.pool2);
check( row.pool1.quantity.symbol == in.symbol && row.pool2.quantity.symbol == out_sym, "MindswapLibrary: invalid pair");
check( row.pool1.quantity.amount > 0 && row.pool2.quantity.amount > 0, "MindswapLibrary: INSUFFICIENT_LIQUIDITY");
auto am_in = in.amount;
auto res_in = row.pool1.quantity.amount + in.amount;
auto res_out = row.pool2.quantity.amount;
auto am_out = am_in * res_out / res_in;
auto fee = (9999 + am_out * row.fee)/10000;
am_out -= fee;
check(am_out > 0, "MindswapLibrary: wrong return");
return asset { am_out, out_sym };
}
}