@metamask-previews/bridge-controller 79.1.0-preview-823dcff → 79.2.0-preview-9c0f91815
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -1
- package/dist/coercers/quote-response-v1-to-v2.cjs +13 -14
- package/dist/coercers/quote-response-v1-to-v2.cjs.map +1 -1
- package/dist/coercers/quote-response-v1-to-v2.mjs +13 -14
- package/dist/coercers/quote-response-v1-to-v2.mjs.map +1 -1
- package/dist/index.cjs +13 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +40 -4
- package/dist/selectors.cjs.map +1 -1
- package/dist/selectors.d.cts +1199 -105
- package/dist/selectors.d.cts.map +1 -1
- package/dist/selectors.d.mts +1199 -105
- package/dist/selectors.d.mts.map +1 -1
- package/dist/selectors.mjs +40 -4
- package/dist/selectors.mjs.map +1 -1
- package/dist/utils/assets.cjs +14 -1
- package/dist/utils/assets.cjs.map +1 -1
- package/dist/utils/assets.d.cts +1 -0
- package/dist/utils/assets.d.cts.map +1 -1
- package/dist/utils/assets.d.mts +1 -0
- package/dist/utils/assets.d.mts.map +1 -1
- package/dist/utils/assets.mjs +12 -0
- package/dist/utils/assets.mjs.map +1 -1
- package/dist/utils/number-formatters.cjs +2 -1
- package/dist/utils/number-formatters.cjs.map +1 -1
- package/dist/utils/number-formatters.d.cts.map +1 -1
- package/dist/utils/number-formatters.d.mts.map +1 -1
- package/dist/utils/number-formatters.mjs +2 -1
- package/dist/utils/number-formatters.mjs.map +1 -1
- package/dist/utils/quote-metadata/calculators.cjs +10 -15
- package/dist/utils/quote-metadata/calculators.cjs.map +1 -1
- package/dist/utils/quote-metadata/calculators.d.cts.map +1 -1
- package/dist/utils/quote-metadata/calculators.d.mts.map +1 -1
- package/dist/utils/quote-metadata/calculators.mjs +10 -15
- package/dist/utils/quote-metadata/calculators.mjs.map +1 -1
- package/dist/utils/quote-metadata/include-if-truthy.cjs +14 -0
- package/dist/utils/quote-metadata/include-if-truthy.cjs.map +1 -0
- package/dist/utils/quote-metadata/include-if-truthy.d.cts +2 -0
- package/dist/utils/quote-metadata/include-if-truthy.d.cts.map +1 -0
- package/dist/utils/quote-metadata/include-if-truthy.d.mts +2 -0
- package/dist/utils/quote-metadata/include-if-truthy.d.mts.map +1 -0
- package/dist/utils/quote-metadata/include-if-truthy.mjs +10 -0
- package/dist/utils/quote-metadata/include-if-truthy.mjs.map +1 -0
- package/dist/utils/quote-metadata/merge.cjs +12 -10
- package/dist/utils/quote-metadata/merge.cjs.map +1 -1
- package/dist/utils/quote-metadata/merge.d.cts +6 -3
- package/dist/utils/quote-metadata/merge.d.cts.map +1 -1
- package/dist/utils/quote-metadata/merge.d.mts +6 -3
- package/dist/utils/quote-metadata/merge.d.mts.map +1 -1
- package/dist/utils/quote-metadata/merge.mjs +12 -10
- package/dist/utils/quote-metadata/merge.mjs.map +1 -1
- package/dist/utils/quote-metadata/to-currency-values.cjs +58 -0
- package/dist/utils/quote-metadata/to-currency-values.cjs.map +1 -0
- package/dist/utils/quote-metadata/to-currency-values.d.cts +12 -0
- package/dist/utils/quote-metadata/to-currency-values.d.cts.map +1 -0
- package/dist/utils/quote-metadata/to-currency-values.d.mts +12 -0
- package/dist/utils/quote-metadata/to-currency-values.d.mts.map +1 -0
- package/dist/utils/quote-metadata/to-currency-values.mjs +54 -0
- package/dist/utils/quote-metadata/to-currency-values.mjs.map +1 -0
- package/dist/utils/quote-metadata/to-quote-metadata-v1.cjs +66 -16
- package/dist/utils/quote-metadata/to-quote-metadata-v1.cjs.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v1.d.cts +3 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v1.d.cts.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v1.d.mts +3 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v1.d.mts.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v1.mjs +66 -16
- package/dist/utils/quote-metadata/to-quote-metadata-v1.mjs.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v2.cjs +21 -42
- package/dist/utils/quote-metadata/to-quote-metadata-v2.cjs.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v2.d.cts.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v2.d.mts.map +1 -1
- package/dist/utils/quote-metadata/to-quote-metadata-v2.mjs +21 -42
- package/dist/utils/quote-metadata/to-quote-metadata-v2.mjs.map +1 -1
- package/dist/utils/quote-metadata/types.cjs +16 -0
- package/dist/utils/quote-metadata/types.cjs.map +1 -1
- package/dist/utils/quote-metadata/types.d.cts +21 -0
- package/dist/utils/quote-metadata/types.d.cts.map +1 -1
- package/dist/utils/quote-metadata/types.d.mts +21 -0
- package/dist/utils/quote-metadata/types.d.mts.map +1 -1
- package/dist/utils/quote-metadata/types.mjs +15 -1
- package/dist/utils/quote-metadata/types.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.d.mts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/merge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"merge.d.mts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/merge.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAuB;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,4CAA2C;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAmB;AAChD,OAAO,EAAE,2BAA2B,EAAE,oBAAmB;AAEzD;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAChC,aAAa,EAAE,aAAa,EAC5B,mBAAmB,GAAE,aAAkB,EACvC,cAAc,GAAE,2BAAgE,EAChF,cAAc,CAAC,EAAE,WAAW,CAAC,aAAa,CAAC,GAC1C,aAAa,GAAG,aAAa,CA8B/B"}
|
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
import { is } from "@metamask/superstruct";
|
|
2
1
|
import $lodash from "lodash";
|
|
3
2
|
const { merge } = $lodash;
|
|
4
|
-
import { QuoteResponseSchemaV1 } from "../../validators/quote-response-v1.mjs";
|
|
5
3
|
import { toNormalizedAmounts } from "./to-normalized-amounts.mjs";
|
|
6
4
|
import { toQuoteMetadataV2 } from "./to-quote-metadata-v2.mjs";
|
|
5
|
+
import { QuoteMetadataMigrationPhase } from "./types.mjs";
|
|
7
6
|
/**
|
|
8
7
|
* Merges legacy {@link QuoteMetadata} values into the {@link QuoteResponse}
|
|
9
8
|
*
|
|
10
|
-
* @param quoteResponse - The {@link QuoteResponse}
|
|
9
|
+
* @param quoteResponse - The {@link QuoteResponse} to merge the metadata into
|
|
11
10
|
* @param legacyQuoteMetadata - The {@link QuoteMetadata} values to merge
|
|
11
|
+
* @param migrationPhase - The active {@link QuoteMetadataMigrationPhase}
|
|
12
|
+
* @param currencyValues - The amounts in the user's currency, derived from the backend's `usd` values
|
|
12
13
|
* @returns The {@link QuoteResponse} with the metadata merged in
|
|
13
14
|
*/
|
|
14
|
-
export function mergeQuoteMetadata(quoteResponse, legacyQuoteMetadata) {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
export function mergeQuoteMetadata(quoteResponse, legacyQuoteMetadata = {}, migrationPhase = QuoteMetadataMigrationPhase.V1Data, currencyValues) {
|
|
16
|
+
const normalizedAmounts = toNormalizedAmounts(quoteResponse);
|
|
17
|
+
if (migrationPhase === QuoteMetadataMigrationPhase.V2Only) {
|
|
18
|
+
return merge({}, quoteResponse, normalizedAmounts, currencyValues);
|
|
17
19
|
}
|
|
18
20
|
const legacyQuoteMetadataV2 = toQuoteMetadataV2(legacyQuoteMetadata, quoteResponse);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
legacyQuoteMetadata);
|
|
21
|
+
if (migrationPhase === QuoteMetadataMigrationPhase.V2WithV1Fallback) {
|
|
22
|
+
return merge({}, legacyQuoteMetadataV2, legacyQuoteMetadata, quoteResponse, normalizedAmounts, currencyValues);
|
|
23
|
+
}
|
|
24
|
+
return merge({}, quoteResponse, normalizedAmounts, legacyQuoteMetadataV2, legacyQuoteMetadata);
|
|
23
25
|
}
|
|
24
26
|
//# sourceMappingURL=merge.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.mjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/merge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"merge.mjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/merge.ts"],"names":[],"mappings":";;AAIA,OAAO,EAAE,mBAAmB,EAAE,oCAAmC;AACjE,OAAO,EAAE,iBAAiB,EAAE,mCAAkC;AAE9D,OAAO,EAAE,2BAA2B,EAAE,oBAAmB;AAEzD;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAChC,aAA4B,EAC5B,sBAAqC,EAAE,EACvC,iBAA8C,2BAA2B,CAAC,MAAM,EAChF,cAA2C;IAE3C,MAAM,iBAAiB,GAAG,mBAAmB,CAAC,aAAa,CAAC,CAAC;IAE7D,IAAI,cAAc,KAAK,2BAA2B,CAAC,MAAM,EAAE,CAAC;QAC1D,OAAO,KAAK,CAAC,EAAE,EAAE,aAAa,EAAE,iBAAiB,EAAE,cAAc,CAAC,CAAC;IACrE,CAAC;IAED,MAAM,qBAAqB,GAAG,iBAAiB,CAC7C,mBAAmB,EACnB,aAAa,CACd,CAAC;IAEF,IAAI,cAAc,KAAK,2BAA2B,CAAC,gBAAgB,EAAE,CAAC;QACpE,OAAO,KAAK,CACV,EAAE,EACF,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EACb,iBAAiB,EACjB,cAAc,CACf,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CACV,EAAE,EACF,aAAa,EACb,iBAAiB,EACjB,qBAAqB,EACrB,mBAAmB,CACpB,CAAC;AACJ,CAAC","sourcesContent":["import { merge } from 'lodash';\n\nimport type { DeepPartial } from '../../types.js';\nimport type { QuoteResponse } from '../../validators/quote-response.js';\nimport { toNormalizedAmounts } from './to-normalized-amounts.js';\nimport { toQuoteMetadataV2 } from './to-quote-metadata-v2.js';\nimport type { QuoteMetadata } from './types.js';\nimport { QuoteMetadataMigrationPhase } from './types.js';\n\n/**\n * Merges legacy {@link QuoteMetadata} values into the {@link QuoteResponse}\n *\n * @param quoteResponse - The {@link QuoteResponse} to merge the metadata into\n * @param legacyQuoteMetadata - The {@link QuoteMetadata} values to merge\n * @param migrationPhase - The active {@link QuoteMetadataMigrationPhase}\n * @param currencyValues - The amounts in the user's currency, derived from the backend's `usd` values\n * @returns The {@link QuoteResponse} with the metadata merged in\n */\nexport function mergeQuoteMetadata(\n quoteResponse: QuoteResponse,\n legacyQuoteMetadata: QuoteMetadata = {},\n migrationPhase: QuoteMetadataMigrationPhase = QuoteMetadataMigrationPhase.V1Data,\n currencyValues?: DeepPartial<QuoteResponse>,\n): QuoteResponse & QuoteMetadata {\n const normalizedAmounts = toNormalizedAmounts(quoteResponse);\n\n if (migrationPhase === QuoteMetadataMigrationPhase.V2Only) {\n return merge({}, quoteResponse, normalizedAmounts, currencyValues);\n }\n\n const legacyQuoteMetadataV2 = toQuoteMetadataV2(\n legacyQuoteMetadata,\n quoteResponse,\n );\n\n if (migrationPhase === QuoteMetadataMigrationPhase.V2WithV1Fallback) {\n return merge(\n {},\n legacyQuoteMetadataV2,\n legacyQuoteMetadata,\n quoteResponse,\n normalizedAmounts,\n currencyValues,\n );\n }\n\n return merge(\n {},\n quoteResponse,\n normalizedAmounts,\n legacyQuoteMetadataV2,\n legacyQuoteMetadata,\n );\n}\n"]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toCurrencyValues = void 0;
|
|
4
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
5
|
+
const quote_js_1 = require("../../validators/quote.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Builds a partial {@link QuoteResponse} object with fiat values derived from the usd values provided by the bridge-api
|
|
8
|
+
*
|
|
9
|
+
* @param quote - The quote response to calculate the metadata for
|
|
10
|
+
* @param usdToFiatExchangeRate - The usd to fiat exchange rate
|
|
11
|
+
* @returns The partial {@link QuoteResponse} object with fiat values
|
|
12
|
+
*/
|
|
13
|
+
const toCurrencyValues = (quote, usdToFiatExchangeRate) => {
|
|
14
|
+
const { quote: { src, dest, feeData, priceData }, } = quote;
|
|
15
|
+
const { adjustedReturn, priceImpact } = priceData ?? {};
|
|
16
|
+
const toFiat = ({ usd, valueInCurrency, }) => {
|
|
17
|
+
if (usd && usdToFiatExchangeRate) {
|
|
18
|
+
return { valueInCurrency: usdToFiatExchangeRate.times(usd).toFixed() };
|
|
19
|
+
}
|
|
20
|
+
if (valueInCurrency) {
|
|
21
|
+
return { valueInCurrency };
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
};
|
|
25
|
+
const priceImpactFiat = priceImpact ? toFiat(priceImpact) : undefined;
|
|
26
|
+
const adjustedReturnFiat = adjustedReturn
|
|
27
|
+
? toFiat(adjustedReturn)
|
|
28
|
+
: undefined;
|
|
29
|
+
const minAmountValueInCurrency = toFiat({
|
|
30
|
+
usd: dest.minAmountUsd,
|
|
31
|
+
valueInCurrency: dest.minAmountValueInCurrency,
|
|
32
|
+
})?.valueInCurrency;
|
|
33
|
+
return {
|
|
34
|
+
quote: {
|
|
35
|
+
src: toFiat(src),
|
|
36
|
+
dest: {
|
|
37
|
+
...toFiat(dest),
|
|
38
|
+
...(minAmountValueInCurrency && {
|
|
39
|
+
minAmountValueInCurrency,
|
|
40
|
+
}),
|
|
41
|
+
},
|
|
42
|
+
feeData: feeData &&
|
|
43
|
+
Object.fromEntries(Object.values(quote_js_1.FeeType)
|
|
44
|
+
.filter((feeType) => feeData[feeType])
|
|
45
|
+
.map((feeType) => [feeType, feeData[feeType]?.map(toFiat)])),
|
|
46
|
+
priceData: {
|
|
47
|
+
...(priceImpactFiat && {
|
|
48
|
+
priceImpact: priceImpactFiat,
|
|
49
|
+
}),
|
|
50
|
+
...(adjustedReturnFiat && {
|
|
51
|
+
adjustedReturn: adjustedReturnFiat,
|
|
52
|
+
}),
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
exports.toCurrencyValues = toCurrencyValues;
|
|
58
|
+
//# sourceMappingURL=to-currency-values.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-currency-values.cjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-currency-values.ts"],"names":[],"mappings":";;;AAAA,+CAAyC;AAKzC,yDAAoD;AAEpD;;;;;;GAMG;AACI,MAAM,gBAAgB,GAAG,CAC9B,KAAoB,EACpB,qBAAiC,EACL,EAAE;IAC9B,MAAM,EACJ,KAAK,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,GACzC,GAAG,KAAK,CAAC;IAEV,MAAM,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,SAAS,IAAI,EAAE,CAAC;IAExD,MAAM,MAAM,GAAG,CAAC,EACd,GAAG,EACH,eAAe,GACkC,EAErC,EAAE;QACd,IAAI,GAAG,IAAI,qBAAqB,EAAE,CAAC;YACjC,OAAO,EAAE,eAAe,EAAE,qBAAqB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;QACzE,CAAC;QACD,IAAI,eAAe,EAAE,CAAC;YACpB,OAAO,EAAE,eAAe,EAAE,CAAC;QAC7B,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;IAEF,MAAM,eAAe,GAAG,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACtE,MAAM,kBAAkB,GAAG,cAAc;QACvC,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;QACxB,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,wBAAwB,GAAG,MAAM,CAAC;QACtC,GAAG,EAAE,IAAI,CAAC,YAAY;QACtB,eAAe,EAAE,IAAI,CAAC,wBAAwB;KAC/C,CAAC,EAAE,eAAe,CAAC;IAEpB,OAAO;QACL,KAAK,EAAE;YACL,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC;YAChB,IAAI,EAAE;gBACJ,GAAG,MAAM,CAAC,IAAI,CAAC;gBACf,GAAG,CAAC,wBAAwB,IAAI;oBAC9B,wBAAwB;iBACzB,CAAC;aACH;YACD,OAAO,EACL,OAAO;gBACP,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,MAAM,CAAC,kBAAO,CAAC;qBACnB,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;qBACrC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAC9D;YACH,SAAS,EAAE;gBACT,GAAG,CAAC,eAAe,IAAI;oBACrB,WAAW,EAAE,eAAe;iBAC7B,CAAC;gBACF,GAAG,CAAC,kBAAkB,IAAI;oBACxB,cAAc,EAAE,kBAAkB;iBACnC,CAAC;aACH;SACF;KACF,CAAC;AACJ,CAAC,CAAC;AA7DW,QAAA,gBAAgB,oBA6D3B","sourcesContent":["import { BigNumber } from 'bignumber.js';\n\nimport type { DeepPartial } from '../../types.js';\nimport type { AmountsAndAsset } from '../../validators/amount-and-asset.js';\nimport type { QuoteResponse } from '../../validators/quote-response.js';\nimport { FeeType } from '../../validators/quote.js';\n\n/**\n * Builds a partial {@link QuoteResponse} object with fiat values derived from the usd values provided by the bridge-api\n *\n * @param quote - The quote response to calculate the metadata for\n * @param usdToFiatExchangeRate - The usd to fiat exchange rate\n * @returns The partial {@link QuoteResponse} object with fiat values\n */\nexport const toCurrencyValues = (\n quote: QuoteResponse,\n usdToFiatExchangeRate?: BigNumber,\n): DeepPartial<QuoteResponse> => {\n const {\n quote: { src, dest, feeData, priceData },\n } = quote;\n\n const { adjustedReturn, priceImpact } = priceData ?? {};\n\n const toFiat = ({\n usd,\n valueInCurrency,\n }: Pick<AmountsAndAsset, 'usd' | 'valueInCurrency'>):\n | Pick<AmountsAndAsset, 'valueInCurrency'>\n | undefined => {\n if (usd && usdToFiatExchangeRate) {\n return { valueInCurrency: usdToFiatExchangeRate.times(usd).toFixed() };\n }\n if (valueInCurrency) {\n return { valueInCurrency };\n }\n return undefined;\n };\n\n const priceImpactFiat = priceImpact ? toFiat(priceImpact) : undefined;\n const adjustedReturnFiat = adjustedReturn\n ? toFiat(adjustedReturn)\n : undefined;\n\n const minAmountValueInCurrency = toFiat({\n usd: dest.minAmountUsd,\n valueInCurrency: dest.minAmountValueInCurrency,\n })?.valueInCurrency;\n\n return {\n quote: {\n src: toFiat(src),\n dest: {\n ...toFiat(dest),\n ...(minAmountValueInCurrency && {\n minAmountValueInCurrency,\n }),\n },\n feeData:\n feeData &&\n Object.fromEntries(\n Object.values(FeeType)\n .filter((feeType) => feeData[feeType])\n .map((feeType) => [feeType, feeData[feeType]?.map(toFiat)]),\n ),\n priceData: {\n ...(priceImpactFiat && {\n priceImpact: priceImpactFiat,\n }),\n ...(adjustedReturnFiat && {\n adjustedReturn: adjustedReturnFiat,\n }),\n },\n },\n };\n};\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BigNumber } from "bignumber.js";
|
|
2
|
+
import type { DeepPartial } from "../../types.cjs";
|
|
3
|
+
import type { QuoteResponse } from "../../validators/quote-response.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Builds a partial {@link QuoteResponse} object with fiat values derived from the usd values provided by the bridge-api
|
|
6
|
+
*
|
|
7
|
+
* @param quote - The quote response to calculate the metadata for
|
|
8
|
+
* @param usdToFiatExchangeRate - The usd to fiat exchange rate
|
|
9
|
+
* @returns The partial {@link QuoteResponse} object with fiat values
|
|
10
|
+
*/
|
|
11
|
+
export declare const toCurrencyValues: (quote: QuoteResponse, usdToFiatExchangeRate?: BigNumber) => DeepPartial<QuoteResponse>;
|
|
12
|
+
//# sourceMappingURL=to-currency-values.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-currency-values.d.cts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-currency-values.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAuB;AAElD,OAAO,KAAK,EAAE,aAAa,EAAE,4CAA2C;AAGxE;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,UACpB,aAAa,0BACI,SAAS,KAChC,YAAY,aAAa,CA0D3B,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BigNumber } from "bignumber.js";
|
|
2
|
+
import type { DeepPartial } from "../../types.mjs";
|
|
3
|
+
import type { QuoteResponse } from "../../validators/quote-response.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Builds a partial {@link QuoteResponse} object with fiat values derived from the usd values provided by the bridge-api
|
|
6
|
+
*
|
|
7
|
+
* @param quote - The quote response to calculate the metadata for
|
|
8
|
+
* @param usdToFiatExchangeRate - The usd to fiat exchange rate
|
|
9
|
+
* @returns The partial {@link QuoteResponse} object with fiat values
|
|
10
|
+
*/
|
|
11
|
+
export declare const toCurrencyValues: (quote: QuoteResponse, usdToFiatExchangeRate?: BigNumber) => DeepPartial<QuoteResponse>;
|
|
12
|
+
//# sourceMappingURL=to-currency-values.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-currency-values.d.mts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-currency-values.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAuB;AAElD,OAAO,KAAK,EAAE,aAAa,EAAE,4CAA2C;AAGxE;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,UACpB,aAAa,0BACI,SAAS,KAChC,YAAY,aAAa,CA0D3B,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BigNumber } from "bignumber.js";
|
|
2
|
+
import { FeeType } from "../../validators/quote.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Builds a partial {@link QuoteResponse} object with fiat values derived from the usd values provided by the bridge-api
|
|
5
|
+
*
|
|
6
|
+
* @param quote - The quote response to calculate the metadata for
|
|
7
|
+
* @param usdToFiatExchangeRate - The usd to fiat exchange rate
|
|
8
|
+
* @returns The partial {@link QuoteResponse} object with fiat values
|
|
9
|
+
*/
|
|
10
|
+
export const toCurrencyValues = (quote, usdToFiatExchangeRate) => {
|
|
11
|
+
const { quote: { src, dest, feeData, priceData }, } = quote;
|
|
12
|
+
const { adjustedReturn, priceImpact } = priceData ?? {};
|
|
13
|
+
const toFiat = ({ usd, valueInCurrency, }) => {
|
|
14
|
+
if (usd && usdToFiatExchangeRate) {
|
|
15
|
+
return { valueInCurrency: usdToFiatExchangeRate.times(usd).toFixed() };
|
|
16
|
+
}
|
|
17
|
+
if (valueInCurrency) {
|
|
18
|
+
return { valueInCurrency };
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
};
|
|
22
|
+
const priceImpactFiat = priceImpact ? toFiat(priceImpact) : undefined;
|
|
23
|
+
const adjustedReturnFiat = adjustedReturn
|
|
24
|
+
? toFiat(adjustedReturn)
|
|
25
|
+
: undefined;
|
|
26
|
+
const minAmountValueInCurrency = toFiat({
|
|
27
|
+
usd: dest.minAmountUsd,
|
|
28
|
+
valueInCurrency: dest.minAmountValueInCurrency,
|
|
29
|
+
})?.valueInCurrency;
|
|
30
|
+
return {
|
|
31
|
+
quote: {
|
|
32
|
+
src: toFiat(src),
|
|
33
|
+
dest: {
|
|
34
|
+
...toFiat(dest),
|
|
35
|
+
...(minAmountValueInCurrency && {
|
|
36
|
+
minAmountValueInCurrency,
|
|
37
|
+
}),
|
|
38
|
+
},
|
|
39
|
+
feeData: feeData &&
|
|
40
|
+
Object.fromEntries(Object.values(FeeType)
|
|
41
|
+
.filter((feeType) => feeData[feeType])
|
|
42
|
+
.map((feeType) => [feeType, feeData[feeType]?.map(toFiat)])),
|
|
43
|
+
priceData: {
|
|
44
|
+
...(priceImpactFiat && {
|
|
45
|
+
priceImpact: priceImpactFiat,
|
|
46
|
+
}),
|
|
47
|
+
...(adjustedReturnFiat && {
|
|
48
|
+
adjustedReturn: adjustedReturnFiat,
|
|
49
|
+
}),
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=to-currency-values.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-currency-values.mjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-currency-values.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAKzC,OAAO,EAAE,OAAO,EAAE,mCAAkC;AAEpD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,KAAoB,EACpB,qBAAiC,EACL,EAAE;IAC9B,MAAM,EACJ,KAAK,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,GACzC,GAAG,KAAK,CAAC;IAEV,MAAM,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,SAAS,IAAI,EAAE,CAAC;IAExD,MAAM,MAAM,GAAG,CAAC,EACd,GAAG,EACH,eAAe,GACkC,EAErC,EAAE;QACd,IAAI,GAAG,IAAI,qBAAqB,EAAE,C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B;iBACnC,CAAC;aACH;SACF;KACF,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { BigNumber } from 'bignumber.js';\n\nimport type { DeepPartial } from '../../types.js';\nimport type { AmountsAndAsset } from '../../validators/amount-and-asset.js';\nimport type { QuoteResponse } from '../../validators/quote-response.js';\nimport { FeeType } from '../../validators/quote.js';\n\n/**\n * Builds a partial {@link QuoteResponse} object with fiat values derived from the usd values provided by the bridge-api\n *\n * @param quote - The quote response to calculate the metadata for\n * @param usdToFiatExchangeRate - The usd to fiat exchange rate\n * @returns The partial {@link QuoteResponse} object with fiat values\n */\nexport const toCurrencyValues = (\n quote: QuoteResponse,\n usdToFiatExchangeRate?: BigNumber,\n): DeepPartial<QuoteResponse> => {\n const {\n quote: { src, dest, feeData, priceData },\n } = quote;\n\n const { adjustedReturn, priceImpact } = priceData ?? {};\n\n const toFiat = ({\n usd,\n valueInCurrency,\n }: Pick<AmountsAndAsset, 'usd' | 'valueInCurrency'>):\n | Pick<AmountsAndAsset, 'valueInCurrency'>\n | undefined => {\n if (usd && usdToFiatExchangeRate) {\n return { valueInCurrency: usdToFiatExchangeRate.times(usd).toFixed() };\n }\n if (valueInCurrency) {\n return { valueInCurrency };\n }\n return undefined;\n };\n\n const priceImpactFiat = priceImpact ? toFiat(priceImpact) : undefined;\n const adjustedReturnFiat = adjustedReturn\n ? toFiat(adjustedReturn)\n : undefined;\n\n const minAmountValueInCurrency = toFiat({\n usd: dest.minAmountUsd,\n valueInCurrency: dest.minAmountValueInCurrency,\n })?.valueInCurrency;\n\n return {\n quote: {\n src: toFiat(src),\n dest: {\n ...toFiat(dest),\n ...(minAmountValueInCurrency && {\n minAmountValueInCurrency,\n }),\n },\n feeData:\n feeData &&\n Object.fromEntries(\n Object.values(FeeType)\n .filter((feeType) => feeData[feeType])\n .map((feeType) => [feeType, feeData[feeType]?.map(toFiat)]),\n ),\n priceData: {\n ...(priceImpactFiat && {\n priceImpact: priceImpactFiat,\n }),\n ...(adjustedReturnFiat && {\n adjustedReturn: adjustedReturnFiat,\n }),\n },\n },\n };\n};\n"]}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toQuoteMetadataV1 = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
4
5
|
const lodash_1 = require("lodash");
|
|
6
|
+
const quote_response_js_1 = require("../../validators/quote-response.cjs");
|
|
7
|
+
const number_formatters_js_1 = require("../number-formatters.cjs");
|
|
8
|
+
const include_if_truthy_js_1 = require("./include-if-truthy.cjs");
|
|
9
|
+
const types_js_1 = require("./types.cjs");
|
|
5
10
|
/**
|
|
6
11
|
* Extracts legacy {@link QuoteMetadata} values from a {@link QuoteResponse} or {@link QuoteResponseV1}.
|
|
7
12
|
* If a QuoteResponse is provided, this assumes that its `valueInCurrency` properties are set.
|
|
8
13
|
*
|
|
9
14
|
* @param quoteResponse - The quote to extract the metadata from
|
|
15
|
+
* @param migrationPhase - The migration phase to use
|
|
10
16
|
* @returns A partial {@link QuoteMetadata} object
|
|
11
17
|
*/
|
|
12
|
-
const toQuoteMetadataV1 = (quoteResponse) => {
|
|
18
|
+
const toQuoteMetadataV1 = (quoteResponse, migrationPhase = types_js_1.QuoteMetadataMigrationPhase.V1Data) => {
|
|
13
19
|
/* istanbul ignore if */
|
|
14
20
|
if (!quoteResponse) {
|
|
15
21
|
return {};
|
|
@@ -22,22 +28,66 @@ const toQuoteMetadataV1 = (quoteResponse) => {
|
|
|
22
28
|
swapRate,
|
|
23
29
|
gasFee,
|
|
24
30
|
totalNetworkFee,
|
|
25
|
-
...(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
...(
|
|
29
|
-
...(
|
|
30
|
-
? { priceImpact }
|
|
31
|
-
: {}),
|
|
32
|
-
...(relayerFee && Object.values(relayerFee).some(Boolean)
|
|
33
|
-
? { relayerFee }
|
|
34
|
-
: {}),
|
|
35
|
-
...(includedTxFees && Object.values(includedTxFees).some(Boolean)
|
|
36
|
-
? { includedTxFees }
|
|
37
|
-
: {}),
|
|
31
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(adjustedReturn, { adjustedReturn }),
|
|
32
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(cost, { cost }),
|
|
33
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(priceImpact, { priceImpact }),
|
|
34
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(relayerFee, { relayerFee }),
|
|
35
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(includedTxFees, { includedTxFees }),
|
|
38
36
|
};
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
if (migrationPhase === types_js_1.QuoteMetadataMigrationPhase.V1Data ||
|
|
38
|
+
!(0, superstruct_1.is)(quoteResponse, quote_response_js_1.QuoteResponseSchemaV2)) {
|
|
39
|
+
return legacyMetadata;
|
|
40
|
+
}
|
|
41
|
+
const { quote: { src, dest, priceData, feeData: { network, relayer, txFee }, }, } = quoteResponse;
|
|
42
|
+
const totalNetworkFeeV2 = (0, number_formatters_js_1.sumAmounts)(network, relayer);
|
|
43
|
+
const toTokenAmountValues = (data) => {
|
|
44
|
+
return {
|
|
45
|
+
amount: data?.normalizedAmount,
|
|
46
|
+
usd: data?.usd,
|
|
47
|
+
valueInCurrency: data?.valueInCurrency,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
// Build V1 from V2 quote
|
|
51
|
+
const v2Metadata = {
|
|
52
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(src, {
|
|
53
|
+
sentAmount: toTokenAmountValues(src),
|
|
54
|
+
}),
|
|
55
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(dest, {
|
|
56
|
+
toTokenAmount: toTokenAmountValues(dest),
|
|
57
|
+
minToTokenAmount: {
|
|
58
|
+
amount: dest.minAmountNormalized,
|
|
59
|
+
valueInCurrency: dest.minAmountValueInCurrency,
|
|
60
|
+
usd: dest.minAmountUsd,
|
|
61
|
+
},
|
|
62
|
+
}),
|
|
63
|
+
...(priceData?.swapRate && { swapRate: priceData.swapRate }),
|
|
64
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(priceData?.adjustedReturn, {
|
|
65
|
+
adjustedReturn: toTokenAmountValues(priceData?.adjustedReturn),
|
|
66
|
+
}),
|
|
67
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(network?.[0], {
|
|
68
|
+
gasFee: {
|
|
69
|
+
total: toTokenAmountValues(network?.[0]),
|
|
70
|
+
},
|
|
71
|
+
}),
|
|
72
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(totalNetworkFeeV2, {
|
|
73
|
+
totalNetworkFee: toTokenAmountValues(totalNetworkFeeV2),
|
|
74
|
+
}),
|
|
75
|
+
...(priceData?.priceImpact && {
|
|
76
|
+
priceImpact: toTokenAmountValues(priceData?.priceImpact),
|
|
77
|
+
// Use priceImpact as cost
|
|
78
|
+
cost: toTokenAmountValues(priceData?.priceImpact),
|
|
79
|
+
}),
|
|
80
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(relayer?.[0], {
|
|
81
|
+
relayerFee: toTokenAmountValues(relayer?.[0]),
|
|
82
|
+
}),
|
|
83
|
+
...(0, include_if_truthy_js_1.includeIfTruthy)(txFee?.[0], {
|
|
84
|
+
includedTxFees: toTokenAmountValues(txFee?.[0]),
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
if (migrationPhase === types_js_1.QuoteMetadataMigrationPhase.V2WithV1Fallback) {
|
|
88
|
+
return (0, lodash_1.merge)({}, legacyMetadata, v2Metadata);
|
|
89
|
+
}
|
|
90
|
+
return v2Metadata;
|
|
41
91
|
};
|
|
42
92
|
exports.toQuoteMetadataV1 = toQuoteMetadataV1;
|
|
43
93
|
//# sourceMappingURL=to-quote-metadata-v1.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-quote-metadata-v1.cjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":";;;AAAA,mCAA+B;
|
|
1
|
+
{"version":3,"file":"to-quote-metadata-v1.cjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":";;;AAAA,uDAA2C;AAC3C,mCAA+B;AAK/B,2EAA2E;AAE3E,mEAAqD;AACrD,kEAAyD;AAEzD,0CAAyD;AAEzD;;;;;;;GAOG;AACI,MAAM,iBAAiB,GAAG,CAC/B,aAEQ,EACR,iBAA8C,sCAA2B,CAAC,MAAM,EACjE,EAAE;IACjB,wBAAwB;IACxB,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,EACJ,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,cAAc,EACd,IAAI,EACJ,cAAc,EACd,UAAU,EACV,eAAe,EACf,MAAM,EACN,WAAW,GACZ,GAAG,aAAa,CAAC;IAElB,MAAM,cAAc,GAAG;QACrB,UAAU;QACV,aAAa;QACb,gBAAgB;QAChB,QAAQ;QACR,MAAM;QACN,eAAe;QACf,GAAG,IAAA,sCAAe,EAAC,cAAc,EAAE,EAAE,cAAc,EAAE,CAAC;QACtD,GAAG,IAAA,sCAAe,EAAC,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC;QAClC,GAAG,IAAA,sCAAe,EAAC,WAAW,EAAE,EAAE,WAAW,EAAE,CAAC;QAChD,GAAG,IAAA,sCAAe,EAAC,UAAU,EAAE,EAAE,UAAU,EAAE,CAAC;QAC9C,GAAG,IAAA,sCAAe,EAAC,cAAc,EAAE,EAAE,cAAc,EAAE,CAAC;KACvD,CAAC;IAEF,IACE,cAAc,KAAK,sCAA2B,CAAC,MAAM;QACrD,CAAC,IAAA,gBAAE,EAAC,aAAa,EAAE,yCAAqB,CAAC,EACzC,CAAC;QACD,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,MAAM,EACJ,KAAK,EAA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Ae,EAAE,mBAAmB,CAAC,iBAAiB,CAAC;SACxD,CAAC;QACF,GAAG,CAAC,SAAS,EAAE,WAAW,IAAI;YAC5B,WAAW,EAAE,mBAAmB,CAAC,SAAS,EAAE,WAAW,CAAC;YACxD,0BAA0B;YAC1B,IAAI,EAAE,mBAAmB,CAAC,SAAS,EAAE,WAAW,CAAC;SAClD,CAAC;QACF,GAAG,IAAA,sCAAe,EAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE;YAC/B,UAAU,EAAE,mBAAmB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;SAC9C,CAAC;QACF,GAAG,IAAA,sCAAe,EAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE;YAC7B,cAAc,EAAE,mBAAmB,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;SAChD,CAAC;KACH,CAAC;IAEF,IAAI,cAAc,KAAK,sCAA2B,CAAC,gBAAgB,EAAE,CAAC;QACpE,OAAO,IAAA,cAAK,EAAC,EAAE,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AAjHW,QAAA,iBAAiB,qBAiH5B","sourcesContent":["import { is } from '@metamask/superstruct';\nimport { merge } from 'lodash';\n\nimport type { DeepPartial } from '../../types.js';\nimport type { AmountsAndAsset } from '../../validators/amount-and-asset.js';\nimport type { QuoteResponseV1 } from '../../validators/quote-response-v1.js';\nimport { QuoteResponseSchemaV2 } from '../../validators/quote-response.js';\nimport type { QuoteResponse } from '../../validators/quote-response.js';\nimport { sumAmounts } from '../number-formatters.js';\nimport { includeIfTruthy } from './include-if-truthy.js';\nimport type { QuoteMetadata, TokenAmountValues } from './types.js';\nimport { QuoteMetadataMigrationPhase } from './types.js';\n\n/**\n * Extracts legacy {@link QuoteMetadata} values from a {@link QuoteResponse} or {@link QuoteResponseV1}.\n * If a QuoteResponse is provided, this assumes that its `valueInCurrency` properties are set.\n *\n * @param quoteResponse - The quote to extract the metadata from\n * @param migrationPhase - The migration phase to use\n * @returns A partial {@link QuoteMetadata} object\n */\nexport const toQuoteMetadataV1 = (\n quoteResponse:\n | (DeepPartial<QuoteResponse | QuoteResponseV1> & QuoteMetadata)\n | null,\n migrationPhase: QuoteMetadataMigrationPhase = QuoteMetadataMigrationPhase.V1Data,\n): QuoteMetadata => {\n /* istanbul ignore if */\n if (!quoteResponse) {\n return {};\n }\n\n const {\n toTokenAmount,\n minToTokenAmount,\n sentAmount,\n swapRate,\n adjustedReturn,\n cost,\n includedTxFees,\n relayerFee,\n totalNetworkFee,\n gasFee,\n priceImpact,\n } = quoteResponse;\n\n const legacyMetadata = {\n sentAmount,\n toTokenAmount,\n minToTokenAmount,\n swapRate,\n gasFee,\n totalNetworkFee,\n ...includeIfTruthy(adjustedReturn, { adjustedReturn }),\n ...includeIfTruthy(cost, { cost }),\n ...includeIfTruthy(priceImpact, { priceImpact }),\n ...includeIfTruthy(relayerFee, { relayerFee }),\n ...includeIfTruthy(includedTxFees, { includedTxFees }),\n };\n\n if (\n migrationPhase === QuoteMetadataMigrationPhase.V1Data ||\n !is(quoteResponse, QuoteResponseSchemaV2)\n ) {\n return legacyMetadata;\n }\n\n const {\n quote: {\n src,\n dest,\n priceData,\n feeData: { network, relayer, txFee },\n },\n } = quoteResponse;\n\n const totalNetworkFeeV2 = sumAmounts(network, relayer);\n\n const toTokenAmountValues = (\n data?: Pick<\n AmountsAndAsset,\n 'normalizedAmount' | 'usd' | 'valueInCurrency'\n >,\n ): Partial<TokenAmountValues> => {\n return {\n amount: data?.normalizedAmount,\n usd: data?.usd,\n valueInCurrency: data?.valueInCurrency,\n };\n };\n\n // Build V1 from V2 quote\n const v2Metadata: QuoteMetadata = {\n ...includeIfTruthy(src, {\n sentAmount: toTokenAmountValues(src),\n }),\n ...includeIfTruthy(dest, {\n toTokenAmount: toTokenAmountValues(dest),\n minToTokenAmount: {\n amount: dest.minAmountNormalized,\n valueInCurrency: dest.minAmountValueInCurrency,\n usd: dest.minAmountUsd,\n },\n }),\n ...(priceData?.swapRate && { swapRate: priceData.swapRate }),\n ...includeIfTruthy(priceData?.adjustedReturn, {\n adjustedReturn: toTokenAmountValues(priceData?.adjustedReturn),\n }),\n ...includeIfTruthy(network?.[0], {\n gasFee: {\n total: toTokenAmountValues(network?.[0]),\n },\n }),\n ...includeIfTruthy(totalNetworkFeeV2, {\n totalNetworkFee: toTokenAmountValues(totalNetworkFeeV2),\n }),\n ...(priceData?.priceImpact && {\n priceImpact: toTokenAmountValues(priceData?.priceImpact),\n // Use priceImpact as cost\n cost: toTokenAmountValues(priceData?.priceImpact),\n }),\n ...includeIfTruthy(relayer?.[0], {\n relayerFee: toTokenAmountValues(relayer?.[0]),\n }),\n ...includeIfTruthy(txFee?.[0], {\n includedTxFees: toTokenAmountValues(txFee?.[0]),\n }),\n };\n\n if (migrationPhase === QuoteMetadataMigrationPhase.V2WithV1Fallback) {\n return merge({}, legacyMetadata, v2Metadata);\n }\n\n return v2Metadata;\n};\n"]}
|
|
@@ -2,12 +2,14 @@ import type { DeepPartial } from "../../types.cjs";
|
|
|
2
2
|
import type { QuoteResponseV1 } from "../../validators/quote-response-v1.cjs";
|
|
3
3
|
import type { QuoteResponse } from "../../validators/quote-response.cjs";
|
|
4
4
|
import type { QuoteMetadata } from "./types.cjs";
|
|
5
|
+
import { QuoteMetadataMigrationPhase } from "./types.cjs";
|
|
5
6
|
/**
|
|
6
7
|
* Extracts legacy {@link QuoteMetadata} values from a {@link QuoteResponse} or {@link QuoteResponseV1}.
|
|
7
8
|
* If a QuoteResponse is provided, this assumes that its `valueInCurrency` properties are set.
|
|
8
9
|
*
|
|
9
10
|
* @param quoteResponse - The quote to extract the metadata from
|
|
11
|
+
* @param migrationPhase - The migration phase to use
|
|
10
12
|
* @returns A partial {@link QuoteMetadata} object
|
|
11
13
|
*/
|
|
12
|
-
export declare const toQuoteMetadataV1: (quoteResponse: (DeepPartial<QuoteResponse | QuoteResponseV1> & QuoteMetadata) | null) => QuoteMetadata;
|
|
14
|
+
export declare const toQuoteMetadataV1: (quoteResponse: (DeepPartial<QuoteResponse | QuoteResponseV1> & QuoteMetadata) | null, migrationPhase?: QuoteMetadataMigrationPhase) => QuoteMetadata;
|
|
13
15
|
//# sourceMappingURL=to-quote-metadata-v1.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-quote-metadata-v1.d.cts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-quote-metadata-v1.d.cts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAuB;AAElD,OAAO,KAAK,EAAE,eAAe,EAAE,+CAA8C;AAE7E,OAAO,KAAK,EAAE,aAAa,EAAE,4CAA2C;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAqB,oBAAmB;AACnE,OAAO,EAAE,2BAA2B,EAAE,oBAAmB;AAEzD;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,kBAExB,CAAC,YAAY,aAAa,GAAG,eAAe,CAAC,GAAG,aAAa,CAAC,GAC9D,IAAI,mBACQ,2BAA2B,KAC1C,aA4GF,CAAC"}
|
|
@@ -2,12 +2,14 @@ import type { DeepPartial } from "../../types.mjs";
|
|
|
2
2
|
import type { QuoteResponseV1 } from "../../validators/quote-response-v1.mjs";
|
|
3
3
|
import type { QuoteResponse } from "../../validators/quote-response.mjs";
|
|
4
4
|
import type { QuoteMetadata } from "./types.mjs";
|
|
5
|
+
import { QuoteMetadataMigrationPhase } from "./types.mjs";
|
|
5
6
|
/**
|
|
6
7
|
* Extracts legacy {@link QuoteMetadata} values from a {@link QuoteResponse} or {@link QuoteResponseV1}.
|
|
7
8
|
* If a QuoteResponse is provided, this assumes that its `valueInCurrency` properties are set.
|
|
8
9
|
*
|
|
9
10
|
* @param quoteResponse - The quote to extract the metadata from
|
|
11
|
+
* @param migrationPhase - The migration phase to use
|
|
10
12
|
* @returns A partial {@link QuoteMetadata} object
|
|
11
13
|
*/
|
|
12
|
-
export declare const toQuoteMetadataV1: (quoteResponse: (DeepPartial<QuoteResponse | QuoteResponseV1> & QuoteMetadata) | null) => QuoteMetadata;
|
|
14
|
+
export declare const toQuoteMetadataV1: (quoteResponse: (DeepPartial<QuoteResponse | QuoteResponseV1> & QuoteMetadata) | null, migrationPhase?: QuoteMetadataMigrationPhase) => QuoteMetadata;
|
|
13
15
|
//# sourceMappingURL=to-quote-metadata-v1.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-quote-metadata-v1.d.mts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-quote-metadata-v1.d.mts","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAuB;AAElD,OAAO,KAAK,EAAE,eAAe,EAAE,+CAA8C;AAE7E,OAAO,KAAK,EAAE,aAAa,EAAE,4CAA2C;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAqB,oBAAmB;AACnE,OAAO,EAAE,2BAA2B,EAAE,oBAAmB;AAEzD;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,kBAExB,CAAC,YAAY,aAAa,GAAG,eAAe,CAAC,GAAG,aAAa,CAAC,GAC9D,IAAI,mBACQ,2BAA2B,KAC1C,aA4GF,CAAC"}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
+
import { is } from "@metamask/superstruct";
|
|
1
2
|
import $lodash from "lodash";
|
|
2
3
|
const { merge } = $lodash;
|
|
4
|
+
import { QuoteResponseSchemaV2 } from "../../validators/quote-response.mjs";
|
|
5
|
+
import { sumAmounts } from "../number-formatters.mjs";
|
|
6
|
+
import { includeIfTruthy } from "./include-if-truthy.mjs";
|
|
7
|
+
import { QuoteMetadataMigrationPhase } from "./types.mjs";
|
|
3
8
|
/**
|
|
4
9
|
* Extracts legacy {@link QuoteMetadata} values from a {@link QuoteResponse} or {@link QuoteResponseV1}.
|
|
5
10
|
* If a QuoteResponse is provided, this assumes that its `valueInCurrency` properties are set.
|
|
6
11
|
*
|
|
7
12
|
* @param quoteResponse - The quote to extract the metadata from
|
|
13
|
+
* @param migrationPhase - The migration phase to use
|
|
8
14
|
* @returns A partial {@link QuoteMetadata} object
|
|
9
15
|
*/
|
|
10
|
-
export const toQuoteMetadataV1 = (quoteResponse) => {
|
|
16
|
+
export const toQuoteMetadataV1 = (quoteResponse, migrationPhase = QuoteMetadataMigrationPhase.V1Data) => {
|
|
11
17
|
/* istanbul ignore if */
|
|
12
18
|
if (!quoteResponse) {
|
|
13
19
|
return {};
|
|
@@ -20,21 +26,65 @@ export const toQuoteMetadataV1 = (quoteResponse) => {
|
|
|
20
26
|
swapRate,
|
|
21
27
|
gasFee,
|
|
22
28
|
totalNetworkFee,
|
|
23
|
-
...(adjustedReturn
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
...(
|
|
27
|
-
...(
|
|
28
|
-
? { priceImpact }
|
|
29
|
-
: {}),
|
|
30
|
-
...(relayerFee && Object.values(relayerFee).some(Boolean)
|
|
31
|
-
? { relayerFee }
|
|
32
|
-
: {}),
|
|
33
|
-
...(includedTxFees && Object.values(includedTxFees).some(Boolean)
|
|
34
|
-
? { includedTxFees }
|
|
35
|
-
: {}),
|
|
29
|
+
...includeIfTruthy(adjustedReturn, { adjustedReturn }),
|
|
30
|
+
...includeIfTruthy(cost, { cost }),
|
|
31
|
+
...includeIfTruthy(priceImpact, { priceImpact }),
|
|
32
|
+
...includeIfTruthy(relayerFee, { relayerFee }),
|
|
33
|
+
...includeIfTruthy(includedTxFees, { includedTxFees }),
|
|
36
34
|
};
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
if (migrationPhase === QuoteMetadataMigrationPhase.V1Data ||
|
|
36
|
+
!is(quoteResponse, QuoteResponseSchemaV2)) {
|
|
37
|
+
return legacyMetadata;
|
|
38
|
+
}
|
|
39
|
+
const { quote: { src, dest, priceData, feeData: { network, relayer, txFee }, }, } = quoteResponse;
|
|
40
|
+
const totalNetworkFeeV2 = sumAmounts(network, relayer);
|
|
41
|
+
const toTokenAmountValues = (data) => {
|
|
42
|
+
return {
|
|
43
|
+
amount: data?.normalizedAmount,
|
|
44
|
+
usd: data?.usd,
|
|
45
|
+
valueInCurrency: data?.valueInCurrency,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
// Build V1 from V2 quote
|
|
49
|
+
const v2Metadata = {
|
|
50
|
+
...includeIfTruthy(src, {
|
|
51
|
+
sentAmount: toTokenAmountValues(src),
|
|
52
|
+
}),
|
|
53
|
+
...includeIfTruthy(dest, {
|
|
54
|
+
toTokenAmount: toTokenAmountValues(dest),
|
|
55
|
+
minToTokenAmount: {
|
|
56
|
+
amount: dest.minAmountNormalized,
|
|
57
|
+
valueInCurrency: dest.minAmountValueInCurrency,
|
|
58
|
+
usd: dest.minAmountUsd,
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
...(priceData?.swapRate && { swapRate: priceData.swapRate }),
|
|
62
|
+
...includeIfTruthy(priceData?.adjustedReturn, {
|
|
63
|
+
adjustedReturn: toTokenAmountValues(priceData?.adjustedReturn),
|
|
64
|
+
}),
|
|
65
|
+
...includeIfTruthy(network?.[0], {
|
|
66
|
+
gasFee: {
|
|
67
|
+
total: toTokenAmountValues(network?.[0]),
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
...includeIfTruthy(totalNetworkFeeV2, {
|
|
71
|
+
totalNetworkFee: toTokenAmountValues(totalNetworkFeeV2),
|
|
72
|
+
}),
|
|
73
|
+
...(priceData?.priceImpact && {
|
|
74
|
+
priceImpact: toTokenAmountValues(priceData?.priceImpact),
|
|
75
|
+
// Use priceImpact as cost
|
|
76
|
+
cost: toTokenAmountValues(priceData?.priceImpact),
|
|
77
|
+
}),
|
|
78
|
+
...includeIfTruthy(relayer?.[0], {
|
|
79
|
+
relayerFee: toTokenAmountValues(relayer?.[0]),
|
|
80
|
+
}),
|
|
81
|
+
...includeIfTruthy(txFee?.[0], {
|
|
82
|
+
includedTxFees: toTokenAmountValues(txFee?.[0]),
|
|
83
|
+
}),
|
|
84
|
+
};
|
|
85
|
+
if (migrationPhase === QuoteMetadataMigrationPhase.V2WithV1Fallback) {
|
|
86
|
+
return merge({}, legacyMetadata, v2Metadata);
|
|
87
|
+
}
|
|
88
|
+
return v2Metadata;
|
|
39
89
|
};
|
|
40
90
|
//# sourceMappingURL=to-quote-metadata-v1.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-quote-metadata-v1.mjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-quote-metadata-v1.mjs","sourceRoot":"","sources":["../../../src/utils/quote-metadata/to-quote-metadata-v1.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,8BAA8B;;;AAM3C,OAAO,EAAE,qBAAqB,EAAE,4CAA2C;AAE3E,OAAO,EAAE,UAAU,EAAE,iCAAgC;AACrD,OAAO,EAAE,eAAe,EAAE,gCAA+B;AAEzD,OAAO,EAAE,2BAA2B,EAAE,oBAAmB;AAEzD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,aAEQ,EACR,iBAA8C,2BAA2B,CAAC,MAAM,EACjE,EAAE;IACjB,wBAAwB;IACxB,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,EACJ,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,cAAc,EACd,IAAI,EACJ,cAAc,EACd,UAAU,EACV,eAAe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AC/C,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC","sourcesContent":["import { is } from '@metamask/superstruct';\nimport { merge } from 'lodash';\n\nimport type { DeepPartial } from '../../types.js';\nimport type { AmountsAndAsset } from '../../validators/amount-and-asset.js';\nimport type { QuoteResponseV1 } from '../../validators/quote-response-v1.js';\nimport { QuoteResponseSchemaV2 } from '../../validators/quote-response.js';\nimport type { QuoteResponse } from '../../validators/quote-response.js';\nimport { sumAmounts } from '../number-formatters.js';\nimport { includeIfTruthy } from './include-if-truthy.js';\nimport type { QuoteMetadata, TokenAmountValues } from './types.js';\nimport { QuoteMetadataMigrationPhase } from './types.js';\n\n/**\n * Extracts legacy {@link QuoteMetadata} values from a {@link QuoteResponse} or {@link QuoteResponseV1}.\n * If a QuoteResponse is provided, this assumes that its `valueInCurrency` properties are set.\n *\n * @param quoteResponse - The quote to extract the metadata from\n * @param migrationPhase - The migration phase to use\n * @returns A partial {@link QuoteMetadata} object\n */\nexport const toQuoteMetadataV1 = (\n quoteResponse:\n | (DeepPartial<QuoteResponse | QuoteResponseV1> & QuoteMetadata)\n | null,\n migrationPhase: QuoteMetadataMigrationPhase = QuoteMetadataMigrationPhase.V1Data,\n): QuoteMetadata => {\n /* istanbul ignore if */\n if (!quoteResponse) {\n return {};\n }\n\n const {\n toTokenAmount,\n minToTokenAmount,\n sentAmount,\n swapRate,\n adjustedReturn,\n cost,\n includedTxFees,\n relayerFee,\n totalNetworkFee,\n gasFee,\n priceImpact,\n } = quoteResponse;\n\n const legacyMetadata = {\n sentAmount,\n toTokenAmount,\n minToTokenAmount,\n swapRate,\n gasFee,\n totalNetworkFee,\n ...includeIfTruthy(adjustedReturn, { adjustedReturn }),\n ...includeIfTruthy(cost, { cost }),\n ...includeIfTruthy(priceImpact, { priceImpact }),\n ...includeIfTruthy(relayerFee, { relayerFee }),\n ...includeIfTruthy(includedTxFees, { includedTxFees }),\n };\n\n if (\n migrationPhase === QuoteMetadataMigrationPhase.V1Data ||\n !is(quoteResponse, QuoteResponseSchemaV2)\n ) {\n return legacyMetadata;\n }\n\n const {\n quote: {\n src,\n dest,\n priceData,\n feeData: { network, relayer, txFee },\n },\n } = quoteResponse;\n\n const totalNetworkFeeV2 = sumAmounts(network, relayer);\n\n const toTokenAmountValues = (\n data?: Pick<\n AmountsAndAsset,\n 'normalizedAmount' | 'usd' | 'valueInCurrency'\n >,\n ): Partial<TokenAmountValues> => {\n return {\n amount: data?.normalizedAmount,\n usd: data?.usd,\n valueInCurrency: data?.valueInCurrency,\n };\n };\n\n // Build V1 from V2 quote\n const v2Metadata: QuoteMetadata = {\n ...includeIfTruthy(src, {\n sentAmount: toTokenAmountValues(src),\n }),\n ...includeIfTruthy(dest, {\n toTokenAmount: toTokenAmountValues(dest),\n minToTokenAmount: {\n amount: dest.minAmountNormalized,\n valueInCurrency: dest.minAmountValueInCurrency,\n usd: dest.minAmountUsd,\n },\n }),\n ...(priceData?.swapRate && { swapRate: priceData.swapRate }),\n ...includeIfTruthy(priceData?.adjustedReturn, {\n adjustedReturn: toTokenAmountValues(priceData?.adjustedReturn),\n }),\n ...includeIfTruthy(network?.[0], {\n gasFee: {\n total: toTokenAmountValues(network?.[0]),\n },\n }),\n ...includeIfTruthy(totalNetworkFeeV2, {\n totalNetworkFee: toTokenAmountValues(totalNetworkFeeV2),\n }),\n ...(priceData?.priceImpact && {\n priceImpact: toTokenAmountValues(priceData?.priceImpact),\n // Use priceImpact as cost\n cost: toTokenAmountValues(priceData?.priceImpact),\n }),\n ...includeIfTruthy(relayer?.[0], {\n relayerFee: toTokenAmountValues(relayer?.[0]),\n }),\n ...includeIfTruthy(txFee?.[0], {\n includedTxFees: toTokenAmountValues(txFee?.[0]),\n }),\n };\n\n if (migrationPhase === QuoteMetadataMigrationPhase.V2WithV1Fallback) {\n return merge({}, legacyMetadata, v2Metadata);\n }\n\n return v2Metadata;\n};\n"]}
|