Skip to main content
The program supplies collateral WETH from the Pot to Aave V3, borrows borrowUsdc USDC at the variable rate against it, reads the Pot’s health factor, and sends the USDC to recipient. If the health factor after the borrow is below minHealthFactor, the borrow delivers a different amount, or Aave refuses the supply or the borrow, the cook reverts and nothing moves. Target chains: Base. The example uses WETH, USDC and AaveV3.Pool; the recorded simulation is on a Base fork.

Prerequisites

Install the Sauce CLI to compile and simulate. To send the transaction, configure a wallet with WETH for the program and native coin for gas and the Kitchen fee.

Program

pot is the Pot the program runs on, available without declaring it (Names).
aave-borrow-guard.sauce.ts

How it works

  • supply and borrow both name pot as onBehalfOf: the aWETH collateral and the USDC debt are the Pot’s, and Aave checks the borrow against the Pot’s own collateral.
  • The health factor is read with AaveV3.Pool.getUserAccountData(pot) after the borrow, in the same transaction, so the require sees the position the borrow left. Aave treats a health factor below 1.0 (1000000000000000000) as liquidatable; minHealthFactor is the higher floor this cook enforces.
  • borrowed is the change in the Pot’s USDC, required to equal borrowUsdc before any USDC leaves the Pot.
The program checks the health factor once, inside this cook. After it, the position stays in the Pot and its health factor moves with the prices Aave reads and with the interest the debt accrues. Repaying or withdrawing takes another cook.

Run the example

1

Compile

2

Simulate

--assume-balance WETH:1 gives the Pot 1 WETH for this simulation only. The result lists the health factor and what the cook left in the Pot.
3

Send

--fund WETH:1 pulls 1 WETH from your wallet into the Pot in the same cook. sauce tx prints the unsigned transactions instead, for your own signer: the Pot’s deployment if it has no code yet, the transfer of the WETH into the Pot, then the cook.

Simulation result

Simulated on a local fork of Base at block 52965104 with the current contracts, with the command above. Nothing was signed or sent.
With 1 WETH supplied and 500 USDC borrowed, the health factor was 3.96, above the 2.0 floor, so the program sent the 500 USDC to the recipient. The Pot keeps the aBasWETH collateral and 500.000002 variableDebtBasUSDC of debt. The cook used 533,175 gas. On the same fork, a borrow of 1,500 USDC passes Aave’s checks but leaves the health factor below 2.0, and the cook reverts:

Next steps