From e6fd92d1d0637571155e207fcae12600396098a8 Mon Sep 17 00:00:00 2001 From: kevincheng96 Date: Mon, 3 Jun 2024 13:40:14 -0700 Subject: [PATCH] Update gas overhead for Paycall and Quotecall --- src/quark-core-scripts/src/Paycall.sol | 3 ++- src/quark-core-scripts/src/Quotecall.sol | 3 ++- test/quark-core-scripts/Paycall.t.sol | 29 ++++++++++++------------ test/quark-core-scripts/Quotecall.t.sol | 6 ++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/quark-core-scripts/src/Paycall.sol b/src/quark-core-scripts/src/Paycall.sol index 0312acf8..aebd4ff0 100644 --- a/src/quark-core-scripts/src/Paycall.sol +++ b/src/quark-core-scripts/src/Paycall.sol @@ -33,7 +33,8 @@ contract Paycall { /// @notice Constant buffer for gas overhead /// This is a constant to account for the gas used by a Quark operation that is not tracked by the Paycall contract itself - uint256 internal constant GAS_OVERHEAD = 67_500; + /// Rough estimation: 30k for initial gas (21k + calldata gas) + 70k for Quark overhead + 35k for ERC20 transfer + uint256 internal constant GAS_OVERHEAD = 135_000; /// @dev The number of decimals for the chain's native token uint256 internal constant NATIVE_TOKEN_DECIMALS = 18; diff --git a/src/quark-core-scripts/src/Quotecall.sol b/src/quark-core-scripts/src/Quotecall.sol index f740ad69..b980b882 100644 --- a/src/quark-core-scripts/src/Quotecall.sol +++ b/src/quark-core-scripts/src/Quotecall.sol @@ -36,7 +36,8 @@ contract Quotecall { /// @notice Constant buffer for gas overhead /// This is a constant to account for the gas used by a Quark operation that is not tracked by the Quotecall contract itself - uint256 internal constant GAS_OVERHEAD = 67_500; + /// Rough estimation: 30k for initial gas (21k + calldata gas) + 70k for Quark overhead + uint256 internal constant GAS_OVERHEAD = 100_000; /// @dev The scale for percentages, used for `maxDeltaPercentage` (e.g. 1e18 = 100%) uint256 internal constant PERCENTAGE_SCALE = 1e18; diff --git a/test/quark-core-scripts/Paycall.t.sol b/test/quark-core-scripts/Paycall.t.sol index 066e5b6e..4cdf1831 100644 --- a/test/quark-core-scripts/Paycall.t.sol +++ b/test/quark-core-scripts/Paycall.t.sol @@ -165,7 +165,7 @@ contract PaycallTest is Test { Paycall.run.selector, multicallAddress, abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas), - 10e6 + 20e6 ), ScriptType.ScriptSource ); @@ -174,11 +174,11 @@ contract PaycallTest is Test { // gas: meter execute vm.resumeGasMetering(); vm.expectEmit(true, true, true, false); // We ignore the amount because it will differ based on via-IR - emit PayForGas(address(wallet), tx.origin, USDC, 6_792_365); + emit PayForGas(address(wallet), tx.origin, USDC, 10_658_763); wallet.executeQuarkOperation(op, v, r, s); assertEq(counter.number(), 15); - assertApproxEqAbs(IERC20(USDC).balanceOf(address(wallet)), 993e6, 1e6); + assertApproxEqAbs(IERC20(USDC).balanceOf(address(wallet)), 989e6, 1e6); } function testSimpleTransferTokenAndPayWithUSDC() public { @@ -211,10 +211,10 @@ contract PaycallTest is Test { // gas: meter execute vm.resumeGasMetering(); vm.expectEmit(true, true, true, false); // We ignore the amount because it will differ based on via-IR - emit PayForGas(address(wallet), tx.origin, USDC, 7_489_695); + emit PayForGas(address(wallet), tx.origin, USDC, 10_921_630); wallet.executeQuarkOperation(op, v, r, s); - assertApproxEqAbs(IERC20(USDC).balanceOf(address(wallet)), 982e6, 1e6); + assertApproxEqAbs(IERC20(USDC).balanceOf(address(wallet)), 979e6, 1e6); assertEq(IERC20(USDC).balanceOf(address(this)), 10e6); } @@ -263,7 +263,7 @@ contract PaycallTest is Test { Paycall.run.selector, multicallAddress, abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas), - 20e6 + 40e6 ), ScriptType.ScriptSource ); @@ -272,10 +272,10 @@ contract PaycallTest is Test { // gas: meter execute vm.resumeGasMetering(); vm.expectEmit(true, true, true, false); // We ignore the amount because it will differ based on via-IR - emit PayForGas(address(wallet), tx.origin, USDC, 17_584_109); + emit PayForGas(address(wallet), tx.origin, USDC, 21_450_507); wallet.executeQuarkOperation(op, v, r, s); - assertApproxEqAbs(IERC20(USDC).balanceOf(address(wallet)), 982e6, 1e6); + assertApproxEqAbs(IERC20(USDC).balanceOf(address(wallet)), 978e6, 1e6); assertEq(IComet(cUSDCv3).collateralBalanceOf(address(wallet), WETH), 100 ether); assertApproxEqAbs(IComet(cUSDCv3).borrowBalanceOf(address(wallet)), 1000e6, 2); } @@ -336,7 +336,7 @@ contract PaycallTest is Test { abi.encodeWithSignature("transfer(address,uint256)", address(this), 1 ether), 0 ), - 10e6 + 20e6 ), ScriptType.ScriptSource ); @@ -344,12 +344,12 @@ contract PaycallTest is Test { vm.resumeGasMetering(); vm.expectEmit(true, true, true, false); // We ignore the amount because it will differ based on via-IR - emit PayForGas(address(wallet), tx.origin, USDT, 6_622_373); + emit PayForGas(address(wallet), tx.origin, USDT, 10_488_771); wallet.executeQuarkOperation(op, v, r, s); assertEq(IERC20(WETH).balanceOf(address(this)), 1 ether); // About $8 in USD fees - assertApproxEqAbs(IERC20(USDT).balanceOf(address(wallet)), 993e6, 1e6); + assertApproxEqAbs(IERC20(USDT).balanceOf(address(wallet)), 989e6, 1e6); } function testPaycallForPayWithWBTC() public { @@ -374,7 +374,7 @@ contract PaycallTest is Test { abi.encodeWithSignature("transfer(address,uint256)", address(this), 1 ether), 0 ), - 30e3 + 60e3 ), ScriptType.ScriptSource ); @@ -382,12 +382,12 @@ contract PaycallTest is Test { vm.resumeGasMetering(); vm.expectEmit(true, true, true, false); // We ignore the amount because it will differ based on via-IR - emit PayForGas(address(wallet), tx.origin, WBTC, 19_079); + emit PayForGas(address(wallet), tx.origin, WBTC, 30_228); wallet.executeQuarkOperation(op, v, r, s); assertEq(IERC20(WETH).balanceOf(address(this)), 1 ether); // Fees in WBTC will be around ~ 0.00021 WBTC - assertApproxEqAbs(IERC20(WBTC).balanceOf(address(wallet)), 99_981_000, 1e3); + assertApproxEqAbs(IERC20(WBTC).balanceOf(address(wallet)), 99_970_000, 1e3); } function testRevertsWhenCostIsMoreThanMaxPaymentCost() public { @@ -442,6 +442,7 @@ contract PaycallTest is Test { ); (uint8 v, bytes32 r, bytes32 s) = new SignatureHelper().signOp(alicePrivateKey, wallet, op); + // gas: meter execute vm.resumeGasMetering(); vm.expectRevert(abi.encodeWithSelector(Reverts.Whoops.selector)); wallet.executeQuarkOperation(op, v, r, s); diff --git a/test/quark-core-scripts/Quotecall.t.sol b/test/quark-core-scripts/Quotecall.t.sol index 2d9e1f11..f0fd943b 100644 --- a/test/quark-core-scripts/Quotecall.t.sol +++ b/test/quark-core-scripts/Quotecall.t.sol @@ -209,7 +209,7 @@ contract QuotecallTest is Test { abi.encodeWithSignature("transfer(address,uint256)", address(this), 10e6), 0 ), - 8e6 + 10e6 ), ScriptType.ScriptSource ); @@ -218,10 +218,10 @@ contract QuotecallTest is Test { // gas: meter execute vm.resumeGasMetering(); vm.expectEmit(true, true, true, true); - emit PayForGas(address(wallet), tx.origin, USDC, 8e6); + emit PayForGas(address(wallet), tx.origin, USDC, 10e6); wallet.executeQuarkOperation(op, v, r, s); - assertEq(IERC20(USDC).balanceOf(address(wallet)), 982e6); + assertEq(IERC20(USDC).balanceOf(address(wallet)), 980e6); assertEq(IERC20(USDC).balanceOf(address(this)), 10e6); }