COOPENOMICS  v1
Кооперативная Экономика
rammarket.hpp
См. документацию.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <eosio/asset.hpp>
6
7#include "../consts.hpp"
8
9namespace eosiosystem {
10using eosio::asset;
11using eosio::symbol;
12
13typedef double real_type;
14
20struct [[eosio::table, eosio::contract(SYSTEM)]] exchange_state {
21 asset supply;
22
23 struct connector {
24 asset balance;
25 double weight = 0.5; //.5
26
27 EOSLIB_SERIALIZE(connector, (balance)(weight))
28 };
29
32
33 uint64_t primary_key() const { return supply.symbol.raw(); }
34
35 asset convert_to_exchange(connector & c, asset in) {
36 real_type R(supply.amount);
37 real_type C(c.balance.amount + in.amount);
38 real_type F(c.weight);
39 real_type T(in.amount);
40 real_type ONE(1.0);
41
42 real_type E = -R * (ONE - pow(ONE + T / C, F));
43 int64_t issued = int64_t(E);
44
45 supply.amount += issued;
46 c.balance.amount += in.amount;
47
48 return asset(issued, supply.symbol);
49 };
50
51 asset convert_from_exchange(connector & c, asset in) {
52 eosio::check(in.symbol == supply.symbol, "unexpected asset symbol input");
53
54 real_type R(supply.amount - in.amount);
55 real_type C(c.balance.amount);
56 real_type F(1.0 / c.weight);
57 real_type E(in.amount);
58 real_type ONE(1.0);
59
60 // potentially more accurate:
61 // The functions std::expm1 and std::log1p are useful for financial
62 // calculations, for example, when calculating small daily interest rates:
63 // (1+x)n -1 can be expressed as std::expm1(n * std::log1p(x)). real_type T
64 // = C * std::expm1( F * std::log1p(E/R) );
65
66 real_type T = C * (std::pow(ONE + E / R, F) - ONE);
67 int64_t out = int64_t(T);
68
69 supply.amount -= in.amount;
70 c.balance.amount -= out;
71
72 return asset(out, c.balance.symbol);
73 };
74
75 asset convert(asset from, const symbol &to) {
76 auto sell_symbol = from.symbol;
77 auto ex_symbol = supply.symbol;
78 auto base_symbol = base.balance.symbol;
79 auto quote_symbol = quote.balance.symbol;
80
81 // print( "From: ", from, " TO ", asset( 0,to), "\n" );
82 // print( "base: ", base_symbol, "\n" );
83 // print( "quote: ", quote_symbol, "\n" );
84 // print( "ex: ", supply.symbol, "\n" );
85
86 if (sell_symbol != ex_symbol) {
87 if (sell_symbol == base_symbol) {
88 from = convert_to_exchange(base, from);
89 } else if (sell_symbol == quote_symbol) {
90 from = convert_to_exchange(quote, from);
91 } else {
92 eosio::check(false, "invalid sell");
93 }
94 } else {
95 if (to == base_symbol) {
96 from = convert_from_exchange(base, from);
97 } else if (to == quote_symbol) {
98 from = convert_from_exchange(quote, from);
99 } else {
100 eosio::check(false, "invalid conversion");
101 }
102 }
103
104 if (to != from.symbol)
105 return convert(from, to);
106
107 return from;
108 };
109
110 EOSLIB_SERIALIZE(exchange_state, (supply)(base)(quote))
111};
112
113typedef eosio::multi_index<"rammarket"_n, exchange_state> rammarket;
114
115eosio::asset determine_ram_price(uint32_t bytes) {
116 eosiosystem::rammarket rammarkettable(_system, _system.value);
117 auto market = rammarkettable.get(_ramcore_symbol.raw());
118 auto ram_price =
119 market.convert(eosio::asset{bytes, RAM_symbol}, _root_symbol);
120 ram_price.amount = (ram_price.amount * 200 + 199) / 199; // add ram fee
121
122 return ram_price;
123}
124
125} // namespace eosiosystem
static constexpr eosio::symbol _ramcore_symbol
Definition: consts.hpp:218
static constexpr eosio::name _system
Definition: consts.hpp:158
static constexpr eosio::symbol RAM_symbol
Definition: consts.hpp:217
static constexpr eosio::symbol _root_symbol
Definition: consts.hpp:208
contract
Definition: eosio.msig_tests.cpp:977
balance
Definition: eosio.token_tests.cpp:226
supply
Definition: eosio.token_tests.cpp:150
Definition: eosio.msig.hpp:34
Definition: rammarket.hpp:9
eosio::multi_index<"rammarket"_n, exchange_state > rammarket
Definition: rammarket.hpp:113
double real_type
Definition: rammarket.hpp:13
eosio::asset determine_ram_price(uint32_t bytes)
Definition: rammarket.hpp:115
Definition: rammarket.hpp:23
double weight
Definition: rammarket.hpp:25
asset balance
Definition: rammarket.hpp:24
Definition: rammarket.hpp:20
uint64_t primary_key() const
Definition: rammarket.hpp:33
connector quote
Definition: rammarket.hpp:31
asset supply
Definition: rammarket.hpp:21
connector base
Definition: rammarket.hpp:30
asset convert_to_exchange(connector &c, asset in)
Definition: rammarket.hpp:35
asset convert(asset from, const symbol &to)
Definition: rammarket.hpp:75
asset convert_from_exchange(connector &c, asset in)
Definition: rammarket.hpp:51