@@ -49,9 +49,9 @@ void graphene::chain::asset_bitasset_data_object::update_median_feeds( time_poin
49
49
{
50
50
bool after_core_hardfork_1270 = ( next_maintenance_time > HARDFORK_CORE_1270_TIME ); // call price caching issue
51
51
current_feed_publication_time = current_time;
52
- vector<std::reference_wrapper<const price_feed >> current_feeds;
52
+ vector<std::reference_wrapper<const price_feed_with_icr >> current_feeds;
53
53
// find feeds that were alive at current_time
54
- for ( const pair<account_id_type, pair<time_point_sec,price_feed >>& f : feeds )
54
+ for ( const pair<account_id_type, pair<time_point_sec,price_feed_with_icr >>& f : feeds )
55
55
{
56
56
if ( (current_time - f.second .first ).to_seconds () < options.feed_lifetime_sec &&
57
57
f.second .first != time_point_sec () )
@@ -67,13 +67,11 @@ void graphene::chain::asset_bitasset_data_object::update_median_feeds( time_poin
67
67
// ... don't calculate a median, and set a null feed
68
68
feed_cer_updated = false ; // new median cer is null, won't update asset_object anyway, set to false for better performance
69
69
current_feed_publication_time = current_time;
70
- current_feed = price_feed ();
70
+ current_feed = price_feed_with_icr ();
71
71
if ( after_core_hardfork_1270 )
72
72
{
73
- // update data derived from MCR
74
- current_maintenance_collateralization = price ();
75
- // update data derived from ICR
76
- current_initial_collateralization = price ();
73
+ // update data derived from MCR, ICR and etc
74
+ refresh_cache ();
77
75
}
78
76
return ;
79
77
}
@@ -85,25 +83,40 @@ void graphene::chain::asset_bitasset_data_object::update_median_feeds( time_poin
85
83
// Note: perhaps can defer updating current_maintenance_collateralization for better performance
86
84
if ( after_core_hardfork_1270 )
87
85
{
88
- // update data derived from MCR
89
- current_maintenance_collateralization = current_feed.maintenance_collateralization ();
90
- // update data derived from ICR
91
- refresh_current_initial_collateralization ();
86
+ const auto & exts = options.extensions .value ;
87
+ if ( exts.maintenance_collateral_ratio .valid () )
88
+ current_feed.maintenance_collateral_ratio = *exts.maintenance_collateral_ratio ;
89
+ if ( exts.maximum_short_squeeze_ratio .valid () )
90
+ current_feed.maximum_short_squeeze_ratio = *exts.maximum_short_squeeze_ratio ;
91
+ if ( exts.initial_collateral_ratio .valid () )
92
+ current_feed.initial_collateral_ratio = *exts.initial_collateral_ratio ;
93
+ // update data derived from MCR, ICR and etc
94
+ refresh_cache ();
92
95
}
93
96
return ;
94
97
}
95
98
96
99
// *** Begin Median Calculations ***
97
- price_feed median_feed;
100
+ price_feed_with_icr median_feed;
98
101
const auto median_itr = current_feeds.begin () + current_feeds.size () / 2 ;
99
102
#define CALCULATE_MEDIAN_VALUE (r, data, field_name ) \
100
103
std::nth_element ( current_feeds.begin (), median_itr, current_feeds.end (), \
101
- [](const price_feed & a, const price_feed & b) { \
104
+ [](const price_feed_with_icr & a, const price_feed_with_icr & b) { \
102
105
return a.field_name < b.field_name ; \
103
106
}); \
104
107
median_feed.field_name = median_itr->get ().field_name ;
105
108
106
- BOOST_PP_SEQ_FOR_EACH ( CALCULATE_MEDIAN_VALUE, ~, GRAPHENE_PRICE_FEED_FIELDS )
109
+ #define CHECK_AND_CALCULATE_MEDIAN_VALUE (r, data, field_name ) \
110
+ if ( options.extensions .value .field_name .valid () ) { \
111
+ median_feed.field_name = *options.extensions .value .field_name ; \
112
+ } else { \
113
+ CALCULATE_MEDIAN_VALUE (r, data, field_name); \
114
+ }
115
+
116
+ BOOST_PP_SEQ_FOR_EACH ( CALCULATE_MEDIAN_VALUE, ~, (settlement_price)(core_exchange_rate) )
117
+ BOOST_PP_SEQ_FOR_EACH ( CHECK_AND_CALCULATE_MEDIAN_VALUE, ~,
118
+ (maintenance_collateral_ratio)(maximum_short_squeeze_ratio)(initial_collateral_ratio) )
119
+ #undef CHECK_AND_CALCULATE_MEDIAN_VALUE
107
120
#undef CALCULATE_MEDIAN_VALUE
108
121
// *** End Median Calculations ***
109
122
@@ -113,25 +126,25 @@ void graphene::chain::asset_bitasset_data_object::update_median_feeds( time_poin
113
126
// Note: perhaps can defer updating current_maintenance_collateralization for better performance
114
127
if ( after_core_hardfork_1270 )
115
128
{
116
- // update data derived from MCR
117
- current_maintenance_collateralization = current_feed.maintenance_collateralization ();
118
- // update data derived from ICR
119
- refresh_current_initial_collateralization ();
129
+ // update data derived from MCR, ICR and etc
130
+ refresh_cache ();
120
131
}
121
132
}
122
133
123
- void asset_bitasset_data_object::refresh_current_initial_collateralization ()
134
+ void asset_bitasset_data_object::refresh_cache ()
124
135
{
125
- if ( current_feed.settlement_price .is_null () )
126
- current_initial_collateralization = price ();
127
- else
128
- {
129
- const auto & icr = options.extensions .value .initial_collateral_ratio ;
130
- if ( icr.valid () && *icr > current_feed.maintenance_collateral_ratio ) // if ICR is set and is above MCR
131
- current_initial_collateralization = current_feed.calculate_initial_collateralization ( *icr );
132
- else // if ICR is not set, or not above MCR
133
- current_initial_collateralization = current_maintenance_collateralization;
134
- }
136
+ current_maintenance_collateralization = current_feed.maintenance_collateralization ();
137
+ if ( current_feed.initial_collateral_ratio > current_feed.maintenance_collateral_ratio ) // if ICR is above MCR
138
+ current_initial_collateralization = current_feed.calculate_initial_collateralization ();
139
+ else // if ICR is not above MCR
140
+ current_initial_collateralization = current_maintenance_collateralization;
141
+ }
142
+
143
+ price price_feed_with_icr::calculate_initial_collateralization ()const
144
+ {
145
+ if ( settlement_price.is_null () )
146
+ return price ();
147
+ return ~settlement_price * ratio_type ( initial_collateral_ratio, GRAPHENE_COLLATERAL_RATIO_DENOM );
135
148
}
136
149
137
150
asset asset_object::amount_from_string (string amount_string) const
@@ -223,6 +236,8 @@ FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::asset_bitasset_data_object, (gr
223
236
(feed_cer_updated)
224
237
)
225
238
239
+ GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::price_feed_with_icr )
240
+
226
241
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::asset_object )
227
242
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::asset_bitasset_data_object )
228
243
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::chain::asset_dynamic_data_object )
0 commit comments