From 7fa151537359bd5f8b4819e56ca6ae37eefb76b4 Mon Sep 17 00:00:00 2001 From: Zach Yang Date: Fri, 20 Dec 2024 12:15:35 -0800 Subject: [PATCH] fix(order-tracking): sfn max attemmpts increase; sfn execution arn naming tweak (#501) * maxAttempts 4 -> 10 * retry to 100 * bump sfn stack version --- bin/definitions/order-tracking-sfn.json | 2 +- bin/stacks/step-function-stack.ts | 2 +- lib/handlers/check-order-status/handler.ts | 6 ++- lib/handlers/shared/sfn.ts | 13 +++--- lib/services/UniswapXOrderService.ts | 7 ++-- .../post-order/post-limit-order.test.ts | 3 +- .../services/UniswapXOrderService.test.ts | 42 ++++++++++++++----- test/unit/util/order-validator.test.ts | 14 +++---- 8 files changed, 56 insertions(+), 33 deletions(-) diff --git a/bin/definitions/order-tracking-sfn.json b/bin/definitions/order-tracking-sfn.json index 9d71fbfd..c8a9760a 100644 --- a/bin/definitions/order-tracking-sfn.json +++ b/bin/definitions/order-tracking-sfn.json @@ -12,7 +12,7 @@ "Lambda.SdkClientException" ], "IntervalSeconds": 2, - "MaxAttempts": 4, + "MaxAttempts": 100, "BackoffRate": 2 } ], diff --git a/bin/stacks/step-function-stack.ts b/bin/stacks/step-function-stack.ts index ddaf07ec..0a235348 100644 --- a/bin/stacks/step-function-stack.ts +++ b/bin/stacks/step-function-stack.ts @@ -48,7 +48,7 @@ export class StepFunctionStack extends cdk.NestedStack { }, timeout: Duration.seconds(60), environment: { - VERSION: '2', + VERSION: '3', NODE_OPTIONS: '--enable-source-maps', ...props.envVars, stage: stage, diff --git a/lib/handlers/check-order-status/handler.ts b/lib/handlers/check-order-status/handler.ts index 4dc30dea..fcb8af18 100644 --- a/lib/handlers/check-order-status/handler.ts +++ b/lib/handlers/check-order-status/handler.ts @@ -25,7 +25,8 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler { //make sure to change "Variable": "$.retryCount", in order-tracking-sfn.json to be 1+retryCount - if (input.requestInjected?.retryCount > 300) { + const retryCount = input.requestInjected?.retryCount ?? 0 + if (retryCount > 300) { const stateMachineArn = input.requestInjected.stateMachineArn await kickoffOrderTrackingSfn( { @@ -36,7 +37,8 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler { expect(validatorMock).toBeCalledWith(order) expect(kickoffOrderTrackingSfn).toHaveBeenCalledWith( expect.objectContaining({ orderType: 'Limit' }), - expect.anything() + expect.anything(), + 0 ) expect(postOrderResponse).toEqual({ body: JSON.stringify({ hash: expectedOrderEntity.orderHash }), diff --git a/test/unit/services/UniswapXOrderService.test.ts b/test/unit/services/UniswapXOrderService.test.ts index 884f2a6f..c94c4c3a 100644 --- a/test/unit/services/UniswapXOrderService.test.ts +++ b/test/unit/services/UniswapXOrderService.test.ts @@ -6,21 +6,21 @@ import { ORDER_STATUS, UniswapXOrderEntity } from '../../../lib/entities' import { OnChainValidatorMap } from '../../../lib/handlers/OnChainValidatorMap' import { kickoffOrderTrackingSfn } from '../../../lib/handlers/shared/sfn' import { DutchV1Order, DutchV2Order } from '../../../lib/models' +import { DutchV3Order } from '../../../lib/models/DutchV3Order' import { LimitOrder } from '../../../lib/models/LimitOrder' import { PriorityOrder } from '../../../lib/models/PriorityOrder' import { BaseOrdersRepository } from '../../../lib/repositories/base' import { AnalyticsService } from '../../../lib/services/analytics-service' import { UniswapXOrderService } from '../../../lib/services/UniswapXOrderService' +import { ChainId } from '../../../lib/util/chain' import { OffChainUniswapXOrderValidator } from '../../../lib/util/OffChainUniswapXOrderValidator' import { DUTCH_LIMIT } from '../../../lib/util/order' import { SDKDutchOrderFactory } from '../../factories/SDKDutchOrderV1Factory' import { SDKDutchOrderV2Factory } from '../../factories/SDKDutchOrderV2Factory' +import { SDKDutchOrderV3Factory } from '../../factories/SDKDutchOrderV3Factory' import { SDKPriorityOrderFactory } from '../../factories/SDKPriorityOrderFactory' import { QueryParamsBuilder } from '../builders/QueryParamsBuilder' import { COSIGNATURE, MOCK_PROVIDER_MAP } from '../fixtures' -import { DutchV3Order } from '../../../lib/models/DutchV3Order' -import { ChainId } from '../../../lib/util/chain' -import { SDKDutchOrderV3Factory } from '../../factories/SDKDutchOrderV3Factory' jest.mock('../../../lib/handlers/shared/sfn', () => { return { kickoffOrderTrackingSfn: jest.fn() } @@ -81,7 +81,8 @@ describe('UniswapXOrderService', () => { quoteId: '', stateMachineArn: undefined, }, - undefined + undefined, + 0 ) }) @@ -131,7 +132,8 @@ describe('UniswapXOrderService', () => { quoteId: '', stateMachineArn: undefined, }, - undefined + undefined, + 0 ) }) @@ -615,9 +617,10 @@ describe('UniswapXOrderService', () => { ) }) - test('getDutchV3Orders calls db with Dutch_V3', async () => { - const dutchV3Orders = [1, 2, 3].map(() => new DutchV3Order(SDKDutchOrderV3Factory.buildDutchV3Order(ChainId.ARBITRUM_ONE), '', ChainId.ARBITRUM_ONE)) + const dutchV3Orders = [1, 2, 3].map( + () => new DutchV3Order(SDKDutchOrderV3Factory.buildDutchV3Order(ChainId.ARBITRUM_ONE), '', ChainId.ARBITRUM_ONE) + ) const mockOrder = dutchV3Orders.map((o) => o.toEntity(ORDER_STATUS.OPEN)) const repository = mock>() repository.getOrdersFilteredByType.mockResolvedValue({ orders: mockOrder }) @@ -636,7 +639,12 @@ describe('UniswapXOrderService', () => { ) const limit = 50 - const params = new QueryParamsBuilder().withDesc().withSort().withSortKey().withChainId(ChainId.ARBITRUM_ONE).build() + const params = new QueryParamsBuilder() + .withDesc() + .withSort() + .withSortKey() + .withChainId(ChainId.ARBITRUM_ONE) + .build() const response = await service.getDutchV3Orders(limit, params, undefined) const expectedResponse = { orders: mockOrder.map((o) => DutchV3Order.fromEntity(o).toGetResponse()), @@ -655,7 +663,9 @@ describe('UniswapXOrderService', () => { }) test('getDutchV3Orders loops with empty response', async () => { - const dutchV3Orders = [1, 2, 3].map(() => new DutchV3Order(SDKDutchOrderV3Factory.buildDutchV3Order(ChainId.ARBITRUM_ONE), '', ChainId.ARBITRUM_ONE)) + const dutchV3Orders = [1, 2, 3].map( + () => new DutchV3Order(SDKDutchOrderV3Factory.buildDutchV3Order(ChainId.ARBITRUM_ONE), '', ChainId.ARBITRUM_ONE) + ) const mockOrder = dutchV3Orders.map((o) => o.toEntity(ORDER_STATUS.OPEN)) const repository = mock>() repository.getOrdersFilteredByType.mockResolvedValueOnce({ orders: [], cursor: 'cursor' }) @@ -675,7 +685,12 @@ describe('UniswapXOrderService', () => { ) const limit = 50 - const params = new QueryParamsBuilder().withDesc().withSort().withSortKey().withChainId(ChainId.ARBITRUM_ONE).build() + const params = new QueryParamsBuilder() + .withDesc() + .withSort() + .withSortKey() + .withChainId(ChainId.ARBITRUM_ONE) + .build() const response = await service.getDutchV3Orders(limit, params, undefined) const expectedResponse = { orders: mockOrder.map((o) => DutchV3Order.fromEntity(o).toGetResponse()), @@ -717,7 +732,12 @@ describe('UniswapXOrderService', () => { ) const limit = 50 - const params = new QueryParamsBuilder().withDesc().withSort().withSortKey().withChainId(ChainId.ARBITRUM_ONE).build() + const params = new QueryParamsBuilder() + .withDesc() + .withSort() + .withSortKey() + .withChainId(ChainId.ARBITRUM_ONE) + .build() const response = await service.getDutchV3Orders(limit, params, undefined) expect(response).toEqual({ orders: [], cursor: 'cursor' }) diff --git a/test/unit/util/order-validator.test.ts b/test/unit/util/order-validator.test.ts index e55907bc..1129e18c 100644 --- a/test/unit/util/order-validator.test.ts +++ b/test/unit/util/order-validator.test.ts @@ -348,18 +348,18 @@ describe('Testing off chain validation', () => { describe('Testing v3 order validation', () => { it('Should return valid', () => { const order = SDKDutchOrderV3Factory.buildDutchV3Order(ChainId.ARBITRUM_ONE, { - cosigner: process.env.LABS_COSIGNER + cosigner: process.env.LABS_COSIGNER, }) order.info.deadline = CURRENT_TIME + ONE_DAY const validationResp = validationProvider.validate(order) expect(validationResp).toEqual({ - valid: true + valid: true, }) }) it('Should throw invalid deadline', () => { const order = SDKDutchOrderV3Factory.buildDutchV3Order(ChainId.ARBITRUM_ONE, { - cosigner: process.env.LABS_COSIGNER + cosigner: process.env.LABS_COSIGNER, }) const validationResp = validationProvider.validate(order) expect(validationResp).toEqual({ @@ -376,7 +376,7 @@ describe('Testing off chain validation', () => { const validationResp = validationProvider.validate(order) expect(validationResp).toEqual({ valid: false, - errorString: 'Invalid cosigner: ValidationError: "value" must be [0x0000000000000000000000000000000000000000]', + errorString: expect.stringContaining('Invalid cosigner: ValidationError: "value" must be'), }) }) @@ -449,7 +449,7 @@ describe('Testing off chain validation', () => { relativeAmounts: [BigInt(1000000000000000000)], }, }, - ] + ], }) order.info.deadline = CURRENT_TIME + ONE_DAY // Set to be empty @@ -529,9 +529,9 @@ describe('Testing off chain validation', () => { outputs: [ { startAmount: BigNumber.from(100), - minAmount: BigNumber.from(100), + minAmount: BigNumber.from(100), }, - ] + ], }) // Override with less than the startAmount order.info.cosignerData.outputOverrides = [BigNumber.from('99')]