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

Commit

Permalink
feat: Support RFQT via Meta-txn Endpoints (#203)
Browse files Browse the repository at this point in the history
* Add API Keys to meta-txn /price & /quote endpoints and pass RFQT opts into Asset-swapper

* Fix prettier
  • Loading branch information
fabioberger authored May 1, 2020
1 parent 5ebc34d commit ea9a7e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/handlers/meta_transaction_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { BigNumber } from '@0x/utils';
import * as express from 'express';
import * as HttpStatus from 'http-status-codes';
import * as _ from 'lodash';
import * as isValidUUID from 'uuid-validate';

import { CHAIN_ID } from '../config';
import { DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE, META_TRANSACTION_DOCS_URL } from '../constants';
import {
GeneralErrorCodes,
generalErrorCodeToReason,
InternalServerError,
RevertAPIError,
ValidationError,
Expand All @@ -32,6 +35,14 @@ export class MetaTransactionHandlers {
this._metaTransactionService = metaTransactionService;
}
public async getQuoteAsync(req: express.Request, res: express.Response): Promise<void> {
const apiKey = req.header('0x-api-key');
if (apiKey !== undefined && !isValidUUID(apiKey)) {
res.status(HttpStatus.BAD_REQUEST).send({
code: GeneralErrorCodes.InvalidAPIKey,
reason: generalErrorCodeToReason[GeneralErrorCodes.InvalidAPIKey],
});
return;
}
// HACK typescript typing does not allow this valid json-schema
schemaUtils.validateSchema(req.query, schemas.metaTransactionQuoteRequestSchema as any);
// parse query params
Expand All @@ -56,6 +67,7 @@ export class MetaTransactionHandlers {
from: takerAddress,
slippagePercentage,
excludedSources,
apiKey,
});
res.status(HttpStatus.OK).send(metaTransactionQuote);
} catch (e) {
Expand Down Expand Up @@ -95,6 +107,14 @@ export class MetaTransactionHandlers {
}
}
public async getPriceAsync(req: express.Request, res: express.Response): Promise<void> {
const apiKey = req.header('0x-api-key');
if (apiKey !== undefined && !isValidUUID(apiKey)) {
res.status(HttpStatus.BAD_REQUEST).send({
code: GeneralErrorCodes.InvalidAPIKey,
reason: generalErrorCodeToReason[GeneralErrorCodes.InvalidAPIKey],
});
return;
}
// HACK typescript typing does not allow this valid json-schema
schemaUtils.validateSchema(req.query, schemas.metaTransactionQuoteRequestSchema as any);
// parse query params
Expand All @@ -110,16 +130,20 @@ export class MetaTransactionHandlers {
const sellTokenAddress = findTokenAddressOrThrowApiError(sellToken, 'sellToken', CHAIN_ID);
const buyTokenAddress = findTokenAddressOrThrowApiError(buyToken, 'buyToken', CHAIN_ID);
try {
const metaTransactionPrice = await this._metaTransactionService.calculateMetaTransactionPriceAsync({
takerAddress,
buyTokenAddress,
sellTokenAddress,
buyAmount,
sellAmount,
from: takerAddress,
slippagePercentage,
excludedSources,
});
const metaTransactionPrice = await this._metaTransactionService.calculateMetaTransactionPriceAsync(
{
takerAddress,
buyTokenAddress,
sellTokenAddress,
buyAmount,
sellAmount,
from: takerAddress,
slippagePercentage,
excludedSources,
apiKey,
},
'price',
);
const metaTransactionPriceResponse = {
price: metaTransactionPrice.price,
buyAmount: metaTransactionPrice.buyAmount,
Expand Down
13 changes: 13 additions & 0 deletions src/services/meta_transaction_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class MetaTransactionService {
}
public async calculateMetaTransactionPriceAsync(
params: CalculateMetaTransactionQuoteParams,
endpoint: 'price' | 'quote',
): Promise<CalculateMetaTransactionPriceResponse> {
const {
takerAddress,
Expand All @@ -46,13 +47,24 @@ export class MetaTransactionService {
sellTokenAddress,
slippagePercentage,
excludedSources,
apiKey,
} = params;

let _rfqt;
if (apiKey !== undefined) {
_rfqt = {
intentOnFilling: endpoint === 'quote',
isIndicative: endpoint === 'price',
apiKey,
takerAddress,
};
}
const assetSwapperOpts = {
...ASSET_SWAPPER_MARKET_ORDERS_OPTS,
slippagePercentage,
bridgeSlippage: slippagePercentage,
excludedSources, // TODO(dave4506): overrides the excluded sources selected by chainId
rfqt: _rfqt,
};

let swapQuote;
Expand Down Expand Up @@ -107,6 +119,7 @@ export class MetaTransactionService {
): Promise<GetMetaTransactionQuoteResponse> {
const { takerAddress, sellAmount, buyAmount, swapQuote, price } = await this.calculateMetaTransactionPriceAsync(
params,
'quote',
);

const floatGasPrice = swapQuote.gasPrice;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,5 @@ export interface CalculateMetaTransactionQuoteParams {
from: string | undefined;
slippagePercentage?: number;
excludedSources?: ERC20BridgeSource[];
apiKey: string | undefined;
}

0 comments on commit ea9a7e0

Please sign in to comment.