Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
abstract out reporting and associate with meta txns
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Klebanoff committed Aug 3, 2020
1 parent 3af429d commit f543b70
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 33 deletions.
34 changes: 8 additions & 26 deletions src/handlers/swap_handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QuoteReport, RfqtRequestOpts, SwapQuoterError } from '@0x/asset-swapper';
import { RfqtRequestOpts, SwapQuoterError } from '@0x/asset-swapper';
import { BigNumber, NULL_ADDRESS } from '@0x/utils';
import * as express from 'express';
import * as HttpStatus from 'http-status-codes';
Expand All @@ -9,7 +9,6 @@ import {
DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE,
MARKET_DEPTH_DEFAULT_DISTRIBUTION,
MARKET_DEPTH_MAX_SAMPLES,
NUMBER_SOURCES_PER_LOG_LINE,
SWAP_DOCS_URL,
} from '../constants';
import {
Expand All @@ -34,6 +33,8 @@ import {
isWETHSymbolOrAddress,
} from '../utils/token_metadata_utils';

import { quoteReportUtils } from './../utils/quote_report_utils';

export class SwapHandlers {
private readonly _swapService: SwapService;
public static rootAsync(_req: express.Request, res: express.Response): void {
Expand Down Expand Up @@ -64,7 +65,11 @@ export class SwapHandlers {
},
});
if (quote.quoteReport) {
logQuoteReport(quote.quoteReport, quote.uniqueIdString);
quoteReportUtils.logQuoteReport({
quoteReport: quote.quoteReport,
submissionBy: 'taker',
uniqueIdString: quote.uniqueIdString,
});
}
}
const cleanedQuote = _.omit(quote, 'quoteReport', 'uniqueIdString');
Expand Down Expand Up @@ -279,29 +284,6 @@ export class SwapHandlers {
}
}

const logQuoteReport = (qr: QuoteReport, uniqueIdString: string) => {
const sourcesConsideredChunks = _.chunk(qr.sourcesConsidered, NUMBER_SOURCES_PER_LOG_LINE);
sourcesConsideredChunks.forEach((chunk, i) => {
logger.info({
firmQuoteReport: true,
sourcesConsideredChunkIndex: i,
sourcesConsideredChunkLength: sourcesConsideredChunks.length,
sourcesConsidered: chunk,
});
});

const sourcesDeliveredChunks = _.chunk(qr.sourcesDelivered, NUMBER_SOURCES_PER_LOG_LINE);
sourcesDeliveredChunks.forEach((chunk, i) => {
logger.info({
firmQuoteReport: true,
firmQuoteUniqueIdString: uniqueIdString,
sourcesDeliveredChunkIndex: i,
sourcesDeliveredChunkLength: sourcesDeliveredChunks.length,
sourcesDelivered: chunk,
});
});
};

