Skip to content

Commit

Permalink
move chainId parsing to function
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-seitz-uniswap committed Apr 19, 2024
1 parent 8a89993 commit 76479f2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
10 changes: 2 additions & 8 deletions lib/handlers/get-orders/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ErrorResponse,
Response,
} from '../base/index'
import { tryParseChainIdFromBody } from '../post-order/handler'
import { ContainerInjected, RequestInjected } from './injector'
import { GetDutchV2OrderResponse } from './schema/GetDutchV2OrderResponse'
import { GetOrdersResponse, GetOrdersResponseJoi } from './schema/GetOrdersResponse'
Expand Down Expand Up @@ -102,14 +103,7 @@ export class GetOrdersHandler extends APIGLambdaHandler<
protected afterResponseHook(event: APIGatewayProxyEvent, _context: Context, response: APIGatewayProxyResult): void {
const { statusCode } = response

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

const getOrdersByChainMetricName = `GetOrdersChainId${chainId.toString()}Status${statusCodeMod}`
Expand Down
23 changes: 15 additions & 8 deletions lib/handlers/post-order/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ import {
import { PostOrderBodyParser } from './PostOrderBodyParser'
import { PostOrderRequestBody, PostOrderRequestBodyJoi, PostOrderResponse, PostOrderResponseJoi } from './schema'

export function tryParseChainIdFromBody(event: APIGatewayProxyEvent) {
// Try and extract the chain id from the raw json.
if (!event || !event.body) {
return '0'
}

try {
const rawBody = JSON.parse(event.body)
const chainId = rawBody.chainId ?? '0'
return chainId
} catch (err) {
return '0'
}
}
export class PostOrderHandler extends APIGLambdaHandler<
unknown,
ApiRInj,
Expand Down Expand Up @@ -119,14 +133,7 @@ export class PostOrderHandler extends APIGLambdaHandler<
protected afterResponseHook(event: APIGatewayProxyEvent, _context: Context, response: APIGatewayProxyResult): void {
const { statusCode } = response

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

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

Expand Down
23 changes: 23 additions & 0 deletions test/unit/handlers/post-order/post-order.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger } from '@aws-lambda-powertools/logger'
import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn'
import { OrderType, OrderValidation, OrderValidator, RelayOrderValidator } from '@uniswap/uniswapx-sdk'
import { APIGatewayEvent } from 'aws-lambda'
import { mockClient } from 'aws-sdk-client-mock'
import { mock } from 'jest-mock-extended'
import { ORDER_STATUS, UniswapXOrderEntity } from '../../../../lib/entities'
Expand Down Expand Up @@ -519,4 +520,26 @@ describe('Testing post order handler.', () => {
})
})
})

describe('tryParseChainIdFromBody', () => {
test('should return 0 when no chainId', () => {
const response = postOrderHandler.tryParseChainIdFromBody({ body: '{}' } as APIGatewayEvent)
expect(response).toEqual('0')
})

test('should return 0 when no body', () => {
const response = postOrderHandler.tryParseChainIdFromBody({} as APIGatewayEvent)
expect(response).toEqual('0')
})

test('should return 0 when JSON.parse errors', () => {
const response = postOrderHandler.tryParseChainIdFromBody({ body: '{;}' } as APIGatewayEvent)
expect(response).toEqual('0')
})

test('should return chainId', () => {
const response = postOrderHandler.tryParseChainIdFromBody({ body: '{"chainId":1}' } as APIGatewayEvent)
expect(response).toEqual(1)
})
})
})

0 comments on commit 76479f2

Please sign in to comment.