Skip to content

Commit

Permalink
Merge pull request #50 from consenlabs/v5.3.0-PVE002
Browse files Browse the repository at this point in the history
validate order in cancelLimitOrder()
  • Loading branch information
charlesjhongc authored Jul 4, 2022
2 parents eeb8f5b + 04f5fc1 commit a401718
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions contracts/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ contract LimitOrder is ILimitOrder, BaseLibEIP712, SignatureValidator, Reentranc
* Cancel limit order
*/
function cancelLimitOrder(LimitOrderLibEIP712.Order calldata _order, bytes calldata _cancelOrderMakerSig) external override onlyUserProxy nonReentrant {
require(_order.expiry > uint64(block.timestamp), "LimitOrder: Order is expired");
bytes32 orderHash = getEIP712Hash(LimitOrderLibEIP712._getOrderStructHash(_order));
bool isCancelled = LibOrderStorage.getStorage().orderHashToCancelled[orderHash];
require(!isCancelled, "LimitOrder: Order is cancelled already");
{
LimitOrderLibEIP712.Order memory cancelledOrder = _order;
cancelledOrder.takerTokenAmount = 0;
Expand All @@ -494,9 +498,7 @@ contract LimitOrder is ILimitOrder, BaseLibEIP712, SignatureValidator, Reentranc
}

// Set cancelled state to storage
bytes32 orderHash = getEIP712Hash(LimitOrderLibEIP712._getOrderStructHash(_order));
LibOrderStorage.getStorage().orderHashToCancelled[orderHash] = true;

emit OrderCancelled(orderHash, _order.maker);
}

Expand Down
22 changes: 22 additions & 0 deletions contracts/test/forkMainnet/LimitOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,28 @@ contract LimitOrderTest is StrategySharedSetup {
userProxy.toLimitOrder(cancelPayload);
}

function testCannotCancelExpiredOrder() public {
LimitOrderLibEIP712.Order memory expiredOrder = DEFAULT_ORDER;
expiredOrder.expiry = 0;

bytes memory payload = _genCancelLimitOrderPayload(expiredOrder, _signOrder(userPrivateKey, expiredOrder, SignatureValidator.SignatureType.EIP712));
vm.expectRevert("LimitOrder: Order is expired");
vm.prank(user, user); // Only EOA
userProxy.toLimitOrder(payload);
}

function testCannotCancelTwice() public {
LimitOrderLibEIP712.Order memory zeroOrder = DEFAULT_ORDER;
zeroOrder.takerTokenAmount = 0;

bytes memory payload = _genCancelLimitOrderPayload(DEFAULT_ORDER, _signOrder(makerPrivateKey, zeroOrder, SignatureValidator.SignatureType.EIP712));
vm.prank(user, user); // Only EOA
userProxy.toLimitOrder(payload);
vm.expectRevert("LimitOrder: Order is cancelled already");
vm.prank(user, user); // Only EOA
userProxy.toLimitOrder(payload);
}

/*********************************
* Helpers *
*********************************/
Expand Down

0 comments on commit a401718

Please sign in to comment.