const parseGetSwapQuoteRequestParams = (
req: express.Request,
endpoint: 'price' | 'quote',
Expand Down
23 changes: 19 additions & 4 deletions src/services/meta_transaction_service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Orderbook, SwapQuoter, SwapQuoteRequestOpts, SwapQuoterOpts } from '@0x/asset-swapper';
import {
MarketBuySwapQuote,
MarketSellSwapQuote,
Orderbook,
SwapQuoter,
SwapQuoteRequestOpts,
SwapQuoterOpts,
} from '@0x/asset-swapper';
import { ContractAddresses, getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
import { ContractWrappers } from '@0x/contract-wrappers';
import { DevUtilsContract } from '@0x/contracts-dev-utils';
Expand Down Expand Up @@ -41,6 +48,7 @@ import {
ZeroExTransactionWithoutDomain,
} from '../types';
import { ethGasStationUtils } from '../utils/gas_station_utils';
import { quoteReportUtils } from '../utils/quote_report_utils';
import { serviceUtils } from '../utils/service_utils';
import { utils } from '../utils/utils';

Expand Down Expand Up @@ -111,7 +119,7 @@ export class MetaTransactionService {
rfqt: _rfqt,
};

let swapQuote;
let swapQuote: MarketSellSwapQuote | MarketBuySwapQuote | undefined;
if (sellAmount !== undefined) {
swapQuote = await this._swapQuoter.getMarketSellSwapQuoteAsync(
buyTokenAddress,
Expand All @@ -129,7 +137,7 @@ export class MetaTransactionService {
} else {
throw new Error('sellAmount or buyAmount required');
}
const { gasPrice } = swapQuote;
const { gasPrice, quoteReport } = swapQuote;
const { gas, protocolFeeInWeiAmount: protocolFee } = swapQuote.worstCaseQuoteInfo;
const makerAssetAmount = swapQuote.bestCaseQuoteInfo.makerAssetAmount;
const totalTakerAssetAmount = swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount;
Expand Down Expand Up @@ -162,6 +170,7 @@ export class MetaTransactionService {
protocolFee,
minimumProtocolFee: protocolFee,
allowanceTarget,
quoteReport,
};
return response;
}
Expand All @@ -177,6 +186,7 @@ export class MetaTransactionService {
estimatedGas,
protocolFee,
minimumProtocolFee,
quoteReport,
} = await this.calculateMetaTransactionPriceAsync(params, 'quote');

const floatGasPrice = swapQuote.gasPrice;
Expand Down Expand Up @@ -206,6 +216,11 @@ export class MetaTransactionService {
)
.callAsync();

// log quote report and associate with txn hash
if (quoteReport) {
quoteReportUtils.logQuoteReport({ submissionBy: 'metaTxn', quoteReport, zeroExTransactionHash });
}

const makerAssetAmount = swapQuote.bestCaseQuoteInfo.makerAssetAmount;
const totalTakerAssetAmount = swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount;
const allowanceTarget = this._contractAddresses.erc20Proxy;
Expand Down Expand Up @@ -339,7 +354,7 @@ export class MetaTransactionService {
status: TransactionStates.Unsubmitted,
takerAddress: zeroExTransaction.signerAddress,
to: this._contractWrappers.exchange.address,
data,
data: data.affiliatedData,
value: protocolFee,
apiKey,
gasPrice: zeroExTransaction.gasPrice,
Expand Down
5 changes: 3 additions & 2 deletions src/services/swap_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,16 @@ export class SwapService {
: this._wethContract.deposit()
).getABIEncodedTransactionData();
const value = isUnwrap ? ZERO : amount;
const affiliatedData = serviceUtils.attributeCallData(data, affiliateAddress);
const attributedCalldata = serviceUtils.attributeCallData(data, affiliateAddress);
// TODO: consider not using protocol fee utils due to lack of need for an aggresive gas price for wrapping/unwrapping
const gasPrice = providedGasPrice || (await this._swapQuoter.getGasPriceEstimationOrThrowAsync());
const gasEstimate = isUnwrap ? UNWRAP_QUOTE_GAS : WRAP_QUOTE_GAS;
const apiSwapQuote: GetSwapQuoteResponse = {
price: ONE,
guaranteedPrice: ONE,
to: this._wethContract.address,
data: affiliatedData,
data: attributedCalldata.affiliatedData,
uniqueIdString: attributedCalldata.uniqueIdString,
value,
gas: gasEstimate,
estimatedGas: gasEstimate,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ export interface CalculateMetaTransactionPriceResponse {
protocolFee: BigNumber;
minimumProtocolFee: BigNumber;
estimatedGas: BigNumber;
quoteReport?: QuoteReport;
allowanceTarget?: string;
}

Expand Down
53 changes: 53 additions & 0 deletions src/utils/quote_report_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { QuoteReport } from '@0x/asset-swapper';
import _ = require('lodash');

import { NUMBER_SOURCES_PER_LOG_LINE } from '../constants';
import { logger } from '../logger';

interface QuoteReportSubmissionByTaker {
quoteReport: QuoteReport;
submissionBy: 'taker';
uniqueIdString: string;
}
interface QuoteReportSubmissionByMetaTxn {
quoteReport: QuoteReport;
submissionBy: 'metaTxn';
zeroExTransactionHash: string;
}
type LogQuoteReportOptions = QuoteReportSubmissionByTaker | QuoteReportSubmissionByMetaTxn;

export const quoteReportUtils = {
logQuoteReport(logOpts: LogQuoteReportOptions): void {
const qr = logOpts.quoteReport;

let logBase: { [key: string]: string | boolean } = {
firmQuoteReport: true,
submissionBy: logOpts.submissionBy,
};
if (logOpts.submissionBy === 'metaTxn') {
logBase = { ...logBase, zeroExTransactionHash: logOpts.zeroExTransactionHash };
} else if (logOpts.submissionBy === 'taker') {
logBase = { ...logBase, uniqueIdString: logOpts.uniqueIdString };
}

// Deliver in chunks since Kibana can't handle logs large requests
const sourcesConsideredChunks = _.chunk(qr.sourcesConsidered, NUMBER_SOURCES_PER_LOG_LINE);
sourcesConsideredChunks.forEach((chunk, i) => {
logger.info({
...logBase,
sourcesConsidered: chunk,
sourcesConsideredChunkIndex: i,
sourcesConsideredChunkLength: sourcesConsideredChunks.length,
});
});
const sourcesDeliveredChunks = _.chunk(qr.sourcesDelivered, NUMBER_SOURCES_PER_LOG_LINE);
sourcesDeliveredChunks.forEach((chunk, i) => {
logger.info({
...logBase,
sourcesDelivered: chunk,
sourcesDeliveredChunkIndex: i,
sourcesDeliveredChunkLength: sourcesDeliveredChunks.length,
});
});
},
};
2 changes: 1 addition & 1 deletion test/service_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe(SUITE_NAME, () => {
it('it returns a reasonable ID and timestamp', () => {
const fakeCallData = '0x0000000000000';
const fakeAffiliate = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const attributedCallData = serviceUtils.attributeCallData(fakeCallData, fakeAffiliate);
const attributedCallData = serviceUtils.attributeCallData(fakeCallData, fakeAffiliate).affiliatedData;
const currentTime = new Date();

// parse out items from call data to ensure they are reasonable values
Expand Down

0 comments on commit f543b70

Please sign in to comment.