Get liquidation LTV
Get liquidation LTV

Aave · Reads onchain state

Reads the liquidation threshold of an asset in an Aave market. The threshold is decoded from the reserve configuration in basis points: the debt-to-collateral ratio at which a position backed by this asset becomes liquidatable. It sits above the max LTV, and the gap between them is the cushion a position has after borrowing to the limit. Reach for it when computing how far price can move before liquidation, or to size a borrow or repay so a predicate keeps the position a chosen distance below the threshold in the same transaction.

Wind or Unwind to Maintain Target HF

plug
aave
math
27actions
  1. 1·Take

    The plug opens by declaring aave_market as a caller supplied parameter, so the market address is whatever the caller passes at execution.

  2. 2·Get account data

    Using that market from slot 0.0, this reads the socket's full Aave position: collateral, debt, borrow capacity, and health factor.

  3. 3·Take

    A second parameter, supply_token, is declared for the caller to supply, naming the collateral asset this plug will work with.

  4. 4·Get price

    With supply_token from slot 2.0 and the market from slot 0.0, this pulls the oracle price the market assigns to that asset.

  5. 5·Get liquidation LTV

    Reading again for supply_token in the same market, this fetches the asset's liquidation threshold.

  6. 6·Calculate proportional value

    Applying the liquidation threshold from slot 4.2 as basis points against the collateral value in slot 1.1.0, this computes the collateral value scaled by that threshold.

  7. 7·Set

    That scaled figure is stored as max_debt_before_liquidation, the debt ceiling at which the position would face liquidation.

  8. 8·Take

    Another parameter, target_health_factor, is declared for the caller to supply the health factor they want the position to hold.

  9. 9·Calculate

    Dividing max_debt_before_liquidation by the target_health_factor from slot 7.0, this derives the debt level consistent with the desired health factor.

  10. 10·Set

    That result is written into target_debt, the debt the position should be steered toward.

  11. 11·Compare two values

    Comparing current debt in slot 1.1.1 against target_debt, this returns a boolean that is true when current debt exceeds the target.

  12. 12·Predicate

    Control flow branches on that boolean equaling true, entering the following block only when current debt is above target_debt.

  13. 13·Calculate

    Inside that branch, subtracting target_debt from current debt gives how far debt sits above the target.

  14. 14·Set

    That excess is stored as repay_amount, the debt to pay down.

  15. 15·Calculate

    Dividing repay_amount by the supply_token price from slot 3.4 converts the debt amount into a quantity of the collateral asset.

  16. 16·Set

    That converted quantity is stored as withdraw_amount.

  17. 17·Take

    A parameter, borrow_token, is declared for the caller to supply the debt asset.

  18. 18·Repay

    Repaying repay_amount of borrow_token to the market from slot 0.0, this pays down the excess debt from the socket's balance.

  19. 19·Withdraw

    Withdrawing withdraw_amount of supply_token from the market, this pulls the matching collateral back to the socket.

  20. 20·Else branch

    Should current debt not exceed target_debt, the else branch opens.

  21. 21·Calculate

    Here subtracting current debt from target_debt gives the shortfall below the target.

  22. 22·Set

    That shortfall is stored as borrow_amount, the additional debt to take on.

  23. 23·Calculate

    Dividing borrow_amount by the supply_token price converts the added debt into a quantity of the collateral asset.

  24. 24·Set

    That converted quantity is stored as supply_amount.

  25. 25·Supply

    Supplying supply_amount of supply_token to the market, this adds collateral to the position.

  26. 26·Borrow

    Borrowing borrow_amount of borrow_token from the market, this draws the additional debt against that collateral.

  27. 27·End block

    The conditional block is closed here, rejoining a single path after whichever branch ran.

Take
Get account data in
Take
Get price of in
Get liquidation LTV of in
Calculate of
Set to
Take
Calculate
Set to
Is
Calculate
Set to
Calculate
Set to
Take
Repay to
Withdraw from
Else
Calculate
Set to
Calculate
Set to
Supply to
Borrow from
End block
  • Wind or Unwind to Maintain Target HF

    plug
    aave
    math
  • Loop WETH/USDC at Target HF

    aave
    evm
    boolean
  • Swarm: Loop WETH/USDC at Target HF

    boolean
    aave
    evm
  • Loop at Target HF + Trailing Stop Loss

    database
    boolean
    aave
  • Loop at Target HF + Borrow Rate Exit Guard

    database
    boolean
    aave
  • Loop at Target HF + Borrow Rate Entry & Exit Guard

    aave
    boolean
    evm

On other protocols · 2