Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gas measurement to tests and refactor some test structures #337

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

alex0207s
Copy link
Contributor

@alex0207s alex0207s commented Jan 2, 2025

  • Update the forge-std library to v1.9.4, which supports the snapshotGasLastCall cheatcode.

  • Add the snapshotGasLastCall cheatcode to tests for measuring gas usage.

  • Refactor test structures to enhance consistency. (e.g. replace vm.prank() with vm.startPrank() and vm.stopPrank()).

  • Fix a bug in a test function where takerTokenAmount and makerTokenAmount were misused.

@@ -1,8 +1,6 @@
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
branch = v5.0.2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch entry is an optional setting. However, its inclusion causes forge update to fail when updating dependencies.

Reference: foundry-rs/foundry#3720 (comment)

}
}

contract EIP712TestContract is EIP712 {
contract EIP712Harness is EIP712 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -14,7 +14,6 @@ import { IUniswapSwapRouter02 } from "test/utils/IUniswapSwapRouter02.sol";
import { MockStrategy } from "test/mocks/MockStrategy.sol";
import { GenericSwap } from "contracts/GenericSwap.sol";
import { AllowanceTarget } from "contracts/AllowanceTarget.sol";
import { TokenCollector } from "contracts/abstracts/TokenCollector.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usused import

@@ -24,18 +23,6 @@ import { ISmartOrderStrategy } from "contracts/interfaces/ISmartOrderStrategy.so
contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper {
using BalanceSnapshot for Snapshot;

event Swap(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the Swap event from the IGenericSwap interface instead of declaring it in the test.

@@ -132,7 +136,7 @@ contract FillTest is LimitOrderSwapTest {
Snapshot memory fcMakerToken = BalanceSnapshot.take({ owner: feeCollector, token: defaultOrder.makerToken });

uint256 takingAmount = defaultOrder.takerTokenAmount / 2;
uint256 makingAmount = defaultOrder.takerTokenAmount / 2;
uint256 makingAmount = defaultOrder.makerTokenAmount / 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be makerTokenAmount instead of takerTokenAmount.

@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import { ILimitOrderSwap } from "contracts/interfaces/ILimitOrderSwap.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

@@ -10,8 +10,6 @@ import { ISmartOrderStrategy } from "contracts/interfaces/ISmartOrderStrategy.so
import { IUniswapSwapRouter02 } from "test/utils/IUniswapSwapRouter02.sol";
import { Constant } from "contracts/libraries/Constant.sol";
import { BalanceSnapshot, Snapshot } from "test/utils/BalanceSnapshot.sol";
import { UniswapV2Library } from "test/utils/UniswapV2Library.sol";
import { UniswapV3 } from "test/utils/UniswapV3.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

@@ -13,9 +13,9 @@ import { ISmartOrderStrategy } from "contracts/interfaces/ISmartOrderStrategy.so
import { ILimitOrderSwap } from "contracts/interfaces/ILimitOrderSwap.sol";
import { TokenCollector } from "contracts/abstracts/TokenCollector.sol";
import { Constant } from "contracts/libraries/Constant.sol";
import { RFQOffer, getRFQOfferHash } from "contracts/libraries/RFQOffer.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

import { RFQTx } from "contracts/libraries/RFQTx.sol";
import { LimitOrder, getLimitOrderHash } from "contracts/libraries/LimitOrder.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import


uint256 balance = token.balanceOf(address(this));
assertEq(balance, amount);
}

/* Token Permit */

TokenPermit DEFAULT_TOKEN_PERMIT =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to the setup function

@alex0207s alex0207s requested a review from NIC619 January 6, 2025 01:55
allowanceTarget.spendFromUserTo(user, address(mockERC20), recipient, amount);
vm.stopPrank();
vm.snapshotGasLastCall("AllowanceTarget", "testSpendFromUserTo: spendFromUserTo()");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move the called function to the front would make it easier to compare in snapshot file

Suggested change
vm.snapshotGasLastCall("AllowanceTarget", "testSpendFromUserTo: spendFromUserTo()");
vm.snapshotGasLastCall("AllowanceTarget", "spendFromUserTo(): testSpendFromUserTo");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which format is better: targetFunction() | Test Function: testFunction() or targetFunction(): testFunction ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think second one will do.

@alex0207s alex0207s force-pushed the test/add-gas-snapshot branch from 56723b9 to 9b4afdb Compare January 16, 2025 09:45
@alex0207s alex0207s merged commit ded59b3 into master Jan 16, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants