
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
Declaring aave_market as a caller supplied parameter, this takes whichever Aave market address is passed at execution time.
2·Get account data
Reading the socket's overall position from the market wired in slot 0.0, this surfaces collateral, debt, borrow capacity, and health factor.
3·Take
Taking supply_token as a second caller supplied parameter, this names the collateral asset the caller passes at execution.
4·Get price
Oracle price for the supply_token in the market is read, giving the asset's value in the market's own units.
5·Get liquidation LTV
Liquidation LTV for the supply_token in the market is read, giving the threshold at which the position becomes liquidatable.
6·Calculate proportional value
A proportional calculation takes the liquidation LTV in slot 4.2 in basis points of the collateral figure in slot 1.1.0, deriving a ceiling figure.
7·Set
Storing that ceiling as max_debt_before_liquidation, this names the highest debt the collateral can carry before liquidation.
8·Take
Taking target_health_factor as a third caller supplied parameter, this names the health factor level the caller passes at execution.
9·Calculate
Dividing max_debt_before_liquidation by target_health_factor sets a debt target scaled by the requested buffer.
10·Set
Holding that quotient as target_debt, this names the debt level the position should be steered toward.
11·Compare two values
Testing current debt in slot 1.1.1 against target_debt with a greater than operator, this returns a boolean about debt sitting above target.
12·Predicate
A predicate opens on that comparison equaling true, so the following branch runs only when debt exceeds target.
13·Calculate
Subtracting target_debt from current debt gives the excess to clear.
14·Set
That result is stored as repay_amount, the amount of debt to pay down.
15·Calculate
Dividing repay_amount by the price in slot 3.4 converts it into collateral units.
16·Set
The quotient is saved as withdraw_amount, the collateral to pull once debt drops.
17·Take
Taking borrow_token as a fourth caller supplied parameter, this names the debt asset the caller passes at execution.
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 the position 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 gives the shortfall to add.
22·Set
That difference is stored as borrow_amount, the additional debt to take on.
23·Calculate
Dividing borrow_amount by the price in slot 3.4 converts it into collateral units.
24·Set
The quotient is held as supply_amount, the collateral to add first.
25·Supply
Supplying supply_amount of supply_token to the market mints aTokens to the wallet.
26·Borrow
Against that fresh collateral, borrow_amount of borrow_token is borrowed from the market, levering up toward target.
27·End block
Closing the innermost open block, this ends the debt branch.
On other protocols · 3