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

Commit

Permalink
feat: add transaction price information to meta-txn endpoints for UI (#…
Browse files Browse the repository at this point in the history
…248)

* feat: add transaction price information to meta-txn endpoints for UI

* Run prettier

* Update minimumProtocolFee

* Add fields to metatx price endpoint

* Update price for consistency

* Update tests

Co-authored-by: Jacob Evans <jacob@dekz.net>
  • Loading branch information
Francesco Agosti and dekz authored Jun 11, 2020
1 parent bd3d3f3 commit d26d6c6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
9 changes: 8 additions & 1 deletion src/handlers/meta_transaction_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as HttpStatus from 'http-status-codes';
import * as isValidUUID from 'uuid-validate';

import { CHAIN_ID } from '../config';
import { DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE, META_TRANSACTION_DOCS_URL } from '../constants';
import { DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE, META_TRANSACTION_DOCS_URL, ZERO } from '../constants';
import { TransactionEntity } from '../entities';
import {
GeneralErrorCodes,
Expand Down Expand Up @@ -158,6 +158,13 @@ export class MetaTransactionHandlers {
sellTokenAddress,
buyTokenAddress,
sources: metaTransactionPrice.sources,
value: metaTransactionPrice.protocolFee,
gasPrice: metaTransactionPrice.gasPrice,
gas: metaTransactionPrice.estimatedGas,
estimatedGas: metaTransactionPrice.estimatedGas,
protocolFee: metaTransactionPrice.protocolFee,
minimumProtocolFee: metaTransactionPrice.minimumProtocolFee,
estimatedGasTokenRefund: ZERO,
};
res.status(HttpStatus.OK).send(metaTransactionPriceResponse);
} catch (e) {
Expand Down
31 changes: 26 additions & 5 deletions src/services/meta_transaction_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
SUBMITTED_TX_DB_POLLING_INTERVAL_MS,
TEN_MINUTES_MS,
TX_HASH_RESPONSE_WAIT_TIME_MS,
ZERO,
} from '../constants';
import { KeyValueEntity, TransactionEntity } from '../entities';
import { logger } from '../logger';
Expand Down Expand Up @@ -135,7 +136,8 @@ export class MetaTransactionService {
} else {
throw new Error('sellAmount or buyAmount required');
}

const { gasPrice } = swapQuote;
const { gas, protocolFeeInWeiAmount: protocolFee } = swapQuote.worstCaseQuoteInfo;
const makerAssetAmount = swapQuote.bestCaseQuoteInfo.makerAssetAmount;
const totalTakerAssetAmount = swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount;

