-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
@@ -1,8 +1,6 @@ | |||
[submodule "lib/openzeppelin-contracts"] | |||
path = lib/openzeppelin-contracts | |||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | |||
branch = v5.0.2 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A best pratice for tseting internal fuction: https://book.getfoundry.sh/tutorials/best-practices?highlight=har#test-harnesses
@@ -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"; |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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
test/AllowanceTarget.t.sol
Outdated
allowanceTarget.spendFromUserTo(user, address(mockERC20), recipient, amount); | ||
vm.stopPrank(); | ||
vm.snapshotGasLastCall("AllowanceTarget", "testSpendFromUserTo: spendFromUserTo()"); |
There was a problem hiding this comment.
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
vm.snapshotGasLastCall("AllowanceTarget", "testSpendFromUserTo: spendFromUserTo()"); | |
vm.snapshotGasLastCall("AllowanceTarget", "spendFromUserTo(): testSpendFromUserTo"); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
56723b9
to
9b4afdb
Compare
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()
withvm.startPrank()
andvm.stopPrank()
).Fix a bug in a test function where
takerTokenAmount
andmakerTokenAmount
were misused.