
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
The plug opens by declaring aave_market as a caller supplied parameter, so the market address is whatever the caller passes at execution.
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·Take
A second parameter, supply_token, is declared for the caller to supply, naming the collateral asset this plug will work with.
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·Get liquidation LTV
Reading again for supply_token in the same market, this fetches the asset's liquidation threshold.
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·Set
That scaled figure is stored as max_debt_before_liquidation, the debt ceiling at which the position would face liquidation.
8·Take
Another parameter, target_health_factor, is declared for the caller to supply the health factor they want the position to hold.
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·Set
That result is written into target_debt, the debt the position should be steered toward.
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·Predicate
Control flow branches on that boolean equaling true, entering the following block only when current debt is above target_debt.
13·Calculate
Inside that branch, subtracting target_debt from current debt gives how far debt sits above the target.
14·Set
That excess is stored as repay_amount, the debt to pay down.
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·Set
That converted quantity is stored as withdraw_amount.
17·Take
A parameter, borrow_token, is declared for the caller to supply the debt asset.
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·Withdraw
Withdrawing withdraw_amount of supply_token from the market, this pulls the matching collateral back to the socket.
20·Else branch
Should current debt not exceed target_debt, the else branch opens.
21·Calculate
Here subtracting current debt from target_debt gives the shortfall below the target.
22·Set
That shortfall is stored as borrow_amount, the additional debt to take on.
23·Calculate
Dividing borrow_amount by the supply_token price converts the added debt into a quantity of the collateral asset.
24·Set
That converted quantity is stored as supply_amount.
25·Supply
Supplying supply_amount of supply_token to the market, this adds collateral to the position.
26·Borrow
Borrowing borrow_amount of borrow_token from the market, this draws the additional debt against that collateral.
27·End block
The conditional block is closed here, rejoining a single path after whichever branch ran.
On other protocols · 2