diff --git a/lib/entities/HardQuoteRequest.ts b/lib/entities/HardQuoteRequest.ts index 28f5366..2a50afb 100644 --- a/lib/entities/HardQuoteRequest.ts +++ b/lib/entities/HardQuoteRequest.ts @@ -1,5 +1,5 @@ import { TradeType } from '@uniswap/sdk-core'; -import { UnsignedV2DutchOrder } from '@uniswap/uniswapx-sdk'; +import { OrderType, UnsignedV2DutchOrder, UnsignedV3DutchOrder, V3DutchOrderBuilder } from '@uniswap/uniswapx-sdk'; import { BigNumber, ethers, utils } from 'ethers'; import { v4 as uuidv4 } from 'uuid'; @@ -11,16 +11,23 @@ export class HardQuoteRequest { public order: UnsignedV2DutchOrder; private data: HardQuoteRequestBody; - public static fromHardRequestBody(body: HardQuoteRequestBody): HardQuoteRequest { - return new HardQuoteRequest(body); + public static fromHardRequestBody(body: HardQuoteRequestBody, orderType: OrderType): HardQuoteRequest { + return new HardQuoteRequest(body, orderType); } - constructor(_data: HardQuoteRequestBody) { + constructor(_data: HardQuoteRequestBody, orderType: OrderType) { this.data = { ..._data, requestId: _data.quoteId ?? uuidv4(), }; - this.order = UnsignedV2DutchOrder.parse(_data.encodedInnerOrder, _data.tokenInChainId); + if (orderType === OrderType.Dutch_V2) { + this.order = UnsignedV2DutchOrder.parse(_data.encodedInnerOrder, _data.tokenInChainId); + // } else if (orderType === OrderType.Dutch_V3) { + // this.order = UnsignedV3DutchOrder.parse(_data.encodedInnerOrder, _data.tokenInChainId); + // } + } else { + throw new Error('Unsupported order type'); + } } public toCleanJSON(): QuoteRequestDataJSON { @@ -110,9 +117,20 @@ export class HardQuoteRequest { } public get type(): TradeType { - return this.order.info.input.startAmount.eq(this.order.info.input.endAmount) - ? TradeType.EXACT_INPUT - : TradeType.EXACT_OUTPUT; + if (this.order instanceof UnsignedV2DutchOrder) { + return this.order.info.input.startAmount.eq(this.order.info.input.endAmount) + ? TradeType.EXACT_INPUT + : TradeType.EXACT_OUTPUT + } + // else if (this.order instanceof UnsignedV3DutchOrder) { + // const startAmount = this.order.info.input.startAmount; + // return startAmount.eq(V3DutchOrderBuilder.getMinAmountOut(startAmount, this.order.info.input.curve.relativeAmounts)) + // ? TradeType.EXACT_INPUT + // : TradeType.EXACT_OUTPUT + // } + else { + throw new Error('Unsupported order type'); + } } public get numOutputs(): number { diff --git a/lib/handlers/hard-quote/handler.ts b/lib/handlers/hard-quote/handler.ts index 61b631c..deeebf8 100644 --- a/lib/handlers/hard-quote/handler.ts +++ b/lib/handlers/hard-quote/handler.ts @@ -2,7 +2,7 @@ import { KMSClient } from '@aws-sdk/client-kms'; import { TradeType } from '@uniswap/sdk-core'; import { KmsSigner } from '@uniswap/signer'; import { MetricLoggerUnit } from '@uniswap/smart-order-router'; -import { CosignedV2DutchOrder, CosignerData } from '@uniswap/uniswapx-sdk'; +import { CosignedV2DutchOrder, CosignerData, OrderType, UniswapXOrderParser, V3CosignerData } from '@uniswap/uniswapx-sdk'; import { BigNumber, ethers } from 'ethers'; import Joi from 'joi'; @@ -45,8 +45,19 @@ export class QuoteHandler extends APIGLambdaHandler< metric.putMetric(Metric.QUOTE_REQUESTED, 1, MetricLoggerUnit.Count); - const request = HardQuoteRequest.fromHardRequestBody(requestBody); + const orderParser = new UniswapXOrderParser(); + const orderType: OrderType = orderParser.getOrderTypeFromEncoded(requestBody.encodedInnerOrder, requestBody.tokenInChainId); + const request = HardQuoteRequest.fromHardRequestBody(requestBody, orderType); + + + // if (orderType == OrderType.Dutch_V2) { + + // } else if (orderType == OrderType.Dutch_V3) { + + // } else { + // throw new Error('Unsupported order type'); + // } // re-create KmsClient every call to avoid clock skew issue // https://github.com/aws/aws-sdk-js-v3/issues/6400 const kmsKeyId = checkDefined(process.env.KMS_KEY_ID, 'KMS_KEY_ID is not defined'); @@ -93,7 +104,7 @@ export class QuoteHandler extends APIGLambdaHandler< let cosignerData: CosignerData; if (bestQuote) { - cosignerData = getCosignerData(request, bestQuote); + cosignerData = getCosignerData(request, bestQuote, orderType); log.info({ bestQuote: bestQuote }, 'bestQuote'); } else { cosignerData = getDefaultCosignerData(request); @@ -162,40 +173,54 @@ export class QuoteHandler extends APIGLambdaHandler< } } -export function getCosignerData(request: HardQuoteRequest, quote: QuoteResponse): CosignerData { - const decayStartTime = getDecayStartTime(request.tokenInChainId); - // default to open order with the original prices - let filler = ethers.constants.AddressZero; - let inputOverride = BigNumber.from(0); - const outputOverrides = request.order.info.outputs.map(() => BigNumber.from(0)); +export function getCosignerData(request: HardQuoteRequest, quote: QuoteResponse, orderType: OrderType): CosignerData { + if (orderType === OrderType.Dutch_V2) { + const decayStartTime = getDecayStartTime(request.tokenInChainId); + // default to open order with the original prices + let filler = ethers.constants.AddressZero; + let inputOverride = BigNumber.from(0); + const outputOverrides = request.order.info.outputs.map(() => BigNumber.from(0)); - // if the quote is better, then increase amounts by the difference - if (request.type === TradeType.EXACT_INPUT) { - if (quote.amountOut.gt(request.totalOutputAmountStart)) { - const increase = quote.amountOut.sub(request.totalOutputAmountStart); - // give all the increase to the first (swapper) output - outputOverrides[0] = request.order.info.outputs[0].startAmount.add(increase); - if (quote.filler) { - filler = quote.filler; + // if the quote is better, then increase amounts by the difference + if (request.type === TradeType.EXACT_INPUT) { + if (quote.amountOut.gt(request.totalOutputAmountStart)) { + const increase = quote.amountOut.sub(request.totalOutputAmountStart); + // give all the increase to the first (swapper) output + outputOverrides[0] = request.order.info.outputs[0].startAmount.add(increase); + if (quote.filler) { + filler = quote.filler; + } } - } - } else { - if (quote.amountIn.lt(request.totalInputAmountStart)) { - inputOverride = quote.amountIn; - if (quote.filler) { - filler = quote.filler; + } else { + if (quote.amountIn.lt(request.totalInputAmountStart)) { + inputOverride = quote.amountIn; + if (quote.filler) { + filler = quote.filler; + } } } - } - return { - decayStartTime: decayStartTime, - decayEndTime: getDecayEndTime(request.tokenInChainId, decayStartTime), - exclusiveFiller: filler, - exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS, - inputOverride: inputOverride, - outputOverrides: outputOverrides, - }; + return { + decayStartTime: decayStartTime, + decayEndTime: getDecayEndTime(request.tokenInChainId, decayStartTime), + exclusiveFiller: filler, + exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS, + inputOverride: inputOverride, + outputOverrides: outputOverrides, + }; + } + // else if (orderType == OrderType.Dutch_V3) { + // return { + // decayStartBlock: 1, + // exclusiveFiller: ethers.constants.AddressZero, + // exclusivityOverrideBps: BigNumber.from(0), + // inputOverride: BigNumber.from(0), + // outputOverrides: request.order.info.outputs.map(() => BigNumber.from(0)), + // } + // } + else { + throw new Error('Unsupported order type'); + } } export function getDefaultCosignerData(request: HardQuoteRequest): CosignerData { diff --git a/test/entities/HardQuoteRequest.test.ts b/test/entities/HardQuoteRequest.test.ts index 26c124c..3f3f24c 100644 --- a/test/entities/HardQuoteRequest.test.ts +++ b/test/entities/HardQuoteRequest.test.ts @@ -1,5 +1,5 @@ import { TradeType } from '@uniswap/sdk-core'; -import { UnsignedV2DutchOrder, UnsignedV2DutchOrderInfo } from '@uniswap/uniswapx-sdk'; +import { OrderType, UnsignedV2DutchOrder, UnsignedV2DutchOrderInfo } from '@uniswap/uniswapx-sdk'; import { BigNumber, ethers } from 'ethers'; import { HardQuoteRequest } from '../../lib/entities'; @@ -55,7 +55,8 @@ const makeRequest = (data: Partial): HardQuoteRequest => { innerSig: '0x', }, data - ) + ), + OrderType.Dutch_V2 ); }; diff --git a/test/entities/HardQuoteResponse.test.ts b/test/entities/HardQuoteResponse.test.ts index 4489792..7b132f1 100644 --- a/test/entities/HardQuoteResponse.test.ts +++ b/test/entities/HardQuoteResponse.test.ts @@ -1,6 +1,7 @@ import { CosignedV2DutchOrder, CosignerData, + OrderType, UnsignedV2DutchOrder, UnsignedV2DutchOrderInfo, } from '@uniswap/uniswapx-sdk'; @@ -52,7 +53,7 @@ describe('HardQuoteResponse', () => { cosignerData, ethers.utils.joinSignature(cosignature) ); - return new V2HardQuoteResponse(new HardQuoteRequest(await getRequest(unsigned)), order); + return new V2HardQuoteResponse(new HardQuoteRequest(await getRequest(unsigned), OrderType.Dutch_V2), order); }; it('toResponseJSON', async () => {