diff --git a/lib/constants.ts b/lib/constants.ts index d5b2952..66f57ad 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -37,3 +37,4 @@ export const POST_ORDER_ERROR_REASON = { export const WEBHOOK_TIMEOUT_MS = 500; export const NOTIFICATION_TIMEOUT_MS = 10; +export const V3_BLOCK_BUFFER = 4; diff --git a/lib/handlers/hard-quote/handler.ts b/lib/handlers/hard-quote/handler.ts index a945681..d5c20da 100644 --- a/lib/handlers/hard-quote/handler.ts +++ b/lib/handlers/hard-quote/handler.ts @@ -15,7 +15,7 @@ import { import { BigNumber, ethers } from 'ethers'; import Joi from 'joi'; -import { POST_ORDER_ERROR_REASON } from '../../constants'; +import { POST_ORDER_ERROR_REASON, V3_BLOCK_BUFFER } from '../../constants'; import { HardQuoteRequest, Metric, QuoteResponse } from '../../entities'; import { V2HardQuoteResponse } from '../../entities/V2HardQuoteResponse'; import { V3HardQuoteResponse } from '../../entities/V3HardQuoteResponse'; @@ -48,7 +48,7 @@ export class QuoteHandler extends APIGLambdaHandler< ): Promise> { const { requestInjected: { log, metric }, - containerInjected: { quoters, orderServiceProvider }, + containerInjected: { quoters, orderServiceProvider, provider }, requestBody, } = params; const start = Date.now(); @@ -109,7 +109,7 @@ export class QuoteHandler extends APIGLambdaHandler< cosignerData = getCosignerData(request, bestQuote, orderType); log.info({ bestQuote: bestQuote }, 'bestQuote'); } else { - cosignerData = getDefaultCosignerData(request, orderType); + cosignerData = await getDefaultCosignerData(request, orderType, provider); log.info({ cosignerData: cosignerData }, 'open order with default cosignerData'); } @@ -221,12 +221,16 @@ export function getCosignerData( throw new Error('Unsupported order type'); } } -export function getDefaultCosignerData(request: HardQuoteRequest, orderType: OrderType): CosignerData | V3CosignerData { +export async function getDefaultCosignerData( + request: HardQuoteRequest, + orderType: OrderType, + provider: ethers.providers.Provider +): Promise { switch (orderType) { case OrderType.Dutch_V2: return getDefaultV2CosignerData(request); case OrderType.Dutch_V3: - return getDefaultV3CosignerData(request); + return await getDefaultV3CosignerData(request, provider); default: throw new Error('Unsupported order type'); } @@ -306,9 +310,11 @@ function getDefaultV2CosignerData(request: HardQuoteRequest): CosignerData { }; } -function getDefaultV3CosignerData(request: HardQuoteRequest): V3CosignerData { +async function getDefaultV3CosignerData(request: HardQuoteRequest, provider: ethers.providers.Provider): Promise { + const currentBlock = await provider.getBlockNumber(); + return { - decayStartBlock: 1, + decayStartBlock: currentBlock + V3_BLOCK_BUFFER, exclusiveFiller: ethers.constants.AddressZero, exclusivityOverrideBps: BigNumber.from(0), inputOverride: BigNumber.from(0), diff --git a/lib/handlers/hard-quote/injector.ts b/lib/handlers/hard-quote/injector.ts index 43a0197..0978ce5 100644 --- a/lib/handlers/hard-quote/injector.ts +++ b/lib/handlers/hard-quote/injector.ts @@ -17,11 +17,13 @@ import { DynamoFillerAddressRepository } from '../../repositories/filler-address import { STAGE } from '../../util/stage'; import { ApiInjector, ApiRInj } from '../base/api-handler'; import { HardQuoteRequestBody } from './schema'; +import { ethers } from 'ethers'; export interface ContainerInjected { quoters: Quoter[]; firehose: FirehoseLogger; orderServiceProvider: OrderServiceProvider; + provider: ethers.providers.Provider; } export interface RequestInjected extends ApiRInj { @@ -74,10 +76,16 @@ export class QuoteInjector extends ApiInjector