Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
exchange_state.cpp
Go to the documentation of this file.
2
3#include <sysio/check.hpp>
4
5#include <cmath>
6
7namespace sysiosystem {
8
9 using sysio::check;
10
12 {
13 const double S0 = supply.amount;
14 const double R0 = reserve.balance.amount;
15 const double dR = payment.amount;
16 const double F = reserve.weight;
17
18 double dS = S0 * ( std::pow(1. + dR / R0, F) - 1. );
19 if ( dS < 0 ) dS = 0; // rounding errors
20 reserve.balance += payment;
21 supply.amount += int64_t(dS);
22 return asset( int64_t(dS), supply.symbol );
23 }
24
26 {
27 const double R0 = reserve.balance.amount;
28 const double S0 = supply.amount;
29 const double dS = -tokens.amount; // dS < 0, tokens are subtracted from supply
30 const double Fi = double(1) / reserve.weight;
31
32 double dR = R0 * ( std::pow(1. + dS / S0, Fi) - 1. ); // dR < 0 since dS < 0
33 if ( dR > 0 ) dR = 0; // rounding errors
34 reserve.balance.amount -= int64_t(-dR);
35 supply -= tokens;
36 return asset( int64_t(-dR), reserve.balance.symbol );
37 }
38
39 asset exchange_state::convert( const asset& from, const symbol& to )
40 {
41 const auto& sell_symbol = from.symbol;
42 const auto& base_symbol = base.balance.symbol;
43 const auto& quote_symbol = quote.balance.symbol;
44 check( sell_symbol != to, "cannot convert to the same symbol" );
45
46 asset out( 0, to );
47 if ( sell_symbol == base_symbol && to == quote_symbol ) {
48 const asset tmp = convert_to_exchange( base, from );
49 out = convert_from_exchange( quote, tmp );
50 } else if ( sell_symbol == quote_symbol && to == base_symbol ) {
51 const asset tmp = convert_to_exchange( quote, from );
52 out = convert_from_exchange( base, tmp );
53 } else {
54 check( false, "invalid conversion" );
55 }
56 return out;
57 }
58
60 {
61 const auto& sell_symbol = from.symbol;
62 const auto& base_symbol = base.balance.symbol;
63 const auto& quote_symbol = quote.balance.symbol;
64 check( sell_symbol != to, "cannot convert to the same symbol" );
65
66 asset out( 0, to );
67 if ( sell_symbol == base_symbol && to == quote_symbol ) {
68 out.amount = get_bancor_output( base.balance.amount, quote.balance.amount, from.amount );
69 base.balance += from;
70 quote.balance -= out;
71 } else if ( sell_symbol == quote_symbol && to == base_symbol ) {
72 out.amount = get_bancor_output( quote.balance.amount, base.balance.amount, from.amount );
73 quote.balance += from;
74 base.balance -= out;
75 } else {
76 check( false, "invalid conversion" );
77 }
78 return out;
79 }
80
82 int64_t out_reserve,
83 int64_t inp )
84 {
85 const double ib = inp_reserve;
86 const double ob = out_reserve;
87 const double in = inp;
88
89 int64_t out = int64_t( (in * ob) / (ib + in) );
90
91 if ( out < 0 ) out = 0;
92
93 return out;
94 }
95
97 int64_t inp_reserve,
98 int64_t out )
99 {
100 const double ob = out_reserve;
101 const double ib = inp_reserve;
102
103 int64_t inp = (ib * out) / (ob - out);
104
105 if ( inp < 0 ) inp = 0;
106
107 return inp;
108 }
109
110}
signed __int64 int64_t
Definition stdint.h:135
static int64_t get_bancor_input(int64_t out_reserve, int64_t inp_reserve, int64_t out)
asset convert_to_exchange(connector &reserve, const asset &payment)
asset convert(const asset &from, const symbol &to)
asset convert_from_exchange(connector &reserve, const asset &tokens)
asset direct_convert(const asset &from, const symbol &to)
static int64_t get_bancor_output(int64_t inp_reserve, int64_t out_reserve, int64_t inp)