Skip to content

Commit

Permalink
Added more gas snapshot tests for the Pool contract (#168)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas <[email protected]>
  • Loading branch information
TepNik and sakulstra authored Jan 2, 2025
1 parent 1f4df52 commit 094e6ec
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 2 deletions.
6 changes: 5 additions & 1 deletion snapshots/Pool.Getters.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"getLiquidationGracePeriod": "7765",
"getReserveData": "32157",
"getUserAccountData: supplies: 0, borrows: 0": "22641",
"getUserAccountData: supplies: 0, borrows: 0 with eMode enabled": "7641",
"getUserAccountData: supplies: 1, borrows: 0": "35053",
"getUserAccountData: supplies: 1, borrows: 0 with eMode enabled": "27360",
"getUserAccountData: supplies: 2, borrows: 0": "47575",
"getUserAccountData: supplies: 2, borrows: 1": "29247"
"getUserAccountData: supplies: 2, borrows: 0 with eMode enabled": "40267",
"getUserAccountData: supplies: 2, borrows: 1": "29247",
"getUserAccountData: supplies: 2, borrows: 1 with eMode enabled": "30439"
}
7 changes: 6 additions & 1 deletion snapshots/Pool.Operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
"liquidationCall: deficit on liquidated asset": "392439",
"liquidationCall: deficit on liquidated asset + other asset": "492078",
"liquidationCall: full liquidation": "392439",
"liquidationCall: full liquidation and receive ATokens": "368797",
"liquidationCall: partial liquidation": "383954",
"liquidationCall: partial liquidation and receive ATokens": "360308",
"repay: full repay": "176521",
"repay: full repay with ATokens": "173922",
"repay: partial repay": "189949",
"repay: partial repay with ATokens": "185129",
"supply: collateralDisabled": "146755",
"supply: collateralEnabled": "146755",
"supply: first supply->collateralEnabled": "176366",
"withdraw: full withdraw": "165226",
"withdraw: partial withdraw": "181916"
"withdraw: partial withdraw": "181916",
"withdraw: partial withdraw with active borrows": "239286"
}
82 changes: 82 additions & 0 deletions tests/gas/Pool.Getters.gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,47 @@ contract PoolGetters_gas_Tests is Testhelpers {
vm.snapshotGasLastCall('Pool.Getters', 'getUserAccountData: supplies: 0, borrows: 0');
}

function test_getUserAccountData_with_eMode_enabled() external {
vm.startPrank(poolAdmin);
EModeCategoryInput memory ct1 = _genCategoryOne();
contracts.poolConfiguratorProxy.setEModeCategory(ct1.id, ct1.ltv, ct1.lt, ct1.lb, ct1.label);
vm.stopPrank();

vm.prank(user);
contracts.poolProxy.setUserEMode(ct1.id);

contracts.poolProxy.getUserAccountData(user);
vm.snapshotGasLastCall(
'Pool.Getters',
'getUserAccountData: supplies: 0, borrows: 0 with eMode enabled'
);
}

function test_getUserAccountData_oneSupplies() external {
_supplyOnReserve(user, 1 ether, tokenList.usdx);
contracts.poolProxy.getUserAccountData(user);
vm.snapshotGasLastCall('Pool.Getters', 'getUserAccountData: supplies: 1, borrows: 0');
}

function test_getUserAccountData_oneSupplies_with_eMode_enabled() external {
vm.startPrank(poolAdmin);
EModeCategoryInput memory ct1 = _genCategoryOne();
contracts.poolConfiguratorProxy.setEModeCategory(ct1.id, ct1.ltv, ct1.lt, ct1.lb, ct1.label);
contracts.poolConfiguratorProxy.setAssetCollateralInEMode(tokenList.usdx, ct1.id, true);
vm.stopPrank();

vm.prank(user);
contracts.poolProxy.setUserEMode(ct1.id);

_supplyOnReserve(user, 1 ether, tokenList.usdx);

contracts.poolProxy.getUserAccountData(user);
vm.snapshotGasLastCall(
'Pool.Getters',
'getUserAccountData: supplies: 1, borrows: 0 with eMode enabled'
);
}

