Skip to content

Commit

Permalink
fix: update gas tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Dec 13, 2024
1 parent ff8b14f commit 794aaf1
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 89 deletions.
6 changes: 0 additions & 6 deletions snapshots/Pool.Borrow.json

This file was deleted.

15 changes: 15 additions & 0 deletions snapshots/Pool.Operations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"borrow: first borrow->borrowingEnabled": "256189",
"borrow: recurrent borrow": "248474",
"liquidationCall: deficit on liquidated asset": "392266",
"liquidationCall: deficit on liquidated asset + other asset": "422243",
"liquidationCall: full liquidation": "392266",
"liquidationCall: partial liquidation": "381149",
"repay: full repay": "176547",
"repay: partial repay": "189971",
"supply: collateralDisabled": "146829",
"supply: collateralEnabled": "146829",
"supply: first supply->collateralEnabled": "188344",
"withdraw: full withdraw": "165303",
"withdraw: partial withdraw": "181992"
}
5 changes: 0 additions & 5 deletions snapshots/Pool.Supply.json

This file was deleted.

49 changes: 0 additions & 49 deletions tests/gas/Pool.Borrow.gas.t.sol

This file was deleted.

155 changes: 155 additions & 0 deletions tests/gas/Pool.Operations.gas.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import 'forge-std/Test.sol';

import {Errors} from '../../src/contracts/protocol/libraries/helpers/Errors.sol';
import {UserConfiguration} from '../../src/contracts/protocol/libraries/configuration/UserConfiguration.sol';
import {Testhelpers, IERC20} from './Testhelpers.sol';

/**
* Scenario suite for common operations supply/borrow/repay/withdraw/liquidationCall.
*/
contract PoolOperations_gas_Tests is Testhelpers {
address supplier = makeAddr('supplier');
address borrower = makeAddr('borrower');
address liquidator = makeAddr('liquidator');

function test_supply() external {
_supplyOnReserve(supplier, 100e6, tokenList.usdx);
vm.snapshotGasLastCall('Pool.Operations', 'supply: first supply->collateralEnabled');

_skip(100);

_supplyOnReserve(supplier, 100e6, tokenList.usdx);
vm.snapshotGasLastCall('Pool.Operations', 'supply: collateralEnabled');
vm.prank(supplier);
contracts.poolProxy.setUserUseReserveAsCollateral(tokenList.usdx, false);

_skip(100);

_supplyOnReserve(supplier, 100e6, tokenList.usdx);
vm.snapshotGasLastCall('Pool.Operations', 'supply: collateralDisabled');
}

function test_withdraw() external {
_supplyOnReserve(supplier, 100e6, tokenList.usdx);
vm.startPrank(supplier);
_skip(100);

contracts.poolProxy.withdraw(tokenList.usdx, 50e6, supplier);
vm.snapshotGasLastCall('Pool.Operations', 'withdraw: partial withdraw');

_skip(100);

contracts.poolProxy.withdraw(tokenList.usdx, type(uint256).max, supplier);
vm.snapshotGasLastCall('Pool.Operations', 'withdraw: full withdraw');
}

function test_borrow() external {
_supplyOnReserve(borrower, 100 ether, tokenList.weth);
uint256 amountToBorrow = 1000e6;
vm.startPrank(borrower);
contracts.poolProxy.borrow(tokenList.usdx, amountToBorrow, 2, 0, borrower);
vm.snapshotGasLastCall('Pool.Operations', 'borrow: first borrow->borrowingEnabled');

_skip(100);

contracts.poolProxy.borrow(tokenList.usdx, amountToBorrow, 2, 0, borrower);
vm.snapshotGasLastCall('Pool.Operations', 'borrow: recurrent borrow');
}

function test_repay() external {
_supplyOnReserve(borrower, 100 ether, tokenList.weth);
uint256 amountToBorrow = 1000e6;
deal(tokenList.usdx, borrower, amountToBorrow);
vm.startPrank(borrower);
contracts.poolProxy.borrow(tokenList.usdx, amountToBorrow, 2, 0, borrower);
IERC20(tokenList.usdx).approve(report.poolProxy, type(uint256).max);

_skip(100);

contracts.poolProxy.repay(tokenList.usdx, amountToBorrow / 2, 2, borrower);
vm.snapshotGasLastCall('Pool.Operations', 'repay: partial repay');

_skip(100);

contracts.poolProxy.repay(tokenList.usdx, type(uint256).max, 2, borrower);
vm.snapshotGasLastCall('Pool.Operations', 'repay: full repay');
}

function test_liquidationCall_partial() external {
uint256 price = contracts.aaveOracle.getAssetPrice(tokenList.weth);
_supplyOnReserve(borrower, (((price * 1e6) / 1e8) * 90) / 100, tokenList.usdx);
_borrowArbitraryAmount(borrower, 1 ether, tokenList.weth);
deal(tokenList.weth, liquidator, 0.5 ether);
vm.startPrank(liquidator);
IERC20(tokenList.weth).approve(report.poolProxy, 0.5 ether);

_skip(100);

contracts.poolProxy.liquidationCall(tokenList.usdx, tokenList.weth, borrower, 0.5 ether, false);
vm.snapshotGasLastCall('Pool.Operations', 'liquidationCall: partial liquidation');
}

function test_liquidationCall_full() external {
uint256 price = contracts.aaveOracle.getAssetPrice(tokenList.weth);
_supplyOnReserve(borrower, (((price * 1e6) / 1e8) * 90) / 100, tokenList.usdx);
_borrowArbitraryAmount(borrower, 1 ether, tokenList.weth);
deal(tokenList.weth, liquidator, 2 ether);
vm.startPrank(liquidator);
IERC20(tokenList.weth).approve(report.poolProxy, type(uint256).max);

_skip(100);

contracts.poolProxy.liquidationCall(
tokenList.usdx,
tokenList.weth,
borrower,
type(uint256).max,
false
);
vm.snapshotGasLastCall('Pool.Operations', 'liquidationCall: full liquidation');
}

function test_liquidationCall_deficit() external {
uint256 price = contracts.aaveOracle.getAssetPrice(tokenList.weth);
_supplyOnReserve(borrower, (price * 1e6) / 1e8, tokenList.usdx);
_borrowArbitraryAmount(borrower, 1 ether, tokenList.weth);
deal(tokenList.weth, liquidator, 2 ether);
vm.startPrank(liquidator);
IERC20(tokenList.weth).approve(report.poolProxy, type(uint256).max);

_skip(100);

contracts.poolProxy.liquidationCall(
tokenList.usdx,
tokenList.weth,
borrower,
type(uint256).max,
false
);
vm.snapshotGasLastCall('Pool.Operations', 'liquidationCall: deficit on liquidated asset');
}

function test_liquidationCall_deficitInAdditionalReserve() external {
uint256 price = contracts.aaveOracle.getAssetPrice(tokenList.weth);
_supplyOnReserve(borrower, (price * 1e6) / 1e8, tokenList.usdx);
_borrowArbitraryAmount(borrower, 1e5, tokenList.wbtc); // additional deficit
_borrowArbitraryAmount(borrower, 1 ether, tokenList.weth);
deal(tokenList.weth, liquidator, 2 ether);
vm.startPrank(liquidator);
IERC20(tokenList.weth).approve(report.poolProxy, type(uint256).max);

_skip(100);

contracts.poolProxy.liquidationCall(
tokenList.usdx,
tokenList.weth,
borrower,
type(uint256).max,
false
);
vm.snapshotGasLastCall('Pool.Operations', 'liquidationCall: deficit on liquidated asset + other asset');
}
}
29 changes: 0 additions & 29 deletions tests/gas/Pool.Supply.gas.t.sol

