Skip to content

Commit 9d982a2

Browse files
move chainId parsing to function
1 parent 8a89993 commit 9d982a2

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

lib/handlers/get-orders/handler.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ErrorResponse,
1616
Response,
1717
} from '../base/index'
18+
import { tryParseChainIdFromBody } from '../post-order/handler'
1819
import { ContainerInjected, RequestInjected } from './injector'
1920
import { GetDutchV2OrderResponse } from './schema/GetDutchV2OrderResponse'
2021
import { GetOrdersResponse, GetOrdersResponseJoi } from './schema/GetOrdersResponse'
@@ -102,14 +103,7 @@ export class GetOrdersHandler extends APIGLambdaHandler<
102103
protected afterResponseHook(event: APIGatewayProxyEvent, _context: Context, response: APIGatewayProxyResult): void {
103104
const { statusCode } = response
104105

105-
// Try and extract the chain id from the raw json.
106-
let chainId = '0'
107-
try {
108-
const rawBody = JSON.parse(event.body || '')
109-
chainId = rawBody.chainId ?? chainId
110-
} catch (err) {
111-
// no-op. If we can't get chainId still log the metric as chain 0
112-
}
106+
const chainId = tryParseChainIdFromBody(event)
113107
const statusCodeMod = (Math.floor(statusCode / 100) * 100).toString().replace(/0/g, 'X')
114108

115109
const getOrdersByChainMetricName = `GetOrdersChainId${chainId.toString()}Status${statusCodeMod}`

lib/handlers/post-order/handler.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ import {
2222
import { PostOrderBodyParser } from './PostOrderBodyParser'
2323
import { PostOrderRequestBody, PostOrderRequestBodyJoi, PostOrderResponse, PostOrderResponseJoi } from './schema'
2424

25+
export function tryParseChainIdFromBody(event: APIGatewayProxyEvent) {
26+
// Try and extract the chain id from the raw json.
27+
if (!event || !event.body) {
28+
return '0'
29+
}
30+
31+
try {
32+
const rawBody = JSON.parse(event.body)
33+
const chainId = rawBody.chainId ?? '0'
34+
return chainId
35+
} catch (err) {
36+
return '0'
37+
}
38+
}
2539
export class PostOrderHandler extends APIGLambdaHandler<
2640
unknown,
2741
ApiRInj,
@@ -119,14 +133,7 @@ export class PostOrderHandler extends APIGLambdaHandler<
119133
protected afterResponseHook(event: APIGatewayProxyEvent, _context: Context, response: APIGatewayProxyResult): void {
120134
const { statusCode } = response
121135

122-
// Try and extract the chain id from the raw json.
123-
let chainId = '0'
124-
try {
125-
const rawBody = JSON.parse(event.body || '')
126-
chainId = rawBody.chainId ?? chainId
127-
} catch (err) {
128-
// no-op. If we can't get chainId still log the metric as chain 0
129-
}
136+
const chainId = tryParseChainIdFromBody(event)
130137

131138
const statusCodeMod = (Math.floor(statusCode / 100) * 100).toString().replace(/0/g, 'X')
132139

test/unit/handlers/post-order/post-order.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Logger } from '@aws-lambda-powertools/logger'
22
import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn'
33
import { OrderType, OrderValidation, OrderValidator, RelayOrderValidator } from '@uniswap/uniswapx-sdk'
4+
import { APIGatewayEvent } from 'aws-lambda'
45
import { mockClient } from 'aws-sdk-client-mock'
56
import { mock } from 'jest-mock-extended'
67
import { ORDER_STATUS, UniswapXOrderEntity } from '../../../../lib/entities'
78
import { ErrorCode } from '../../../../lib/handlers/base'
89
import { DEFAULT_MAX_OPEN_ORDERS } from '../../../../lib/handlers/constants'
910
import { OnChainValidatorMap } from '../../../../lib/handlers/OnChainValidatorMap'
10-
import { PostOrderHandler } from '../../../../lib/handlers/post-order/handler'
11+
import { PostOrderHandler, tryParseChainIdFromBody } from '../../../../lib/handlers/post-order/handler'
1112
import { getMaxOpenOrders } from '../../../../lib/handlers/post-order/injector'
1213
import { PostOrderBodyParser } from '../../../../lib/handlers/post-order/PostOrderBodyParser'
1314
import { kickoffOrderTrackingSfn } from '../../../../lib/handlers/shared/sfn'
@@ -519,4 +520,26 @@ describe('Testing post order handler.', () => {
519520
})
520521
})
521522
})
523+
524+
describe('tryParseChainIdFromBody', () => {
525+
test('should return 0 when no chainId', () => {
526+
const response = tryParseChainIdFromBody({ body: '{}' } as APIGatewayEvent)
527+
expect(response).toEqual('0')
528+
})
529+
530+
test('should return 0 when no body', () => {
531+
const response = tryParseChainIdFromBody({} as APIGatewayEvent)
532+
expect(response).toEqual('0')
533+
})
534+
535+
test('should return 0 when JSON.parse errors', () => {
536+
const response = tryParseChainIdFromBody({ body: '{;}' } as APIGatewayEvent)
537+
expect(response).toEqual('0')
538+
})
539+
540+
test('should return chainId', () => {
541+
const response = tryParseChainIdFromBody({ body: '{"chainId":1}' } as APIGatewayEvent)
542+
expect(response).toEqual(1)
543+
})
544+
})
522545
})

0 commit comments

Comments
 (0)