94491f
How to test a lending integration on a mainnet fork
To test a lending integration on a mainnet fork, pin Ethereum to a block, supply WETH, borrow DAI, then assert collateral, debt and health factor.
The Hashbeam Desk··5 min read

To test a lending protocol integration on a mainnet fork, run a Foundry test against deployed Ethereum contracts, then assert the position’s collateral, debt and health factor after each transaction.
How do you set up the fork and open a position?
A pinned Ethereum fork lets your test call deployed contracts with the caps and prices recorded at that block, without spending funds on mainnet. SparkLend uses Aave V3 code, so its Pool exposes the familiar supply, borrow, repay and withdraw methods. That background helps you map an integration flow onto the deployed Pool interface.
- Fork a fixed block. Start with an empty Foundry project, an Ethereum RPC URL, and the Pool and oracle addresses from official sources. In a test’s setUp, call vm.createSelectFork(rpcUrl, blockNumber) with a chosen block number. You can also start Anvil with anvil --fork-url $ETH_RPC_URL --fork-block-number $BLOCK and point your tests at it. Pinning the block keeps reserve caps, rates and oracle prices consistent between runs; an unpinned fork can change behavior as mainnet advances.
- Fund the test account with WETH. Set the test actor, then use Foundry’s deal(WETH, actor, amount) helper to assign it WETH on the fork. If the flow needs native ETH for wrapping or transaction costs, use vm.deal(actor, amount) as well. This changes local fork state only. Use the deployed WETH address and a test amount appropriate to the position you intend to open.
- Approve the Pool and supply WETH. From the actor, approve the Pool to spend the WETH, then call supply(WETH, amount, actor, 0). The Pool transfers the underlying asset and credits the position’s interest-bearing spToken balance. Read getUserAccountData(actor) after the call: total collateral should be nonzero, debt should remain zero, and health factor should be above the liquidation threshold. These values are denominated in the protocol’s base currency, so compare them with expected values in that unit rather than raw WETH units.
- Borrow DAI against the supplied collateral. Call the Pool’s borrow(DAI, amount, 2, 0, actor) for variable-rate debt, then read account data again. The returned health factor is scaled by 1e18; divide by 1e18 when displaying it as a human-readable number, or compare the raw value against a threshold scaled by the same factor. A borrow above the account’s available borrowing power reverts. A full reserve cap can also block a transaction even when the actor has enough collateral.
For the illustrative run, supplying WETH leaves the actor with collateral and no debt; borrowing DAI adds debt and lowers the health factor. The pinned block fixes the prices used for those checks, while the account data confirms the deployed Pool accepted the position.
How do you finish the test and check its result?
Repay the DAI debt, withdraw the WETH collateral, and make the final assertions against the Pool’s account data. A passing test confirms both the integration calls and the resulting position state.
- Repay DAI and withdraw WETH. First fund the actor with enough DAI using deal(DAI, actor, amount), approve the Pool, and call repay(DAI, amount, 2, actor). Then call withdraw(WETH, amount, actor). Use the same rate mode for repayment as for borrowing. If repaying a chosen amount leaves dust because the accrued debt differs from the borrowed amount, repay the full debt using the Pool interface’s supported full-repayment amount.
- Check the final account state. Call getUserAccountData(actor) once more. Assert that total debt is zero and that total collateral reflects the withdrawal; if all WETH was withdrawn, collateral should also be zero. The test is done when it passes with final debt at zero. Keeping assertions after each state-changing call makes a failure point to the specific transition that broke.
- Exercise accrual and liquidation as separate cases. To simulate interest accrual, record the debt and spToken balance, advance time with vm.warp, then query balances again. Debt and the interest-bearing token balance should grow as the protocol accrues interest; the exact change depends on elapsed time and the reserve rate at the pinned block. For a liquidation test, time alone will not move a Chainlink price on a fork: the oracle value is frozen at the fork block. Instead, configure a test price source, impersonate the authorized oracle administrator with vm.prank, and replace the collateral asset’s source through the oracle’s supported admin method. Lower the collateral price enough to make the position liquidatable, then call liquidationCall and assert the resulting collateral and debt changes. This tests the liquidation path without waiting for an external price update.
Related
How do I simulate interest accrual in a fork test?
Advance the fork’s timestamp with vm.warp, then read the debt and spToken balances again. The pinned block supplies the starting rates and prices; the elapsed time drives accrual within the test.
Why does my fork test revert with a cap error?
A reserve cap can prevent additional supply or borrowing even when the actor has funds and collateral. Check the reserve’s current state at the pinned block, then choose an amount within its available capacity or pin a block where the intended transaction is possible.