
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



1·Take
Declares aave_market as a caller supplied parameter, taking whichever Aave market address is passed at execution time.
2·Get account data
Reads the socket's overall position in that market from the market wired in slot 0.0, surfacing collateral, debt, borrow capacity, and health factor.
3·Take
Declares supply_token as a second caller supplied parameter, the collateral asset the caller names.
4·Get price
Reads the oracle price the market assigns to supply_token in that same market.
5·Get liquidation LTV
Pulls the liquidation LTV of supply_token in the market, the threshold at which the position becomes liquidatable.
6·Calculate proportional value
Taking the liquidation LTV in basis points of the collateral figure from the account data, this derives the debt ceiling before liquidation.
7·Set
Storing that ceiling as max_debt_before_liquidation, this names the maximum debt the collateral can carry.
8·Take
Declares target_health_factor as a third caller supplied parameter, the buffer divisor supplied at execution time.
9·Calculate
Dividing max_debt_before_liquidation by target_health_factor sets a debt target under the ceiling.
10·Set
Holding that quotient as target_debt, this names the debt level the plug steers toward.
11·Compare two values
Comparing current debt from the account data against target_debt with a greater than operator, this returns a boolean about debt sitting above target.
12·Predicate
A predicate opens on that boolean being true, so the following branch runs only when debt exceeds target.
13·Calculate
Subtracting target_debt from current debt sizes the excess to clear.
14·Set
That excess is stored as repay_amount, the debt to retire on this side.
15·Calculate
Dividing repay_amount by the supply_token price converts the excess into collateral units.
16·Set
The quotient is saved as withdraw_amount, the collateral to pull once debt is cleared.
17·Take
Declares borrow_token as a fourth caller supplied parameter, the debt asset the caller names.
18·Repay
Repaying repay_amount of borrow_token to the market from the socket's balance clears the excess debt.
19·Withdraw
Withdrawing withdraw_amount of supply_token from the market deleverages toward target.
20·Else branch
Opening the else branch, this defines what runs when debt is at or below target.
21·Calculate
Subtracting current debt from target_debt sizes the shortfall to add.
22·Set
That shortfall is stored as borrow_amount, the debt to add on this side.
23·Calculate
Dividing borrow_amount by the supply_token price converts the shortfall into collateral units.
24·Set
The quotient is held as supply_amount, the collateral to add before borrowing.
25·Supply
Supplying supply_amount of supply_token to the market mints aTokens to the wallet.
26·Borrow
Borrowing borrow_amount of borrow_token from the market against the fresh collateral levers up toward target.
27·End block
Closing the innermost open block, this ends the conditional opened above.
On other protocols · 3