Skip to content

Commit 1c3a401

Browse files
fix: only log user settled amounts (#330)
* only log user outputs * add unit test * unit test
1 parent ac167d2 commit 1c3a401

File tree

3 files changed

+90
-32
lines changed

3 files changed

+90
-32
lines changed

lib/entities/Order.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export type OrderOutput = {
2323
}
2424

2525
export type SettledAmount = {
26-
tokenOut?: string
27-
amountOut?: string
28-
tokenIn?: string
29-
amountIn?: string
26+
tokenOut: string
27+
amountOut: string
28+
tokenIn: string
29+
amountIn: string
3030
}
3131

3232
export type OrderEntity = {

lib/handlers/check-order-status/handler.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DutchOrder, FillInfo, OrderValidation } from '@uniswap/uniswapx-sdk'
2+
23
import { Unit } from 'aws-embedded-metrics'
34
import { default as Logger } from 'bunyan'
45
import { ethers } from 'ethers'
@@ -95,7 +96,7 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler<ContainerInjected,
9596
gasCostInETH,
9697
receipt.effectiveGasPrice.toString(),
9798
receipt.gasUsed.toString(),
98-
settledAmounts
99+
settledAmounts.reduce((prev, cur) => (prev && prev.amountOut > cur.amountOut ? prev : cur))
99100
)
100101

101102
const percentDecayed = (timestamp - order.decayStartTime) / (order.decayEndTime - order.decayStartTime)
@@ -197,7 +198,7 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler<ContainerInjected,
197198
gasCostInETH,
198199
receipt.effectiveGasPrice.toString(),
199200
receipt.gasUsed.toString(),
200-
settledAmounts
201+
settledAmounts.reduce((prev, cur) => (prev && prev.amountOut > cur.amountOut ? prev : cur))
201202
)
202203

