Skip to content

Commit

Permalink
fix(dutchV3): Update v3 order validation (#495)
Browse files Browse the repository at this point in the history
* Bump up encoded order length limit to 4k for DutchV3

* Fix v3 order validation

* Remove .only from test

* test case uses array of nums

* relativeBlocks should always be number[] in OrderService
  • Loading branch information
codyborn authored Dec 3, 2024
1 parent f528711 commit d9903a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/handlers/get-orders/schema/GetDutchV3OrderResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type GetDutchV3OrderResponse = {
token: string
startAmount: string
curve: {
relativeBlocks: string[]
relativeBlocks: number[]
relativeAmounts: string[]
}
maxAmount: string
Expand All @@ -30,7 +30,7 @@ export type GetDutchV3OrderResponse = {
token: string
startAmount: string
curve: {
relativeBlocks: string[]
relativeBlocks: number[]
relativeAmounts: string[]
}
recipient: string
Expand Down Expand Up @@ -73,7 +73,7 @@ export const GetDutchV3OrderResponseEntryJoi = Joi.object({
token: FieldValidator.isValidEthAddress().required(),
startAmount: FieldValidator.isValidAmount().required(),
curve: Joi.object({
relativeBlocks: Joi.array().items(FieldValidator.isValidAmount()),
relativeBlocks: Joi.array().items(FieldValidator.isValidNumber()),
relativeAmounts: Joi.array().items(FieldValidator.isValidAmount()),
}),
maxAmount: FieldValidator.isValidAmount(),
Expand All @@ -84,7 +84,7 @@ export const GetDutchV3OrderResponseEntryJoi = Joi.object({
token: FieldValidator.isValidEthAddress().required(),
startAmount: FieldValidator.isValidAmount().required(),
curve: Joi.object({
relativeBlocks: Joi.array().items(FieldValidator.isValidAmount()),
relativeBlocks: Joi.array().items(FieldValidator.isValidNumber()),
relativeAmounts: Joi.array().items(FieldValidator.isValidAmount()),
}),
recipient: FieldValidator.isValidEthAddress().required(),
Expand Down
4 changes: 2 additions & 2 deletions lib/models/DutchV3Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class DutchV3Order extends Order {
token: this.inner.info.input.token,
startAmount: this.inner.info.input.startAmount.toString(),
curve: {
relativeBlocks: this.inner.info.input.curve.relativeBlocks.map((a) => a.toString()),
relativeBlocks: this.inner.info.input.curve.relativeBlocks,
relativeAmounts: this.inner.info.input.curve.relativeAmounts.map((a) => a.toString()),
},
maxAmount: this.inner.info.input.maxAmount.toString(),
Expand All @@ -116,7 +116,7 @@ export class DutchV3Order extends Order {
token: o.token,
startAmount: o.startAmount.toString(),
curve: {
relativeBlocks: o.curve.relativeBlocks.map((a) => a.toString()),
relativeBlocks: o.curve.relativeBlocks,
relativeAmounts: o.curve.relativeAmounts.map((a) => a.toString()),
},
minAmount: o.minAmount.toString(),
Expand Down
4 changes: 4 additions & 0 deletions lib/util/field-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ export default class FieldValidator {
return this.BIG_NUMBER_JOI
}

public static isValidNumber(): NumberSchema {
return this.NUMBER_JOI
}

public static isValidOrderHashes(): StringSchema {
return this.ORDER_HASHES_JOI
}
Expand Down
20 changes: 18 additions & 2 deletions test/unit/handlers/get-orders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { OrderDispatcher } from '../../../lib/services/OrderDispatcher'
import { SUPPORTED_CHAINS } from '../../../lib/util/chain'
import { HeaderExpectation } from '../../HeaderExpectation'
import { REQUEST_ID } from '../fixtures'
import Joi from 'joi'
import { GetDutchV3OrderResponse, GetDutchV3OrderResponseEntryJoi } from '../../../lib/handlers/get-orders/schema/GetDutchV3OrderResponse'

describe('Testing get orders handler.', () => {
const MOCK_ORDER = {
Expand Down Expand Up @@ -51,7 +53,7 @@ describe('Testing get orders handler.', () => {
startAmount: '1000000000000000000',
adjustmentPerGweiBaseFee: '5000',
curve: {
relativeBlocks: ['1', '2', '3'],
relativeBlocks: [1, 2, 3],
relativeAmounts: ['4', '5', '6'],
},
maxAmount: '1000000000000000000',
Expand All @@ -70,7 +72,7 @@ describe('Testing get orders handler.', () => {
startAmount: '3000000000000000000',
adjustmentPerGweiBaseFee: '5000',
curve: {
relativeBlocks: ['1', '2', '3'],
relativeBlocks: [1, 2, 3],
relativeAmounts: ['4', '5', '6'],
},
minAmount: '2000000000000000000',
Expand Down Expand Up @@ -429,4 +431,18 @@ describe('Testing get orders handler.', () => {
expect(JSON.parse(getOrdersResponse.body).orders[0].quoteId).not.toBeDefined()
})
})

describe('response validation', () => {
it('DutchV3 order passes response validation', async () => {

const v3order: GetDutchV3OrderResponse = JSON.parse('{"outputs":[{"recipient":"0x250a94c03b9b57c93cc5549760d59d6eacfb136d","minAmount":"6097025","startAmount":"6119175","curve":{"relativeAmounts":["22150"],"relativeBlocks":[8]},"token":"0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8","adjustmentPerGweiBaseFee":"0"}],"filler_offerer_orderStatus":"0x0000000000000000000000000000000000000000_0x250a94c03b9b57c93cc5549760d59d6eacfb136d_open","reactor":"0xb274d5f4b833b61b340b654d600a864fb604a87c","offerer_orderStatus":"0x250a94c03b9b57c93cc5549760d59d6eacfb136d_open","encodedOrder":"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000004449cd34d1eb1fedcf02a1be3834ffde8e6a6180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000004600000000000000000000000000000000000000000000000000000000000000540000000000000000000000000b274d5f4b833b61b340b654d600a864fb604a87c000000000000000000000000250a94c03b9b57c93cc5549760d59d6eacfb136df038f18e74bf2b1cbb5d94d8029d443414976bd38efec768706eca33b004fa0000000000000000000000000000000000000000000000000000000000674ec602000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab100000000000000000000000000000000000000000000000000060a24181e400000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000060a24181e40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc800000000000000000000000000000000000000000000000000000000005d5f0700000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000250a94c03b9b57c93cc5549760d59d6eacfb136d00000000000000000000000000000000000000000000000000000000005d0881000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000056860000000000000000000000000000000000000000000000000000000010bd7baa00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041d1c99bcbee1bed23ee847cfe3d501f034f23b8ba67c249dc07a2ca7c376dd9c04f31eabe9dd15d9b3464cd72196975fbb71a1455233afe0d1050e312c58976fc1b00000000000000000000000000000000000000000000000000000000000000","requestId":"6fe3b04d-658a-4529-af66-76bcdd7d5265","signature":"0xaebea601f90e77d5ae72f5c28a14ccc71e88ac61e2af0fc8f45278c1367696fb080ddebb4b66b05559d5fe34533f756844b279fecc18c562233d9b0a2fbb870a1c","deadline":1733215746,"cosignature":"0xd1c99bcbee1bed23ee847cfe3d501f034f23b8ba67c249dc07a2ca7c376dd9c04f31eabe9dd15d9b3464cd72196975fbb71a1455233afe0d1050e312c58976fc1b","cosignerData":{"exclusiveFiller":"0x0000000000000000000000000000000000000000","inputOverride":"0","outputOverrides":["0"],"decayStartBlock":280853418},"modified":"2024-12-03T08:44:07.702Z","input":{"startAmount":"1700000000000000","maxAmount":"1700000000000000","curve":{"relativeAmounts":["0"],"relativeBlocks":[8]},"token":"0x82aF49447D8a07e3bd95BD0d56f35241523fBab1","adjustmentPerGweiBaseFee":"0"},"filler":"0x0000000000000000000000000000000000000000","orderStatus":"open","chainId_orderStatus_filler":"42161_open_0x0000000000000000000000000000000000000000","createdAt":1733215447,"chainId_filler":"42161_0x0000000000000000000000000000000000000000","entity":"Order","quoteId":"6fe3b04d-658a-4529-af66-76bcdd7d5265","filler_offerer":"0x0000000000000000000000000000000000000000_0x250a94c03b9b57c93cc5549760d59d6eacfb136d","filler_orderStatus":"0x0000000000000000000000000000000000000000_open","created":"2024-12-03T08:44:07.702Z","chainId":42161,"orderHash":"0xa694497e3644c2ce58a22b4b3fcd6004e3faf8057dc409a70da08aea85a92b4b","chainId_orderStatus":"42161_open","nonce":"108655694257088393948171748675121105290610995892134912696918847811839007717888","startingBaseFee":"0","type":"Dutch_V3","swapper":"0x250a94c03b9b57c93cc5549760d59d6eacfb136d"}');
const responseSchema = Joi.alternatives(GetDutchV3OrderResponseEntryJoi);
const result = responseSchema.validate(v3order, {
allowUnknown: true,
stripUnknown: true, // Ensure no unexpected fields returned to users.
})

expect(result.error).toBeUndefined()
})
})
})

0 comments on commit d9903a1

Please sign in to comment.