@metamask-previews/bridge-controller 8.0.0-preview-03c4c9c3 → 8.0.0-preview-e744341c
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 +0 -14
- package/dist/bridge-controller.cjs +25 -96
- package/dist/bridge-controller.cjs.map +1 -1
- package/dist/bridge-controller.d.cts +7 -7
- package/dist/bridge-controller.d.cts.map +1 -1
- package/dist/bridge-controller.d.mts +7 -7
- package/dist/bridge-controller.d.mts.map +1 -1
- package/dist/bridge-controller.mjs +26 -97
- package/dist/bridge-controller.mjs.map +1 -1
- package/dist/constants/bridge.cjs +1 -2
- package/dist/constants/bridge.cjs.map +1 -1
- package/dist/constants/bridge.d.cts +1 -2
- package/dist/constants/bridge.d.cts.map +1 -1
- package/dist/constants/bridge.d.mts +1 -2
- package/dist/constants/bridge.d.mts.map +1 -1
- package/dist/constants/bridge.mjs +1 -2
- package/dist/constants/bridge.mjs.map +1 -1
- package/dist/constants/chains.cjs +12 -1
- package/dist/constants/chains.cjs.map +1 -1
- package/dist/constants/chains.d.cts +2 -0
- package/dist/constants/chains.d.cts.map +1 -1
- package/dist/constants/chains.d.mts +2 -0
- package/dist/constants/chains.d.mts.map +1 -1
- package/dist/constants/chains.mjs +11 -0
- package/dist/constants/chains.mjs.map +1 -1
- package/dist/constants/tokens.cjs +38 -61
- package/dist/constants/tokens.cjs.map +1 -1
- package/dist/constants/tokens.d.cts +48 -100
- package/dist/constants/tokens.d.cts.map +1 -1
- package/dist/constants/tokens.d.mts +48 -100
- package/dist/constants/tokens.d.mts.map +1 -1
- package/dist/constants/tokens.mjs +14 -37
- package/dist/constants/tokens.mjs.map +1 -1
- package/dist/index.cjs +1 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -16
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -16
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -11
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs +3 -4
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +54 -108
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +54 -108
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +3 -4
- package/dist/types.mjs.map +1 -1
- package/dist/utils/bridge.cjs +5 -53
- package/dist/utils/bridge.cjs.map +1 -1
- package/dist/utils/bridge.d.cts +4 -26
- package/dist/utils/bridge.d.cts.map +1 -1
- package/dist/utils/bridge.d.mts +4 -26
- package/dist/utils/bridge.d.mts.map +1 -1
- package/dist/utils/bridge.mjs +5 -50
- package/dist/utils/bridge.mjs.map +1 -1
- package/dist/utils/fetch.cjs +26 -34
- package/dist/utils/fetch.cjs.map +1 -1
- package/dist/utils/fetch.d.cts +5 -6
- package/dist/utils/fetch.d.cts.map +1 -1
- package/dist/utils/fetch.d.mts +5 -6
- package/dist/utils/fetch.d.mts.map +1 -1
- package/dist/utils/fetch.mjs +24 -32
- package/dist/utils/fetch.mjs.map +1 -1
- package/dist/utils/quote.cjs +2 -12
- package/dist/utils/quote.cjs.map +1 -1
- package/dist/utils/quote.d.cts +2 -2
- package/dist/utils/quote.d.cts.map +1 -1
- package/dist/utils/quote.d.mts +2 -2
- package/dist/utils/quote.d.mts.map +1 -1
- package/dist/utils/quote.mjs +2 -12
- package/dist/utils/quote.mjs.map +1 -1
- package/dist/utils/validators.cjs +16 -13
- package/dist/utils/validators.cjs.map +1 -1
- package/dist/utils/validators.d.cts +3 -2
- package/dist/utils/validators.d.cts.map +1 -1
- package/dist/utils/validators.d.mts +3 -2
- package/dist/utils/validators.d.mts.map +1 -1
- package/dist/utils/validators.mjs +17 -14
- package/dist/utils/validators.mjs.map +1 -1
- package/package.json +1 -6
- package/dist/utils/caip-formatters.cjs +0 -102
- package/dist/utils/caip-formatters.cjs.map +0 -1
- package/dist/utils/caip-formatters.d.cts +0 -31
- package/dist/utils/caip-formatters.d.cts.map +0 -1
- package/dist/utils/caip-formatters.d.mts +0 -31
- package/dist/utils/caip-formatters.d.mts.map +0 -1
- package/dist/utils/caip-formatters.mjs +0 -95
- package/dist/utils/caip-formatters.mjs.map +0 -1
package/dist/utils/bridge.cjs
CHANGED
|
@@ -1,40 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const constants_1 = require("@ethersproject/constants");
|
|
3
|
+
exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getDefaultBridgeControllerState = void 0;
|
|
5
4
|
const contracts_1 = require("@ethersproject/contracts");
|
|
6
|
-
const keyring_api_1 = require("@metamask/keyring-api");
|
|
7
5
|
const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
|
|
8
|
-
const utils_1 = require("@metamask/utils");
|
|
9
|
-
const caip_formatters_1 = require("./caip-formatters.cjs");
|
|
10
6
|
const bridge_1 = require("../constants/bridge.cjs");
|
|
11
7
|
const chains_1 = require("../constants/chains.cjs");
|
|
12
8
|
const tokens_1 = require("../constants/tokens.cjs");
|
|
13
|
-
const types_1 = require("../types.cjs");
|
|
14
9
|
const getDefaultBridgeControllerState = () => {
|
|
15
10
|
return bridge_1.DEFAULT_BRIDGE_CONTROLLER_STATE;
|
|
16
11
|
};
|
|
17
12
|
exports.getDefaultBridgeControllerState = getDefaultBridgeControllerState;
|
|
18
|
-
const getNativeAssetCaipAssetType = (chainId, nativeCurrencySymbol) => {
|
|
19
|
-
return `${(0, caip_formatters_1.formatChainIdToCaip)(chainId)}/${tokens_1.SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Returns the native swaps or bridge asset for a given chainId
|
|
23
|
-
*
|
|
24
|
-
* @param chainId - The chainId to get the default token for
|
|
25
|
-
* @returns The native asset for the given chainId
|
|
26
|
-
*/
|
|
27
|
-
const getNativeAssetForChainId = (chainId) => {
|
|
28
|
-
const chainIdInCaip = (0, caip_formatters_1.formatChainIdToCaip)(chainId);
|
|
29
|
-
const nativeToken = tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[(0, caip_formatters_1.formatChainIdToHex)(chainId)] ??
|
|
30
|
-
tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[(0, caip_formatters_1.formatChainIdToCaip)(chainId)];
|
|
31
|
-
return {
|
|
32
|
-
...nativeToken,
|
|
33
|
-
chainId: (0, caip_formatters_1.formatChainIdToDec)(chainId),
|
|
34
|
-
assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
exports.getNativeAssetForChainId = getNativeAssetForChainId;
|
|
38
13
|
/**
|
|
39
14
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
40
15
|
*
|
|
@@ -73,7 +48,8 @@ const isSwapsDefaultTokenAddress = (address, chainId) => {
|
|
|
73
48
|
if (!address || !chainId) {
|
|
74
49
|
return false;
|
|
75
50
|
}
|
|
76
|
-
return address ===
|
|
51
|
+
return (address ===
|
|
52
|
+
tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address);
|
|
77
53
|
};
|
|
78
54
|
exports.isSwapsDefaultTokenAddress = isSwapsDefaultTokenAddress;
|
|
79
55
|
/**
|
|
@@ -88,32 +64,8 @@ const isSwapsDefaultTokenSymbol = (symbol, chainId) => {
|
|
|
88
64
|
if (!symbol || !chainId) {
|
|
89
65
|
return false;
|
|
90
66
|
}
|
|
91
|
-
return symbol ===
|
|
67
|
+
return (symbol ===
|
|
68
|
+
tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.symbol);
|
|
92
69
|
};
|
|
93
70
|
exports.isSwapsDefaultTokenSymbol = isSwapsDefaultTokenSymbol;
|
|
94
|
-
/**
|
|
95
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
96
|
-
*
|
|
97
|
-
* @param address - The address to check
|
|
98
|
-
* @returns Whether the address is a native asset
|
|
99
|
-
*/
|
|
100
|
-
const isNativeAddress = (address) => address === constants_1.AddressZero || // bridge and swap apis set the native asset address to zero
|
|
101
|
-
address === '' || // assets controllers set the native asset address to an empty string
|
|
102
|
-
!address ||
|
|
103
|
-
address.endsWith('11111111111111111111111111111111') || // token-api and bridge-api use this as the solana native assetId
|
|
104
|
-
[(0, exports.getNativeAssetForChainId)(types_1.ChainId.SOLANA).assetId].some((assetId) => assetId.includes(address) && !(0, utils_1.isStrictHexString)(address)); // solana native assetId used in the extension client
|
|
105
|
-
exports.isNativeAddress = isNativeAddress;
|
|
106
|
-
/**
|
|
107
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
108
|
-
*
|
|
109
|
-
* @param chainId - The chainId to check
|
|
110
|
-
* @returns Whether the chainId is Solana
|
|
111
|
-
*/
|
|
112
|
-
const isSolanaChainId = (chainId) => {
|
|
113
|
-
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
114
|
-
return chainId === keyring_api_1.SolScope.Mainnet.toString();
|
|
115
|
-
}
|
|
116
|
-
return chainId.toString() === types_1.ChainId.SOLANA.toString();
|
|
117
|
-
};
|
|
118
|
-
exports.isSolanaChainId = isSolanaChainId;
|
|
119
71
|
//# sourceMappingURL=bridge.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,wDAAoD;AACpD,mEAAuD;AAGvD,oDAI6B;AAC7B,oDAAgD;AAChD,oDAAsE;AAG/D,MAAM,+BAA+B,GAAG,GAA0B,EAAE;IACzE,OAAO,wCAA+B,CAAC;AACzC,CAAC,CAAC;AAFW,QAAA,+B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rC;AACF;;;;;;;GAOG;AAEI,MAAM,yBAAyB,GAAG,CAAC,MAAc,EAAE,OAAY,EAAE,EAAE;IACxE,IAAI,CAAC,MAAM,IAAI,CAAC,OAAO,EAAE;QACvB,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CACL,MAAM;QACN,wCAA+B,CAC7B,OAAuD,CACxD,EAAE,MAAM,CACV,CAAC;AACJ,CAAC,CAAC;AAXW,QAAA,yBAAyB,6BAWpC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n DEFAULT_BRIDGE_CONTROLLER_STATE,\n ETH_USDT_ADDRESS,\n METABRIDGE_ETHEREUM_ADDRESS,\n} from '../constants/bridge';\nimport { CHAIN_IDS } from '../constants/chains';\nimport { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../constants/tokens';\nimport type { BridgeControllerState } from '../types';\n\nexport const getDefaultBridgeControllerState = (): BridgeControllerState => {\n return DEFAULT_BRIDGE_CONTROLLER_STATE;\n};\n\n/**\n * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum\n *\n * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API\n */\n\nexport const getEthUsdtResetData = () => {\n const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n .interface;\n const data = UsdtContractInterface.encodeFunctionData('approve', [\n METABRIDGE_ETHEREUM_ADDRESS,\n '0',\n ]);\n\n return data;\n};\n\nexport const isEthUsdt = (chainId: Hex, address: string) =>\n chainId === CHAIN_IDS.MAINNET &&\n address.toLowerCase() === ETH_USDT_ADDRESS.toLowerCase();\n\nexport const sumHexes = (...hexStrings: string[]): Hex => {\n if (hexStrings.length === 0) {\n return '0x0';\n }\n\n const sum = hexStrings.reduce((acc, hex) => acc + BigInt(hex), BigInt(0));\n return `0x${sum.toString(16)}`;\n};\n/**\n * Checks whether the provided address is strictly equal to the address for\n * the default swaps token of the provided chain.\n *\n * @param address - The string to compare to the default token address\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the address is the provided chain's default token address\n */\n\nexport const isSwapsDefaultTokenAddress = (address: string, chainId: Hex) => {\n if (!address || !chainId) {\n return false;\n }\n\n return (\n address ===\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ]?.address\n );\n};\n/**\n * Checks whether the provided symbol is strictly equal to the symbol for\n * the default swaps token of the provided chain.\n *\n * @param symbol - The string to compare to the default token symbol\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the symbol is the provided chain's default token symbol\n */\n\nexport const isSwapsDefaultTokenSymbol = (symbol: string, chainId: Hex) => {\n if (!symbol || !chainId) {\n return false;\n }\n\n return (\n symbol ===\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ]?.symbol\n );\n};\n"]}
|
package/dist/utils/bridge.d.cts
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import type { BridgeAsset, BridgeControllerState } from "../types.cjs";
|
|
1
|
+
import type { Hex } from "@metamask/utils";
|
|
2
|
+
import type { BridgeControllerState } from "../types.cjs";
|
|
4
3
|
export declare const getDefaultBridgeControllerState: () => BridgeControllerState;
|
|
5
|
-
/**
|
|
6
|
-
* Returns the native swaps or bridge asset for a given chainId
|
|
7
|
-
*
|
|
8
|
-
* @param chainId - The chainId to get the default token for
|
|
9
|
-
* @returns The native asset for the given chainId
|
|
10
|
-
*/
|
|
11
|
-
export declare const getNativeAssetForChainId: (chainId: string | number | Hex | CaipChainId) => BridgeAsset;
|
|
12
4
|
/**
|
|
13
5
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
14
6
|
*
|
|
@@ -25,7 +17,7 @@ export declare const sumHexes: (...hexStrings: string[]) => Hex;
|
|
|
25
17
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
26
18
|
* @returns Whether the address is the provided chain's default token address
|
|
27
19
|
*/
|
|
28
|
-
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
20
|
+
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex) => boolean;
|
|
29
21
|
/**
|
|
30
22
|
* Checks whether the provided symbol is strictly equal to the symbol for
|
|
31
23
|
* the default swaps token of the provided chain.
|
|
@@ -34,19 +26,5 @@ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
|
34
26
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
35
27
|
* @returns Whether the symbol is the provided chain's default token symbol
|
|
36
28
|
*/
|
|
37
|
-
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex
|
|
38
|
-
/**
|
|
39
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
40
|
-
*
|
|
41
|
-
* @param address - The address to check
|
|
42
|
-
* @returns Whether the address is a native asset
|
|
43
|
-
*/
|
|
44
|
-
export declare const isNativeAddress: (address?: string | null) => boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
47
|
-
*
|
|
48
|
-
* @param chainId - The chainId to check
|
|
49
|
-
* @returns Whether the chainId is Solana
|
|
50
|
-
*/
|
|
51
|
-
export declare const isSolanaChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
|
|
29
|
+
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex) => boolean;
|
|
52
30
|
//# sourceMappingURL=bridge.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bridge.d.cts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bridge.d.cts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAiB;AAEtD,eAAO,MAAM,+BAA+B,QAAO,qBAElD,CAAC;AAEF;;;;GAIG;AAEH,eAAO,MAAM,mBAAmB,cAS/B,CAAC;AAEF,eAAO,MAAM,SAAS,YAAa,GAAG,WAAW,MAAM,YAEG,CAAC;AAE3D,eAAO,MAAM,QAAQ,kBAAmB,MAAM,EAAE,KAAG,GAOlD,CAAC;AACF;;;;;;;GAOG;AAEH,eAAO,MAAM,0BAA0B,YAAa,MAAM,WAAW,GAAG,YAWvE,CAAC;AACF;;;;;;;GAOG;AAEH,eAAO,MAAM,yBAAyB,WAAY,MAAM,WAAW,GAAG,YAWrE,CAAC"}
|
package/dist/utils/bridge.d.mts
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import type { BridgeAsset, BridgeControllerState } from "../types.mjs";
|
|
1
|
+
import type { Hex } from "@metamask/utils";
|
|
2
|
+
import type { BridgeControllerState } from "../types.mjs";
|
|
4
3
|
export declare const getDefaultBridgeControllerState: () => BridgeControllerState;
|
|
5
|
-
/**
|
|
6
|
-
* Returns the native swaps or bridge asset for a given chainId
|
|
7
|
-
*
|
|
8
|
-
* @param chainId - The chainId to get the default token for
|
|
9
|
-
* @returns The native asset for the given chainId
|
|
10
|
-
*/
|
|
11
|
-
export declare const getNativeAssetForChainId: (chainId: string | number | Hex | CaipChainId) => BridgeAsset;
|
|
12
4
|
/**
|
|
13
5
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
14
6
|
*
|
|
@@ -25,7 +17,7 @@ export declare const sumHexes: (...hexStrings: string[]) => Hex;
|
|
|
25
17
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
26
18
|
* @returns Whether the address is the provided chain's default token address
|
|
27
19
|
*/
|
|
28
|
-
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
20
|
+
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex) => boolean;
|
|
29
21
|
/**
|
|
30
22
|
* Checks whether the provided symbol is strictly equal to the symbol for
|
|
31
23
|
* the default swaps token of the provided chain.
|
|
@@ -34,19 +26,5 @@ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
|
34
26
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
35
27
|
* @returns Whether the symbol is the provided chain's default token symbol
|
|
36
28
|
*/
|
|
37
|
-
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex
|
|
38
|
-
/**
|
|
39
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
40
|
-
*
|
|
41
|
-
* @param address - The address to check
|
|
42
|
-
* @returns Whether the address is a native asset
|
|
43
|
-
*/
|
|
44
|
-
export declare const isNativeAddress: (address?: string | null) => boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
47
|
-
*
|
|
48
|
-
* @param chainId - The chainId to check
|
|
49
|
-
* @returns Whether the chainId is Solana
|
|
50
|
-
*/
|
|
51
|
-
export declare const isSolanaChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
|
|
29
|
+
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex) => boolean;
|
|
52
30
|
//# sourceMappingURL=bridge.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bridge.d.mts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bridge.d.mts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAiB;AAEtD,eAAO,MAAM,+BAA+B,QAAO,qBAElD,CAAC;AAEF;;;;GAIG;AAEH,eAAO,MAAM,mBAAmB,cAS/B,CAAC;AAEF,eAAO,MAAM,SAAS,YAAa,GAAG,WAAW,MAAM,YAEG,CAAC;AAE3D,eAAO,MAAM,QAAQ,kBAAmB,MAAM,EAAE,KAAG,GAOlD,CAAC;AACF;;;;;;;GAOG;AAEH,eAAO,MAAM,0BAA0B,YAAa,MAAM,WAAW,GAAG,YAWvE,CAAC;AACF;;;;;;;GAOG;AAEH,eAAO,MAAM,yBAAyB,WAAY,MAAM,WAAW,GAAG,YAWrE,CAAC"}
|
package/dist/utils/bridge.mjs
CHANGED
|
@@ -1,35 +1,11 @@
|
|
|
1
|
-
import { AddressZero } from "@ethersproject/constants";
|
|
2
1
|
import { Contract } from "@ethersproject/contracts";
|
|
3
|
-
import { SolScope } from "@metamask/keyring-api";
|
|
4
2
|
import { abiERC20 } from "@metamask/metamask-eth-abis";
|
|
5
|
-
import { isCaipChainId, isStrictHexString } from "@metamask/utils";
|
|
6
|
-
import { formatChainIdToCaip, formatChainIdToDec, formatChainIdToHex } from "./caip-formatters.mjs";
|
|
7
3
|
import { DEFAULT_BRIDGE_CONTROLLER_STATE, ETH_USDT_ADDRESS, METABRIDGE_ETHEREUM_ADDRESS } from "../constants/bridge.mjs";
|
|
8
4
|
import { CHAIN_IDS } from "../constants/chains.mjs";
|
|
9
|
-
import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP
|
|
10
|
-
import { ChainId } from "../types.mjs";
|
|
5
|
+
import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from "../constants/tokens.mjs";
|
|
11
6
|
export const getDefaultBridgeControllerState = () => {
|
|
12
7
|
return DEFAULT_BRIDGE_CONTROLLER_STATE;
|
|
13
8
|
};
|
|
14
|
-
const getNativeAssetCaipAssetType = (chainId, nativeCurrencySymbol) => {
|
|
15
|
-
return `${formatChainIdToCaip(chainId)}/${SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Returns the native swaps or bridge asset for a given chainId
|
|
19
|
-
*
|
|
20
|
-
* @param chainId - The chainId to get the default token for
|
|
21
|
-
* @returns The native asset for the given chainId
|
|
22
|
-
*/
|
|
23
|
-
export const getNativeAssetForChainId = (chainId) => {
|
|
24
|
-
const chainIdInCaip = formatChainIdToCaip(chainId);
|
|
25
|
-
const nativeToken = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[formatChainIdToHex(chainId)] ??
|
|
26
|
-
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[formatChainIdToCaip(chainId)];
|
|
27
|
-
return {
|
|
28
|
-
...nativeToken,
|
|
29
|
-
chainId: formatChainIdToDec(chainId),
|
|
30
|
-
assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
9
|
/**
|
|
34
10
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
35
11
|
*
|
|
@@ -65,7 +41,8 @@ export const isSwapsDefaultTokenAddress = (address, chainId) => {
|
|
|
65
41
|
if (!address || !chainId) {
|
|
66
42
|
return false;
|
|
67
43
|
}
|
|
68
|
-
return address ===
|
|
44
|
+
return (address ===
|
|
45
|
+
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address);
|
|
69
46
|
};
|
|
70
47
|
/**
|
|
71
48
|
* Checks whether the provided symbol is strictly equal to the symbol for
|
|
@@ -79,29 +56,7 @@ export const isSwapsDefaultTokenSymbol = (symbol, chainId) => {
|
|
|
79
56
|
if (!symbol || !chainId) {
|
|
80
57
|
return false;
|
|
81
58
|
}
|
|
82
|
-
return symbol ===
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
86
|
-
*
|
|
87
|
-
* @param address - The address to check
|
|
88
|
-
* @returns Whether the address is a native asset
|
|
89
|
-
*/
|
|
90
|
-
export const isNativeAddress = (address) => address === AddressZero || // bridge and swap apis set the native asset address to zero
|
|
91
|
-
address === '' || // assets controllers set the native asset address to an empty string
|
|
92
|
-
!address ||
|
|
93
|
-
address.endsWith('11111111111111111111111111111111') || // token-api and bridge-api use this as the solana native assetId
|
|
94
|
-
[getNativeAssetForChainId(ChainId.SOLANA).assetId].some((assetId) => assetId.includes(address) && !isStrictHexString(address)); // solana native assetId used in the extension client
|
|
95
|
-
/**
|
|
96
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
97
|
-
*
|
|
98
|
-
* @param chainId - The chainId to check
|
|
99
|
-
* @returns Whether the chainId is Solana
|
|
100
|
-
*/
|
|
101
|
-
export const isSolanaChainId = (chainId) => {
|
|
102
|
-
if (isCaipChainId(chainId)) {
|
|
103
|
-
return chainId === SolScope.Mainnet.toString();
|
|
104
|
-
}
|
|
105
|
-
return chainId.toString() === ChainId.SOLANA.toString();
|
|
59
|
+
return (symbol ===
|
|
60
|
+
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.symbol);
|
|
106
61
|
};
|
|
107
62
|
//# sourceMappingURL=bridge.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bridge.mjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"bridge.mjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAGvD,OAAO,EACL,+BAA+B,EAC/B,gBAAgB,EAChB,2BAA2B,EAC5B,gCAA4B;AAC7B,OAAO,EAAE,SAAS,EAAE,gCAA4B;AAChD,OAAO,EAAE,+BAA+B,EAAE,gCAA4B;AAGtE,MAAM,CAAC,MAAM,+BAA+B,GAAG,GAA0B,EAAE;IACzE,OAAO,+BAA+B,CAAC;AACzC,CAAC,CAAC;AAEF;;;;GAIG;AAEH,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,EAAE;IACtC,MAAM,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AAK,CAAC;KACd;IAED,OAAO,CACL,MAAM;QACN,+BAA+B,CAC7B,OAAuD,CACxD,EAAE,MAAM,CACV,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n DEFAULT_BRIDGE_CONTROLLER_STATE,\n ETH_USDT_ADDRESS,\n METABRIDGE_ETHEREUM_ADDRESS,\n} from '../constants/bridge';\nimport { CHAIN_IDS } from '../constants/chains';\nimport { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../constants/tokens';\nimport type { BridgeControllerState } from '../types';\n\nexport const getDefaultBridgeControllerState = (): BridgeControllerState => {\n return DEFAULT_BRIDGE_CONTROLLER_STATE;\n};\n\n/**\n * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum\n *\n * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API\n */\n\nexport const getEthUsdtResetData = () => {\n const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n .interface;\n const data = UsdtContractInterface.encodeFunctionData('approve', [\n METABRIDGE_ETHEREUM_ADDRESS,\n '0',\n ]);\n\n return data;\n};\n\nexport const isEthUsdt = (chainId: Hex, address: string) =>\n chainId === CHAIN_IDS.MAINNET &&\n address.toLowerCase() === ETH_USDT_ADDRESS.toLowerCase();\n\nexport const sumHexes = (...hexStrings: string[]): Hex => {\n if (hexStrings.length === 0) {\n return '0x0';\n }\n\n const sum = hexStrings.reduce((acc, hex) => acc + BigInt(hex), BigInt(0));\n return `0x${sum.toString(16)}`;\n};\n/**\n * Checks whether the provided address is strictly equal to the address for\n * the default swaps token of the provided chain.\n *\n * @param address - The string to compare to the default token address\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the address is the provided chain's default token address\n */\n\nexport const isSwapsDefaultTokenAddress = (address: string, chainId: Hex) => {\n if (!address || !chainId) {\n return false;\n }\n\n return (\n address ===\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ]?.address\n );\n};\n/**\n * Checks whether the provided symbol is strictly equal to the symbol for\n * the default swaps token of the provided chain.\n *\n * @param symbol - The string to compare to the default token symbol\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the symbol is the provided chain's default token symbol\n */\n\nexport const isSwapsDefaultTokenSymbol = (symbol: string, chainId: Hex) => {\n if (!symbol || !chainId) {\n return false;\n }\n\n return (\n symbol ===\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ]?.symbol\n );\n};\n"]}
|
package/dist/utils/fetch.cjs
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchBridgeQuotes = exports.fetchBridgeTokens = exports.fetchBridgeFeatureFlags = exports.getClientIdHeader = void 0;
|
|
4
4
|
const utils_1 = require("@metamask/utils");
|
|
5
|
-
const
|
|
5
|
+
const bridge_1 = require("./bridge.cjs");
|
|
6
6
|
const validators_1 = require("./validators.cjs");
|
|
7
|
-
const
|
|
7
|
+
const bridge_2 = require("../constants/bridge.cjs");
|
|
8
|
+
const tokens_1 = require("../constants/tokens.cjs");
|
|
8
9
|
const types_1 = require("../types.cjs");
|
|
9
|
-
|
|
10
|
+
// TODO put this back in once we have a fetchWithCache equivalent
|
|
11
|
+
// const CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;
|
|
10
12
|
const getClientIdHeader = (clientId) => ({
|
|
11
13
|
'X-Client-Id': clientId,
|
|
12
14
|
});
|
|
@@ -23,13 +25,11 @@ async function fetchBridgeFeatureFlags(clientId, fetchFn, bridgeApiBaseUrl) {
|
|
|
23
25
|
const url = `${bridgeApiBaseUrl}/getAllFeatureFlags`;
|
|
24
26
|
const rawFeatureFlags = await fetchFn(url, {
|
|
25
27
|
headers: (0, exports.getClientIdHeader)(clientId),
|
|
26
|
-
cacheOptions: { cacheRefreshTime: CACHE_REFRESH_TEN_MINUTES },
|
|
27
|
-
functionName: 'fetchBridgeFeatureFlags',
|
|
28
28
|
});
|
|
29
29
|
if ((0, validators_1.validateFeatureFlagsResponse)(rawFeatureFlags)) {
|
|
30
30
|
const getChainsObj = (chains) => Object.entries(chains).reduce((acc, [chainId, value]) => ({
|
|
31
31
|
...acc,
|
|
32
|
-
[(0,
|
|
32
|
+
[(0, utils_1.numberToHex)(Number(chainId))]: value,
|
|
33
33
|
}), {});
|
|
34
34
|
return {
|
|
35
35
|
[types_1.BridgeFeatureFlagsKey.EXTENSION_CONFIG]: {
|
|
@@ -43,8 +43,8 @@ async function fetchBridgeFeatureFlags(clientId, fetchFn, bridgeApiBaseUrl) {
|
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
45
|
return {
|
|
46
|
-
[types_1.BridgeFeatureFlagsKey.EXTENSION_CONFIG]:
|
|
47
|
-
[types_1.BridgeFeatureFlagsKey.MOBILE_CONFIG]:
|
|
46
|
+
[types_1.BridgeFeatureFlagsKey.EXTENSION_CONFIG]: bridge_2.DEFAULT_FEATURE_FLAG_CONFIG,
|
|
47
|
+
[types_1.BridgeFeatureFlagsKey.MOBILE_CONFIG]: bridge_2.DEFAULT_FEATURE_FLAG_CONFIG,
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
exports.fetchBridgeFeatureFlags = fetchBridgeFeatureFlags;
|
|
@@ -59,27 +59,30 @@ exports.fetchBridgeFeatureFlags = fetchBridgeFeatureFlags;
|
|
|
59
59
|
*/
|
|
60
60
|
async function fetchBridgeTokens(chainId, clientId, fetchFn, bridgeApiBaseUrl) {
|
|
61
61
|
// TODO make token api v2 call
|
|
62
|
-
const url = `${bridgeApiBaseUrl}/getTokens?chainId=${(0,
|
|
62
|
+
const url = `${bridgeApiBaseUrl}/getTokens?chainId=${(0, utils_1.hexToNumber)(chainId)}`;
|
|
63
63
|
// TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:
|
|
64
64
|
// If we allow selecting dest networks which the user has not imported,
|
|
65
65
|
// note that the Assets controller won't be able to provide tokens. In extension we fetch+cache the token list from bridge-api to handle this
|
|
66
66
|
const tokens = await fetchFn(url, {
|
|
67
67
|
headers: (0, exports.getClientIdHeader)(clientId),
|
|
68
|
-
cacheOptions: { cacheRefreshTime: CACHE_REFRESH_TEN_MINUTES },
|
|
69
|
-
functionName: 'fetchBridgeTokens',
|
|
70
68
|
});
|
|
69
|
+
const nativeToken = tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId];
|
|
71
70
|
const transformedTokens = {};
|
|
71
|
+
if (nativeToken) {
|
|
72
|
+
transformedTokens[nativeToken.address] = nativeToken;
|
|
73
|
+
}
|
|
72
74
|
tokens.forEach((token) => {
|
|
73
|
-
if ((0, validators_1.validateSwapsTokenObject)(token)
|
|
75
|
+
if ((0, validators_1.validateSwapsTokenObject)(token) &&
|
|
76
|
+
!((0, bridge_1.isSwapsDefaultTokenSymbol)(token.symbol, chainId) ||
|
|
77
|
+
(0, bridge_1.isSwapsDefaultTokenAddress)(token.address, chainId))) {
|
|
74
78
|
transformedTokens[token.address] = token;
|
|
75
79
|
}
|
|
76
80
|
});
|
|
77
81
|
return transformedTokens;
|
|
78
82
|
}
|
|
79
83
|
exports.fetchBridgeTokens = fetchBridgeTokens;
|
|
84
|
+
// Returns a list of bridge tx quotes
|
|
80
85
|
/**
|
|
81
|
-
* Converts the generic quote request to the type that the bridge-api expects
|
|
82
|
-
* then fetches quotes from the bridge-api
|
|
83
86
|
*
|
|
84
87
|
* @param request - The quote request
|
|
85
88
|
* @param signal - The abort signal
|
|
@@ -89,32 +92,21 @@ exports.fetchBridgeTokens = fetchBridgeTokens;
|
|
|
89
92
|
* @returns A list of bridge tx quotes
|
|
90
93
|
*/
|
|
91
94
|
async function fetchBridgeQuotes(request, signal, clientId, fetchFn, bridgeApiBaseUrl) {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
destChainId: (0, caip_formatters_1.formatChainIdToDec)(request.destChainId),
|
|
99
|
-
srcTokenAddress: (0, caip_formatters_1.formatAddressToCaipReference)(request.srcTokenAddress),
|
|
100
|
-
destTokenAddress: (0, caip_formatters_1.formatAddressToCaipReference)(request.destTokenAddress),
|
|
95
|
+
const queryParams = new URLSearchParams({
|
|
96
|
+
walletAddress: request.walletAddress,
|
|
97
|
+
srcChainId: request.srcChainId.toString(),
|
|
98
|
+
destChainId: request.destChainId.toString(),
|
|
99
|
+
srcTokenAddress: request.srcTokenAddress,
|
|
100
|
+
destTokenAddress: request.destTokenAddress,
|
|
101
101
|
srcTokenAmount: request.srcTokenAmount,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (request.slippage !== undefined) {
|
|
106
|
-
normalizedRequest.slippage = request.slippage;
|
|
107
|
-
}
|
|
108
|
-
const queryParams = new URLSearchParams();
|
|
109
|
-
Object.entries(normalizedRequest).forEach(([key, value]) => {
|
|
110
|
-
queryParams.append(key, value.toString());
|
|
102
|
+
slippage: request.slippage.toString(),
|
|
103
|
+
insufficientBal: request.insufficientBal ? 'true' : 'false',
|
|
104
|
+
resetApproval: request.resetApproval ? 'true' : 'false',
|
|
111
105
|
});
|
|
112
106
|
const url = `${bridgeApiBaseUrl}/getQuote?${queryParams}`;
|
|
113
107
|
const quotes = await fetchFn(url, {
|
|
114
108
|
headers: (0, exports.getClientIdHeader)(clientId),
|
|
115
109
|
signal,
|
|
116
|
-
cacheOptions: { cacheRefreshTime: 0 },
|
|
117
|
-
functionName: 'fetchBridgeQuotes',
|
|
118
110
|
});
|
|
119
111
|
const filteredQuotes = quotes.filter((quoteResponse) => {
|
|
120
112
|
return (0, validators_1.validateQuoteResponse)(quoteResponse);
|
package/dist/utils/fetch.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":";;;AACA,2CAA2D;AAE3D,yCAGkB;AAClB,iDAIsB;AACtB,oDAAkE;AAElE,oDAAsE;AAQtE,wCAA6D;AAE7D,iEAAiE;AACjE,0DAA0D;AAEnD,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC,CAAC;IACtD,aAAa,EAAE,QAAQ;CACxB,CAAC,CAAC;AAFU,QAAA,iBAAiB,qBAE3B;AAEH;;;;;;;GAOG;AACI,KAAK,UAAU,uBAAuB,CAC3C,QAAgB,EAChB,OAAsB,EACtB,gBAAwB;IAExB,MAAM,GAAG,GAAG,GAAG,gBAAgB,qBAAqB,CAAC;IACrD,MAAM,eAAe,GAAY,MAAM,OAAO,CAAC,GAAG,EAAE;QAClD,OAAO,EAAE,IAAA,yBAAiB,EAAC,QAAQ,CAAC;KACrC,CAAC,CAAC;IAEH,IAAI,IAAA,yCAA4B,EAAC,eAAe,CAAC,EAAE;QACjD,MAAM,YAAY,GAAG,CAAC,MAA0C,EAAE,EAAE,CAClE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAC3B,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;YAC1B,GAAG,GAAG;YACN,CAAC,IAAA,mBAAW,EAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK;SACtC,CAAC,EACF,EAAE,CACH,CAAC;QAEJ,OAAO;YACL,CAAC,6BAAqB,CAAC,gBAAgB,CAAC,EAAE;gBACxC,GAAG,eAAe,CAAC,kBAAU,CAAC,gBAAgB,CAAC;gBAC/C,MAAM,EAAE,YAAY,CAClB,eAAe,CAAC,kBAAU,CAAC,gBAAgB,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umber, numberToHex } from '@metamask/utils';\n\nimport {\n isSwapsDefaultTokenAddress,\n isSwapsDefaultTokenSymbol,\n} from './bridge';\nimport {\n validateFeatureFlagsResponse,\n validateQuoteResponse,\n validateSwapsTokenObject,\n} from './validators';\nimport { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';\nimport type { SwapsTokenObject } from '../constants/tokens';\nimport { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../constants/tokens';\nimport type {\n QuoteRequest,\n QuoteResponse,\n BridgeFeatureFlags,\n FetchFunction,\n ChainConfiguration,\n} from '../types';\nimport { BridgeFlag, BridgeFeatureFlagsKey } from '../types';\n\n// TODO put this back in once we have a fetchWithCache equivalent\n// const CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;\n\nexport const getClientIdHeader = (clientId: string) => ({\n 'X-Client-Id': clientId,\n});\n\n/**\n * Fetches the bridge feature flags\n *\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns The bridge feature flags\n */\nexport async function fetchBridgeFeatureFlags(\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<BridgeFeatureFlags> {\n const url = `${bridgeApiBaseUrl}/getAllFeatureFlags`;\n const rawFeatureFlags: unknown = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n });\n\n if (validateFeatureFlagsResponse(rawFeatureFlags)) {\n const getChainsObj = (chains: Record<number, ChainConfiguration>) =>\n Object.entries(chains).reduce(\n (acc, [chainId, value]) => ({\n ...acc,\n [numberToHex(Number(chainId))]: value,\n }),\n {},\n );\n\n return {\n [BridgeFeatureFlagsKey.EXTENSION_CONFIG]: {\n ...rawFeatureFlags[BridgeFlag.EXTENSION_CONFIG],\n chains: getChainsObj(\n rawFeatureFlags[BridgeFlag.EXTENSION_CONFIG].chains,\n ),\n },\n [BridgeFeatureFlagsKey.MOBILE_CONFIG]: {\n ...rawFeatureFlags[BridgeFlag.MOBILE_CONFIG],\n chains: getChainsObj(rawFeatureFlags[BridgeFlag.MOBILE_CONFIG].chains),\n },\n };\n }\n\n return {\n [BridgeFeatureFlagsKey.EXTENSION_CONFIG]: DEFAULT_FEATURE_FLAG_CONFIG,\n [BridgeFeatureFlagsKey.MOBILE_CONFIG]: DEFAULT_FEATURE_FLAG_CONFIG,\n };\n}\n\n/**\n * Returns a list of enabled (unblocked) tokens\n *\n * @param chainId - The chain ID to fetch tokens for\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of enabled (unblocked) tokens\n */\nexport async function fetchBridgeTokens(\n chainId: Hex,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<Record<string, SwapsTokenObject>> {\n // TODO make token api v2 call\n const url = `${bridgeApiBaseUrl}/getTokens?chainId=${hexToNumber(chainId)}`;\n\n // TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:\n // If we allow selecting dest networks which the user has not imported,\n // note that the Assets controller won't be able to provide tokens. In extension we fetch+cache the token list from bridge-api to handle this\n const tokens = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n });\n\n const nativeToken =\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ];\n\n const transformedTokens: Record<string, SwapsTokenObject> = {};\n if (nativeToken) {\n transformedTokens[nativeToken.address] = nativeToken;\n }\n\n tokens.forEach((token: unknown) => {\n if (\n validateSwapsTokenObject(token) &&\n !(\n isSwapsDefaultTokenSymbol(token.symbol, chainId) ||\n isSwapsDefaultTokenAddress(token.address, chainId)\n )\n ) {\n transformedTokens[token.address] = token;\n }\n });\n return transformedTokens;\n}\n\n// Returns a list of bridge tx quotes\n/**\n *\n * @param request - The quote request\n * @param signal - The abort signal\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of bridge tx quotes\n */\nexport async function fetchBridgeQuotes(\n request: QuoteRequest,\n signal: AbortSignal,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<QuoteResponse[]> {\n const queryParams = new URLSearchParams({\n walletAddress: request.walletAddress,\n srcChainId: request.srcChainId.toString(),\n destChainId: request.destChainId.toString(),\n srcTokenAddress: request.srcTokenAddress,\n destTokenAddress: request.destTokenAddress,\n srcTokenAmount: request.srcTokenAmount,\n slippage: request.slippage.toString(),\n insufficientBal: request.insufficientBal ? 'true' : 'false',\n resetApproval: request.resetApproval ? 'true' : 'false',\n });\n const url = `${bridgeApiBaseUrl}/getQuote?${queryParams}`;\n const quotes: unknown[] = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n signal,\n });\n\n const filteredQuotes = quotes.filter((quoteResponse: unknown) => {\n return validateQuoteResponse(quoteResponse);\n });\n return filteredQuotes as QuoteResponse[];\n}\n"]}
|
package/dist/utils/fetch.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { Hex } from "@metamask/utils";
|
|
2
|
+
import type { SwapsTokenObject } from "../constants/tokens.cjs";
|
|
3
|
+
import type { QuoteRequest, QuoteResponse, BridgeFeatureFlags, FetchFunction } from "../types.cjs";
|
|
3
4
|
export declare const getClientIdHeader: (clientId: string) => {
|
|
4
5
|
'X-Client-Id': string;
|
|
5
6
|
};
|
|
@@ -21,10 +22,8 @@ export declare function fetchBridgeFeatureFlags(clientId: string, fetchFn: Fetch
|
|
|
21
22
|
* @param bridgeApiBaseUrl - The base URL for the bridge API
|
|
22
23
|
* @returns A list of enabled (unblocked) tokens
|
|
23
24
|
*/
|
|
24
|
-
export declare function fetchBridgeTokens(chainId: Hex
|
|
25
|
+
export declare function fetchBridgeTokens(chainId: Hex, clientId: string, fetchFn: FetchFunction, bridgeApiBaseUrl: string): Promise<Record<string, SwapsTokenObject>>;
|
|
25
26
|
/**
|
|
26
|
-
* Converts the generic quote request to the type that the bridge-api expects
|
|
27
|
-
* then fetches quotes from the bridge-api
|
|
28
27
|
*
|
|
29
28
|
* @param request - The quote request
|
|
30
29
|
* @param signal - The abort signal
|
|
@@ -33,5 +32,5 @@ export declare function fetchBridgeTokens(chainId: Hex | CaipChainId, clientId:
|
|
|
33
32
|
* @param bridgeApiBaseUrl - The base URL for the bridge API
|
|
34
33
|
* @returns A list of bridge tx quotes
|
|
35
34
|
*/
|
|
36
|
-
export declare function fetchBridgeQuotes(request:
|
|
35
|
+
export declare function fetchBridgeQuotes(request: QuoteRequest, signal: AbortSignal, clientId: string, fetchFn: FetchFunction, bridgeApiBaseUrl: string): Promise<QuoteResponse[]>;
|
|
37
36
|
//# sourceMappingURL=fetch.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.cts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"fetch.d.cts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAa3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,gCAA4B;AAE5D,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,aAAa,EAEd,qBAAiB;AAMlB,eAAO,MAAM,iBAAiB,aAAc,MAAM;;CAEhD,CAAC;AAEH;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,kBAAkB,CAAC,CAkC7B;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,GAAG,EACZ,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAiC3C;AAGD;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,aAAa,EAAE,CAAC,CAsB1B"}
|
package/dist/utils/fetch.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { Hex } from "@metamask/utils";
|
|
2
|
+
import type { SwapsTokenObject } from "../constants/tokens.mjs";
|
|
3
|
+
import type { QuoteRequest, QuoteResponse, BridgeFeatureFlags, FetchFunction } from "../types.mjs";
|
|
3
4
|
export declare const getClientIdHeader: (clientId: string) => {
|
|
4
5
|
'X-Client-Id': string;
|
|
5
6
|
};
|
|
@@ -21,10 +22,8 @@ export declare function fetchBridgeFeatureFlags(clientId: string, fetchFn: Fetch
|
|
|
21
22
|
* @param bridgeApiBaseUrl - The base URL for the bridge API
|
|
22
23
|
* @returns A list of enabled (unblocked) tokens
|
|
23
24
|
*/
|
|
24
|
-
export declare function fetchBridgeTokens(chainId: Hex
|
|
25
|
+
export declare function fetchBridgeTokens(chainId: Hex, clientId: string, fetchFn: FetchFunction, bridgeApiBaseUrl: string): Promise<Record<string, SwapsTokenObject>>;
|
|
25
26
|
/**
|
|
26
|
-
* Converts the generic quote request to the type that the bridge-api expects
|
|
27
|
-
* then fetches quotes from the bridge-api
|
|
28
27
|
*
|
|
29
28
|
* @param request - The quote request
|
|
30
29
|
* @param signal - The abort signal
|
|
@@ -33,5 +32,5 @@ export declare function fetchBridgeTokens(chainId: Hex | CaipChainId, clientId:
|
|
|
33
32
|
* @param bridgeApiBaseUrl - The base URL for the bridge API
|
|
34
33
|
* @returns A list of bridge tx quotes
|
|
35
34
|
*/
|
|
36
|
-
export declare function fetchBridgeQuotes(request:
|
|
35
|
+
export declare function fetchBridgeQuotes(request: QuoteRequest, signal: AbortSignal, clientId: string, fetchFn: FetchFunction, bridgeApiBaseUrl: string): Promise<QuoteResponse[]>;
|
|
37
36
|
//# sourceMappingURL=fetch.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.mts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"fetch.d.mts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAa3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,gCAA4B;AAE5D,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,aAAa,EAEd,qBAAiB;AAMlB,eAAO,MAAM,iBAAiB,aAAc,MAAM;;CAEhD,CAAC;AAEH;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,kBAAkB,CAAC,CAkC7B;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,GAAG,EACZ,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAiC3C;AAGD;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,aAAa,EAAE,CAAC,CAsB1B"}
|