From fdedffdd89a2be9d07f732fa1c5b50d294e61020 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Thu, 1 Aug 2024 13:04:28 +0530 Subject: [PATCH] Add bot documentation and architecture (#684) * add bot spec * increase limit * update --- x/leveragelp/spec/bot.md | 45 ++++++++++++++++++++++++++++++++++++++ x/leveragelp/types/keys.go | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 x/leveragelp/spec/bot.md diff --git a/x/leveragelp/spec/bot.md b/x/leveragelp/spec/bot.md new file mode 100644 index 000000000..ea17b31a4 --- /dev/null +++ b/x/leveragelp/spec/bot.md @@ -0,0 +1,45 @@ +# Off-Chain Logic for Position Health and Stop-Loss Checks + +## Check Position Health Logic + +### Requirements: + +1. **Position and Debt Objects:** + - `Position` object contains `lpAmount` and other relevant details. + - `Debt` object contains information about the borrowed amount, interest, etc. + +2. **Position Health Calculation:** + - **Formula:** Position Health = (Value of `lpAmount` in USDC) / (Liability in USDC). + - Ref: [Position Health Calculation](https://github.com/elys-network/elys/blob/90d8b7e326dd424a622bb7977e5f2bf1f2a0f1ad/x/leveragelp/keeper/position.go#L388). + - **Value of `lpAmount` in USDC:** + - Calculated using the AMM (Automated Market Maker) pool. + - **Liability Calculation:** + - Include interest stacked over time on the debt. + - Use `Debt` object and `stablestake` parameters to calculate interest stacked. + - Ref: [Debt Interest Calculation](https://github.com/elys-network/elys/blob/90d8b7e326dd424a622bb7977e5f2bf1f2a0f1ad/x/stablestake/keeper/debt.go#L63). + - After calculating use **Formula:** Liability = Debt.Borrowed + Debt.InterestStacked - Debt.InterestPaid. + + - If position health less than safety factor, trigger `closePositions` function. + +## Check Stop-Loss Conditions + +### Requirements: + +1. **Stop-Loss Trigger:** + - Trigger the `closePosition` function if the `lpAmount` price reaches the stop-loss price. + - **Required Data:** + - `lpAmount` price (obtained using the AMM module). + - Stop-loss price (present in the `Position` object). + +### Summary: + +1. Calculate the current value of `lpAmount` in USDC using the AMM pool. +2. Calculate the liability by accounting for borrowed amount and interest (stacked and paid). +3. Calculate the Position Health: Position Health = (Value of `lpAmount` in USDC) / (Liability in USDC). +4. If position health less than safety factor, trigger `closePositions` function. +5. Check if the current `lpAmount` price has reached the stop-loss price, and if so, trigger the `closePositions` function. + +### closePositions function +- `closePositions(liquidate: [](address, u64), stopLoss: [](address, u64))` + - liquidate: list of position ids that needs to be liquidated, function will verify and liquidate. Tuple needs to have user address and position id. + - stopLoss: list of position ids that needs to be closed, function will check for stop loss and close position. Tuple needs to have user address and position id. diff --git a/x/leveragelp/types/keys.go b/x/leveragelp/types/keys.go index 7d73d4bb2..1b7e1501c 100644 --- a/x/leveragelp/types/keys.go +++ b/x/leveragelp/types/keys.go @@ -24,7 +24,7 @@ const ( ParamsKey = "leveragelp_params" ) -const MaxPageLimit = 1000 +const MaxPageLimit = 50000 var ( PositionPrefix = []byte{0x01}