Skip to content

Commit

Permalink
chore: overlap fill block check (#498)
Browse files Browse the repository at this point in the history
overlap fill block check
  • Loading branch information
ConjunctiveNormalForm authored Dec 12, 2024
1 parent bc89286 commit 56fcc61
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
52 changes: 28 additions & 24 deletions lib/handlers/check-order-status/fill-event-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export type ProcessFillEventRequest = {
startingBlockNumber: number
settledAmounts: SettledAmount[]
quoteId?: string
tx: ethers.providers.TransactionResponse
block: ethers.providers.Block
tx?: ethers.providers.TransactionResponse
block?: ethers.providers.Block
timestamp: number
}
export class FillEventLogger {
Expand All @@ -40,32 +40,36 @@ export class FillEventLogger {
block,
timestamp,
}: ProcessFillEventRequest): Promise<SettledAmount[]> {
const receipt = await tx.wait()
const gasCostInETH = ethers.utils.formatEther(receipt.effectiveGasPrice.mul(receipt.gasUsed))
if (tx && block) {
const receipt = await tx.wait()
const gasCostInETH = ethers.utils.formatEther(receipt.effectiveGasPrice.mul(receipt.gasUsed))

this.analyticsService.logFillInfo(
fillEvent,
order,
quoteId,
timestamp,
gasCostInETH,
receipt.effectiveGasPrice.toString(),
receipt.gasUsed.toString(),
receipt.effectiveGasPrice.sub(block.baseFeePerGas ?? 0).toString(),
settledAmounts.reduce((prev, cur) => (prev && BigNumber.from(prev.amountOut).gt(cur.amountOut) ? prev : cur))
)
this.analyticsService.logFillInfo(
fillEvent,
order,
quoteId,
timestamp,
gasCostInETH,
receipt.effectiveGasPrice.toString(),
receipt.gasUsed.toString(),
receipt.effectiveGasPrice.sub(block.baseFeePerGas ?? 0).toString(),
settledAmounts.reduce((prev, cur) => (prev && BigNumber.from(prev.amountOut).gt(cur.amountOut) ? prev : cur))
)

if (order.type === OrderType.Dutch || order.type === OrderType.Dutch_V2) {
const percentDecayed = this.calculatePercentDecayed(order, timestamp)
metrics.putMetric(`OrderSfn-PercentDecayedUntilFill-chain-${chainId}`, percentDecayed, Unit.Percent)
}
if (order.type === OrderType.Dutch || order.type === OrderType.Dutch_V2) {
const percentDecayed = this.calculatePercentDecayed(order, timestamp)
metrics.putMetric(`OrderSfn-PercentDecayedUntilFill-chain-${chainId}`, percentDecayed, Unit.Percent)
}

// blocks until fill is the number of blocks between the fill event and the starting block number (need to add back the look back blocks)
if (startingBlockNumber != 0) {
const blocksUntilFill = fillEvent.blockNumber - (startingBlockNumber + this.fillEventBlockLookback(chainId))
metrics.putMetric(`OrderSfn-BlocksUntilFill-chain-${chainId}`, blocksUntilFill, Unit.Count)
// blocks until fill is the number of blocks between the fill event and the starting block number (need to add back the look back blocks)
if (startingBlockNumber != 0) {
const blocksUntilFill = fillEvent.blockNumber - (startingBlockNumber + this.fillEventBlockLookback(chainId))
metrics.putMetric(`OrderSfn-BlocksUntilFill-chain-${chainId}`, blocksUntilFill, Unit.Count)
}
return settledAmounts
} else {
return []
}
return settledAmounts
}

private calculatePercentDecayed(order: DutchV1OrderEntity | DutchV2OrderEntity, timestamp: number): number {
Expand Down
76 changes: 46 additions & 30 deletions lib/handlers/check-order-status/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { SfnStateInputOutput } from '../base'
import { FillEventLogger } from './fill-event-logger'
import { getSettledAmounts, IS_TERMINAL_STATE } from './util'

const FILL_CHECK_OVERLAP_BLOCK = 20

export type CheckOrderStatusRequest = {
chainId: number
orderHash: string
Expand Down Expand Up @@ -123,39 +125,53 @@ export class CheckOrderStatusService {
// so check for a fillEvent
// if no fill event, process in the unfilled path
if (validation === OrderValidation.NonceUsed || validation === OrderValidation.Expired) {
const fillEvent = await this.getFillEventForOrder(orderHash, fromBlock, curBlockNumber, orderWatcher)
const fillEvent = await this.getFillEventForOrder(
orderHash,
fromBlock - FILL_CHECK_OVERLAP_BLOCK,
curBlockNumber,
orderWatcher
)
if (fillEvent) {
const [tx, block] = await Promise.all([
provider.getTransaction(fillEvent.txHash),
provider.getBlock(fillEvent.blockNumber),
])
const settledAmounts = getSettledAmounts(
fillEvent,
{
timestamp: block.timestamp,
gasPrice: tx.gasPrice,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
maxFeePerGas: tx.maxFeePerGas,
},
parsedOrder as DutchOrder | CosignedV2DutchOrder | CosignedV3DutchOrder | CosignedPriorityOrder
)
try {
const [tx, block] = await Promise.all([
provider.getTransaction(fillEvent.txHash),
provider.getBlock(fillEvent.blockNumber),
])
const settledAmounts = getSettledAmounts(
fillEvent,
{
timestamp: block.timestamp,
gasPrice: tx.gasPrice,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
maxFeePerGas: tx.maxFeePerGas,
},
parsedOrder as DutchOrder | CosignedV2DutchOrder | CosignedV3DutchOrder | CosignedPriorityOrder
)

await this.fillEventLogger.processFillEvent({
fillEvent,
quoteId,
chainId,
startingBlockNumber,
order,
settledAmounts,
tx,
block,
timestamp: block.timestamp,
})
await this.fillEventLogger.processFillEvent({
fillEvent,
quoteId,
chainId,
startingBlockNumber,
order,
settledAmounts,
tx,
block,
timestamp: block.timestamp,
})

extraUpdateInfo = {
orderStatus: ORDER_STATUS.FILLED,
txHash: fillEvent.txHash,
settledAmounts,
extraUpdateInfo = {
orderStatus: ORDER_STATUS.FILLED,
txHash: fillEvent.txHash,
settledAmounts,
}
} catch (e) {
log.error('error processing fill event', { error: e })
extraUpdateInfo = {
orderStatus: ORDER_STATUS.FILLED,
txHash: '',
settledAmounts: [],
}
}
}
}
Expand Down

0 comments on commit 56fcc61

Please sign in to comment.