Skip to content

Commit

Permalink
Add bot documentation and architecture (#684)
Browse files Browse the repository at this point in the history
* add bot spec

* increase limit

* update
  • Loading branch information
amityadav0 authored Aug 1, 2024
1 parent 8ca39e3 commit fdedffd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions x/leveragelp/spec/bot.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion x/leveragelp/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
ParamsKey = "leveragelp_params"
)

const MaxPageLimit = 1000
const MaxPageLimit = 50000

var (
PositionPrefix = []byte{0x01}
Expand Down

0 comments on commit fdedffd

Please sign in to comment.