You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- add PTE UI git submodule
- add oracle for price lookups
- add helper functions for displaying global contract position data
- add redemption and insolvency check code
interest_rate: dec!("0.05"),// TODO - variable loan interest rate. For now, placeholder 5% interest rate.
70
72
positions_counter:0,
71
-
is_insolvent:false
73
+
is_insolvent:false,
74
+
oracle_address: oracle
72
75
}
73
76
.instantiate()
74
77
.add_access_check(rules)
@@ -126,7 +129,7 @@ blueprint! {
126
129
"The position_badge bucket must contain exactly one position badge NFT"
127
130
);
128
131
129
-
let required_collateral_xrd_amount = RaiTest::calc_required_collateral_xrd_amount(requested_rai);
132
+
let required_collateral_xrd_amount = RaiTest::calc_required_collateral_xrd_amount(requested_rai,self.get_xrd_price());
130
133
let position_id = position_badge.non_fungible::<PositionData>().id();
131
134
let position = self.positions.get_mut(&position_id).unwrap();
132
135
@@ -290,11 +293,13 @@ blueprint! {
290
293
"The position_badge bucket does not contain a position badge NFT"
291
294
);
292
295
296
+
let xrd_price = self.get_xrd_price();
297
+
293
298
let position_id = &position_badge.non_fungible::<PositionData>().id();
294
299
let position = self.positions.get_mut(&position_id).unwrap();
295
300
let principal_and_interest = RaiTest::calc_principal_and_interest(position.loan_amount,self.interest_rate, position.start_epoch);
296
301
297
-
let required_collateral_xrd_amount = RaiTest::calc_required_collateral_xrd_amount(principal_and_interest);
302
+
let required_collateral_xrd_amount = RaiTest::calc_required_collateral_xrd_amount(principal_and_interest, xrd_price);
298
303
299
304
info!("Partial Withdraw Collateral - Position ID {} - {:?}", position_id, position);
300
305
info!("Position Principal and Interest - {} RAI, minimum collateral required to maintain position - {} XRD", principal_and_interest, required_collateral_xrd_amount);
@@ -309,7 +314,7 @@ blueprint! {
309
314
}
310
315
311
316
// Callable by anyone acting as a liquidator - provide undercollateralized position id and minimum RAI P&I payment to foreclose on position collateral
info!("Position id {} being liquidated, p&i is {} and required collateral xrd is {}, position only contains {} xrd collateral",
@@ -363,9 +369,13 @@ blueprint! {
363
369
pubfn check_protocol_solvency(&mutself){
364
370
let rai_manager = borrow_resource_manager!(self.rai_resource);
365
371
let total_rai_supply = rai_manager.total_supply();
366
-
let pooled_collateral_value = RaiTest::calc_xrd_value(self.pooled_collateral_vault.amount());
372
+
let pooled_collateral_value = self.calc_xrd_value(self.pooled_collateral_vault.amount());
373
+
info!("Collateral pool xrd amount: {} XRD price: {} Pool value: {} Total RAI supply: {}",self.pooled_collateral_vault.amount(),self.get_xrd_price(), pooled_collateral_value, total_rai_supply);
367
374
if total_rai_supply > pooled_collateral_value {
368
375
self.is_insolvent = true;
376
+
info!("!! Protocol is insolvent !! Freezing liquidations and new positions, redemptions against collateral pool allowed now");
377
+
} else {
378
+
info!("Protocol solvent");
369
379
}
370
380
}
371
381
@@ -384,6 +394,7 @@ blueprint! {
384
394
);
385
395
let rai_manager = borrow_resource_manager!(self.rai_resource);
386
396
let total_rai_supply = rai_manager.total_supply();
397
+
info!("Total RAI supply outstanding: {}", total_rai_supply);
387
398
let percentage_of_total = rai_to_redeem.amount() / total_rai_supply;
388
399
389
400
let collateral_redemption_amount = percentage_of_total *self.pooled_collateral_vault.amount();
@@ -395,21 +406,63 @@ blueprint! {
395
406
let rai_manager = borrow_resource_manager!(self.rai_resource);
396
407
rai_manager.burn(rai_to_redeem);
397
408
});
409
+
410
+
info!("Redeem - {}% of RAI supply redemption, returning {}% of collateral pool - {} xrd", percentage_of_total*100, percentage_of_total*100, redemption_collateral.amount());
398
411
399
412
redemption_collateral
400
413
}
401
414
402
-
fn calc_xrd_value(xrd_amount:Decimal) -> Decimal{
403
-
// TODO - get xrd price from oracle. Fixed placeholder value for now.
404
-
let xrd_price = dec!("0.10");
415
+
// Normally, liquidators would be expected to run bots to subscribe to new `open_position` `draw` `paydown` `close_position`
416
+
// events and maintain an off-ledger system for identifying which vaults are undercollateralized, and call liquidate on
417
+
// the corresponding undercollateralized positions. However since subscription events are not yet available in Babylon,
418
+
// provide this convenience function to print the all outstanding positions and identify if any vaults are available for
419
+
// liquidation and allow manual inspection for liquidation.
420
+
pubfn print_all_positions(&self){
421
+
let xrd_price = self.get_xrd_price();
422
+
info!("xrd price ${}", xrd_price);
423
+
for position_id in self.positions.keys(){
424
+
let position = self.positions.get(position_id).unwrap();
425
+
let principal_and_interest = RaiTest::calc_principal_and_interest(position.loan_amount,self.interest_rate, position.start_epoch);
426
+
let required_collateral_amount = RaiTest::calc_required_collateral_xrd_amount(principal_and_interest,self.get_xrd_price());
427
+
let required_collateral_value = required_collateral_amount * xrd_price;
428
+
let undercollateralized = if position.collateral_amount < required_collateral_amount {true} else {false};
429
+
let collateral_value = self.calc_xrd_value(position.collateral_amount);
0 commit comments