-
Notifications
You must be signed in to change notification settings - Fork 37
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
chore: migrate GPv2Signing recoverOrderFromTrade tests to Foundry #204
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d0173f0
chore: migrate GPv2Signing domain separator tests to Foundry
fedgiac c7d4149
Fix unused import
fedgiac bc7f235
Remove duplicated import
fedgiac 0721892
chore: migrate GPv2Signing set pre-signature tests to Foundry
fedgiac 56ff598
chore: migrate GPv2Signing recoverOrderFromTrade tests to Foundry
fedgiac f71e28d
Merge branch 'main' into migrate-test-signing-domain-separator
fedgiac 2816827
Add explanatory comment on domain separator struct
fedgiac 585703d
Reuse existing PRE_SIGNED variable from library
fedgiac d51fced
Merge branch 'main' into migrate-test-signing-domain-separator
fedgiac e233612
Merge branch 'migrate-test-signing-domain-separator' into migrate-tes…
fedgiac 84eb7fc
Fix missing setting of pre-signature at start of test
fedgiac 6e618dc
Clean up imports
fedgiac 61a0da9
Merge branch 'main' into migrate-test-signing-set-pre-signature
fedgiac a16ab67
Merge branch 'main' into migrate-test-signing-set-pre-signature
fedgiac 2ebcc44
Merge branch 'migrate-test-signing-set-pre-signature' into migrate-te…
fedgiac 8dd66e7
Merge fuzzed order library into order library
fedgiac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
pragma solidity ^0.8; | ||
|
||
import {Vm} from "forge-std/Test.sol"; | ||
|
||
import {EIP1271Verifier, GPv2EIP1271, GPv2Order, GPv2Signing} from "src/contracts/mixins/GPv2Signing.sol"; | ||
|
||
import {Helper} from "./Helper.sol"; | ||
import {Order} from "test/libraries/Order.sol"; | ||
import {OrderFuzz} from "test/libraries/OrderFuzz.sol"; | ||
import {Sign} from "test/libraries/Sign.sol"; | ||
import {SettlementEncoder} from "test/libraries/encoders/SettlementEncoder.sol"; | ||
|
||
contract RecoverOrderFromTrade is Helper { | ||
using SettlementEncoder for SettlementEncoder.State; | ||
using Sign for EIP1271Verifier; | ||
|
||
Vm.Wallet private trader; | ||
|
||
constructor() { | ||
trader = vm.createWallet("GPv2Signing.RecoverOrderFromTrade: trader"); | ||
} | ||
|
||
function test_should_round_trip_encode_order_data_and_unique_identifier( | ||
OrderFuzz.Params memory params, | ||
uint256 executedAmount | ||
) public { | ||
GPv2Order.Data memory order = OrderFuzz.order(params); | ||
|
||
SettlementEncoder.State storage encoder = SettlementEncoder.makeSettlementEncoder(); | ||
encoder.signEncodeTrade(vm, trader, order, domainSeparator, GPv2Signing.Scheme.Eip712, executedAmount); | ||
|
||
GPv2Signing.RecoveredOrder memory recovered = | ||
executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[0]); | ||
assertEq(abi.encode(recovered.data), abi.encode(order)); | ||
assertEq(recovered.uid, Order.computeOrderUid(order, domainSeparator, trader.addr)); | ||
} | ||
|
||
function test_should_recover_the_order_for_all_signing_schemes(OrderFuzz.Params memory params) public { | ||
GPv2Order.Data memory order = OrderFuzz.order(params); | ||
|
||
address traderPreSign = makeAddr("trader pre-sign"); | ||
EIP1271Verifier traderEip1271 = EIP1271Verifier(makeAddr("eip1271 verifier")); | ||
Vm.Wallet memory traderEip712 = vm.createWallet("trader eip712"); | ||
Vm.Wallet memory traderEthsign = vm.createWallet("trader ethsign"); | ||
|
||
bytes memory uidPreSign = Order.computeOrderUid(order, domainSeparator, traderPreSign); | ||
vm.prank(traderPreSign); | ||
executor.setPreSignature(uidPreSign, true); | ||
|
||
vm.mockCallRevert(address(traderEip1271), hex"", "unexpected call to mock contract"); | ||
vm.mockCall( | ||
address(traderEip1271), | ||
abi.encodePacked(EIP1271Verifier.isValidSignature.selector), | ||
abi.encode(GPv2EIP1271.MAGICVALUE) | ||
); | ||
|
||
SettlementEncoder.State storage encoder = SettlementEncoder.makeSettlementEncoder(); | ||
encoder.encodeTrade(order, Sign.preSign(traderPreSign), 0); | ||
encoder.encodeTrade(order, Sign.sign(traderEip1271, hex""), 0); | ||
encoder.signEncodeTrade(vm, traderEip712, order, domainSeparator, GPv2Signing.Scheme.Eip712, 0); | ||
encoder.signEncodeTrade(vm, traderEthsign, order, domainSeparator, GPv2Signing.Scheme.EthSign, 0); | ||
|
||
GPv2Signing.RecoveredOrder memory recovered; | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[0]); | ||
assertEq(recovered.owner, traderPreSign); | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[1]); | ||
assertEq(recovered.owner, address(traderEip1271)); | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[2]); | ||
assertEq(recovered.owner, traderEip712.addr); | ||
recovered = executor.recoverOrderFromTradeTest(encoder.tokens(), encoder.trades[3]); | ||
assertEq(recovered.owner, traderEthsign.addr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
pragma solidity ^0.8; | ||
|
||
import {GPv2Order, IERC20} from "src/contracts/libraries/GPv2Order.sol"; | ||
import {GPv2Trade} from "src/contracts/libraries/GPv2Trade.sol"; | ||
|
||
import {Order} from "./Order.sol"; | ||
|
||
library OrderFuzz { | ||
using GPv2Order for GPv2Order.Data; | ||
using GPv2Order for bytes; | ||
using GPv2Trade for uint256; | ||
|
||
struct Params { | ||
address sellToken; | ||
address buyToken; | ||
address receiver; | ||
uint256 sellAmount; | ||
uint256 buyAmount; | ||
uint32 validTo; | ||
bytes32 appData; | ||
uint256 feeAmount; | ||
bytes32 flagsPick; | ||
} | ||
|
||
function order(Params memory params) internal pure returns (GPv2Order.Data memory) { | ||
Order.Flags[] memory allFlags = Order.ALL_FLAGS(); | ||
// `flags` isn't exactly random, but for fuzzing purposes it should be | ||
// more than enough. | ||
Order.Flags memory flags = allFlags[uint256(params.flagsPick) % allFlags.length]; | ||
return GPv2Order.Data({ | ||
sellToken: IERC20(params.sellToken), | ||
buyToken: IERC20(params.buyToken), | ||
receiver: params.receiver, | ||
sellAmount: params.sellAmount, | ||
buyAmount: params.buyAmount, | ||
validTo: params.validTo, | ||
appData: params.appData, | ||
feeAmount: params.feeAmount, | ||
partiallyFillable: flags.partiallyFillable, | ||
kind: flags.kind, | ||
sellTokenBalance: flags.sellTokenBalance, | ||
buyTokenBalance: flags.buyTokenBalance | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a specific reason why this is not part of the
Order
test library? If not, I'd be a fan of including this in theOrder
test library - thinking of the event that downstream consumers want to fuzz orders as well (such as some kind of testing for programmatic orders) - having this all in one library to retrieve would be helpful.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 used to be that the library was an abstract contract to be inherited by the test (I was experimenting with Foundry's fixtures) but I came to the conclusion it was better to keep it a simple library. Agreed that merging into
Order
makes sense.