Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysiosystem::exchange_state Struct Reference

#include <exchange_state.hpp>

Collaboration diagram for sysiosystem::exchange_state:

Classes

struct  connector
 

Public Member Functions

uint64_t primary_key () const
 
asset convert_to_exchange (connector &reserve, const asset &payment)
 
asset convert_from_exchange (connector &reserve, const asset &tokens)
 
asset convert (const asset &from, const symbol &to)
 
asset direct_convert (const asset &from, const symbol &to)
 

Static Public Member Functions

static int64_t get_bancor_output (int64_t inp_reserve, int64_t out_reserve, int64_t inp)
 
static int64_t get_bancor_input (int64_t out_reserve, int64_t inp_reserve, int64_t out)
 

Public Attributes

asset supply
 
connector base
 
connector quote
 

Detailed Description

Uses Bancor math to create a 50/50 relay between two asset types.

The state of the bancor exchange is entirely contained within this struct. There are no external side effects associated with using this API.

Definition at line 17 of file exchange_state.hpp.

Member Function Documentation

◆ convert()

asset sysiosystem::exchange_state::convert ( const asset & from,
const symbol & to )

Definition at line 39 of file exchange_state.cpp.

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 }
asset convert_to_exchange(connector &reserve, const asset &payment)
asset convert_from_exchange(connector &reserve, const asset &tokens)
Here is the call graph for this function:

◆ convert_from_exchange()

asset sysiosystem::exchange_state::convert_from_exchange ( connector & reserve,
const asset & tokens )

Definition at line 25 of file exchange_state.cpp.

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 }
signed __int64 int64_t
Definition stdint.h:135
Here is the caller graph for this function:

◆ convert_to_exchange()

asset sysiosystem::exchange_state::convert_to_exchange ( connector & reserve,
const asset & payment )

Definition at line 11 of file exchange_state.cpp.

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 }
Here is the caller graph for this function:

◆ direct_convert()

asset sysiosystem::exchange_state::direct_convert ( const asset & from,
const symbol & to )

Definition at line 59 of file exchange_state.cpp.

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 }
static int64_t get_bancor_output(int64_t inp_reserve, int64_t out_reserve, int64_t inp)
Here is the call graph for this function:

◆ get_bancor_input()

int64_t sysiosystem::exchange_state::get_bancor_input ( int64_t out_reserve,
int64_t inp_reserve,
int64_t out )
static

Definition at line 96 of file exchange_state.cpp.

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 }
Here is the caller graph for this function:

◆ get_bancor_output()

int64_t sysiosystem::exchange_state::get_bancor_output ( int64_t inp_reserve,
int64_t out_reserve,
int64_t inp )
static

Definition at line 81 of file exchange_state.cpp.

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 }
Here is the caller graph for this function:

◆ primary_key()

uint64_t sysiosystem::exchange_state::primary_key ( ) const
inline

Definition at line 30 of file exchange_state.hpp.

30{ return supply.symbol.raw(); }

Member Data Documentation

◆ base

connector sysiosystem::exchange_state::base

Definition at line 27 of file exchange_state.hpp.

◆ quote

connector sysiosystem::exchange_state::quote

Definition at line 28 of file exchange_state.hpp.

◆ supply

asset sysiosystem::exchange_state::supply

Definition at line 18 of file exchange_state.hpp.


The documentation for this struct was generated from the following files: