-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description Reduces pressure on the API by doing some sanity checks on orders from contracts. # Changes - [x] Filters out invalid orders that will be rejected by the API ## How to test 1. Run on staging 2. Observe no API errors for orders that are filtered ## Related Issues Fixes #122
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Order } from "@cowprotocol/contracts"; | ||
import { BigNumber, ethers } from "ethers"; | ||
|
||
/** | ||
* Process an order to determine if it is valid | ||
* @param order The GPv2.Order data struct to validate | ||
* @throws Error if the order is invalid | ||
*/ | ||
export function validateOrder(order: Order) { | ||
// amounts must be non-zero | ||
if (BigNumber.from(order.sellAmount).isZero()) { | ||
throw new Error("Order has zero sell amount"); | ||
} | ||
|
||
if (BigNumber.from(order.buyAmount).isZero()) { | ||
throw new Error("Order has zero buy amount"); | ||
} | ||
|
||
// token addresses must not be the ZeroAddress | ||
if (order.sellToken === ethers.constants.AddressZero) { | ||
throw new Error("Order has zero sell token address"); | ||
} | ||
|
||
if (order.buyToken === ethers.constants.AddressZero) { | ||
throw new Error("Order has zero buy token address"); | ||
} | ||
|
||
// tokens must not be the same | ||
if (order.sellToken === order.buyToken) { | ||
throw new Error("Order has identical sell and buy token addresses"); | ||
} | ||
|
||
// Check to make sure that the order has at least 120s of validity | ||
if (Math.floor(Date.now() / 1000) + 60 > Number(order.validTo)) { | ||
throw new Error("Order expires too soon"); | ||
} | ||
} |