Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/lint removal #431

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-this-alias": [
"error",
{
"allowDestructuring": true, // Allow `const { props, state } = this`; false by default
"allowedNames": [
"self" // Allow `const self= this`; `[]` by default
]
}
],
"@typescript-eslint/no-this-alias": "error",
"@typescript-eslint/ban-types": "warn",
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "warn",
"jest/prefer-to-have-length": "warn",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "error",
"jest/valid-expect": "error"
}
}
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
3 changes: 2 additions & 1 deletion lib/util/OffChainRelayOrderValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class OffChainRelayOrderValidator {
}

private validateReactorAddress(reactor: string, chainId: number): OrderValidationResponse {
if (reactor.toLowerCase() != REACTOR_ADDRESS_MAPPING[chainId][OrderType.Relay]!.toLowerCase()) {
const reactorMapping = REACTOR_ADDRESS_MAPPING[chainId][OrderType.Relay]
if (!reactorMapping || reactorMapping.toLowerCase() != reactor.toLowerCase()) {
return {
valid: false,
errorString: `Invalid reactor address`,
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe.skip('/dutch-auction/order', () => {
const responses = await Promise.all(promises)
expect(responses.every((resp) => resp.status === 200)).toBe(true)
const orders = responses.map((resp) => resp.data.orders[0])
expect(orders.length).toEqual(orderHashes.length)
expect(orders).toHaveLength(orderHashes.length)
const orderStatuses = orders.map((order) => order!.orderStatus)
if (orderStatuses.every((status) => status === 'open')) {
return true
Expand All @@ -170,7 +170,7 @@ describe.skip('/dutch-auction/order', () => {
`${URL}dutch-auction/orders?orderHash=${orderHash}`
)
expect(resp.status).toEqual(200)
expect(resp.data.orders.length).toEqual(1)
expect(resp.data.orders).toHaveLength(1)
const order = resp.data.orders[0]
expect(order).toBeDefined()
expect(order!.orderHash).toEqual(orderHash)
Expand Down
2 changes: 1 addition & 1 deletion test/factories/SDKDutchOrderV2Factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('SDKDutchOrderV2Factory', () => {
},
],
})
expect(actual.info.outputs.length).toEqual(1)
expect(actual.info.outputs).toHaveLength(1)
expect(actual.info.outputs[0].token).toEqual('0xabc')
})

Expand Down
4 changes: 2 additions & 2 deletions test/integ/crons/gs-reaper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ describe('deleteStaleOrders Test', () => {
it('should delete stale orders', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction(MOCK_ORDER)
let staleOrders = await ordersRepository.getByOrderStatus(ORDER_STATUS.OPEN)
expect(staleOrders.orders.length).toBe(1)
expect(staleOrders.orders).toHaveLength(1)
await deleteStaleOrders(ordersRepository, log)
staleOrders = await ordersRepository.getByOrderStatus(ORDER_STATUS.OPEN)
expect(staleOrders.orders.length).toBe(0)
expect(staleOrders.orders).toHaveLength(0)
})

it('should page through all stale orders if necessary', async () => {
Expand Down
56 changes: 28 additions & 28 deletions test/integ/repositories/dynamo-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('OrdersRepository put item test', () => {
describe('OrdersRepository getOrders test', () => {
it('should successfully get orders given an orderHash', async () => {
const orders = await ordersRepository.getOrders(10, { orderHash: MOCK_ORDER_2.orderHash })
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})

Expand All @@ -160,7 +160,7 @@ describe('OrdersRepository getOrders test', () => {
const orders = await ordersRepository.getOrders(10, {
orderHashes: [MOCK_ORDER_2.orderHash, MOCK_ORDER_3.orderHash],
})
expect(orders.orders.length).toEqual(2)
expect(orders.orders).toHaveLength(2)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
expect(orders.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
Expand All @@ -174,7 +174,7 @@ describe('OrdersRepository getOrders test', () => {

it('should successfully get orders given an offerer', async () => {
const queryResult = await ordersRepository.getOrders(10, { offerer: MOCK_ORDER_2.offerer })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders).toHaveLength(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})
Expand All @@ -186,7 +186,7 @@ describe('OrdersRepository getOrders test', () => {

it('should successfully get orders given an orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, { orderStatus: ORDER_STATUS.OPEN })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders).toHaveLength(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
Expand All @@ -198,7 +198,7 @@ describe('OrdersRepository getOrders test', () => {

it('should successfully get orders given an chainId', async () => {
const queryResult = await ordersRepository.getOrders(10, { chainId: 137 })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders).toHaveLength(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})
Expand All @@ -210,7 +210,7 @@ describe('OrdersRepository getOrders test', () => {

it('should successfully get orders given an chainId and filler', async () => {
const queryResult = await ordersRepository.getOrders(10, { chainId: 137, filler: '0x1' })
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})

Expand All @@ -221,7 +221,7 @@ describe('OrdersRepository getOrders test', () => {

it('should successfully get orders given an chainId and orderStatus', async () => {
const queryResult = await ordersRepository.getOrders(10, { chainId: 1, orderStatus: ORDER_STATUS.OPEN })
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
})

Expand All @@ -236,18 +236,18 @@ describe('OrdersRepository getOrders test', () => {
orderStatus: ORDER_STATUS.FILLED,
filler: '0x3',
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})

it('should return no orders for chainId and orderStatus', async () => {
it('should return no orders for chainId, orderStatus, filler, when none match', async () => {
const orders = await ordersRepository.getOrders(10, { chainId: 137, orderStatus: ORDER_STATUS.OPEN, filler: '0x3' })
expect(orders.orders).toEqual([])
})

it('should successfully get orders given a filler', async () => {
const queryResult = await ordersRepository.getOrders(10, { filler: ADDITIONAL_FIELDS_ORDER_1.filler })
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders).toHaveLength(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
Expand All @@ -262,7 +262,7 @@ describe('OrdersRepository getOrders test', () => {
filler: ADDITIONAL_FIELDS_ORDER_1.filler,
orderStatus: ORDER_STATUS.OPEN,
})
expect(queryResult.orders.length).toEqual(2)
expect(queryResult.orders).toHaveLength(2)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})
Expand All @@ -280,7 +280,7 @@ describe('OrdersRepository getOrders test', () => {
filler: ADDITIONAL_FIELDS_ORDER_3.filler,
offerer: ADDITIONAL_FIELDS_ORDER_3.offerer,
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
})

Expand All @@ -298,7 +298,7 @@ describe('OrdersRepository getOrders test', () => {
offerer: ADDITIONAL_FIELDS_ORDER_3.offerer,
orderStatus: ADDITIONAL_FIELDS_ORDER_3.orderStatus,
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0].orderHash).toEqual(MOCK_ORDER_3.orderHash)
expect((queryResult.orders[0] as any).filler_offerer_orderStatus).toEqual(
`${ADDITIONAL_FIELDS_ORDER_3.filler}_${MOCK_ORDER_3.offerer}_${MOCK_ORDER_3.orderStatus}`
Expand All @@ -319,7 +319,7 @@ describe('OrdersRepository getOrders test', () => {
orderStatus: ORDER_STATUS.OPEN,
offerer: MOCK_ORDER_2.offerer,
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(
expect.objectContaining({
...MOCK_ORDER_2,
Expand Down Expand Up @@ -358,48 +358,48 @@ describe('OrdersRepository getOrders test', () => {
describe('OrdersRepository getOrders test with pagination', () => {
it('should successfully page through orders with offerer', async () => {
let orders = await ordersRepository.getOrders(1, { offerer: 'riley.eth' })
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
orders = await ordersRepository.getOrders(2, { offerer: 'riley.eth' }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
expect(orders.cursor).toEqual(undefined)
})

it('should successfully page through orders with orderStatus', async () => {
let orders = await ordersRepository.getOrders(1, { orderStatus: ORDER_STATUS.OPEN })
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(orders.cursor).toEqual(undefined)
})

it('should successfully page through orders with chainId', async () => {
let orders = await ordersRepository.getOrders(1, { chainId: 137 })
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
orders = await ordersRepository.getOrders(2, { chainId: 137 }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_3))
expect(orders.cursor).toEqual(undefined)
})

it('should successfully page through orders with chainId, orderStatus', async () => {
await ordersRepository.putOrderAndUpdateNonceTransaction(ADDITIONAL_FIELDS_ORDER_5 as UniswapXOrderEntity)
let orders = await ordersRepository.getOrders(1, { orderStatus: ORDER_STATUS.OPEN, chainId: 1 })
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN, chainId: 1 }, orders.cursor)
expect(orders.orders.length).toEqual(1)
expect(orders.orders).toHaveLength(1)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(orders.cursor).toEqual(undefined)
})

it('should throw an Error for cursor with the wrong index', async () => {
const orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN })
expect(orders.orders.length).toEqual(2)
expect(orders.orders).toHaveLength(2)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
expect(orders.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
await expect(() => ordersRepository.getOrders(0, { offerer: 'riley.eth' }, orders.cursor)).rejects.toThrow(
Expand All @@ -409,7 +409,7 @@ describe('OrdersRepository getOrders test with pagination', () => {

it('should throw an Error for cursor with the wrong cursor', async () => {
const orders = await ordersRepository.getOrders(2, { orderStatus: ORDER_STATUS.OPEN })
expect(orders.orders.length).toEqual(2)
expect(orders.orders).toHaveLength(2)
expect(orders.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
expect(orders.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
await expect(() => ordersRepository.getOrders(0, { offerer: 'riley.eth' }, 'wrong_cursor')).rejects.toThrow(
Expand All @@ -425,7 +425,7 @@ describe('OrdersRepository getOrders test with sorting', () => {
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'lte(2)',
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})

Expand All @@ -444,7 +444,7 @@ describe('OrdersRepository getOrders test with sorting', () => {
sortKey: SORT_FIELDS.CREATED_AT,
sort: 'gte(2)',
})
expect(queryResult.orders.length).toEqual(1)
expect(queryResult.orders).toHaveLength(1)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
})

Expand All @@ -464,7 +464,7 @@ describe('OrdersRepository getOrders test with sorting', () => {
sort: 'between(1,3)',
desc: true,
})
expect(queryResult.orders.length).toEqual(3)
expect(queryResult.orders).toHaveLength(3)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_2))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[2]).toEqual(expect.objectContaining(MOCK_ORDER_5))
Expand All @@ -477,7 +477,7 @@ describe('OrdersRepository getOrders test with sorting', () => {
sort: 'between(1,3)',
desc: false,
})
expect(queryResult.orders.length).toEqual(3)
expect(queryResult.orders).toHaveLength(3)
expect(queryResult.orders[0]).toEqual(expect.objectContaining(MOCK_ORDER_5))
expect(queryResult.orders[1]).toEqual(expect.objectContaining(MOCK_ORDER_1))
expect(queryResult.orders[2]).toEqual(expect.objectContaining(MOCK_ORDER_2))
Expand Down
Loading