Skip to content

Commit

Permalink
Merge pull request #1403 from morpho-dao/test/max-gas-supply
Browse files Browse the repository at this point in the history
Added maxGasForMatching test for supply
  • Loading branch information
Rubilmax authored Nov 9, 2022
2 parents e2dcac5 + 993e121 commit 8468142
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 43 deletions.
13 changes: 7 additions & 6 deletions test-foundry/aave-v2/TestBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -249,32 +249,33 @@ contract TestBorrow is TestSetup {
borrower1.borrow(aUsdc, to6Decimals(amount));
}

function testShouldUseRightAmountOfGas() public {
function testShouldMatchBorrowWithCorrectAmountOfGas() public {
uint256 amount = 100 ether;
createSigners(30);

uint256 snapshotId = vm.snapshot();
uint256 gasUsed1 = _getBorrowGasUsage(amount, 1e5);

vm.revertTo(snapshotId);
uint256 gasUsed2 = _getBorrowGasUsage(amount, 2e5);

assertGt(gasUsed2, gasUsed1 + 1e4);
}

/// @dev Helper for gas usage test
function _getBorrowGasUsage(uint256 amount, uint256 maxGas) internal returns (uint256 gasUsed) {
// 2 * NMAX suppliers supply suppliedAmount
// 2 * NMAX suppliers supply amount
for (uint256 i; i < 30; i++) {
suppliers[i].setMorphoAddresses(morpho);
suppliers[i].approve(dai, type(uint256).max);
suppliers[i].supply(aDai, amount);
}

borrower1.setMorphoAddresses(morpho);
borrower1.approve(usdc, to6Decimals(amount * 200));
borrower1.supply(aUsdc, to6Decimals(amount * 200));

uint256 gasLeftBefore = gasleft();
borrower1.borrow(aDai, amount * 20, maxGas);
uint256 gasLeftAfter = gasleft();
gasUsed = gasLeftBefore - gasLeftAfter;

gasUsed = gasLeftBefore - gasleft();
}
}
30 changes: 30 additions & 0 deletions test-foundry/aave-v2/TestSupply.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,34 @@ contract TestSupply is TestSetup {
// Staking rewards should accrue on stETH even if there's is no supply interest rate on Aave.
assertGt(withdrawn, deposited);
}

function testShouldMatchSupplyWithCorrectAmountOfGas() public {
uint256 amount = 100 ether;
createSigners(30);

uint256 snapshotId = vm.snapshot();
uint256 gasUsed1 = _getSupplyGasUsage(amount, 1e5);

vm.revertTo(snapshotId);
uint256 gasUsed2 = _getSupplyGasUsage(amount, 2e5);

assertGt(gasUsed2, gasUsed1 + 1e4);
}