203204
const percentDecayed =
@@ -374,37 +375,35 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler<ContainerInjected,
374375
gasCostInETH: string,
375376
gasPriceWei: string,
376377
gasUsed: string,
377-
settledAmounts: SettledAmount[]
378+
userAmount: SettledAmount
378379
): void {
379-
settledAmounts.forEach((settledAmount) => {
380-
log.info({
381-
orderInfo: {
382-
orderStatus: ORDER_STATUS.FILLED,
383-
orderHash: fill.orderHash,
384-
quoteId: quoteId,
385-
filler: fill.filler,
386-
nonce: fill.nonce.toString(),
387-
offerer: fill.swapper,
388-
tokenIn: settledAmount.tokenIn,
389-
amountIn: settledAmount.amountIn,
390-
tokenOut: settledAmount.tokenOut,
391-
amountOut: settledAmount.amountOut,
392-
blockNumber: fill.blockNumber,
393-
txHash: fill.txHash,
394-
fillTimestamp: timestamp,
395-
gasPriceWei: gasPriceWei,
396-
gasUsed: gasUsed,
397-
gasCostInETH: gasCostInETH,
398-
logTime: Math.floor(Date.now() / 1000).toString(),
399-
},
400-
})
380+
log.info({
381+
orderInfo: {
382+
orderStatus: ORDER_STATUS.FILLED,
383+
orderHash: fill.orderHash,
384+
quoteId: quoteId,
385+
filler: fill.filler,
386+
nonce: fill.nonce.toString(),
387+
offerer: fill.swapper,
388+
tokenIn: userAmount.tokenIn,
389+
amountIn: userAmount.amountIn,
390+
tokenOut: userAmount.tokenOut,
391+
amountOut: userAmount.amountOut,
392+
blockNumber: fill.blockNumber,
393+
txHash: fill.txHash,
394+
fillTimestamp: timestamp,
395+
gasPriceWei: gasPriceWei,
396+
gasUsed: gasUsed,
397+
gasCostInETH: gasCostInETH,
398+
logTime: Math.floor(Date.now() / 1000).toString(),
399+
},
401400
})
402401
}
403402

404403
public getSettledAmounts(fill: FillInfo, fillTimestamp: number, parsedOrder: DutchOrder): SettledAmount[] {
405404
const nativeOutputs = parsedOrder.info.outputs.filter((output) => output.token.toLowerCase() === NATIVE_ADDRESS)
406405
const settledAmounts: SettledAmount[] = []
407-
let amountIn: string | undefined
406+
let amountIn: string
408407
if (parsedOrder.info.input.endAmount.eq(parsedOrder.info.input.startAmount)) {
409408
// If the order is EXACT_INPUT then the input will not decay and resolves to the startAmount/endAmount.
410409
amountIn = parsedOrder.info.input.startAmount.toString()
@@ -428,10 +427,12 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler<ContainerInjected,
428427
} else {
429428
// If the order is EXACT_OUTPUT we will have all the ERC20 transfers in the fill logs,
430429
// only log the amountIn that matches the order input token.
430+
431+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
431432
const input = fill.inputs.find(
432433
(input) => input.token.toLowerCase() === parsedOrder.info.input.token.toLowerCase()
433-
)
434-
amountIn = input?.amount.toString()
434+
)!
435+
amountIn = input.amount.toString()
435436

436437
// Add all the native outputs to the settledAmounts as they are not included in the fill logs.
437438
// The amount is just the startAmount because the order is EXACT_OUTPUT so there is no decay on the outputs.

test/handlers/check-order-status.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('Testing check order status handler', () => {
5555
const getByHashMock = jest.fn().mockReturnValue(MOCK_ORDER_ENTITY)
5656
const updateOrderStatusMock = jest.fn().mockReturnValue(Promise<void>)
5757
const providerMock = jest.fn().mockReturnValue(mockedBlockNumber)
58+
const getTransactionMock = jest.fn()
5859

5960
const buildInjectorPromiseMock = (retryCount: number, orderStatus: string) => {
6061
return {
@@ -82,6 +83,10 @@ describe('Testing check order status handler', () => {
8283
},
8384
provider: {
8485
getBlockNumber: providerMock,
86+
getTransaction: getTransactionMock,
87+
getBlock: () => Promise.resolve({
88+
timestamp: 123456
89+
})
8590
},
8691
}
8792
},
@@ -154,6 +159,58 @@ describe('Testing check order status handler', () => {
154159
})
155160
})
156161

162+
it('should check fill events when order expired', async () => {
163+
const checkorderStatusHandler = new CheckOrderStatusHandler('check-order-status', initialInjectorPromiseMock)
164+
validateMock.mockReturnValue(OrderValidation.Expired)
165+
getTransactionMock.mockReturnValueOnce({
166+
wait: () => Promise.resolve({
167+
effectiveGasPrice: BigNumber.from(1),
168+
gasUsed: 100
169+
})
170+
})
171+
getFillInfoMock.mockReturnValue([{
172+
orderHash: MOCK_ORDER_HASH,
173+
filler: '0x123',
174+
nonce: BigNumber.from(1),
175+
swapper: '0x123',
176+
blockNumber: 12321312313,
177+
txHash: '0x1244345323',
178+
inputs: [{token: 'USDC', amount: BigNumber.from(100)}],
179+
outputs: [{token: 'WETH', amount: BigNumber.from(1)}],
180+
}])
181+
182+
expect(await checkorderStatusHandler.handler(handlerEventMock)).toMatchObject({
183+
orderStatus: ORDER_STATUS.FILLED,
184+
})
185+
expect(getFillInfoMock).toBeCalled()
186+
})
187+
188+
it('should check fill events when nonceUsed', async () => {
189+
const checkorderStatusHandler = new CheckOrderStatusHandler('check-order-status', initialInjectorPromiseMock)
190+
validateMock.mockReturnValue(OrderValidation.NonceUsed)
191+
getTransactionMock.mockReturnValueOnce({
192+
wait: () => Promise.resolve({
193+
effectiveGasPrice: BigNumber.from(1),
194+
gasUsed: 100
195+
})
196+
})
197+
getFillInfoMock.mockReturnValue([{
198+
orderHash: MOCK_ORDER_HASH,
199+
filler: '0x123',
200+
nonce: BigNumber.from(1),
201+
swapper: '0x123',
202+
blockNumber: 12321312313,
203+
txHash: '0x1244345323',
204+
inputs: [{token: 'USDC', amount: BigNumber.from(100)}],
205+
outputs: [{token: 'WETH', amount: BigNumber.from(1)}],
206+
}])
207+
208+
expect(await checkorderStatusHandler.handler(handlerEventMock)).toMatchObject({
209+
orderStatus: ORDER_STATUS.FILLED,
210+
})
211+
expect(getFillInfoMock).toBeCalled()
212+
})
213+
157214
it('should return insufficient-funds order status', async () => {
158215
const checkorderStatusHandler = new CheckOrderStatusHandler('check-order-status', initialInjectorPromiseMock)
159216
validateMock.mockReturnValue(OrderValidation.InsufficientFunds)

0 commit comments

Comments
 (0)