Skip to content

Commit

Permalink
Update gas overhead for Paycall and Quotecall
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincheng96 committed Jun 4, 2024
1 parent adffc6d commit e6fd92d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/quark-core-scripts/src/Paycall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/quark-core-scripts/src/Quotecall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 15 additions & 14 deletions test/quark-core-scripts/Paycall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ contract PaycallTest is Test {
Paycall.run.selector,
multicallAddress,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
10e6
20e6
),
ScriptType.ScriptSource
);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -263,7 +263,7 @@ contract PaycallTest is Test {
Paycall.run.selector,
multicallAddress,
abi.encodeWithSelector(Multicall.run.selector, callContracts, callDatas),
20e6
40e6
),
ScriptType.ScriptSource
);
Expand All @@ -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);
}
Expand Down Expand Up @@ -336,20 +336,20 @@ contract PaycallTest is Test {
abi.encodeWithSignature("transfer(address,uint256)", address(this), 1 ether),
0
),
10e6
20e6
),
ScriptType.ScriptSource
);
(uint8 v, bytes32 r, bytes32 s) = new SignatureHelper().signOp(alicePrivateKey, wallet, op);

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 {
Expand All @@ -374,20 +374,20 @@ contract PaycallTest is Test {
abi.encodeWithSignature("transfer(address,uint256)", address(this), 1 ether),
0
),
30e3
60e3
),
ScriptType.ScriptSource
);
(uint8 v, bytes32 r, bytes32 s) = new SignatureHelper().signOp(alicePrivateKey, wallet, op);

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 {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test/quark-core-scripts/Quotecall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ contract QuotecallTest is Test {
abi.encodeWithSignature("transfer(address,uint256)", address(this), 10e6),
0
),
8e6
10e6
),
ScriptType.ScriptSource
);
Expand All @@ -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);
}

Expand Down

0 comments on commit e6fd92d

Please sign in to comment.