-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathbuildQuoteDetails.ts
More file actions
80 lines (69 loc) · 2.48 KB
/
Copy pathbuildQuoteDetails.ts
File metadata and controls
80 lines (69 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import BigNumber from "bignumber.js";
import type { RawQuote } from "../service/types";
import type { Quote, QuoteNetworkFeeAmount } from "../types";
import type { FeeEstimate } from "./networkFeeEstimate";
import { buildNetworkFees, buildPayoutNetworkFees } from "./networkFees";
import { buildPermitData } from "./permitData";
import { computeLiquiditySource, normalizeSlippage } from "./quoteHelpers";
import { buildTags } from "./tags";
import { buildTokenAllowance } from "./tokenAllowance";
export function buildQuoteDetails(
quote: RawQuote,
gasLess: boolean,
feeEstimate?: FeeEstimate,
): Quote["quoteDetails"] {
const details: Quote["quoteDetails"] = {
type: quote.type,
sendAmount: quote.amountFrom ?? 0,
receiveAmount: quote.amountTo,
gasLess,
networkFees: buildNetworkFees(quote),
slippage: normalizeSlippage(quote.slippage),
exchangeRate: quote.exchangeRate,
liquiditySource: computeLiquiditySource(quote),
};
const payoutNetworkFees = buildPayoutNetworkFees(quote);
if (payoutNetworkFees !== undefined) {
details.payoutNetworkFees = payoutNetworkFees;
}
const tokenAllowance = buildTokenAllowance(quote);
if (tokenAllowance !== undefined) {
details.tokenAllowance = tokenAllowance;
}
details.tags = buildTags(quote);
const permitData = buildPermitData(quote);
if (permitData !== undefined) {
details.permitData = permitData;
}
if (feeEstimate?.estimatedNetworkFee) {
details.estimatedNetworkFee = feeEstimate.estimatedNetworkFee;
}
if (feeEstimate?.approvalNetworkFee) {
details.approvalNetworkFee = feeEstimate.approvalNetworkFee;
}
const totalNetworkFee = buildTotalNetworkFee(feeEstimate);
if (totalNetworkFee) {
details.totalNetworkFee = totalNetworkFee;
}
return details;
}
/**
* Build the user-visible network fee total from the structured fee parts.
*/
function buildTotalNetworkFee(
feeEstimate: FeeEstimate | undefined,
): QuoteNetworkFeeAmount | undefined {
const estimatedNetworkFee = feeEstimate?.estimatedNetworkFee;
const approvalNetworkFee = feeEstimate?.approvalNetworkFee;
const currencyId = estimatedNetworkFee?.currencyId ?? approvalNetworkFee?.currencyId;
if (!currencyId) {
return undefined;
}
const estimated = new BigNumber(estimatedNetworkFee?.amount ?? 0);
const approval = new BigNumber(approvalNetworkFee?.amount ?? 0);
const amount = estimated.plus(approval);
if (!amount.gt(0)) {
return undefined;
}
return { amount: amount.toFixed(0), currencyId };
}