Expand All @@ -161,16 +163,26 @@ export class MetaTransactionService {
price,
swapQuote,
sources: serviceUtils.convertSourceBreakdownToArray(swapQuote.sourceBreakdown),
estimatedGas: new BigNumber(gas),
gasPrice,
protocolFee,
minimumProtocolFee: protocolFee,
};
return response;
}
public async calculateMetaTransactionQuoteAsync(
params: CalculateMetaTransactionQuoteParams,
): Promise<GetMetaTransactionQuoteResponse> {
const { takerAddress, sellAmount, buyAmount, swapQuote, price } = await this.calculateMetaTransactionPriceAsync(
params,
'quote',
);
const {
takerAddress,
sellAmount,
buyAmount,
swapQuote,
price,
estimatedGas,
protocolFee,
minimumProtocolFee,
} = await this.calculateMetaTransactionPriceAsync(params, 'quote');

const floatGasPrice = swapQuote.gasPrice;
const gasPrice = floatGasPrice
Expand Down Expand Up @@ -203,12 +215,21 @@ export class MetaTransactionService {
const totalTakerAssetAmount = swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount;
const apiMetaTransactionQuote: GetMetaTransactionQuoteResponse = {
price,
sellTokenAddress: params.sellTokenAddress,
buyTokenAddress: params.buyTokenAddress,
zeroExTransactionHash,
zeroExTransaction,
buyAmount: makerAssetAmount,
sellAmount: totalTakerAssetAmount,
orders: serviceUtils.cleanSignedOrderFields(orders),
sources: serviceUtils.convertSourceBreakdownToArray(sourceBreakdown),
gasPrice,
estimatedGas,
gas: estimatedGas,
protocolFee,
minimumProtocolFee,
estimatedGasTokenRefund: ZERO,
value: protocolFee,
};
return apiMetaTransactionQuote;
}
Expand Down
15 changes: 7 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,6 @@ interface BasePriceResponse {
sellTokenAddress: string;
buyTokenAddress: string;
sources: GetSwapQuoteResponseLiquiditySource[];
}

export interface GetSwapPriceResponse extends BasePriceResponse {
value: BigNumber;
gasPrice: BigNumber;
gas: BigNumber;
Expand All @@ -407,16 +404,14 @@ export interface GetSwapPriceResponse extends BasePriceResponse {
minimumProtocolFee: BigNumber;
}

export interface GetSwapPriceResponse extends BasePriceResponse {}

export type GetTokenPricesResponse = Price[];

export interface GetMetaTransactionQuoteResponse {
price: BigNumber;
export interface GetMetaTransactionQuoteResponse extends BasePriceResponse {
zeroExTransactionHash: string;
zeroExTransaction: ZeroExTransaction;
orders: SignedOrder[];
buyAmount: BigNumber;
sellAmount: BigNumber;
sources: GetSwapQuoteResponseLiquiditySource[];
}

export interface GetMetaTransactionPriceResponse extends BasePriceResponse {}
Expand All @@ -440,6 +435,10 @@ export interface CalculateMetaTransactionPriceResponse {
takerAddress: string;
swapQuote: MarketSellSwapQuote | MarketBuySwapQuote;
sources: GetSwapQuoteResponseLiquiditySource[];
gasPrice: BigNumber;
protocolFee: BigNumber;
minimumProtocolFee: BigNumber;
estimatedGas: BigNumber;
}

export interface PostTransactionResponse {
Expand Down
20 changes: 10 additions & 10 deletions test/meta_transaction_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ describe(SUITE_NAME, () => {
const response = await httpGetAsync({ route });
expect(response.type).to.be.eq('application/json');
expect(response.status).to.be.eq(HttpStatus.OK);
expect(response.body).to.be.deep.eq({
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
expect(response.body).to.include({
price,
buyAmount,
sellAmount,
sellTokenAddress,
buyTokenAddress,
sources: liquiditySources0xOnly,
});
});

Expand All @@ -264,13 +264,13 @@ describe(SUITE_NAME, () => {
const response = await httpGetAsync({ route });
expect(response.type).to.be.eq('application/json');
expect(response.status).to.be.eq(HttpStatus.OK);
expect(response.body).to.be.deep.eq({
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
expect(response.body).to.include({
price,
buyAmount,
sellAmount,
sellTokenAddress,
buyTokenAddress,
sources: liquiditySources0xOnly,
});
});

Expand All @@ -291,13 +291,13 @@ describe(SUITE_NAME, () => {
const response = await httpGetAsync({ route });
expect(response.type).to.be.eq('application/json');
expect(response.status).to.be.eq(HttpStatus.OK);
expect(response.body).to.be.deep.eq({
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
expect(response.body).to.include({
price: largeOrderPrice,
buyAmount: largeBuyAmount,
sellAmount: largeSellAmount,
sellTokenAddress,
buyTokenAddress,
sources: liquiditySources0xOnly,
});
});
});
Expand Down Expand Up @@ -544,13 +544,13 @@ describe(SUITE_NAME, () => {
const response = await httpGetAsync({ route });
expect(response.type).to.be.eq('application/json');
expect(response.status).to.be.eq(HttpStatus.OK);
expect(response.body).to.be.deep.eq({
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
expect(response.body).to.include({
price,
buyAmount,
sellAmount,
sellTokenAddress,
buyTokenAddress,
sources: liquiditySources0xOnly,
});
});

Expand Down Expand Up @@ -656,12 +656,12 @@ describe(SUITE_NAME, () => {
const response = await httpGetAsync({ route });
expect(response.type).to.be.eq('application/json');
expect(response.status).to.be.eq(HttpStatus.OK);
expect(response.body).to.be.deep.eq({
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
expect(response.body).to.include({
price,
buyAmount: largeBuyAmount,
sellTokenAddress,
buyTokenAddress,
sources: liquiditySources0xOnly,
});
});

Expand Down

0 comments on commit d26d6c6

Please sign in to comment.