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

[ZeroEx] add test for Otc order signed by another address #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/zeroEx_otcOrder_DAI-WETH.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93071
78527
2 changes: 1 addition & 1 deletion .forge-snapshots/zeroEx_otcOrder_USDC-WETH.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112545
98001
2 changes: 1 addition & 1 deletion .forge-snapshots/zeroEx_otcOrder_USDT-WETH.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
104183
89639
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
80812
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100286
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
91924
16 changes: 15 additions & 1 deletion test/integration/BasePairTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ abstract contract BasePairTest is Test, GasSnapshot, Permit2Signature {
address internal FROM = vm.addr(FROM_PRIVATE_KEY);
uint256 internal constant MAKER_PRIVATE_KEY = 0x0ff1c1a1;
address internal MAKER = vm.addr(MAKER_PRIVATE_KEY);
uint256 internal constant OTHER_MAKER_PRIVATE_KEY = 0x0cacacac;
address internal OTHER_MAKER = vm.addr(OTHER_MAKER_PRIVATE_KEY);

address internal constant BURN_ADDRESS = 0x2222222222222222222222222222222222222222;

Expand Down Expand Up @@ -78,11 +80,23 @@ abstract contract BasePairTest is Test, GasSnapshot, Permit2Signature {
function warmZeroExOtcNonce(address who) internal {
// mapping(address => mapping(uint64 => uint128)) txOriginNonces;
// tx.origin bucket min nonce
// OtcOrders is 8th in LibStorage Enum
// OtcOrders is LibStorage[8] Enum https://github.com/0xProject/protocol/blob/master/contracts/zero-ex/contracts/src/storage/LibStorage.sol#L26
bytes32 slotId = keccak256(abi.encode(uint256(0), keccak256(abi.encode(who, (uint256(8) + 1) << 128))));
vm.store(address(ZERO_EX_ADDRESS), slotId, bytes32(uint256(1)));
}

/// @dev Manually set a registered signer in 0xV4 Native Orders
/// note: we attempt to avoid touching storage by the usual means to side
/// step gas metering
function allowRegisteredSigner(address maker, address signer) internal {
// mapping(address => mapping(address => bool)) orderSignerRegistry;
// maker signer allowed
// NativeOrders is LibStorage[7] Enum https://github.com/0xProject/protocol/blob/master/contracts/zero-ex/contracts/src/storage/LibStorage.sol#L26
// orderSignerRegistry is Storage[4] in the Storage struct https://github.com/0xProject/protocol/blob/master/contracts/zero-ex/contracts/src/storage/LibNativeOrdersStorage.sol#L38
bytes32 slotId = keccak256(abi.encode(signer, keccak256(abi.encode(maker, ((uint256(7) + 1) << 128) + 4))));
vm.store(address(ZERO_EX_ADDRESS), slotId, bytes32(uint256(1)));
}

modifier skipIf(bool condition) {
if (!condition) {
_;
Expand Down
18 changes: 17 additions & 1 deletion test/integration/ZeroExPairTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ abstract contract ZeroExPairTest is BasePairTest {
otcOrder.maker = MAKER;
otcOrderHash = ZERO_EX.getOtcOrderHash(otcOrder);

// Set up MAKER to also allow signatures from OTHER_MAKER
// vm.startPrank(MAKER);
// ZERO_EX.registerAllowedOrderSigner(OTHER_MAKER, true);
allowRegisteredSigner(MAKER, OTHER_MAKER);

// MetaTransactionV2
IZeroEx.BatchSellSubcall[] memory calls = new IZeroEx.BatchSellSubcall[](1);
calls[0] = IZeroEx.BatchSellSubcall({
Expand Down Expand Up @@ -82,10 +87,21 @@ abstract contract ZeroExPairTest is BasePairTest {
}

function testZeroEx_otcOrder() public {
IZeroEx.OtcOrder memory _otcOrder = otcOrder;
(uint8 v, bytes32 r, bytes32 s) = vm.sign(MAKER_PRIVATE_KEY, otcOrderHash);
vm.startPrank(FROM, FROM);
snapStartName("zeroEx_otcOrder");
ZERO_EX.fillOtcOrder(otcOrder, IZeroEx.Signature(IZeroEx.SignatureType.EIP712, v, r, s), uint128(amount()));
ZERO_EX.fillOtcOrder(_otcOrder, IZeroEx.Signature(IZeroEx.SignatureType.EIP712, v, r, s), uint128(amount()));
snapEnd();
}

function testZeroEx_otcOrder_allowedSigner() public {
IZeroEx.OtcOrder memory _otcOrder = otcOrder;
// MAKER has already registered OTHER_MAKER as a signer
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OTHER_MAKER_PRIVATE_KEY, otcOrderHash);
vm.startPrank(FROM, FROM);
snapStartName("zeroEx_otcOrder_allowedSigner");
ZERO_EX.fillOtcOrder(_otcOrder, IZeroEx.Signature(IZeroEx.SignatureType.EIP712, v, r, s), uint128(amount()));
snapEnd();
}

Expand Down
1 change: 1 addition & 0 deletions test/integration/vendor/IZeroEx.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ interface IZeroEx {

function getOtcOrderHash(IZeroEx.OtcOrder memory order) external view returns (bytes32 orderHash);
function lastOtcTxOriginNonce(address txOrigin, uint64 nonceBucket) external view returns (uint128 lastNonce);
function registerAllowedOrderSigner(address signer, bool allowed) external;

function getMetaTransactionV2Hash(IMetaTransactionsFeatureV2.MetaTransactionDataV2 calldata mtx)
external
Expand Down
Loading