Compare commits

..

3 Commits

Author SHA1 Message Date
Github Actions
6ce4458a5d Publish
- @0x/asset-swapper@16.27.2
2021-09-14 17:13:46 +00:00
Github Actions
fad6e65c07 Updated CHANGELOGS & MD docs 2021-09-14 17:13:42 +00:00
Daniel Pyrathon
840c85373e fix: Refactor integrator ID and add Prometheus metrics (#322)
* Refactor integrator ID and add Prometheus metrics

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Added documentation and fixed some minor requests

* Added more metrics

* more docs

* lint fix

* added new Integrator ID addition

* refactor tests

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>
2021-09-14 12:45:41 -04:00
11 changed files with 162 additions and 33 deletions

View File

@@ -1,4 +1,13 @@
[
{
"timestamp": 1631639620,
"version": "16.27.2",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"version": "16.27.1",
"changes": [

View File

@@ -5,6 +5,10 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v16.27.2 - _September 14, 2021_
* Dependencies updated
## v16.27.1 - _September 8, 2021_
* Fix ApproximateBuys sampler to terminate if the buy amount is not met (#319)

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/asset-swapper",
"version": "16.27.1",
"version": "16.27.2",
"engines": {
"node": ">=6.12"
},

View File

@@ -50,7 +50,7 @@ const DEFAULT_SWAP_QUOTER_OPTS: SwapQuoterOpts = {
samplerGasLimit: 500e6,
ethGasStationUrl: ETH_GAS_STATION_API_URL,
rfqt: {
takerApiKeyWhitelist: [],
integratorsWhitelist: [],
makerAssetOfferings: {},
txOriginBlacklist: new Set(),
},

View File

@@ -88,6 +88,7 @@ export {
ExchangeProxyContractOpts,
ExchangeProxyRefundReceiver,
GetExtensionContractTypeOpts,
Integrator,
LogFunction,
MarketBuySwapQuote,
MarketOperation,

View File

@@ -75,6 +75,7 @@ export class SwapQuoter {
private readonly _marketOperationUtils: MarketOperationUtils;
private readonly _rfqtOptions?: SwapQuoterRfqOpts;
private readonly _quoteRequestorHttpClient: AxiosInstance;
private readonly _integratorIdsSet: Set<string>;
/**
* Instantiates a new SwapQuoter instance
@@ -164,6 +165,9 @@ export class SwapQuoter {
httpsAgent: new HttpsAgent({ keepAlive: true, timeout: KEEP_ALIVE_TTL }),
...(rfqt ? rfqt.axiosInstanceOpts : {}),
});
const integratorIds = this._rfqtOptions?.integratorsWhitelist.map(integrator => integrator.integratorId) || [];
this._integratorIdsSet = new Set(integratorIds);
}
public async getBatchMarketBuySwapQuoteAsync(
@@ -414,12 +418,11 @@ export class SwapQuoter {
return isOpenOrder && !willOrderExpire && isFeeTypeAllowed;
}; // tslint:disable-line:semicolon
private _isApiKeyWhitelisted(apiKey: string | undefined): boolean {
if (!apiKey) {
private _isIntegratorIdWhitelisted(integratorId: string | undefined): boolean {
if (!integratorId) {
return false;
}
const whitelistedApiKeys = this._rfqtOptions ? this._rfqtOptions.takerApiKeyWhitelist : [];
return whitelistedApiKeys.includes(apiKey);
return this._integratorIdsSet.has(integratorId);
}
private _isTxOriginBlacklisted(txOrigin: string | undefined): boolean {
@@ -438,19 +441,19 @@ export class SwapQuoter {
return rfqt;
}
// tslint:disable-next-line: boolean-naming
const { apiKey, nativeExclusivelyRFQ, intentOnFilling, txOrigin } = rfqt;
const { integrator, nativeExclusivelyRFQ, intentOnFilling, txOrigin } = rfqt;
// If RFQ-T is enabled and `nativeExclusivelyRFQ` is set, then `ERC20BridgeSource.Native` should
// never be excluded.
if (nativeExclusivelyRFQ === true && !sourceFilters.isAllowed(ERC20BridgeSource.Native)) {
throw new Error('Native liquidity cannot be excluded if "rfqt.nativeExclusivelyRFQ" is set');
}
// If an API key was provided, but the key is not whitelisted, raise a warning and disable RFQ
if (!this._isApiKeyWhitelisted(apiKey)) {
// If an integrator ID was provided, but the ID is not whitelisted, raise a warning and disable RFQ
if (!this._isIntegratorIdWhitelisted(integrator.integratorId)) {
if (this._rfqtOptions && this._rfqtOptions.warningLogger) {
this._rfqtOptions.warningLogger(
{
apiKey,
...integrator,
},
'Attempt at using an RFQ API key that is not whitelisted. Disabling RFQ for the request lifetime.',
);
@@ -474,7 +477,7 @@ export class SwapQuoter {
// Otherwise check other RFQ options
if (
intentOnFilling && // The requestor is asking for a firm quote
this._isApiKeyWhitelisted(apiKey) && // A valid API key was provided
this._isIntegratorIdWhitelisted(integrator.integratorId) && // A valid API key was provided
sourceFilters.isAllowed(ERC20BridgeSource.Native) // Native liquidity is not excluded
) {
if (!txOrigin || txOrigin === constants.NULL_ADDRESS) {

View File

@@ -243,7 +243,7 @@ export interface RfqmRequestOptions extends RfqRequestOpts {
export interface RfqRequestOpts {
takerAddress: string;
txOrigin: string;
apiKey: string;
integrator: Integrator;
apiKeyWhitelist?: string[];
intentOnFilling: boolean;
isIndicative?: boolean;
@@ -294,8 +294,13 @@ export interface RfqFirmQuoteValidator {
getRfqtTakerFillableAmountsAsync(quotes: RfqOrder[]): Promise<BigNumber[]>;
}
export interface Integrator {
integratorId: string;
label: string;
}
export interface SwapQuoterRfqOpts {
takerApiKeyWhitelist: string[];
integratorsWhitelist: Integrator[];
makerAssetOfferings: RfqMakerAssetOfferings;
txOriginBlacklist: Set<string>;
altRfqCreds?: {

View File

@@ -14,6 +14,7 @@ import { constants } from '../constants';
import {
AltQuoteModel,
AltRfqMakerAssetOfferings,
Integrator,
LogFunction,
MarketOperation,
RfqMakerAssetOfferings,
@@ -61,6 +62,31 @@ export interface MetricsProxy {
* @param expirationTimeSeconds the expiration time in seconds
*/
incrementFillRatioWarningCounter(isLastLook: boolean, maker: string): void;
/**
* Logs the outcome of a network (HTTP) interaction with a market maker.
*
* @param interaction.isLastLook true if the request is RFQM
* @param interaction.integrator the integrator that is requesting the RFQ quote
* @param interaction.url the URL of the market maker
* @param interaction.quoteType indicative or firm quote
* @param interaction.statusCode the statusCode returned by a market maker
* @param interaction.latencyMs the latency of the HTTP request (in ms)
* @param interaction.included if a firm quote that was returned got included in the next step of processing.
* NOTE: this does not mean that the request returned a valid fillable order. It just
* means that the network response was successful.
*/
logRfqMakerNetworkInteraction(interaction: {
isLastLook: boolean;
integrator: Integrator;
url: string;
quoteType: 'firm' | 'indicative';
statusCode: number | undefined;
latencyMs: number;
included: boolean;
sellTokenAddress: string;
buyTokenAddress: string;
}): void;
}
/**
@@ -453,7 +479,20 @@ export class QuoteRequestor {
// filter out requests to skip
const isBlacklisted = rfqMakerBlacklist.isMakerBlacklisted(typedMakerUrl.url);
const partialLogEntry = { url: typedMakerUrl.url, quoteType, requestParams, isBlacklisted };
const { isLastLook, integrator } = options;
const { sellTokenAddress, buyTokenAddress } = requestParams;
if (isBlacklisted) {
this._metrics?.logRfqMakerNetworkInteraction({
isLastLook: false,
url: typedMakerUrl.url,
quoteType,
statusCode: undefined,
sellTokenAddress,
buyTokenAddress,
latencyMs: 0,
included: false,
integrator,
});
this._infoLogger({ rfqtMakerInteraction: { ...partialLogEntry } });
return;
} else if (
@@ -472,18 +511,32 @@ export class QuoteRequestor {
try {
if (typedMakerUrl.pairType === RfqPairType.Standard) {
const response = await this._quoteRequestorHttpClient.get(`${typedMakerUrl.url}/${quotePath}`, {
headers: { '0x-api-key': options.apiKey },
headers: {
'0x-api-key': options.integrator.integratorId,
'0x-integrator-id': options.integrator.integratorId,
},
params: requestParams,
timeout: timeoutMs,
cancelToken: cancelTokenSource.token,
});
const latencyMs = Date.now() - timeBeforeAwait;
this._metrics?.logRfqMakerNetworkInteraction({
isLastLook: isLastLook || false,
url: typedMakerUrl.url,
quoteType,
statusCode: response.status,
sellTokenAddress,
buyTokenAddress,
latencyMs,
included: true,
integrator,
});
this._infoLogger({
rfqtMakerInteraction: {
...partialLogEntry,
response: {
included: true,
apiKey: options.apiKey,
apiKey: options.integrator.integratorId,
takerAddress: requestParams.takerAddress,
txOrigin: requestParams.txOrigin,
statusCode: response.status,
@@ -501,7 +554,7 @@ export class QuoteRequestor {
typedMakerUrl.url,
this._altRfqCreds.altRfqApiKey,
this._altRfqCreds.altRfqProfile,
options.apiKey,
options.integrator.integratorId,
quoteType === 'firm' ? AltQuoteModel.Firm : AltQuoteModel.Indicative,
makerToken,
takerToken,
@@ -514,12 +567,23 @@ export class QuoteRequestor {
);
const latencyMs = Date.now() - timeBeforeAwait;
this._metrics?.logRfqMakerNetworkInteraction({
isLastLook: isLastLook || false,
url: typedMakerUrl.url,
quoteType,
statusCode: quote.status,
sellTokenAddress,
buyTokenAddress,
latencyMs,
included: true,
integrator,
});
this._infoLogger({
rfqtMakerInteraction: {
...partialLogEntry,
response: {
included: true,
apiKey: options.apiKey,
apiKey: options.integrator.integratorId,
takerAddress: requestParams.takerAddress,
txOrigin: requestParams.txOrigin,
statusCode: quote.status,
@@ -533,12 +597,23 @@ export class QuoteRequestor {
} catch (err) {
// log error if any
const latencyMs = Date.now() - timeBeforeAwait;
this._metrics?.logRfqMakerNetworkInteraction({
isLastLook: isLastLook || false,
url: typedMakerUrl.url,
quoteType,
statusCode: err.response?.status,
sellTokenAddress,
buyTokenAddress,
latencyMs,
included: false,
integrator,
});
this._infoLogger({
rfqtMakerInteraction: {
...partialLogEntry,
response: {
included: false,
apiKey: options.apiKey,
apiKey: options.integrator.integratorId,
takerAddress: requestParams.takerAddress,
txOrigin: requestParams.txOrigin,
statusCode: err.response ? err.response.status : undefined,
@@ -549,7 +624,7 @@ export class QuoteRequestor {
rfqMakerBlacklist.logTimeoutOrLackThereof(typedMakerUrl.url, latencyMs >= timeoutMs);
this._warningLogger(
convertIfAxiosError(err),
`Failed to get RFQ-T ${quoteType} quote from market maker endpoint ${typedMakerUrl.url} for API key ${options.apiKey} for taker address ${options.takerAddress} and tx origin ${options.txOrigin}`,
`Failed to get RFQ-T ${quoteType} quote from market maker endpoint ${typedMakerUrl.url} for integrator ${options.integrator.integratorId} (${options.integrator.label}) for taker address ${options.takerAddress} and tx origin ${options.txOrigin}`,
);
return;
}

View File

@@ -16,7 +16,7 @@ import * as _ from 'lodash';
import * as TypeMoq from 'typemoq';
import { MarketOperation, QuoteRequestor, RfqRequestOpts, SignedNativeOrder } from '../src';
import { NativeOrderWithFillableAmounts } from '../src/types';
import { Integrator, NativeOrderWithFillableAmounts } from '../src/types';
import { MarketOperationUtils } from '../src/utils/market_operation_utils/';
import {
BUY_SOURCE_FILTER_BY_CHAIN_ID,
@@ -62,6 +62,10 @@ const SELL_SOURCES = SELL_SOURCE_FILTER_BY_CHAIN_ID[ChainId.Mainnet].sources;
const TOKEN_ADJACENCY_GRAPH: TokenAdjacencyGraph = { default: [] };
const SIGNATURE = { v: 1, r: NULL_BYTES, s: NULL_BYTES, signatureType: SignatureType.EthSign };
const FOO_INTEGRATOR: Integrator = {
integratorId: 'foo',
label: 'foo',
};
/**
* gets the orders required for a market sell operation by (potentially) merging native orders with
@@ -745,7 +749,7 @@ describe('MarketOperationUtils tests', () => {
feeSchedule,
rfqt: {
isIndicative: false,
apiKey: 'foo',
integrator: FOO_INTEGRATOR,
takerAddress: randomAddress(),
txOrigin: randomAddress(),
intentOnFilling: true,
@@ -790,7 +794,7 @@ describe('MarketOperationUtils tests', () => {
...DEFAULT_OPTS,
rfqt: {
isIndicative: false,
apiKey: 'foo',
integrator: FOO_INTEGRATOR,
takerAddress: randomAddress(),
intentOnFilling: true,
txOrigin: randomAddress(),
@@ -837,7 +841,7 @@ describe('MarketOperationUtils tests', () => {
...DEFAULT_OPTS,
rfqt: {
isIndicative: true,
apiKey: 'foo',
integrator: FOO_INTEGRATOR,
takerAddress: randomAddress(),
txOrigin: randomAddress(),
intentOnFilling: true,
@@ -896,7 +900,10 @@ describe('MarketOperationUtils tests', () => {
...DEFAULT_OPTS,
rfqt: {
isIndicative: false,
apiKey: 'foo',
integrator: {
integratorId: 'foo',
label: 'foo',
},
takerAddress: randomAddress(),
intentOnFilling: true,
txOrigin: randomAddress(),
@@ -954,7 +961,7 @@ describe('MarketOperationUtils tests', () => {
...DEFAULT_OPTS,
rfqt: {
isIndicative: false,
apiKey: 'foo',
integrator: FOO_INTEGRATOR,
takerAddress: randomAddress(),
txOrigin: randomAddress(),
intentOnFilling: true,

View File

@@ -240,7 +240,10 @@ describe('QuoteRequestor', async () => {
MarketOperation.Sell,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin: takerAddress,
intentOnFilling: true,
@@ -435,7 +438,10 @@ describe('QuoteRequestor', async () => {
MarketOperation.Sell,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin: takerAddress,
intentOnFilling: true,
@@ -551,7 +557,10 @@ describe('QuoteRequestor', async () => {
MarketOperation.Sell,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin: takerAddress,
intentOnFilling: true,
@@ -675,7 +684,10 @@ describe('QuoteRequestor', async () => {
MarketOperation.Sell,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin: takerAddress,
intentOnFilling: true,
@@ -762,7 +774,10 @@ describe('QuoteRequestor', async () => {
MarketOperation.Sell,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin: takerAddress,
intentOnFilling: true,
@@ -823,7 +838,10 @@ describe('QuoteRequestor', async () => {
MarketOperation.Buy,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin: takerAddress,
intentOnFilling: true,
@@ -1088,7 +1106,10 @@ describe('QuoteRequestor', async () => {
altScenario.requestedOperation,
undefined,
{
apiKey,
integrator: {
integratorId: apiKey,
label: 'foo',
},
takerAddress,
txOrigin,
intentOnFilling: true,

View File

@@ -48,7 +48,11 @@ export const testHelpers = {
// Mock out Standard RFQ-T/M responses
for (const mockedResponse of standardMockedResponses) {
const { endpoint, requestApiKey, requestParams, responseData, responseCode } = mockedResponse;
const requestHeaders = { Accept: 'application/json, text/plain, */*', '0x-api-key': requestApiKey };
const requestHeaders = {
Accept: 'application/json, text/plain, */*',
'0x-api-key': requestApiKey,
'0x-integrator-id': requestApiKey,
};
if (mockedResponse.callback !== undefined) {
mockedAxios
.onGet(`${endpoint}/${quoteType}`, { params: requestParams }, requestHeaders)