nolus-protocol / nolus-money-market

Core logic as CosmWasm contracts defining the Nolus money market
Apache License 2.0
22 stars 15 forks source link

[Lease][Close] Stop Loss and Take Profit Triggers #381

Open gmanev7 opened 2 months ago

gmanev7 commented 2 months ago

The lease owner can set Stop Loss, SL, or/and TakeProfit, TP, triggers after the lease has been fully opened. They may be set up and removed individually, or together until the lease is fully paid, fully closed, or fully liquidated.

The trigger of SL and TP is defined as LTV percent such as Liquidation% > SL% > current LTV% >= TP%. Note that the Liquidation% is set on the Lease Open time and remains intact over the Lease Lifetime.

Correspondingly, a Full Close of the position occurs if

If the Liquidation% and SL% are surpassed simultaneously, and since the higher amount of liquidation and the stop-loss should be closed, the protocol should take the SL event with precedence and act accordingly.

The full position close implies that a trigger is consumed and no longer valid once fired.

It's worth noting that since TP and SL are meant to be triggered on price changes, not liquidations or payments, the TP% is reset if a partial liquidation or a payment takes the position LTV below the TP%. Both would be communicated with the customers, the former on setting TP, and the latter on payment.

Design Define struct ClosePolicy and keep an instance in lease::position::spec::Spec next to liability. Introduce enum Close<Asset> with two variants Full and Partial, similar to lease::position::Liquidation<Asset>, and fn Position::trigger(&Due, Price<Asset>) -> Option<Close<Asset>> . Two options to continue, either (a) Rename appropriately, and amend fn Lease::check_debt to accommodate the trigger result handling, optionally starting a position close. Add a new variant DebtStatus::FireClose(Close<asset>) (how are the alarms scheduled?) , or (b) add a sibling method to fn Lease::check_debt that checks for a position close. Merge the liability and trigger zones to proceed with setting up alarms.

Tasks

gmanev7 commented 2 months ago

@kostovster selected Medium size though my confidence is not higher than 50%, so would say it would take somewhat in between Medium and Large Tasks, i.e. 3-4w