function test_getUserAccountData_twoSupplies() external {
_supplyOnReserve(user, 1 ether, tokenList.usdx);
_supplyOnReserve(user, 1 ether, tokenList.weth);
Expand All @@ -38,6 +73,27 @@ contract PoolGetters_gas_Tests is Testhelpers {
vm.snapshotGasLastCall('Pool.Getters', 'getUserAccountData: supplies: 2, borrows: 0');
}

function test_getUserAccountData_twoSupplies_with_eMode_enabled() external {
vm.startPrank(poolAdmin);
EModeCategoryInput memory ct1 = _genCategoryOne();
contracts.poolConfiguratorProxy.setEModeCategory(ct1.id, ct1.ltv, ct1.lt, ct1.lb, ct1.label);
contracts.poolConfiguratorProxy.setAssetCollateralInEMode(tokenList.usdx, ct1.id, true);
contracts.poolConfiguratorProxy.setAssetCollateralInEMode(tokenList.weth, ct1.id, true);
vm.stopPrank();

vm.prank(user);
contracts.poolProxy.setUserEMode(ct1.id);

_supplyOnReserve(user, 1 ether, tokenList.usdx);
_supplyOnReserve(user, 1 ether, tokenList.weth);

contracts.poolProxy.getUserAccountData(user);
vm.snapshotGasLastCall(
'Pool.Getters',
'getUserAccountData: supplies: 2, borrows: 0 with eMode enabled'
);
}

function test_getUserAccountData_twoSupplies_oneBorrows() external {
_supplyOnReserve(user, 1 ether, tokenList.usdx);
_supplyOnReserve(user, 1 ether, tokenList.weth);
Expand All @@ -50,6 +106,32 @@ contract PoolGetters_gas_Tests is Testhelpers {
vm.snapshotGasLastCall('Pool.Getters', 'getUserAccountData: supplies: 2, borrows: 1');
}

function test_getUserAccountData_twoSupplies_oneBorrows_with_eMode_enabled() external {
vm.startPrank(poolAdmin);
EModeCategoryInput memory ct1 = _genCategoryOne();
contracts.poolConfiguratorProxy.setEModeCategory(ct1.id, ct1.ltv, ct1.lt, ct1.lb, ct1.label);
contracts.poolConfiguratorProxy.setAssetCollateralInEMode(tokenList.usdx, ct1.id, true);
contracts.poolConfiguratorProxy.setAssetCollateralInEMode(tokenList.weth, ct1.id, true);
contracts.poolConfiguratorProxy.setAssetBorrowableInEMode(tokenList.wbtc, ct1.id, true);
vm.stopPrank();

vm.prank(user);
contracts.poolProxy.setUserEMode(ct1.id);

_supplyOnReserve(user, 1 ether, tokenList.usdx);
_supplyOnReserve(user, 1 ether, tokenList.weth);

_supplyOnReserve(address(1), 0.001e8, tokenList.wbtc);
vm.prank(user);
contracts.poolProxy.borrow(tokenList.wbtc, 0.001e8, 2, 0, user);

contracts.poolProxy.getUserAccountData(user);
vm.snapshotGasLastCall(
'Pool.Getters',
'getUserAccountData: supplies: 2, borrows: 1 with eMode enabled'
);
}

function test_getEModeCategoryData() external {
contracts.poolProxy.getEModeCategoryData(1);
vm.snapshotGasLastCall('Pool.Getters', 'getEModeCategoryData');
Expand Down
70 changes: 70 additions & 0 deletions tests/gas/Pool.Operations.gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ contract PoolOperations_gas_Tests is Testhelpers {
vm.snapshotGasLastCall('Pool.Operations', 'withdraw: full withdraw');
}

function test_withdraw_with_active_borrows() external {
_supplyOnReserve(borrower, 100 ether, tokenList.weth);
uint256 amountToBorrow = 1000e6;
vm.startPrank(borrower);
contracts.poolProxy.borrow(tokenList.usdx, amountToBorrow, 2, 0, borrower);
_skip(100);

contracts.poolProxy.withdraw(tokenList.weth, 1 ether, supplier);
vm.snapshotGasLastCall('Pool.Operations', 'withdraw: partial withdraw with active borrows');
}

function test_borrow() external {
_supplyOnReserve(borrower, 100 ether, tokenList.weth);
uint256 amountToBorrow = 1000e6;
Expand Down Expand Up @@ -82,6 +93,25 @@ contract PoolOperations_gas_Tests is Testhelpers {
vm.snapshotGasLastCall('Pool.Operations', 'repay: full repay');
}

function test_repay_with_ATokens() external {
_supplyOnReserve(borrower, 1_000_000e6, tokenList.usdx);
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.repayWithATokens(tokenList.usdx, amountToBorrow / 2, 2);
vm.snapshotGasLastCall('Pool.Operations', 'repay: partial repay with ATokens');

_skip(100);

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

function test_liquidationCall_partial() external {
// on v3.3 the amounts need to be adjusted to not cause error 103 (min leftover) issues
uint256 scalingFactor = 10;
Expand Down Expand Up @@ -122,6 +152,46 @@ contract PoolOperations_gas_Tests is Testhelpers {
vm.snapshotGasLastCall('Pool.Operations', 'liquidationCall: full liquidation');
}

function test_liquidationCall_receive_ATokens_partial() external {
uint256 price = contracts.aaveOracle.getAssetPrice(tokenList.weth);
_supplyOnReserve(borrower, (((price * 3e6) / 1e8) * 90) / 100, tokenList.usdx);
_borrowArbitraryAmount(borrower, 3 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, true);
vm.snapshotGasLastCall(
'Pool.Operations',
'liquidationCall: partial liquidation and receive ATokens'
);
}

function test_liquidationCall_receive_ATokens_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,
true
);
vm.snapshotGasLastCall(
'Pool.Operations',
'liquidationCall: full liquidation and receive ATokens'
);
}

function test_liquidationCall_deficit() external {
uint256 price = contracts.aaveOracle.getAssetPrice(tokenList.weth);
_supplyOnReserve(borrower, (price * 1e6) / 1e8, tokenList.usdx);
Expand Down

0 comments on commit 094e6ec

Please sign in to comment.