-
Notifications
You must be signed in to change notification settings - Fork 650
/
Copy pathasset_object.cpp
243 lines (211 loc) · 9.35 KB
/
asset_object.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
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/chain/hardfork.hpp>
#include <fc/io/raw.hpp>
#include <fc/uint128.hpp>
using namespace graphene::chain;
share_type asset_bitasset_data_object::max_force_settlement_volume(share_type current_supply) const
{
if( options.maximum_force_settlement_volume == 0 )
return 0;
if( options.maximum_force_settlement_volume == GRAPHENE_100_PERCENT )
return current_supply + force_settled_volume;
fc::uint128_t volume = current_supply.value;
volume += force_settled_volume.value;
volume *= options.maximum_force_settlement_volume;
volume /= GRAPHENE_100_PERCENT;
return static_cast<uint64_t>(volume);
}
void graphene::chain::asset_bitasset_data_object::update_median_feeds( time_point_sec current_time,
time_point_sec next_maintenance_time )
{
bool after_core_hardfork_1270 = ( next_maintenance_time > HARDFORK_CORE_1270_TIME ); // call price caching issue
current_feed_publication_time = current_time;
vector<std::reference_wrapper<const price_feed_with_icr>> current_feeds;
// find feeds that were alive at current_time
for( const pair<account_id_type, pair<time_point_sec,price_feed_with_icr>>& f : feeds )
{
if( (current_time - f.second.first).to_seconds() < options.feed_lifetime_sec &&
f.second.first != time_point_sec() )
{
current_feeds.emplace_back(f.second.second);
current_feed_publication_time = std::min(current_feed_publication_time, f.second.first);
}
}
// If there are no valid feeds, or the number available is less than the minimum to calculate a median...
if( current_feeds.size() < options.minimum_feeds )
{
//... don't calculate a median, and set a null feed
feed_cer_updated = false; // new median cer is null, won't update asset_object anyway, set to false for better performance
current_feed_publication_time = current_time;
current_feed = price_feed_with_icr();
if( after_core_hardfork_1270 )
{
// update data derived from MCR, ICR and etc
refresh_cache();
}
return;
}
if( current_feeds.size() == 1 )
{
if( current_feed.core_exchange_rate != current_feeds.front().get().core_exchange_rate )
feed_cer_updated = true;
current_feed = current_feeds.front();
// Note: perhaps can defer updating current_maintenance_collateralization for better performance
if( after_core_hardfork_1270 )
{
const auto& exts = options.extensions.value;
if( exts.maintenance_collateral_ratio.valid() )
current_feed.maintenance_collateral_ratio = *exts.maintenance_collateral_ratio;
if( exts.maximum_short_squeeze_ratio.valid() )
current_feed.maximum_short_squeeze_ratio = *exts.maximum_short_squeeze_ratio;
if( exts.initial_collateral_ratio.valid() )
current_feed.initial_collateral_ratio = *exts.initial_collateral_ratio;
// update data derived from MCR, ICR and etc
refresh_cache();
}
return;
}
// *** Begin Median Calculations ***
price_feed_with_icr median_feed;
const auto median_itr = current_feeds.begin() + current_feeds.size() / 2;
#define CALCULATE_MEDIAN_VALUE(r, data, field_name) \
std::nth_element( current_feeds.begin(), median_itr, current_feeds.end(), \
[](const price_feed_with_icr& a, const price_feed_with_icr& b) { \
return a.field_name < b.field_name; \
}); \
median_feed.field_name = median_itr->get().field_name;
#define CHECK_AND_CALCULATE_MEDIAN_VALUE(r, data, field_name) \
if( options.extensions.value.field_name.valid() ) { \
median_feed.field_name = *options.extensions.value.field_name; \
} else { \
CALCULATE_MEDIAN_VALUE(r, data, field_name); \
}
BOOST_PP_SEQ_FOR_EACH( CALCULATE_MEDIAN_VALUE, ~, (settlement_price)(core_exchange_rate) )
BOOST_PP_SEQ_FOR_EACH( CHECK_AND_CALCULATE_MEDIAN_VALUE, ~,
(maintenance_collateral_ratio)(maximum_short_squeeze_ratio)(initial_collateral_ratio) )
#undef CHECK_AND_CALCULATE_MEDIAN_VALUE
#undef CALCULATE_MEDIAN_VALUE
// *** End Median Calculations ***
if( current_feed.core_exchange_rate != median_feed.core_exchange_rate )
feed_cer_updated = true;
current_feed = median_feed;
// Note: perhaps can defer updating current_maintenance_collateralization for better performance
if( after_core_hardfork_1270 )
{
// update data derived from MCR, ICR and etc
refresh_cache();
}
}
void asset_bitasset_data_object::refresh_cache()
{
current_maintenance_collateralization = current_feed.maintenance_collateralization();
if( current_feed.initial_collateral_ratio > current_feed.maintenance_collateral_ratio ) // if ICR is above MCR
current_initial_collateralization = current_feed.calculate_initial_collateralization();
else // if ICR is not above MCR
current_initial_collateralization = current_maintenance_collateralization;
}
price price_feed_with_icr::calculate_initial_collateralization()const
{
if( settlement_price.is_null() )
return price();
return ~settlement_price * ratio_type( initial_collateral_ratio, GRAPHENE_COLLATERAL_RATIO_DENOM );
}
asset asset_object::amount_from_string(string amount_string) const
{ try {
bool negative_found = false;
bool decimal_found = false;
for( const char c : amount_string )
{
if( isdigit( c ) )
continue;
if( c == '-' && !negative_found )
{
negative_found = true;
continue;
}
if( c == '.' && !decimal_found )
{
decimal_found = true;
continue;
}
FC_THROW( (amount_string) );
}
share_type satoshis = 0;
share_type scaled_precision = asset::scaled_precision( precision );
const auto decimal_pos = amount_string.find( '.' );
const string lhs = amount_string.substr( negative_found, decimal_pos );
if( !lhs.empty() )
satoshis += fc::safe<int64_t>(std::stoll(lhs)) *= scaled_precision;
if( decimal_found )
{
const size_t max_rhs_size = std::to_string( scaled_precision.value ).substr( 1 ).size();
string rhs = amount_string.substr( decimal_pos + 1 );
FC_ASSERT( rhs.size() <= max_rhs_size );
while( rhs.size() < max_rhs_size )
rhs += '0';
if( !rhs.empty() )
satoshis += std::stoll( rhs );
}
FC_ASSERT( satoshis <= GRAPHENE_MAX_SHARE_SUPPLY );
if( negative_found )
satoshis *= -1;
return amount(satoshis);
} FC_CAPTURE_AND_RETHROW( (amount_string) ) }
string asset_object::amount_to_string(share_type amount) const
{
share_type scaled_precision = asset::scaled_precision( precision );
string result = fc::to_string(amount.value / scaled_precision.value);
auto decimals = abs( amount.value % scaled_precision.value );
if( decimals )
{
if( amount < 0 && result == "0" )
result = "-0";
result += "." + fc::to_string(scaled_precision.value + decimals).erase(0,1);
}
return result;
}
FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::asset_dynamic_data_object, (graphene::db::object),
(current_supply)(confidential_supply)(accumulated_fees)(accumulated_collateral_fees)(fee_pool) )
FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::asset_bitasset_data_object, (graphene::db::object),
(asset_id)
(feeds)
(current_feed)
(current_feed_publication_time)
(current_maintenance_collateralization)
(current_initial_collateralization)
(options)
(force_settled_volume)
(is_prediction_market)
(settlement_price)
(settlement_fund)
(asset_cer_updated)
(feed_cer_updated)
)
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::price_feed_with_icr )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::asset_object )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::asset_bitasset_data_object )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::asset_dynamic_data_object )