From d34d46b7fd68a5bb8944c08721c6db9f8a64b3ef Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 4 Sep 2020 09:54:47 -0500 Subject: [PATCH] change look up from order hash to order signature --- .../src/utils/quote_report_generator.ts | 73 ++++++++++--------- .../src/utils/quote_requestor.ts | 10 +-- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/packages/asset-swapper/src/utils/quote_report_generator.ts b/packages/asset-swapper/src/utils/quote_report_generator.ts index d4f870cdb9..4884540431 100644 --- a/packages/asset-swapper/src/utils/quote_report_generator.ts +++ b/packages/asset-swapper/src/utils/quote_report_generator.ts @@ -1,4 +1,3 @@ -import { orderHashUtils } from '@0x/order-utils'; import { SignedOrder } from '@0x/types'; import { BigNumber } from '@0x/utils'; import * as _ from 'lodash'; @@ -31,7 +30,6 @@ interface NativeReportSourceBase { liquiditySource: ERC20BridgeSource.Native; makerAmount: BigNumber; takerAmount: BigNumber; - orderHash: string; nativeOrder: SignedOrder; fillableTakerAmount: BigNumber; } @@ -53,17 +51,6 @@ export interface QuoteReport { sourcesDelivered: QuoteReportSource[]; } -const nativeOrderFromCollapsedFill = (cf: CollapsedFill): SignedOrder | undefined => { - // Cast as NativeCollapsedFill and then check - // if it really is a NativeCollapsedFill - const possibleNativeCollapsedFill = cf as NativeCollapsedFill; - if (possibleNativeCollapsedFill.fillData && possibleNativeCollapsedFill.fillData.order) { - return possibleNativeCollapsedFill.fillData.order; - } else { - return undefined; - } -}; - /** * Generates a report of sources considered while computing the optimized * swap quote, and the sources ultimately included in the computed quote. @@ -77,23 +64,9 @@ export function generateQuoteReport( liquidityDelivered: CollapsedFill[] | DexSample, quoteRequestor?: QuoteRequestor, ): QuoteReport { - // convert order fillable amount array to easy to look up hash - if (orderFillableAmounts.length !== nativeOrders.length) { - // length mismatch, abort - throw new Error('orderFillableAmounts must be the same length as nativeOrders'); - } - const orderHashesToFillableAmounts: { [orderHash: string]: BigNumber } = {}; - nativeOrders.forEach((nativeOrder, idx) => { - orderHashesToFillableAmounts[orderHashUtils.getOrderHash(nativeOrder)] = orderFillableAmounts[idx]; - }); - const dexReportSourcesConsidered = dexQuotes.map(quote => _dexSampleToReportSource(quote, marketOperation)); - const nativeOrderSourcesConsidered = nativeOrders.map(order => - _nativeOrderToReportSource( - order, - orderHashesToFillableAmounts[orderHashUtils.getOrderHash(order)], - quoteRequestor, - ), + const nativeOrderSourcesConsidered = nativeOrders.map((order, idx) => + _nativeOrderToReportSource(order, orderFillableAmounts[idx], quoteRequestor), ); const multiHopSourcesConsidered = multiHopQuotes.map(quote => _multiHopSampleToReportSource(quote, marketOperation), @@ -106,12 +79,18 @@ export function generateQuoteReport( let sourcesDelivered; if (Array.isArray(liquidityDelivered)) { + // create easy way to look up fillable amounts + const nativeOrderSignaturesToFillableAmounts = _nativeOrderSignaturesToFillableAmounts( + nativeOrders, + orderFillableAmounts, + ); + // map sources delivered sourcesDelivered = liquidityDelivered.map(collapsedFill => { - const foundNativeOrder = nativeOrderFromCollapsedFill(collapsedFill); + const foundNativeOrder = _nativeOrderFromCollapsedFill(collapsedFill); if (foundNativeOrder) { return _nativeOrderToReportSource( foundNativeOrder, - orderHashesToFillableAmounts[orderHashUtils.getOrderHash(foundNativeOrder)], + nativeOrderSignaturesToFillableAmounts[foundNativeOrder.signature], quoteRequestor, ); } else { @@ -179,24 +158,48 @@ function _multiHopSampleToReportSource( } } +function _nativeOrderSignaturesToFillableAmounts( + nativeOrders: SignedOrder[], + fillableAmounts: BigNumber[], +): { [orderSignature: string]: BigNumber } { + // create easy way to look up fillable amounts based on native order signatures + if (fillableAmounts.length !== nativeOrders.length) { + // length mismatch, abort + throw new Error('orderFillableAmounts must be the same length as nativeOrders'); + } + const nativeOrderSignaturesToFillableAmounts: { [orderSignature: string]: BigNumber } = {}; + nativeOrders.forEach((nativeOrder, idx) => { + nativeOrderSignaturesToFillableAmounts[nativeOrder.signature] = fillableAmounts[idx]; + }); + return nativeOrderSignaturesToFillableAmounts; +} + +function _nativeOrderFromCollapsedFill(cf: CollapsedFill): SignedOrder | undefined { + // Cast as NativeCollapsedFill and then check + // if it really is a NativeCollapsedFill + const possibleNativeCollapsedFill = cf as NativeCollapsedFill; + if (possibleNativeCollapsedFill.fillData && possibleNativeCollapsedFill.fillData.order) { + return possibleNativeCollapsedFill.fillData.order; + } else { + return undefined; + } +} + function _nativeOrderToReportSource( nativeOrder: SignedOrder, fillableAmount: BigNumber, quoteRequestor?: QuoteRequestor, ): NativeRFQTReportSource | NativeOrderbookReportSource { - const orderHash = orderHashUtils.getOrderHash(nativeOrder); - const nativeOrderBase: NativeReportSourceBase = { liquiditySource: ERC20BridgeSource.Native, makerAmount: nativeOrder.makerAssetAmount, takerAmount: nativeOrder.takerAssetAmount, fillableTakerAmount: fillableAmount, nativeOrder, - orderHash, }; // if we find this is an rfqt order, label it as such and associate makerUri - const foundRfqtMakerUri = quoteRequestor && quoteRequestor.getMakerUriForOrderHash(orderHash); + const foundRfqtMakerUri = quoteRequestor && quoteRequestor.getMakerUriForOrderSignature(nativeOrder.signature); if (foundRfqtMakerUri) { const rfqtSource: NativeRFQTReportSource = { ...nativeOrderBase, diff --git a/packages/asset-swapper/src/utils/quote_requestor.ts b/packages/asset-swapper/src/utils/quote_requestor.ts index d39e9efdcf..ae964804ac 100644 --- a/packages/asset-swapper/src/utils/quote_requestor.ts +++ b/packages/asset-swapper/src/utils/quote_requestor.ts @@ -106,7 +106,7 @@ export type LogFunction = (obj: object, msg?: string, ...args: any[]) => void; export class QuoteRequestor { private readonly _schemaValidator: SchemaValidator = new SchemaValidator(); - private readonly _orderHashToMakerUri: { [orderHash: string]: string } = {}; + private readonly _orderSignatureToMakerUri: { [orderSignature: string]: string } = {}; constructor( private readonly _rfqtAssetOfferings: RfqtMakerAssetOfferings, @@ -196,7 +196,7 @@ export class QuoteRequestor { } // Store makerUri for looking up later - this._orderHashToMakerUri[orderHashUtils.getOrderHash(orderWithBigNumberInts)] = firmQuoteResponse.makerUri; + this._orderSignatureToMakerUri[orderWithBigNumberInts.signature] = firmQuoteResponse.makerUri; // Passed all validation, add it to result result.push({ signedOrder: orderWithBigNumberInts }); @@ -269,10 +269,10 @@ export class QuoteRequestor { } /** - * Given an order hash, returns the makerUri that the order originated from + * Given an order signature, returns the makerUri that the order originated from */ - public getMakerUriForOrderHash(orderHash: string): string | undefined { - return this._orderHashToMakerUri[orderHash]; + public getMakerUriForOrderSignature(orderSignature: string): string | undefined { + return this._orderSignatureToMakerUri[orderSignature]; } private _isValidRfqtIndicativeQuoteResponse(response: RFQTIndicativeQuote): boolean {