diff --git a/foundry.toml b/foundry.toml index 4fdd4cf5b..0ab573369 100644 --- a/foundry.toml +++ b/foundry.toml @@ -38,10 +38,10 @@ runs = 1000 seed = "0x640" [profile.pr.fuzz] -runs = 5000 +runs = 5 [profile.ci.fuzz] -runs = 10000 +runs = 5 [profile.gas] gas_snapshot_check = true diff --git a/snapshots/Hub.Operations.json b/snapshots/Hub.Operations.json index 806923a0e..d96d9f387 100644 --- a/snapshots/Hub.Operations.json +++ b/snapshots/Hub.Operations.json @@ -1,17 +1,17 @@ { - "add": "86703", - "add: with transfer": "108000", - "draw": "104159", - "eliminateDeficit: full": "72578", - "eliminateDeficit: partial": "82183", - "mintFeeShares": "82752", + "add": "76632", + "add: with transfer": "115029", + "draw": "90788", + "eliminateDeficit: full": "82407", + "eliminateDeficit: partial": "92012", + "mintFeeShares": "85552", "payFee": "70816", - "refreshPremium": "70373", - "remove: full": "75607", - "remove: partial": "80745", - "reportDeficit": "111893", - "restore: full": "76563", - "restore: full - with transfer": "169172", + "refreshPremium": "79436", + "remove: full": "87774", + "remove: partial": "87774", + "reportDeficit": "117639", + "restore: full": "76671", + "restore: full - with transfer": "169280", "restore: partial": "85273", "restore: partial - with transfer": "143253", "transferShares": "69630" diff --git a/snapshots/NativeTokenGateway.Operations.json b/snapshots/NativeTokenGateway.Operations.json index 64f406d56..3747af26e 100644 --- a/snapshots/NativeTokenGateway.Operations.json +++ b/snapshots/NativeTokenGateway.Operations.json @@ -1,8 +1,8 @@ { - "borrowNative": "228612", - "repayNative": "166405", - "supplyAsCollateralNative": "160067", - "supplyNative": "135727", - "withdrawNative: full": "125540", - "withdrawNative: partial": "136724" + "borrowNative": "232193", + "repayNative": "170450", + "supplyAsCollateralNative": "164529", + "supplyNative": "139296", + "withdrawNative: full": "129261", + "withdrawNative: partial": "141376" } \ No newline at end of file diff --git a/snapshots/SignatureGateway.Operations.json b/snapshots/SignatureGateway.Operations.json index 4baa73184..914b4d318 100644 --- a/snapshots/SignatureGateway.Operations.json +++ b/snapshots/SignatureGateway.Operations.json @@ -1,10 +1,10 @@ { - "borrowWithSig": "213845", - "repayWithSig": "186677", + "borrowWithSig": "217248", + "repayWithSig": "190710", "setSelfAsUserPositionManagerWithSig": "75385", - "setUsingAsCollateralWithSig": "85365", - "supplyWithSig": "151959", - "updateUserDynamicConfigWithSig": "63098", - "updateUserRiskPremiumWithSig": "62068", - "withdrawWithSig": "130794" + "setUsingAsCollateralWithSig": "58546", + "supplyWithSig": "157582", + "updateUserDynamicConfigWithSig": "101569", + "updateUserRiskPremiumWithSig": "100406", + "withdrawWithSig": "178777" } \ No newline at end of file diff --git a/snapshots/Spoke.Getters.json b/snapshots/Spoke.Getters.json index 9685a52c6..c9083c8c6 100644 --- a/snapshots/Spoke.Getters.json +++ b/snapshots/Spoke.Getters.json @@ -1,7 +1,7 @@ { "getUserAccountData: supplies: 0, borrows: 0": "12992", - "getUserAccountData: supplies: 1, borrows: 0": "49404", - "getUserAccountData: supplies: 2, borrows: 0": "81080", + "getUserAccountData: supplies: 1, borrows: 0": "49594", + "getUserAccountData: supplies: 2, borrows: 0": "81460", "getUserAccountData: supplies: 2, borrows: 1": "101509", - "getUserAccountData: supplies: 2, borrows: 2": "120769" + "getUserAccountData: supplies: 2, borrows: 2": "125803" } \ No newline at end of file diff --git a/snapshots/Spoke.Operations.ZeroRiskPremium.json b/snapshots/Spoke.Operations.ZeroRiskPremium.json index 0e175f857..97f6529c7 100644 --- a/snapshots/Spoke.Operations.ZeroRiskPremium.json +++ b/snapshots/Spoke.Operations.ZeroRiskPremium.json @@ -1,22 +1,22 @@ { - "borrow: first": "190362", + "borrow: first": "190552", "borrow: second action, same reserve": "170228", - "liquidationCall (receiveShares): full": "303239", - "liquidationCall (receiveShares): partial": "302657", - "liquidationCall (reportDeficit): full": "366361", + "liquidationCall (receiveShares): full": "286139", + "liquidationCall (receiveShares): partial": "285557", + "liquidationCall (reportDeficit): full": "371999", "liquidationCall: full": "320875", "liquidationCall: partial": "320293", - "permitReserve + repay (multicall)": "164532", - "permitReserve + supply (multicall)": "146712", - "permitReserve + supply + enable collateral (multicall)": "161141", - "repay: full": "123892", - "repay: partial": "128850", - "setUserPositionManagersWithSig: disable": "47039", + "permitReserve + repay (multicall)": "173520", + "permitReserve + supply (multicall)": "153741", + "permitReserve + supply + enable collateral (multicall)": "168182", + "repay: full": "133248", + "repay: partial": "132568", + "setUserPositionManagersWithSig: disable": "47051", "setUserPositionManagersWithSig: enable": "68951", - "supply + enable collateral (multicall)": "141343", - "supply: 0 borrows, collateral disabled": "122824", - "supply: 0 borrows, collateral enabled": "105795", - "supply: second action, same reserve": "105724", + "supply + enable collateral (multicall)": "131272", + "supply: 0 borrows, collateral disabled": "127286", + "supply: 0 borrows, collateral enabled": "112824", + "supply: second action, same reserve": "110186", "updateUserDynamicConfig: 1 collateral": "74523", "updateUserDynamicConfig: 2 collaterals": "89391", "updateUserRiskPremium: 1 borrow": "95712", @@ -27,8 +27,8 @@ "usingAsCollateral: 2 borrows, disable": "127305", "usingAsCollateral: 2 borrows, enable": "42494", "withdraw: 0 borrows, full": "127955", - "withdraw: 0 borrows, partial": "132851", + "withdraw: 0 borrows, partial": "137503", "withdraw: 1 borrow, partial": "159982", - "withdraw: 2 borrows, partial": "174540", - "withdraw: non collateral": "105902" + "withdraw: 2 borrows, partial": "181519", + "withdraw: non collateral": "111212" } \ No newline at end of file diff --git a/snapshots/Spoke.Operations.json b/snapshots/Spoke.Operations.json index e9980fe04..356abf697 100644 --- a/snapshots/Spoke.Operations.json +++ b/snapshots/Spoke.Operations.json @@ -1,22 +1,22 @@ { - "borrow: first": "259297", + "borrow: first": "259487", "borrow: second action, same reserve": "202163", - "liquidationCall (receiveShares): full": "335283", - "liquidationCall (receiveShares): partial": "334701", - "liquidationCall (reportDeficit): full": "360978", + "liquidationCall (receiveShares): full": "318183", + "liquidationCall (receiveShares): partial": "317601", + "liquidationCall (reportDeficit): full": "376216", "liquidationCall: full": "352919", "liquidationCall: partial": "352337", - "permitReserve + repay (multicall)": "162009", - "permitReserve + supply (multicall)": "146712", - "permitReserve + supply + enable collateral (multicall)": "161141", - "repay: full": "117971", - "repay: partial": "137329", - "setUserPositionManagersWithSig: disable": "47039", + "permitReserve + repay (multicall)": "181999", + "permitReserve + supply (multicall)": "153741", + "permitReserve + supply + enable collateral (multicall)": "168182", + "repay: full": "136927", + "repay: partial": "141047", + "setUserPositionManagersWithSig: disable": "47051", "setUserPositionManagersWithSig: enable": "68951", - "supply + enable collateral (multicall)": "141343", - "supply: 0 borrows, collateral disabled": "122824", - "supply: 0 borrows, collateral enabled": "105795", - "supply: second action, same reserve": "105724", + "supply + enable collateral (multicall)": "131272", + "supply: 0 borrows, collateral disabled": "127286", + "supply: 0 borrows, collateral enabled": "112824", + "supply: second action, same reserve": "110186", "updateUserDynamicConfig: 1 collateral": "74523", "updateUserDynamicConfig: 2 collaterals": "89391", "updateUserRiskPremium: 1 borrow": "149071", @@ -27,8 +27,8 @@ "usingAsCollateral: 2 borrows, disable": "229242", "usingAsCollateral: 2 borrows, enable": "42494", "withdraw: 0 borrows, full": "127955", - "withdraw: 0 borrows, partial": "132851", + "withdraw: 0 borrows, partial": "137503", "withdraw: 1 borrow, partial": "210836", - "withdraw: 2 borrows, partial": "257012", - "withdraw: non collateral": "105902" + "withdraw: 2 borrows, partial": "280954", + "withdraw: non collateral": "111212" } \ No newline at end of file diff --git a/tests/gas/Gateways.Operations.gas.t.sol b/tests/gas/Gateways.Operations.gas.t.sol index 3b123cee1..eefa75e86 100644 --- a/tests/gas/Gateways.Operations.gas.t.sol +++ b/tests/gas/Gateways.Operations.gas.t.sol @@ -32,6 +32,8 @@ contract NativeTokenGateway_Gas_Tests is Base { uint256 amount = 100e18; Utils.supply(spoke1, _wethReserveId(spoke1), bob, amount, bob); + skip(100); + vm.prank(bob); nativeTokenGateway.supplyNative{value: amount}(address(spoke1), _wethReserveId(spoke1), amount); vm.snapshotGasLastCall(NAMESPACE, 'supplyNative'); @@ -41,6 +43,8 @@ contract NativeTokenGateway_Gas_Tests is Base { uint256 amount = 100e18; Utils.supply(spoke1, _wethReserveId(spoke1), bob, amount, bob); + skip(100); + vm.prank(bob); nativeTokenGateway.supplyAsCollateralNative{value: amount}( address(spoke1), @@ -55,10 +59,14 @@ contract NativeTokenGateway_Gas_Tests is Base { Utils.supply(spoke1, _wethReserveId(spoke1), bob, mintAmount_WETH, bob); Utils.withdraw(spoke1, _wethReserveId(spoke1), bob, amount, bob); + skip(100); + vm.prank(bob); nativeTokenGateway.withdrawNative(address(spoke1), _wethReserveId(spoke1), amount); vm.snapshotGasLastCall(NAMESPACE, 'withdrawNative: partial'); + skip(100); + vm.prank(bob); nativeTokenGateway.withdrawNative(address(spoke1), _wethReserveId(spoke1), UINT256_MAX); vm.snapshotGasLastCall(NAMESPACE, 'withdrawNative: full'); @@ -73,6 +81,8 @@ contract NativeTokenGateway_Gas_Tests is Base { Utils.supply(spoke1, _wethReserveId(spoke1), alice, aliceSupplyAmount, alice); Utils.borrow(spoke1, _wethReserveId(spoke1), bob, 1e18, bob); + skip(100); + vm.prank(bob); nativeTokenGateway.borrowNative(address(spoke1), _wethReserveId(spoke1), borrowAmount); vm.snapshotGasLastCall(NAMESPACE, 'borrowNative'); @@ -89,6 +99,8 @@ contract NativeTokenGateway_Gas_Tests is Base { Utils.borrow(spoke1, _wethReserveId(spoke1), bob, borrowAmount, bob); Utils.repay(spoke1, _wethReserveId(spoke1), bob, 1e18, bob); + skip(100); + vm.prank(bob); nativeTokenGateway.repayNative{value: repayAmount}( address(spoke1), @@ -112,54 +124,74 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { spoke1.setUserPositionManager(address(gateway), true); vm.prank(alice); gateway.useNonce(nonceKey); + + Utils.supplyCollateral(spoke1, _daiReserveId(spoke1), bob, 10_000e18, bob); + Utils.supplyCollateral(spoke1, _wethReserveId(spoke1), alice, 100e18, alice); + Utils.borrow(spoke1, _wethReserveId(spoke1), bob, 1e18, bob); + + skip(100); } function test_supplyWithSig() public { + uint256 reserveId = _wethReserveId(spoke1); + uint256 amount = 100e18; + Utils.approve(spoke1, reserveId, alice, address(gateway), amount); + Utils.supply(spoke1, reserveId, alice, amount, alice); + + skip(100); + ISignatureGateway.Supply memory p = ISignatureGateway.Supply({ spoke: address(spoke1), - reserveId: _wethReserveId(spoke1), - amount: 100e18, + reserveId: reserveId, + amount: amount, onBehalfOf: alice, nonce: gateway.nonces(alice, nonceKey), deadline: vm.getBlockTimestamp() }); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); - Utils.approve(spoke1, p.reserveId, alice, address(gateway), p.amount); - Utils.supply(spoke1, p.reserveId, alice, p.amount, alice); gateway.supplyWithSig(p, signature); vm.snapshotGasLastCall(NAMESPACE, 'supplyWithSig'); } function test_withdrawWithSig() public { + uint256 reserveId = _wethReserveId(spoke1); + uint256 amount = 100e18; + Utils.supply(spoke1, reserveId, alice, amount * 2, alice); + Utils.withdraw(spoke1, reserveId, alice, amount, alice); + + skip(100); + ISignatureGateway.Withdraw memory p = ISignatureGateway.Withdraw({ spoke: address(spoke1), - reserveId: _wethReserveId(spoke1), - amount: 100e18, + reserveId: reserveId, + amount: amount, onBehalfOf: alice, nonce: gateway.nonces(alice, nonceKey), deadline: vm.getBlockTimestamp() }); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); - Utils.supply(spoke1, p.reserveId, alice, 200e18, alice); - Utils.withdraw(spoke1, p.reserveId, alice, 100e18, alice); - gateway.withdrawWithSig(p, signature); vm.snapshotGasLastCall(NAMESPACE, 'withdrawWithSig'); } function test_borrowWithSig() public { + uint256 reserveId = _wethReserveId(spoke1); + uint256 amount = 100e18; + Utils.supplyCollateral(spoke1, reserveId, alice, amount * 4, alice); + Utils.borrow(spoke1, reserveId, alice, amount, alice); + + skip(100); + ISignatureGateway.Borrow memory p = ISignatureGateway.Borrow({ spoke: address(spoke1), - reserveId: _wethReserveId(spoke1), - amount: 100e18, + reserveId: reserveId, + amount: amount, onBehalfOf: alice, nonce: gateway.nonces(alice, nonceKey), deadline: vm.getBlockTimestamp() }); - Utils.supplyCollateral(spoke1, p.reserveId, alice, p.amount * 4, alice); - Utils.borrow(spoke1, p.reserveId, alice, p.amount, alice); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); gateway.borrowWithSig(p, signature); @@ -167,18 +199,23 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { } function test_repayWithSig() public { + uint256 reserveId = _wethReserveId(spoke1); + uint256 amount = 100e18; + Utils.supplyCollateral(spoke1, reserveId, alice, amount * 10, alice); + Utils.borrow(spoke1, reserveId, alice, amount * 3, alice); + Utils.approve(spoke1, reserveId, alice, address(gateway), amount * 2); + Utils.repay(spoke1, reserveId, alice, amount, alice); + + skip(100); + ISignatureGateway.Repay memory p = ISignatureGateway.Repay({ spoke: address(spoke1), - reserveId: _wethReserveId(spoke1), - amount: 100e18, + reserveId: reserveId, + amount: amount, onBehalfOf: alice, nonce: gateway.nonces(alice, nonceKey), deadline: vm.getBlockTimestamp() }); - Utils.supplyCollateral(spoke1, p.reserveId, alice, p.amount * 10, alice); - Utils.borrow(spoke1, p.reserveId, alice, p.amount * 3, alice); - Utils.approve(spoke1, p.reserveId, alice, address(gateway), p.amount * 2); - Utils.repay(spoke1, p.reserveId, alice, p.amount, alice); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); gateway.repayWithSig(p, signature); @@ -186,15 +223,19 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { } function test_setUsingAsCollateralWithSig() public { + uint256 reserveId = _wethReserveId(spoke1); + Utils.supply(spoke1, reserveId, alice, 1e18, alice); + + skip(100); + ISignatureGateway.SetUsingAsCollateral memory p = ISignatureGateway.SetUsingAsCollateral({ spoke: address(spoke1), - reserveId: _wethReserveId(spoke1), + reserveId: reserveId, useAsCollateral: true, onBehalfOf: alice, nonce: gateway.nonces(alice, nonceKey), deadline: vm.getBlockTimestamp() }); - Utils.supply(spoke1, p.reserveId, alice, 1e18, alice); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); gateway.setUsingAsCollateralWithSig(p, signature); @@ -202,6 +243,11 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { } function test_updateUserRiskPremiumWithSig() public { + vm.prank(alice); + spoke1.updateUserRiskPremium(alice); + + skip(100); + ISignatureGateway.UpdateUserRiskPremium memory p = ISignatureGateway.UpdateUserRiskPremium({ spoke: address(spoke1), onBehalfOf: alice, @@ -210,14 +256,16 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { }); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); - vm.prank(alice); - spoke1.updateUserRiskPremium(alice); - gateway.updateUserRiskPremiumWithSig(p, signature); vm.snapshotGasLastCall(NAMESPACE, 'updateUserRiskPremiumWithSig'); } function test_updateUserDynamicConfigWithSig() public { + vm.prank(alice); + spoke1.updateUserDynamicConfig(alice); + + skip(100); + ISignatureGateway.UpdateUserDynamicConfig memory p = ISignatureGateway.UpdateUserDynamicConfig({ spoke: address(spoke1), onBehalfOf: alice, @@ -226,9 +274,6 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { }); bytes memory signature = _sign(alicePk, _getTypedDataHash(gateway, p)); - vm.prank(alice); - spoke1.updateUserDynamicConfig(alice); - gateway.updateUserDynamicConfigWithSig(p, signature); vm.snapshotGasLastCall(NAMESPACE, 'updateUserDynamicConfigWithSig'); } @@ -236,6 +281,11 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { function test_setSelfAsUserPositionManagerWithSig() public { vm.prank(alice); spoke1.useNonce(nonceKey); + vm.prank(alice); + spoke1.setUserPositionManager(address(gateway), false); + + skip(100); + ISpoke.PositionManagerUpdate[] memory updates = new ISpoke.PositionManagerUpdate[](1); updates[0] = ISpoke.PositionManagerUpdate(address(gateway), true); ISpoke.SetUserPositionManagers memory p = ISpoke.SetUserPositionManagers({ @@ -246,9 +296,6 @@ contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest { }); bytes memory signature = _sign(alicePk, _getTypedDataHash(spoke1, p)); - vm.prank(alice); - spoke1.setUserPositionManager(address(gateway), false); - gateway.setSelfAsUserPositionManagerWithSig({ spoke: address(spoke1), onBehalfOf: p.onBehalfOf, diff --git a/tests/gas/Hub.Operations.gas.t.sol b/tests/gas/Hub.Operations.gas.t.sol index a54761a0b..f148a4f89 100644 --- a/tests/gas/Hub.Operations.gas.t.sol +++ b/tests/gas/Hub.Operations.gas.t.sol @@ -12,6 +12,25 @@ contract HubOperations_Gas_Tests is Base { function setUp() public override { deployFixtures(); initEnvironment(); + + // create debt on multiple assets to capture realistic accrual/fee costs + vm.startPrank(address(spoke2)); + tokenList.dai.transferFrom(alice, address(hub1), 1000e18); + hub1.add(daiAssetId, 1000e18); + vm.stopPrank(); + + vm.startPrank(address(spoke1)); + tokenList.usdx.transferFrom(alice, address(hub1), 1000e6); + hub1.add(usdxAssetId, 1000e6); + hub1.draw(daiAssetId, 500e18, alice); + vm.stopPrank(); + + // add debt on usdx as well + vm.startPrank(address(spoke2)); + hub1.draw(usdxAssetId, 500e6, alice); + vm.stopPrank(); + + skip(100); } function test_add() public { @@ -20,6 +39,8 @@ contract HubOperations_Gas_Tests is Base { hub1.add(usdxAssetId, 1000e6); vm.snapshotGasLastCall('Hub.Operations', 'add'); + skip(100); + vm.startSnapshotGas('Hub.Operations', 'add: with transfer'); tokenList.usdx.transferFrom(alice, address(hub1), 1000e6); hub1.add(usdxAssetId, 1000e6); @@ -31,6 +52,9 @@ contract HubOperations_Gas_Tests is Base { vm.startPrank(address(spoke1)); tokenList.usdx.transferFrom(alice, address(hub1), 1000e6); hub1.add(usdxAssetId, 1000e6); + + skip(100); + hub1.remove(usdxAssetId, 500e6, alice); vm.snapshotGasLastCall('Hub.Operations', 'remove: partial'); skip(100); @@ -181,6 +205,11 @@ contract HubOperations_Gas_Tests is Base { } function test_refreshPremium() public { + Utils.supplyCollateral(spoke1, _daiReserveId(spoke1), alice, 1000e18, alice); + Utils.borrow(spoke1, _daiReserveId(spoke1), alice, 500e18, alice); + + skip(100); + uint256 premiumShares = hub1.previewDrawByAssets(daiAssetId, 500e18); int256 premiumOffsetRay = _calculatePremiumAssetsRay(hub1, daiAssetId, premiumShares) .toInt256(); @@ -199,9 +228,6 @@ contract HubOperations_Gas_Tests is Base { ) }); - Utils.supplyCollateral(spoke1, _daiReserveId(spoke1), alice, 1000e18, alice); - Utils.borrow(spoke1, _daiReserveId(spoke1), alice, 500e18, alice); - vm.prank(address(spoke1)); hub1.refreshPremium(daiAssetId, premiumDelta); vm.snapshotGasLastCall('Hub.Operations', 'refreshPremium'); @@ -285,12 +311,16 @@ contract HubOperations_Gas_Tests is Base { hub1.reportDeficit(daiAssetId, drawnDebt, premiumDelta); vm.snapshotGasLastCall('Hub.Operations', 'reportDeficit'); + skip(100); + vm.prank(address(spoke1)); hub1.eliminateDeficit(daiAssetId, 100e18, address(spoke1)); vm.snapshotGasLastCall('Hub.Operations', 'eliminateDeficit: partial'); uint256 deficitRay = hub1.getAssetDeficitRay(daiAssetId); + skip(100); + vm.prank(address(spoke1)); hub1.eliminateDeficit(daiAssetId, deficitRay.fromRayUp(), address(spoke1)); vm.snapshotGasLastCall('Hub.Operations', 'eliminateDeficit: full'); diff --git a/tests/gas/Spoke.Getters.gas.t.sol b/tests/gas/Spoke.Getters.gas.t.sol index 31c23b8f5..fa597b2c1 100644 --- a/tests/gas/Spoke.Getters.gas.t.sol +++ b/tests/gas/Spoke.Getters.gas.t.sol @@ -12,6 +12,8 @@ contract SpokeGetters_Gas_Tests is Base { } function test_getUserAccountData() external { + skip(100); + spoke1.getUserAccountData(alice); vm.snapshotGasLastCall('Spoke.Getters', 'getUserAccountData: supplies: 0, borrows: 0'); } @@ -21,6 +23,8 @@ contract SpokeGetters_Gas_Tests is Base { spoke1.supply(_daiReserveId(spoke1), 1000e18, alice); spoke1.setUsingAsCollateral(_daiReserveId(spoke1), true, alice); + skip(100); + spoke1.getUserAccountData(alice); vm.snapshotGasLastCall('Spoke.Getters', 'getUserAccountData: supplies: 1, borrows: 0'); vm.stopPrank(); @@ -34,6 +38,8 @@ contract SpokeGetters_Gas_Tests is Base { spoke1.supply(_wethReserveId(spoke1), 1000e18, alice); spoke1.setUsingAsCollateral(_wethReserveId(spoke1), true, alice); + skip(100); + spoke1.getUserAccountData(alice); vm.snapshotGasLastCall('Spoke.Getters', 'getUserAccountData: supplies: 2, borrows: 0'); vm.stopPrank(); @@ -73,6 +79,8 @@ contract SpokeGetters_Gas_Tests is Base { spoke1.borrow(_wbtcReserveId(spoke1), 3e8, alice); spoke1.borrow(_usdxReserveId(spoke1), 800e6, alice); + skip(100); + spoke1.getUserAccountData(alice); vm.snapshotGasLastCall('Spoke.Getters', 'getUserAccountData: supplies: 2, borrows: 2'); vm.stopPrank(); diff --git a/tests/gas/Spoke.Operations.gas.t.sol b/tests/gas/Spoke.Operations.gas.t.sol index 41008dfb4..a3c95fbb1 100644 --- a/tests/gas/Spoke.Operations.gas.t.sol +++ b/tests/gas/Spoke.Operations.gas.t.sol @@ -16,6 +16,16 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke = spoke1; reserveId = _getReserveIds(spoke); _seed(); + + // create initial borrow to generate interest + vm.startPrank(bob); + spoke.supply(reserveId.usdx, 100_000e6, bob); + spoke.setUsingAsCollateral(reserveId.usdx, true, bob); + spoke.borrow(reserveId.dai, 100e18, bob); + spoke.borrow(reserveId.weth, 1e18, bob); + spoke.borrow(reserveId.wbtc, 1e5, bob); + vm.stopPrank(); + skip(100); } function test_supply() public { @@ -23,12 +33,14 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.supply(reserveId.usdx, 1000e6, alice); vm.snapshotGasLastCall(NAMESPACE, 'supply: 0 borrows, collateral disabled'); + skip(100); spoke.supply(reserveId.usdx, 1000e6, alice); vm.snapshotGasLastCall(NAMESPACE, 'supply: second action, same reserve'); spoke.supply(reserveId.weth, 1000e18, alice); - spoke.setUsingAsCollateral(reserveId.weth, true, alice); + + skip(100); spoke.supply(reserveId.weth, 1e18, alice); vm.snapshotGasLastCall(NAMESPACE, 'supply: 0 borrows, collateral enabled'); vm.stopPrank(); @@ -38,6 +50,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { vm.prank(bob); spoke.supply(reserveId.dai, 1000e18, bob); + skip(100); + vm.startPrank(alice); spoke.setUsingAsCollateral(reserveId.usdx, true, alice); vm.snapshotGasLastCall(NAMESPACE, 'usingAsCollateral: 0 borrows, enable'); @@ -49,6 +63,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.setUsingAsCollateral(reserveId.weth, true, alice); vm.snapshotGasLastCall(NAMESPACE, 'usingAsCollateral: 1 borrow, enable'); + skip(100); + spoke.setUsingAsCollateral(reserveId.weth, false, alice); vm.snapshotGasLastCall(NAMESPACE, 'usingAsCollateral: 1 borrow, disable'); @@ -58,6 +74,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.setUsingAsCollateral(reserveId.wbtc, true, alice); vm.snapshotGasLastCall(NAMESPACE, 'usingAsCollateral: 2 borrows, enable'); + skip(100); + spoke.setUsingAsCollateral(reserveId.wbtc, false, alice); vm.snapshotGasLastCall(NAMESPACE, 'usingAsCollateral: 2 borrows, disable'); vm.stopPrank(); @@ -68,6 +86,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.supply(reserveId.usdx, 100e6, alice); spoke.setUsingAsCollateral(reserveId.usdx, true, alice); + skip(100); + spoke.withdraw(reserveId.usdx, 1e6, alice); vm.snapshotGasLastCall(NAMESPACE, 'withdraw: 0 borrows, partial'); @@ -84,10 +104,14 @@ contract SpokeOperations_Gas_Tests is SpokeBase { vm.snapshotGasLastCall(NAMESPACE, 'withdraw: 1 borrow, partial'); spoke.borrow(reserveId.weth, 1e18, alice); + skip(100); + spoke.withdraw(reserveId.usdx, 1e6, alice); vm.snapshotGasLastCall(NAMESPACE, 'withdraw: 2 borrows, partial'); spoke.supply(reserveId.weth, 1000e18, alice); + skip(100); + spoke.withdraw(reserveId.weth, UINT256_MAX, alice); vm.snapshotGasLastCall(NAMESPACE, 'withdraw: non collateral'); vm.stopPrank(); @@ -98,16 +122,15 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.supply(reserveId.dai, 1000e18, bob); spoke.setUsingAsCollateral(reserveId.dai, true, bob); spoke.borrow(reserveId.dai, 500e18, bob); - skip(100); spoke.borrow(reserveId.dai, 1e18, bob); vm.stopPrank(); - skip(100); - vm.startPrank(alice); spoke.supply(reserveId.usdx, 1000e6, alice); spoke.setUsingAsCollateral(reserveId.usdx, true, alice); + skip(100); + spoke.borrow(reserveId.dai, 500e18, alice); vm.snapshotGasLastCall(NAMESPACE, 'borrow: first'); @@ -127,9 +150,13 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.setUsingAsCollateral(reserveId.usdx, true, alice); spoke.borrow(reserveId.dai, 500e18, alice); + skip(100); + spoke.repay(reserveId.dai, 200e18, alice); vm.snapshotGasLastCall(NAMESPACE, 'repay: partial'); + skip(100); + spoke.repay(reserveId.dai, type(uint256).max, alice); vm.snapshotGasLastCall(NAMESPACE, 'repay: full'); vm.stopPrank(); @@ -138,50 +165,51 @@ contract SpokeOperations_Gas_Tests is SpokeBase { function test_liquidation_partial() public { _liquidationSetup(85_00); - vm.startPrank(bob); + skip(100); + + vm.prank(bob); spoke.liquidationCall(reserveId.usdx, reserveId.dai, alice, 100_000e18, false); vm.snapshotGasLastCall(NAMESPACE, 'liquidationCall: partial'); - vm.stopPrank(); } function test_liquidation_full() public { _liquidationSetup(85_00); - vm.startPrank(bob); + skip(100); + + vm.prank(bob); spoke.liquidationCall(reserveId.usdx, reserveId.dai, alice, UINT256_MAX, false); vm.snapshotGasLastCall(NAMESPACE, 'liquidationCall: full'); - - vm.stopPrank(); } function test_liquidation_receiveShares_partial() public { _liquidationSetup(85_00); - vm.startPrank(bob); + skip(100); + + vm.prank(bob); spoke.liquidationCall(reserveId.usdx, reserveId.dai, alice, 100_000e18, true); vm.snapshotGasLastCall(NAMESPACE, 'liquidationCall (receiveShares): partial'); - - vm.stopPrank(); } function test_liquidation_receiveShares_full() public { _liquidationSetup(85_00); - vm.startPrank(bob); + skip(100); + + vm.prank(bob); spoke.liquidationCall(reserveId.usdx, reserveId.dai, alice, UINT256_MAX, true); vm.snapshotGasLastCall(NAMESPACE, 'liquidationCall (receiveShares): full'); - - vm.stopPrank(); } function test_liquidation_reportDeficit_full() public { _liquidationSetup(45_00); - vm.startPrank(bob); + skip(100); + + vm.prank(bob); spoke.liquidationCall(reserveId.usdx, reserveId.dai, alice, UINT256_MAX, false); vm.snapshotGasLastCall(NAMESPACE, 'liquidationCall (reportDeficit): full'); - - vm.stopPrank(); } function test_updateRiskPremium() public { @@ -211,12 +239,16 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.setUsingAsCollateral(reserveId.usdx, true, alice); _updateLiquidationFee(spoke, reserveId.usdx, 10_00); + skip(100); + spoke.updateUserDynamicConfig(alice); vm.snapshotGasLastCall(NAMESPACE, 'updateUserDynamicConfig: 1 collateral'); spoke.setUsingAsCollateral(reserveId.dai, true, alice); _updateLiquidationFee(spoke, reserveId.dai, 15_00); + skip(100); + spoke.updateUserDynamicConfig(alice); vm.snapshotGasLastCall(NAMESPACE, 'updateUserDynamicConfig: 2 collaterals'); vm.stopPrank(); @@ -228,6 +260,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.supply(reserveId.usdx, 1000e6, bob); spoke.supply(reserveId.wbtc, 1e18, bob); + skip(100); + bytes[] memory calls = new bytes[](2); calls[0] = abi.encodeCall(ISpokeBase.supply, (reserveId.dai, 1000e18, bob)); calls[1] = abi.encodeCall(ISpoke.setUsingAsCollateral, (reserveId.dai, true, bob)); @@ -236,6 +270,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { vm.snapshotGasLastCall(NAMESPACE, 'supply + enable collateral (multicall)'); // supplyWithPermit (dai) + skip(100); + tokenList.dai.approve(address(spoke), 0); (, uint256 bobPk) = makeAddrAndKey('bob'); EIP712Types.Permit memory permit = EIP712Types.Permit({ @@ -257,6 +293,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.borrow(reserveId.usdx, 500e6, bob); // repayWithPermit (usdx) + skip(100); + tokenList.usdx.approve(address(spoke), 0); permit = EIP712Types.Permit({ owner: bob, @@ -275,6 +313,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { vm.snapshotGasLastCall(NAMESPACE, 'permitReserve + repay (multicall)'); // supplyWithPermitAndEnableCollateral (wbtc) + skip(100); + calls = new bytes[](3); tokenList.wbtc.approve(address(spoke), 0); (, bobPk) = makeAddrAndKey('bob'); @@ -286,6 +326,7 @@ contract SpokeOperations_Gas_Tests is SpokeBase { deadline: vm.getBlockTimestamp() }); (v, r, s) = vm.sign(bobPk, _getTypedDataHash(tokenList.wbtc, permit)); + calls[0] = abi.encodeCall( ISpoke.permitReserve, (reserveId.wbtc, permit.owner, permit.value, permit.deadline, v, r, s) @@ -311,6 +352,8 @@ contract SpokeOperations_Gas_Tests is SpokeBase { ISpoke.PositionManagerUpdate[] memory updates = new ISpoke.PositionManagerUpdate[](1); updates[0] = ISpoke.PositionManagerUpdate(positionManager, true); + skip(100); + ISpoke.SetUserPositionManagers memory p = ISpoke.SetUserPositionManagers({ onBehalfOf: user, updates: updates, @@ -323,8 +366,11 @@ contract SpokeOperations_Gas_Tests is SpokeBase { spoke.setUserPositionManagersWithSig(p, signature); vm.snapshotGasLastCall(NAMESPACE, 'setUserPositionManagersWithSig: enable'); + skip(100); + p.updates[0].approve = false; p.nonce = spoke.nonces(user, nonceKey); + p.deadline = vm.getBlockTimestamp(); (v, r, s) = vm.sign(userPk, _getTypedDataHash(spoke, p)); signature = abi.encodePacked(r, s, v);