This file was deleted.

19 changes: 19 additions & 0 deletions tests/gas/Testhelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.0;

import {IERC20} from 'openzeppelin-contracts/contracts/interfaces/IERC20.sol';
import {IPriceOracleGetter} from '../../src/contracts/interfaces/IPriceOracleGetter.sol';
import {TestnetProcedures} from '../utils/TestnetProcedures.sol';

contract Testhelpers is TestnetProcedures {
Expand All @@ -23,6 +24,9 @@ contract Testhelpers is TestnetProcedures {
_skip(100); // skip some blocks to allow interest to accrue & the block to be not cached
}

/**
* Supplies the specified amount of asset to the reserve.
*/
function _supplyOnReserve(address user, uint256 amount, address asset) internal {
vm.startPrank(user);
deal(asset, user, amount);
Expand All @@ -31,6 +35,21 @@ contract Testhelpers is TestnetProcedures {
vm.stopPrank();
}

// assumes that the caller has at least one unit of collateralAsset that is not the borrowAsset
function _borrowArbitraryAmount(address borrower, uint256 amount, address asset) internal {
// set the oracle price of the borrow asset to 0
vm.mockCall(
address(contracts.aaveOracle),
abi.encodeWithSelector(IPriceOracleGetter.getAssetPrice.selector, address(asset)),
abi.encode(0)
);
// borrow the full emount of the asset
vm.prank(borrower);
contracts.poolProxy.borrow(asset, amount, 2, 0, borrower);
// revert the oracle price
vm.clearMockedCalls();
}

/**
* Skips the specified amount of blocks and adjusts the time accordingly.
* Using vm. methods for --via-ir compat.
Expand Down

0 comments on commit 794aaf1

Please sign in to comment.