From a7a905de4c63ad4d8c01aadb27f1d2afa20ae1b3 Mon Sep 17 00:00:00 2001 From: Daniel Pyrathon Date: Wed, 13 Jan 2021 17:07:45 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20Upgrade=20quote=20server=20and=20Asset?= =?UTF-8?q?=20Swapper=20types=20(and=20specify=20protocol=20ve=E2=80=A6=20?= =?UTF-8?q?(#108)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Upgrade quote server and Asset Swapper types (and specify protocol version in API requests) * add package and lockfile * fix tests --- packages/asset-swapper/package.json | 2 +- packages/asset-swapper/src/index.ts | 2 +- .../src/utils/market_operation_utils/index.ts | 6 +++--- .../utils/market_operation_utils/orders.ts | 4 ++-- .../src/utils/market_operation_utils/types.ts | 4 ++-- .../src/utils/quote_requestor.ts | 19 +++++++++++-------- .../test/quote_requestor_test.ts | 3 +++ yarn.lock | 8 +++++--- 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/asset-swapper/package.json b/packages/asset-swapper/package.json index f9a3af179d..87fdd57d5c 100644 --- a/packages/asset-swapper/package.json +++ b/packages/asset-swapper/package.json @@ -65,7 +65,7 @@ "@0x/json-schemas": "^5.3.4", "@0x/order-utils": "^10.4.14", "@0x/orderbook": "0xProject/gitpkg-registry#0x-orderbook-v2.2.7-e10a81023", - "@0x/quote-server": "^3.1.0", + "@0x/quote-server": "^4.0.1", "@0x/types": "^3.3.1", "@0x/typescript-typings": "^5.1.6", "@0x/utils": "^6.1.1", diff --git a/packages/asset-swapper/src/index.ts b/packages/asset-swapper/src/index.ts index 1fbaa783f4..8e24ce7b69 100644 --- a/packages/asset-swapper/src/index.ts +++ b/packages/asset-swapper/src/index.ts @@ -18,7 +18,7 @@ export { SRAPollingOrderProviderOpts, SRAWebsocketOrderProviderOpts, } from '@0x/orderbook'; -export { RFQTFirmQuote, RFQTIndicativeQuote, TakerRequestQueryParams } from '@0x/quote-server'; +export { V3RFQFirmQuote, V3RFQIndicativeQuote, TakerRequestQueryParams } from '@0x/quote-server'; export { APIOrder, Asset, diff --git a/packages/asset-swapper/src/utils/market_operation_utils/index.ts b/packages/asset-swapper/src/utils/market_operation_utils/index.ts index f31c1e8b48..dd09dda91a 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/index.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/index.ts @@ -1,4 +1,4 @@ -import { RFQTIndicativeQuote } from '@0x/quote-server'; +import { V3RFQIndicativeQuote } from '@0x/quote-server'; import { SignedOrder } from '@0x/types'; import { BigNumber, NULL_ADDRESS } from '@0x/utils'; import * as _ from 'lodash'; @@ -59,7 +59,7 @@ export async function getRfqtIndicativeQuotesAsync( assetFillAmount: BigNumber, comparisonPrice: BigNumber | undefined, opts: Partial, -): Promise { +): Promise { if (opts.rfqt && opts.rfqt.isIndicative === true && opts.rfqt.quoteRequestor) { return opts.rfqt.quoteRequestor.requestRfqtIndicativeQuotesAsync( makerAssetData, @@ -70,7 +70,7 @@ export async function getRfqtIndicativeQuotesAsync( opts.rfqt, ); } else { - return Promise.resolve([]); + return Promise.resolve([]); } } diff --git a/packages/asset-swapper/src/utils/market_operation_utils/orders.ts b/packages/asset-swapper/src/utils/market_operation_utils/orders.ts index 8dac145d03..e4277e0ba8 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/orders.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/orders.ts @@ -1,5 +1,5 @@ import { assetDataUtils, ERC20AssetData, generatePseudoRandomSalt, orderCalculationUtils } from '@0x/order-utils'; -import { RFQTIndicativeQuote } from '@0x/quote-server'; +import { V3RFQIndicativeQuote } from '@0x/quote-server'; import { SignedOrder } from '@0x/types'; import { AbiEncoder, BigNumber } from '@0x/utils'; @@ -498,7 +498,7 @@ export function createNativeOrder(fill: NativeCollapsedFill): OptimizedMarketOrd } export function createSignedOrdersFromRfqtIndicativeQuotes( - quotes: RFQTIndicativeQuote[], + quotes: V3RFQIndicativeQuote[], ): SignedOrderWithFillableAmounts[] { return quotes.map(quote => { return { diff --git a/packages/asset-swapper/src/utils/market_operation_utils/types.ts b/packages/asset-swapper/src/utils/market_operation_utils/types.ts index d061aa6f84..db719435b8 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/types.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/types.ts @@ -1,4 +1,4 @@ -import { RFQTIndicativeQuote } from '@0x/quote-server'; +import { V3RFQIndicativeQuote } from '@0x/quote-server'; import { MarketOperation, SignedOrder } from '@0x/types'; import { BigNumber } from '@0x/utils'; @@ -374,7 +374,7 @@ export interface MarketSideLiquidity { orderFillableAmounts: BigNumber[]; ethToOutputRate: BigNumber; ethToInputRate: BigNumber; - rfqtIndicativeQuotes: RFQTIndicativeQuote[]; + rfqtIndicativeQuotes: V3RFQIndicativeQuote[]; twoHopQuotes: Array>; quoteSourceFilters: SourceFilters; makerTokenDecimals: number; diff --git a/packages/asset-swapper/src/utils/quote_requestor.ts b/packages/asset-swapper/src/utils/quote_requestor.ts index c2e5ea99a8..c4016cdcbf 100644 --- a/packages/asset-swapper/src/utils/quote_requestor.ts +++ b/packages/asset-swapper/src/utils/quote_requestor.ts @@ -1,6 +1,6 @@ import { schemas, SchemaValidator } from '@0x/json-schemas'; import { assetDataUtils, orderCalculationUtils, SignedOrder } from '@0x/order-utils'; -import { RFQTFirmQuote, RFQTIndicativeQuote, TakerRequestQueryParams } from '@0x/quote-server'; +import { TakerRequestQueryParams, V3RFQFirmQuote, V3RFQIndicativeQuote } from '@0x/quote-server'; import { ERC20AssetData } from '@0x/types'; import { BigNumber } from '@0x/utils'; import Axios, { AxiosInstance } from 'axios'; @@ -111,12 +111,15 @@ export class QuoteRequestor { const requestParamsWithBigNumbers: Pick< TakerRequestQueryParams, - 'buyTokenAddress' | 'sellTokenAddress' | 'takerAddress' | 'comparisonPrice' + 'buyTokenAddress' | 'sellTokenAddress' | 'takerAddress' | 'comparisonPrice' | 'protocolVersion' > = { takerAddress, comparisonPrice: comparisonPrice === undefined ? undefined : comparisonPrice.toString(), buyTokenAddress, sellTokenAddress, + + // The request parameter below defines what protocol version the RFQ servers should be returning. + protocolVersion: '3', }; // convert BigNumbers to strings @@ -152,7 +155,7 @@ export class QuoteRequestor { marketOperation: MarketOperation, comparisonPrice: BigNumber | undefined, options: RfqtRequestOpts, - ): Promise { + ): Promise { const _opts: RfqtRequestOpts = { ...constants.DEFAULT_RFQT_REQUEST_OPTS, ...options }; if ( _opts.takerAddress === undefined || @@ -164,7 +167,7 @@ export class QuoteRequestor { throw new Error('RFQ-T firm quotes require the presence of a taker address'); } - const firmQuoteResponses = await this._getQuotesAsync( // not yet BigNumber + const firmQuoteResponses = await this._getQuotesAsync( // not yet BigNumber makerAssetData, takerAssetData, assetFillAmount, @@ -174,7 +177,7 @@ export class QuoteRequestor { 'firm', ); - const result: RFQTFirmQuote[] = []; + const result: V3RFQFirmQuote[] = []; firmQuoteResponses.forEach(firmQuoteResponse => { const orderWithStringInts = firmQuoteResponse.response.signedOrder; @@ -242,7 +245,7 @@ export class QuoteRequestor { marketOperation: MarketOperation, comparisonPrice: BigNumber | undefined, options: RfqtRequestOpts, - ): Promise { + ): Promise { const _opts: RfqtRequestOpts = { ...constants.DEFAULT_RFQT_REQUEST_OPTS, ...options }; // Originally a takerAddress was required for indicative quotes, but @@ -254,7 +257,7 @@ export class QuoteRequestor { _opts.takerAddress = constants.NULL_ADDRESS; } - const responsesWithStringInts = await this._getQuotesAsync( // not yet BigNumber + const responsesWithStringInts = await this._getQuotesAsync( // not yet BigNumber makerAssetData, takerAssetData, assetFillAmount, @@ -307,7 +310,7 @@ export class QuoteRequestor { return this._orderSignatureToMakerUri[orderSignature]; } - private _isValidRfqtIndicativeQuoteResponse(response: RFQTIndicativeQuote): boolean { + private _isValidRfqtIndicativeQuoteResponse(response: V3RFQIndicativeQuote): boolean { const hasValidMakerAssetAmount = response.makerAssetAmount !== undefined && this._schemaValidator.isValid(response.makerAssetAmount, schemas.wholeNumberSchema); diff --git a/packages/asset-swapper/test/quote_requestor_test.ts b/packages/asset-swapper/test/quote_requestor_test.ts index a30e2bd658..e76946eef0 100644 --- a/packages/asset-swapper/test/quote_requestor_test.ts +++ b/packages/asset-swapper/test/quote_requestor_test.ts @@ -42,6 +42,7 @@ describe('QuoteRequestor', async () => { sellAmountBaseUnits: '10000', comparisonPrice: undefined, takerAddress, + protocolVersion: '3', }; // Successful response const successfulOrder1 = testOrderFactory.generateTestSignedOrder({ @@ -214,6 +215,7 @@ describe('QuoteRequestor', async () => { buyTokenAddress: makerToken, sellAmountBaseUnits: '10000', comparisonPrice: undefined, + protocolVersion: '3', takerAddress, }; // Successful response @@ -313,6 +315,7 @@ describe('QuoteRequestor', async () => { buyTokenAddress: makerToken, buyAmountBaseUnits: '10000', comparisonPrice: undefined, + protocolVersion: '3', takerAddress, }; // Successful response diff --git a/yarn.lock b/yarn.lock index 9108e7e5e1..fd80c9be15 100644 --- a/yarn.lock +++ b/yarn.lock @@ -892,12 +892,14 @@ "@0x/order-utils" "^10.4.2" "@0x/utils" "^6.1.0" -"@0x/quote-server@^3.1.0": - version "3.1.0" - resolved "https://registry.yarnpkg.com/@0x/quote-server/-/quote-server-3.1.0.tgz#ba5c0de9f88fedfd522ec1ef608dd8eebb868509" +"@0x/quote-server@^4.0.1": + version "4.0.1" + resolved "https://registry.yarnpkg.com/@0x/quote-server/-/quote-server-4.0.1.tgz#05947589bfa7905d274ac3c726cb9918b93b0f9e" + integrity sha512-/5yP07a78Fqfj7IKicz57Z1R337iLYQP5qi17lMAmuIjwr7DOoUPMh0HL8FSmrf/Mk79GqC+f3jApTkXyR99fw== dependencies: "@0x/json-schemas" "^5.0.7" "@0x/order-utils" "^10.2.4" + "@0x/protocol-utils" "^1.0.1" "@0x/utils" "^5.4.1" "@types/express" "^4.17.3" express "^4.17.1"