/// @dev Helper for gas usage test
function _getSupplyGasUsage(uint256 amount, uint256 maxGas) internal returns (uint256 gasUsed) {
// 2 * NMAX borrowers borrow amount
for (uint256 i; i < 30; i++) {
borrowers[i].approve(usdc, type(uint256).max);
borrowers[i].supply(aUsdc, to6Decimals(amount * 3));
borrowers[i].borrow(aDai, amount);
}

supplier1.approve(dai, amount * 20);

uint256 gasLeftBefore = gasleft();
supplier1.supply(aDai, amount * 20, maxGas);

gasUsed = gasLeftBefore - gasleft();
}
}
12 changes: 4 additions & 8 deletions test-foundry/aave-v2/helpers/User.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ contract User {
IAaveIncentivesController public aaveIncentivesController;

constructor(Morpho _morpho) {
setMorphoAddresses(_morpho);
morpho = _morpho;
rewardsManager = _morpho.rewardsManager();
pool = _morpho.pool();
aaveIncentivesController = _morpho.aaveIncentivesController();
}

receive() external payable {}
Expand Down Expand Up @@ -179,11 +182,4 @@ contract User {
function setIsLiquidateBorrowPaused(address _poolToken, bool _isPaused) external {
morpho.setIsLiquidateBorrowPaused(_poolToken, _isPaused);
}

function setMorphoAddresses(Morpho _morpho) public {
morpho = _morpho;
rewardsManager = _morpho.rewardsManager();
pool = _morpho.pool();
aaveIncentivesController = _morpho.aaveIncentivesController();
}
}
11 changes: 6 additions & 5 deletions test-foundry/aave-v3/TestBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -256,32 +256,33 @@ contract TestBorrow is TestSetup {
borrower1.borrow(aUsdc, to6Decimals(amount));
}

function testShouldUseRightAmountOfGas() public {
function testShouldMatchBorrowWithCorrectAmountOfGas() public {
uint256 amount = 100 ether;
createSigners(30);

uint256 snapshotId = vm.snapshot();
uint256 gasUsed1 = _getBorrowGasUsage(amount, 1e5);

vm.revertTo(snapshotId);
uint256 gasUsed2 = _getBorrowGasUsage(amount, 2e5);

assertGt(gasUsed2, gasUsed1 + 1e4);
}

/// @dev Helper for gas usage test
function _getBorrowGasUsage(uint256 amount, uint256 maxGas) internal returns (uint256 gasUsed) {
// 2 * NMAX suppliers supply suppliedAmount
for (uint256 i; i < 30; i++) {
suppliers[i].setMorphoAddresses(morpho);
suppliers[i].approve(dai, type(uint256).max);
suppliers[i].supply(aDai, amount);
}

borrower1.setMorphoAddresses(morpho);
borrower1.approve(usdc, to6Decimals(amount * 200));
borrower1.supply(aUsdc, to6Decimals(amount * 200));

uint256 gasLeftBefore = gasleft();
borrower1.borrow(aDai, amount * 20, maxGas);
uint256 gasLeftAfter = gasleft();
gasUsed = gasLeftBefore - gasLeftAfter;

gasUsed = gasLeftBefore - gasleft();
}
}
30 changes: 30 additions & 0 deletions test-foundry/aave-v3/TestSupply.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,34 @@ contract TestSupply is TestSetup {
vm.warp(block.timestamp + 1);
supplier1.supply(aDai, amount);
}

function testShouldMatchSupplyWithCorrectAmountOfGas() public {
uint256 amount = 100 ether;
createSigners(30);

uint256 snapshotId = vm.snapshot();
uint256 gasUsed1 = _getSupplyGasUsage(amount, 1e5);

vm.revertTo(snapshotId);
uint256 gasUsed2 = _getSupplyGasUsage(amount, 2e5);

assertGt(gasUsed2, gasUsed1 + 1e4);
}

/// @dev Helper for gas usage test
function _getSupplyGasUsage(uint256 amount, uint256 maxGas) internal returns (uint256 gasUsed) {
// 2 * NMAX borrowers borrow amount
for (uint256 i; i < 30; i++) {
borrowers[i].approve(usdc, type(uint256).max);
borrowers[i].supply(aUsdc, to6Decimals(amount * 3));
borrowers[i].borrow(aDai, amount);
}

supplier1.approve(dai, amount * 20);

uint256 gasLeftBefore = gasleft();
supplier1.supply(aDai, amount * 20, maxGas);

gasUsed = gasLeftBefore - gasleft();
}
}
12 changes: 4 additions & 8 deletions test-foundry/aave-v3/helpers/User.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ contract User is Test {
uint256 tm = 1;

constructor(Morpho _morpho) {
setMorphoAddresses(_morpho);
morpho = _morpho;
rewardsManager = _morpho.rewardsManager();
pool = _morpho.pool();
rewardsController = _morpho.rewardsController();
}

receive() external payable {}
Expand Down Expand Up @@ -162,11 +165,4 @@ contract User is Test {
function setIsLiquidateBorrowPaused(address _poolToken, bool _isPaused) external {
morpho.setIsLiquidateBorrowPaused(_poolToken, _isPaused);
}

function setMorphoAddresses(Morpho _morpho) public {
morpho = _morpho;
rewardsManager = _morpho.rewardsManager();
pool = _morpho.pool();
rewardsController = _morpho.rewardsController();
}
}
18 changes: 10 additions & 8 deletions test-foundry/compound/TestBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ contract TestBorrow is TestSetup {
morpho.p2pBorrowIndex(cDai)
);
uint256 expectedBorrowOnPool = (borrowAmount -
getBalanceOnCompound(amount, cDaiSupplyIndex)).div(ICToken(cDai).borrowIndex());
getBalanceOnCompound(amount, cDaiSupplyIndex))
.div(ICToken(cDai).borrowIndex());

