26 #include <boost/multiprecision/cpp_int.hpp>
63 const uint16_t maintenance_collateral_ratio,
68 feed_price = ~feed_price;
73 bool after_core_hardfork_1270 = maintenance_collateralization.
valid();
76 if( after_core_hardfork_1270 )
89 if( ( !after_core_hardfork_1270 &&
call_price > feed_price )
90 || ( after_core_hardfork_1270 &&
collateralization() > *maintenance_collateralization ) )
99 price target_collateralization = ( after_core_hardfork_1270 ?
105 match_price = ~match_price;
110 using i256 = boost::multiprecision::int256_t;
117 i256 numerator = fp_coll_amt * mp_debt_amt *
debt.
value * tcr
123 if( denominator <= 0 )
128 i256 to_cover_i256 = ( numerator / denominator );
131 share_type to_cover_amt =
static_cast< int64_t
>( to_cover_i256 );
136 asset to_cover = to_pay * match_price;
145 std::function<bool()> result_is_good = after_core_hardfork_1270 ?
146 std::function<bool()>( [
this,&to_cover,&to_pay,target_collateralization]() ->
bool
149 return ( new_collateralization > target_collateralization );
151 std::function<
bool()>( [
this,&to_cover,&to_pay,tcr,feed_price]() ->
bool
154 return ( new_call_price > feed_price );
158 if( result_is_good() )
159 return to_cover.amount;
162 numerator += fp_coll_amt * mp_debt_amt * tcr;
163 to_cover_i256 = ( numerator / denominator ) + 1;
166 to_cover_amt =
static_cast< int64_t
>( to_cover_i256 );
180 if( max_to_pay <= to_pay || max_to_cover <= to_cover )
183 FC_ASSERT( max_to_pay > to_pay && max_to_cover > to_cover );
185 asset min_to_pay = to_pay;
186 asset min_to_cover = to_cover;
191 bool max_is_ok =
false;
195 if( match_price.base.amount < match_price.quote.amount )
198 if( to_pay.amount == max_to_pay.
amount )
199 to_cover.amount = max_to_cover.
amount;
202 to_cover = to_pay * match_price;
203 if( to_cover.amount >= max_to_cover.
amount )
205 to_pay.amount = max_to_pay.
amount;
206 to_cover.amount = max_to_cover.
amount;
210 to_pay = to_cover.multiply_and_round_up( match_price );
217 to_cover.amount = ( min_to_cover.
amount + max_to_cover.
amount ) / 2;
218 if( to_cover.amount == max_to_cover.
amount )
219 to_pay.amount = max_to_pay.
amount;
222 to_pay = to_cover.multiply_and_round_up( match_price );
223 if( to_pay.amount >= max_to_pay.
amount )
225 to_pay.amount = max_to_pay.
amount;
226 to_cover.amount = max_to_cover.
amount;
230 to_cover = to_pay * match_price;
231 if( to_cover.amount >= max_to_cover.
amount )
233 to_pay.amount = max_to_pay.
amount;
234 to_cover.amount = max_to_cover.
amount;
241 if( to_pay.amount <= min_to_pay.
amount || to_cover.amount <= min_to_cover.
amount
242 || to_pay.amount > max_to_pay.
amount || to_cover.amount > max_to_cover.
amount )
244 to_pay.amount = max_to_pay.
amount;
245 to_cover.amount = max_to_cover.
amount;
249 if( to_pay.amount == max_to_pay.
amount && ( max_is_ok || to_pay.amount ==
collateral ) )
250 return to_cover.amount;
254 if( result_is_good() )
256 if( to_pay.amount == max_to_pay.
amount )
257 return to_cover.amount;
258 max_to_pay.
amount = to_pay.amount;
259 max_to_cover.
amount = to_cover.amount;
264 if( to_pay.amount == max_to_pay.
amount )
266 min_to_pay.
amount = to_pay.amount;
267 min_to_cover.
amount = to_cover.amount;
272 for( uint64_t d1 = 0, d2 = 1, d3 = 1; ; d1 = d2, d2 = d3, d3 = d1 + d2 )
274 if( match_price.base.amount > match_price.quote.amount )
279 to_cover = to_pay * match_price;
280 if( to_cover.amount >=
debt )
282 to_pay = to_cover.multiply_and_round_up( match_price );
288 to_cover.amount += d2;
289 if( to_cover.amount >=
debt )
291 to_pay = to_cover.multiply_and_round_up( match_price );
294 to_cover = to_pay * match_price;
295 if( to_cover.amount >=
debt )
303 if( result_is_good() )
304 return to_cover.amount;
311 (expiration)(seller)(for_sale)(sell_price)(filled_amount)(deferred_fee)(deferred_paid_fee)
312 (is_settled_debt)(on_fill)(take_profit_order_id)
316 (borrower)(collateral)(debt)(call_price)(target_collateral_ratio) )
320 (owner)(balance)(settlement_date)
324 (bidder)(inv_swan_price) )