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

Commit d26d6c6

Browse files
Francesco Agostidekz
andauthored
feat: add transaction price information to meta-txn endpoints for UI (#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>
1 parent bd3d3f3 commit d26d6c6

4 files changed

Lines changed: 51 additions & 24 deletions

File tree

src/handlers/meta_transaction_handlers.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as HttpStatus from 'http-status-codes';
66
import * as isValidUUID from 'uuid-validate';
77

88
import { CHAIN_ID } from '../config';
9-
import { DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE, META_TRANSACTION_DOCS_URL } from '../constants';
9+
import { DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE, META_TRANSACTION_DOCS_URL, ZERO } from '../constants';
1010
import { TransactionEntity } from '../entities';
1111
import {
1212
GeneralErrorCodes,
@@ -158,6 +158,13 @@ export class MetaTransactionHandlers {
158158
sellTokenAddress,
159159
buyTokenAddress,
160160
sources: metaTransactionPrice.sources,
161+
value: metaTransactionPrice.protocolFee,
162+
gasPrice: metaTransactionPrice.gasPrice,
163+
gas: metaTransactionPrice.estimatedGas,
164+
estimatedGas: metaTransactionPrice.estimatedGas,
165+
protocolFee: metaTransactionPrice.protocolFee,
166+
minimumProtocolFee: metaTransactionPrice.minimumProtocolFee,
167+
estimatedGasTokenRefund: ZERO,
161168
};
162169
res.status(HttpStatus.OK).send(metaTransactionPriceResponse);
163170
} catch (e) {

src/services/meta_transaction_service.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
SUBMITTED_TX_DB_POLLING_INTERVAL_MS,
3131
TEN_MINUTES_MS,
3232
TX_HASH_RESPONSE_WAIT_TIME_MS,
33+
ZERO,
3334
} from '../constants';
3435
import { KeyValueEntity, TransactionEntity } from '../entities';
3536
import { logger } from '../logger';
@@ -135,7 +136,8 @@ export class MetaTransactionService {
135136
} else {
136137
throw new Error('sellAmount or buyAmount required');
137138
}
138-
139+
const { gasPrice } = swapQuote;
140+
const { gas, protocolFeeInWeiAmount: protocolFee } = swapQuote.worstCaseQuoteInfo;
139141
const makerAssetAmount = swapQuote.bestCaseQuoteInfo.makerAssetAmount;
140142
const totalTakerAssetAmount = swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount;
141143

@@ -161,16 +163,26 @@ export class MetaTransactionService {
161163
price,
162164
swapQuote,
163165
sources: serviceUtils.convertSourceBreakdownToArray(swapQuote.sourceBreakdown),
166+
estimatedGas: new BigNumber(gas),
167+
gasPrice,
168+
protocolFee,
169+
minimumProtocolFee: protocolFee,
164170
};
165171
return response;
166172
}
167173
public async calculateMetaTransactionQuoteAsync(
168174
params: CalculateMetaTransactionQuoteParams,
169175
): Promise<GetMetaTransactionQuoteResponse> {
170-
const { takerAddress, sellAmount, buyAmount, swapQuote, price } = await this.calculateMetaTransactionPriceAsync(
171-
params,
172-
'quote',
173-
);
176+
const {
177+
takerAddress,
178+
sellAmount,
179+
buyAmount,
180+
swapQuote,
181+
price,
182+
estimatedGas,
183+
protocolFee,
184+
minimumProtocolFee,
185+
} = await this.calculateMetaTransactionPriceAsync(params, 'quote');
174186

175187
const floatGasPrice = swapQuote.gasPrice;
176188
const gasPrice = floatGasPrice
@@ -203,12 +215,21 @@ export class MetaTransactionService {
203215
const totalTakerAssetAmount = swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount;
204216
const apiMetaTransactionQuote: GetMetaTransactionQuoteResponse = {
205217
price,
218+
sellTokenAddress: params.sellTokenAddress,
219+
buyTokenAddress: params.buyTokenAddress,
206220
zeroExTransactionHash,
207221
zeroExTransaction,
208222
buyAmount: makerAssetAmount,
209223
sellAmount: totalTakerAssetAmount,
210224
orders: serviceUtils.cleanSignedOrderFields(orders),
211225
sources: serviceUtils.convertSourceBreakdownToArray(sourceBreakdown),
226+
gasPrice,
227+
estimatedGas,
228+
gas: estimatedGas,
229+
protocolFee,
230+
minimumProtocolFee,
231+
estimatedGasTokenRefund: ZERO,
232+
value: protocolFee,
212233
};
213234
return apiMetaTransactionQuote;
214235
}

src/types.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,6 @@ interface BasePriceResponse {
395395
sellTokenAddress: string;
396396
buyTokenAddress: string;
397397
sources: GetSwapQuoteResponseLiquiditySource[];
398-
}
399-
400-
export interface GetSwapPriceResponse extends BasePriceResponse {
401398
value: BigNumber;
402399
gasPrice: BigNumber;
403400
gas: BigNumber;
@@ -407,16 +404,14 @@ export interface GetSwapPriceResponse extends BasePriceResponse {
407404
minimumProtocolFee: BigNumber;
408405
}
409406

407+
export interface GetSwapPriceResponse extends BasePriceResponse {}
408+
410409
export type GetTokenPricesResponse = Price[];
411410

412-
export interface GetMetaTransactionQuoteResponse {
413-
price: BigNumber;
411+
export interface GetMetaTransactionQuoteResponse extends BasePriceResponse {
414412
zeroExTransactionHash: string;
415413
zeroExTransaction: ZeroExTransaction;
416414
orders: SignedOrder[];
417-
buyAmount: BigNumber;
418-
sellAmount: BigNumber;
419-
sources: GetSwapQuoteResponseLiquiditySource[];
420415
}
421416

422417
export interface GetMetaTransactionPriceResponse extends BasePriceResponse {}
@@ -440,6 +435,10 @@ export interface CalculateMetaTransactionPriceResponse {
440435
takerAddress: string;
441436
swapQuote: MarketSellSwapQuote | MarketBuySwapQuote;
442437
sources: GetSwapQuoteResponseLiquiditySource[];
438+
gasPrice: BigNumber;
439+
protocolFee: BigNumber;
440+
minimumProtocolFee: BigNumber;
441+
estimatedGas: BigNumber;
443442
}
444443

445444
export interface PostTransactionResponse {

test/meta_transaction_test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ describe(SUITE_NAME, () => {
241241
const response = await httpGetAsync({ route });
242242
expect(response.type).to.be.eq('application/json');
243243
expect(response.status).to.be.eq(HttpStatus.OK);
244-
expect(response.body).to.be.deep.eq({
244+
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
245+
expect(response.body).to.include({
245246
price,
246247
buyAmount,
247248
sellAmount,
248249
sellTokenAddress,
249250
buyTokenAddress,
250-
sources: liquiditySources0xOnly,
251251
});
252252
});
253253

@@ -264,13 +264,13 @@ describe(SUITE_NAME, () => {
264264
const response = await httpGetAsync({ route });
265265
expect(response.type).to.be.eq('application/json');
266266
expect(response.status).to.be.eq(HttpStatus.OK);
267-
expect(response.body).to.be.deep.eq({
267+
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
268+
expect(response.body).to.include({
268269
price,
269270
buyAmount,
270271
sellAmount,
271272
sellTokenAddress,
272273
buyTokenAddress,
273-
sources: liquiditySources0xOnly,
274274
});
275275
});
276276

@@ -291,13 +291,13 @@ describe(SUITE_NAME, () => {
291291
const response = await httpGetAsync({ route });
292292
expect(response.type).to.be.eq('application/json');
293293
expect(response.status).to.be.eq(HttpStatus.OK);
294-
expect(response.body).to.be.deep.eq({
294+
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
295+
expect(response.body).to.include({
295296
price: largeOrderPrice,
296297
buyAmount: largeBuyAmount,
297298
sellAmount: largeSellAmount,
298299
sellTokenAddress,
299300
buyTokenAddress,
300-
sources: liquiditySources0xOnly,
301301
});
302302
});
303303
});
@@ -544,13 +544,13 @@ describe(SUITE_NAME, () => {
544544
const response = await httpGetAsync({ route });
545545
expect(response.type).to.be.eq('application/json');
546546
expect(response.status).to.be.eq(HttpStatus.OK);
547-
expect(response.body).to.be.deep.eq({
547+
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
548+
expect(response.body).to.include({
548549
price,
549550
buyAmount,
550551
sellAmount,
551552
sellTokenAddress,
552553
buyTokenAddress,
553-
sources: liquiditySources0xOnly,
554554
});
555555
});
556556

@@ -656,12 +656,12 @@ describe(SUITE_NAME, () => {
656656
const response = await httpGetAsync({ route });
657657
expect(response.type).to.be.eq('application/json');
658658
expect(response.status).to.be.eq(HttpStatus.OK);
659-
expect(response.body).to.be.deep.eq({
659+
expect(response.body.sources).to.be.deep.eq(liquiditySources0xOnly);
660+
expect(response.body).to.include({
660661
price,
661662
buyAmount: largeBuyAmount,
662663
sellTokenAddress,
663664
buyTokenAddress,
664-
sources: liquiditySources0xOnly,
665665
});
666666
});
667667

0 commit comments

Comments
 (0)