testEquality(inP2P, expectedBorrowInP2P, "Borrower1 in peer-to-peer");
testEquality(onPool, expectedBorrowOnPool, "Borrower1 on pool");
Expand Down Expand Up @@ -283,32 +284,33 @@ contract TestBorrow is TestSetup {
assertEq(p2pSupplyDeltaAfter, 0);
}

function testShouldUseRightAmountOfGas() public {
function testShouldMatchBorrowWithCorrectAmountOfGas() public {
uint256 amount = 100 ether;
createSigners(30);

uint256 snapshotId = vm.snapshot();
uint256 gasUsed1 = _getBorrowGasUsage(amount, 1e5);

vm.revertTo(snapshotId);
uint256 gasUsed2 = _getBorrowGasUsage(amount, 2e5);
assertGt(gasUsed2, gasUsed1 + 1e4);

assertGt(gasUsed2, gasUsed1 + 5e4);
}

/// @dev Helper for gas usage test
function _getBorrowGasUsage(uint256 amount, uint256 maxGas) internal returns (uint256 gasUsed) {
// 2 * NMAX suppliers supply suppliedAmount
// 2 * NMAX suppliers supply amount
for (uint256 i; i < 30; i++) {
suppliers[i].setMorphoAddresses(morpho);
suppliers[i].approve(dai, type(uint256).max);
suppliers[i].supply(cDai, amount);
}

borrower1.setMorphoAddresses(morpho);
borrower1.approve(usdc, to6Decimals(amount * 200));
borrower1.supply(cUsdc, to6Decimals(amount * 200));

uint256 gasLeftBefore = gasleft();
borrower1.borrow(cDai, amount * 20, maxGas);
uint256 gasLeftAfter = gasleft();
gasUsed = gasLeftBefore - gasLeftAfter;

gasUsed = gasLeftBefore - gasleft();
}
}
30 changes: 30 additions & 0 deletions test-foundry/compound/TestSupply.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,34 @@ contract TestSupply is TestSetup {
assertEq(usdcPoolSupplyIndexAfter, usdcPoolSupplyIndexBefore);
assertEq(usdcPoolBorrowIndexAfter, usdcPoolBorrowIndexBefore);
}

function testShouldMatchSupplyWithCorrectAmountOfGas() public {
uint256 amount = 100 ether;
createSigners(30);

uint256 snapshotId = vm.snapshot();
uint256 gasUsed1 = _getSupplyGasUsage(amount, 1e5);

vm.revertTo(snapshotId);
uint256 gasUsed2 = _getSupplyGasUsage(amount, 2e5);

assertGt(gasUsed2, gasUsed1 + 5e4);
}

/// @dev Helper for gas usage test
function _getSupplyGasUsage(uint256 amount, uint256 maxGas) internal returns (uint256 gasUsed) {
// 2 * NMAX borrowers borrow amount
for (uint256 i; i < 30; i++) {
borrowers[i].approve(usdc, type(uint256).max);
borrowers[i].supply(cUsdc, to6Decimals(amount * 3));
borrowers[i].borrow(cDai, amount);
}

supplier1.approve(dai, amount * 20);

uint256 gasLeftBefore = gasleft();
supplier1.supply(cDai, amount * 20, maxGas);

gasUsed = gasLeftBefore - gasleft();
}
}
12 changes: 4 additions & 8 deletions test-foundry/compound/helpers/User.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ contract User {
IComptroller internal comptroller;

constructor(Morpho _morpho) {
setMorphoAddresses(_morpho);
morpho = _morpho;
interestRatesManager = InterestRatesManager(address(_morpho.interestRatesManager()));
rewardsManager = _morpho.rewardsManager();
comptroller = morpho.comptroller();
}

receive() external payable {}
Expand Down Expand Up @@ -196,11 +199,4 @@ contract User {
function setIsLiquidateBorrowPaused(address _poolToken, bool _isPaused) external {
morpho.setIsLiquidateBorrowPaused(_poolToken, _isPaused);
}

function setMorphoAddresses(Morpho _morpho) public {
morpho = _morpho;
interestRatesManager = InterestRatesManager(address(_morpho.interestRatesManager()));
rewardsManager = _morpho.rewardsManager();
comptroller = morpho.comptroller();
}
}

0 comments on commit 8468142

Please sign in to comment.