@metamask-previews/bridge-controller 10.0.0-preview-e7013a54 → 10.0.0-preview-e56c504a
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 -18
- package/dist/bridge-controller.cjs +25 -96
- package/dist/bridge-controller.cjs.map +1 -1
- package/dist/bridge-controller.d.cts +5 -5
- package/dist/bridge-controller.d.cts.map +1 -1
- package/dist/bridge-controller.d.mts +5 -5
- 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 -21
- 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/balance.cjs +2 -2
- package/dist/utils/balance.cjs.map +1 -1
- package/dist/utils/balance.d.cts.map +1 -1
- package/dist/utils/balance.d.mts.map +1 -1
- package/dist/utils/balance.mjs +2 -2
- package/dist/utils/balance.mjs.map +1 -1
- package/dist/utils/bridge.cjs +5 -65
- package/dist/utils/bridge.cjs.map +1 -1
- package/dist/utils/bridge.d.cts +4 -27
- package/dist/utils/bridge.d.cts.map +1 -1
- package/dist/utils/bridge.d.mts +4 -27
- package/dist/utils/bridge.d.mts.map +1 -1
- package/dist/utils/bridge.mjs +5 -62
- 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 -15
- 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 -16
- package/dist/utils/validators.mjs.map +1 -1
- package/package.json +2 -7
- 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/balance.cjs
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.hasSufficientBalance = exports.calcLatestSrcBalance = exports.fetchTokenBalance = void 0;
|
|
4
4
|
const address_1 = require("@ethersproject/address");
|
|
5
|
+
const constants_1 = require("@ethersproject/constants");
|
|
5
6
|
const contracts_1 = require("@ethersproject/contracts");
|
|
6
7
|
const providers_1 = require("@ethersproject/providers");
|
|
7
8
|
const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
|
|
8
|
-
const bridge_1 = require("./bridge.cjs");
|
|
9
9
|
const fetchTokenBalance = async (address, userAddress, provider) => {
|
|
10
10
|
const ethersProvider = new providers_1.Web3Provider(provider);
|
|
11
11
|
const tokenContract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC20, ethersProvider);
|
|
@@ -17,7 +17,7 @@ const fetchTokenBalance = async (address, userAddress, provider) => {
|
|
|
17
17
|
exports.fetchTokenBalance = fetchTokenBalance;
|
|
18
18
|
const calcLatestSrcBalance = async (provider, selectedAddress, tokenAddress, chainId) => {
|
|
19
19
|
if (tokenAddress && chainId) {
|
|
20
|
-
if (
|
|
20
|
+
if (tokenAddress === constants_1.AddressZero) {
|
|
21
21
|
const ethersProvider = new providers_1.Web3Provider(provider);
|
|
22
22
|
return await ethersProvider.getBalance((0, address_1.getAddress)(selectedAddress));
|
|
23
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"balance.cjs","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;AAEpD,wDAAoD;AACpD,wDAAwD;AACxD,mEAAuD;
|
|
1
|
+
{"version":3,"file":"balance.cjs","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;AAEpD,wDAAuD;AACvD,wDAAoD;AACpD,wDAAwD;AACxD,mEAAuD;AAIhD,MAAM,iBAAiB,GAAG,KAAK,EACpC,OAAe,EACf,WAAmB,EACnB,QAAkB,EACc,EAAE;IAClC,MAAM,cAAc,GAAG,IAAI,wBAAY,CAAC,QAAQ,CAAC,CAAC;IAClD,MAAM,aAAa,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,4BAAQ,EAAE,cAAc,CAAC,CAAC;IACtE,MAAM,mBAAmB,GACvB,OAAO,aAAa,EAAE,SAAS,KAAK,UAAU;QAC5C,CAAC,CAAC,aAAa,CAAC,SAAS,CAAC,WAAW,CAAC;QACtC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACjC,OAAO,MAAM,mBAAmB,CAAC;AACnC,CAAC,CAAC;AAZW,QAAA,iBAAiB,qBAY5B;AAEK,MAAM,oBAAoB,GAAG,KAAK,EACvC,QAAkB,EAClB,eAAuB,EACvB,YAAoB,EACpB,OAAY,EACoB,EAAE;IAClC,IAAI,YAAY,IAAI,OAAO,EAAE;QAC3B,IAAI,YAAY,KAAK,uBAAW,EAAE;YAChC,MAAM,cAAc,GAAG,IAAI,wBAAY,CAAC,QAAQ,CAAC,CAAC;YAClD,OAAO,MAAM,cAAc,CAAC,UAAU,CAAC,IAAA,oBAAU,EAAC,eAAe,CAAC,CAAC,CAAC;SACrE;QACD,OAAO,MAAM,IAAA,yBAAiB,EAAC,YAAY,EAAE,eAAe,EAAE,QAAQ,CAAC,CAAC;KACzE;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAdW,QAAA,oBAAoB,wBAc/B;AAEK,MAAM,oBAAoB,GAAG,KAAK,EACvC,QAAkB,EAClB,eAAuB,EACvB,YAAoB,EACpB,eAAuB,EACvB,OAAY,EACZ,EAAE;IACF,MAAM,eAAe,GAAG,MAAM,IAAA,4BAAoB,EAChD,QAAQ,EACR,eAAe,EACf,YAAY,EACZ,OAAO,CACR,CAAC;IAEF,OAAO,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AACxE,CAAC,CAAC;AAfW,QAAA,oBAAoB,wBAe/B","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport type { BigNumber } from '@ethersproject/bignumber';\nimport { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nexport const fetchTokenBalance = async (\n address: string,\n userAddress: string,\n provider: Provider,\n): Promise<BigNumber | undefined> => {\n const ethersProvider = new Web3Provider(provider);\n const tokenContract = new Contract(address, abiERC20, ethersProvider);\n const tokenBalancePromise =\n typeof tokenContract?.balanceOf === 'function'\n ? tokenContract.balanceOf(userAddress)\n : Promise.resolve(undefined);\n return await tokenBalancePromise;\n};\n\nexport const calcLatestSrcBalance = async (\n provider: Provider,\n selectedAddress: string,\n tokenAddress: string,\n chainId: Hex,\n): Promise<BigNumber | undefined> => {\n if (tokenAddress && chainId) {\n if (tokenAddress === AddressZero) {\n const ethersProvider = new Web3Provider(provider);\n return await ethersProvider.getBalance(getAddress(selectedAddress));\n }\n return await fetchTokenBalance(tokenAddress, selectedAddress, provider);\n }\n return undefined;\n};\n\nexport const hasSufficientBalance = async (\n provider: Provider,\n selectedAddress: string,\n tokenAddress: string,\n fromTokenAmount: string,\n chainId: Hex,\n) => {\n const srcTokenBalance = await calcLatestSrcBalance(\n provider,\n selectedAddress,\n tokenAddress,\n chainId,\n );\n\n return srcTokenBalance ? srcTokenBalance.gte(fromTokenAmount) : false;\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"balance.d.cts","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAiC;
|
|
1
|
+
{"version":3,"file":"balance.d.cts","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAiC;AAK1D,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAC7D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,eAAO,MAAM,iBAAiB,YACnB,MAAM,eACF,MAAM,YACT,QAAQ,KACjB,QAAQ,SAAS,GAAG,SAAS,CAQ/B,CAAC;AAEF,eAAO,MAAM,oBAAoB,aACrB,QAAQ,mBACD,MAAM,gBACT,MAAM,WACX,GAAG,KACX,QAAQ,SAAS,GAAG,SAAS,CAS/B,CAAC;AAEF,eAAO,MAAM,oBAAoB,aACrB,QAAQ,mBACD,MAAM,gBACT,MAAM,mBACH,MAAM,WACd,GAAG,qBAUb,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"balance.d.mts","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAiC;
|
|
1
|
+
{"version":3,"file":"balance.d.mts","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAiC;AAK1D,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAC7D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,eAAO,MAAM,iBAAiB,YACnB,MAAM,eACF,MAAM,YACT,QAAQ,KACjB,QAAQ,SAAS,GAAG,SAAS,CAQ/B,CAAC;AAEF,eAAO,MAAM,oBAAoB,aACrB,QAAQ,mBACD,MAAM,gBACT,MAAM,WACX,GAAG,KACX,QAAQ,SAAS,GAAG,SAAS,CAS/B,CAAC;AAEF,eAAO,MAAM,oBAAoB,aACrB,QAAQ,mBACD,MAAM,gBACT,MAAM,mBACH,MAAM,WACd,GAAG,qBAUb,CAAC"}
|
package/dist/utils/balance.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { getAddress } from "@ethersproject/address";
|
|
2
|
+
import { AddressZero } from "@ethersproject/constants";
|
|
2
3
|
import { Contract } from "@ethersproject/contracts";
|
|
3
4
|
import { Web3Provider } from "@ethersproject/providers";
|
|
4
5
|
import { abiERC20 } from "@metamask/metamask-eth-abis";
|
|
5
|
-
import { isNativeAddress } from "./bridge.mjs";
|
|
6
6
|
export const fetchTokenBalance = async (address, userAddress, provider) => {
|
|
7
7
|
const ethersProvider = new Web3Provider(provider);
|
|
8
8
|
const tokenContract = new Contract(address, abiERC20, ethersProvider);
|
|
@@ -13,7 +13,7 @@ export const fetchTokenBalance = async (address, userAddress, provider) => {
|
|
|
13
13
|
};
|
|
14
14
|
export const calcLatestSrcBalance = async (provider, selectedAddress, tokenAddress, chainId) => {
|
|
15
15
|
if (tokenAddress && chainId) {
|
|
16
|
-
if (
|
|
16
|
+
if (tokenAddress === AddressZero) {
|
|
17
17
|
const ethersProvider = new Web3Provider(provider);
|
|
18
18
|
return await ethersProvider.getBalance(getAddress(selectedAddress));
|
|
19
19
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"balance.mjs","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,+BAA+B;AAEpD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"balance.mjs","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,+BAA+B;AAEpD,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,YAAY,EAAE,iCAAiC;AACxD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAIvD,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EACpC,OAAe,EACf,WAAmB,EACnB,QAAkB,EACc,EAAE;IAClC,MAAM,cAAc,GAAG,IAAI,YAAY,CAAC,QAAQ,CAAC,CAAC;IAClD,MAAM,aAAa,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE,QAAQ,EAAE,cAAc,CAAC,CAAC;IACtE,MAAM,mBAAmB,GACvB,OAAO,aAAa,EAAE,SAAS,KAAK,UAAU;QAC5C,CAAC,CAAC,aAAa,CAAC,SAAS,CAAC,WAAW,CAAC;QACtC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACjC,OAAO,MAAM,mBAAmB,CAAC;AACnC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,EACvC,QAAkB,EAClB,eAAuB,EACvB,YAAoB,EACpB,OAAY,EACoB,EAAE;IAClC,IAAI,YAAY,IAAI,OAAO,EAAE;QAC3B,IAAI,YAAY,KAAK,WAAW,EAAE;YAChC,MAAM,cAAc,GAAG,IAAI,YAAY,CAAC,QAAQ,CAAC,CAAC;YAClD,OAAO,MAAM,cAAc,CAAC,UAAU,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,CAAC;SACrE;QACD,OAAO,MAAM,iBAAiB,CAAC,YAAY,EAAE,eAAe,EAAE,QAAQ,CAAC,CAAC;KACzE;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,EACvC,QAAkB,EAClB,eAAuB,EACvB,YAAoB,EACpB,eAAuB,EACvB,OAAY,EACZ,EAAE;IACF,MAAM,eAAe,GAAG,MAAM,oBAAoB,CAChD,QAAQ,EACR,eAAe,EACf,YAAY,EACZ,OAAO,CACR,CAAC;IAEF,OAAO,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AACxE,CAAC,CAAC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport type { BigNumber } from '@ethersproject/bignumber';\nimport { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { Provider } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nexport const fetchTokenBalance = async (\n address: string,\n userAddress: string,\n provider: Provider,\n): Promise<BigNumber | undefined> => {\n const ethersProvider = new Web3Provider(provider);\n const tokenContract = new Contract(address, abiERC20, ethersProvider);\n const tokenBalancePromise =\n typeof tokenContract?.balanceOf === 'function'\n ? tokenContract.balanceOf(userAddress)\n : Promise.resolve(undefined);\n return await tokenBalancePromise;\n};\n\nexport const calcLatestSrcBalance = async (\n provider: Provider,\n selectedAddress: string,\n tokenAddress: string,\n chainId: Hex,\n): Promise<BigNumber | undefined> => {\n if (tokenAddress && chainId) {\n if (tokenAddress === AddressZero) {\n const ethersProvider = new Web3Provider(provider);\n return await ethersProvider.getBalance(getAddress(selectedAddress));\n }\n return await fetchTokenBalance(tokenAddress, selectedAddress, provider);\n }\n return undefined;\n};\n\nexport const hasSufficientBalance = async (\n provider: Provider,\n selectedAddress: string,\n tokenAddress: string,\n fromTokenAmount: string,\n chainId: Hex,\n) => {\n const srcTokenBalance = await calcLatestSrcBalance(\n provider,\n selectedAddress,\n tokenAddress,\n chainId,\n );\n\n return srcTokenBalance ? srcTokenBalance.gte(fromTokenAmount) : false;\n};\n"]}
|
package/dist/utils/bridge.cjs
CHANGED
|
@@ -1,52 +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
|
-
/**
|
|
19
|
-
* Returns the native assetType for a given chainId and native currency symbol
|
|
20
|
-
* Note that the return value is used as the assetId although it is a CaipAssetType
|
|
21
|
-
*
|
|
22
|
-
* @param chainId - The chainId to get the native assetType for
|
|
23
|
-
* @param nativeCurrencySymbol - The native currency symbol for the given chainId
|
|
24
|
-
* @returns The native assetType for the given chainId
|
|
25
|
-
*/
|
|
26
|
-
const getNativeAssetCaipAssetType = (chainId, nativeCurrencySymbol) => {
|
|
27
|
-
return `${(0, caip_formatters_1.formatChainIdToCaip)(chainId)}/${tokens_1.SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Returns the native swaps or bridge asset for a given chainId
|
|
31
|
-
*
|
|
32
|
-
* @param chainId - The chainId to get the default token for
|
|
33
|
-
* @returns The native asset for the given chainId
|
|
34
|
-
* @throws If no native asset is defined for the given chainId
|
|
35
|
-
*/
|
|
36
|
-
const getNativeAssetForChainId = (chainId) => {
|
|
37
|
-
const chainIdInCaip = (0, caip_formatters_1.formatChainIdToCaip)(chainId);
|
|
38
|
-
const nativeToken = tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[(0, caip_formatters_1.formatChainIdToCaip)(chainId)] ??
|
|
39
|
-
tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[(0, caip_formatters_1.formatChainIdToHex)(chainId)];
|
|
40
|
-
if (!nativeToken) {
|
|
41
|
-
throw new Error(`No XChain Swaps native asset found for chainId: ${chainId}`);
|
|
42
|
-
}
|
|
43
|
-
return {
|
|
44
|
-
...nativeToken,
|
|
45
|
-
chainId: (0, caip_formatters_1.formatChainIdToDec)(chainId),
|
|
46
|
-
assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
exports.getNativeAssetForChainId = getNativeAssetForChainId;
|
|
50
13
|
/**
|
|
51
14
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
52
15
|
*
|
|
@@ -85,7 +48,8 @@ const isSwapsDefaultTokenAddress = (address, chainId) => {
|
|
|
85
48
|
if (!address || !chainId) {
|
|
86
49
|
return false;
|
|
87
50
|
}
|
|
88
|
-
return address ===
|
|
51
|
+
return (address ===
|
|
52
|
+
tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address);
|
|
89
53
|
};
|
|
90
54
|
exports.isSwapsDefaultTokenAddress = isSwapsDefaultTokenAddress;
|
|
91
55
|
/**
|
|
@@ -100,32 +64,8 @@ const isSwapsDefaultTokenSymbol = (symbol, chainId) => {
|
|
|
100
64
|
if (!symbol || !chainId) {
|
|
101
65
|
return false;
|
|
102
66
|
}
|
|
103
|
-
return symbol ===
|
|
67
|
+
return (symbol ===
|
|
68
|
+
tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.symbol);
|
|
104
69
|
};
|
|
105
70
|
exports.isSwapsDefaultTokenSymbol = isSwapsDefaultTokenSymbol;
|
|
106
|
-
/**
|
|
107
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
108
|
-
*
|
|
109
|
-
* @param address - The address to check
|
|
110
|
-
* @returns Whether the address is a native asset
|
|
111
|
-
*/
|
|
112
|
-
const isNativeAddress = (address) => address === constants_1.AddressZero || // bridge and swap apis set the native asset address to zero
|
|
113
|
-
address === '' || // assets controllers set the native asset address to an empty string
|
|
114
|
-
!address ||
|
|
115
|
-
address.endsWith('11111111111111111111111111111111') || // token-api and bridge-api use this as the solana native assetId
|
|
116
|
-
[(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
|
|
117
|
-
exports.isNativeAddress = isNativeAddress;
|
|
118
|
-
/**
|
|
119
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
120
|
-
*
|
|
121
|
-
* @param chainId - The chainId to check
|
|
122
|
-
* @returns Whether the chainId is Solana
|
|
123
|
-
*/
|
|
124
|
-
const isSolanaChainId = (chainId) => {
|
|
125
|
-
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
126
|
-
return chainId === keyring_api_1.SolScope.Mainnet.toString();
|
|
127
|
-
}
|
|
128
|
-
return chainId.toString() === types_1.ChainId.SOLANA.toString();
|
|
129
|
-
};
|
|
130
|
-
exports.isSolanaChainId = isSolanaChainId;
|
|
131
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,+BAA+B,mCAE1C;AAEF;;;;GAIG;AAEI,MAAM,mBAAmB,GAAG,GAAG,EAAE;IACtC,MAAM,qBAAqB,GAAG,IAAI,oBAAQ,CAAC,yBAAgB,EAAE,4BAAQ,CAAC;SACnE,SAAS,CAAC;IACb,MAAM,IAAI,GAAG,qBAAqB,CAAC,kBAAkB,CAAC,SAAS,EAAE;QAC/D,oCAA2B;QAC3B,GAAG;KACJ,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AATW,QAAA,mBAAmB,uBAS9B;AAEK,MAAM,SAAS,GAAG,CAAC,OAAY,EAAE,OAAe,EAAE,EAAE,CACzD,OAAO,KAAK,kBAAS,CAAC,OAAO;IAC7B,OAAO,CAAC,WAAW,EAAE,KAAK,yBAAgB,CAAC,WAAW,EAAE,CAAC;AAF9C,QAAA,SAAS,aAEqC;AAEpD,MAAM,QAAQ,GAAG,CAAC,GAAG,UAAoB,EAAO,EAAE;IACvD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;QAC3B,OAAO,KAAK,CAAC;KACd;IAED,MAAM,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1E,OAAO,KAAK,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;AACjC,CAAC,CAAC;AAPW,QAAA,QAAQ,YAOnB;AACF;;;;;;;GAOG;AAEI,MAAM,0BAA0B,GAAG,CAAC,OAAe,EAAE,OAAY,EAAE,EAAE;IAC1E,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CACL,OAAO;QACP,wCAA+B,CAC7B,OAAuD,CACxD,EAAE,OAAO,CACX,CAAC;AACJ,CAAC,CAAC;AAXW,QAAA,0BAA0B,8BAWrC;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,15 +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
|
-
* @throws If no native asset is defined for the given chainId
|
|
11
|
-
*/
|
|
12
|
-
export declare const getNativeAssetForChainId: (chainId: string | number | Hex | CaipChainId) => BridgeAsset;
|
|
13
4
|
/**
|
|
14
5
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
15
6
|
*
|
|
@@ -26,7 +17,7 @@ export declare const sumHexes: (...hexStrings: string[]) => Hex;
|
|
|
26
17
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
27
18
|
* @returns Whether the address is the provided chain's default token address
|
|
28
19
|
*/
|
|
29
|
-
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
20
|
+
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex) => boolean;
|
|
30
21
|
/**
|
|
31
22
|
* Checks whether the provided symbol is strictly equal to the symbol for
|
|
32
23
|
* the default swaps token of the provided chain.
|
|
@@ -35,19 +26,5 @@ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
|
35
26
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
36
27
|
* @returns Whether the symbol is the provided chain's default token symbol
|
|
37
28
|
*/
|
|
38
|
-
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex
|
|
39
|
-
/**
|
|
40
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
41
|
-
*
|
|
42
|
-
* @param address - The address to check
|
|
43
|
-
* @returns Whether the address is a native asset
|
|
44
|
-
*/
|
|
45
|
-
export declare const isNativeAddress: (address?: string | null) => boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
48
|
-
*
|
|
49
|
-
* @param chainId - The chainId to check
|
|
50
|
-
* @returns Whether the chainId is Solana
|
|
51
|
-
*/
|
|
52
|
-
export declare const isSolanaChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
|
|
29
|
+
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex) => boolean;
|
|
53
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,15 +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
|
-
* @throws If no native asset is defined for the given chainId
|
|
11
|
-
*/
|
|
12
|
-
export declare const getNativeAssetForChainId: (chainId: string | number | Hex | CaipChainId) => BridgeAsset;
|
|
13
4
|
/**
|
|
14
5
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
15
6
|
*
|
|
@@ -26,7 +17,7 @@ export declare const sumHexes: (...hexStrings: string[]) => Hex;
|
|
|
26
17
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
27
18
|
* @returns Whether the address is the provided chain's default token address
|
|
28
19
|
*/
|
|
29
|
-
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
20
|
+
export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex) => boolean;
|
|
30
21
|
/**
|
|
31
22
|
* Checks whether the provided symbol is strictly equal to the symbol for
|
|
32
23
|
* the default swaps token of the provided chain.
|
|
@@ -35,19 +26,5 @@ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex
|
|
|
35
26
|
* @param chainId - The hex encoded chain ID of the default swaps token to check
|
|
36
27
|
* @returns Whether the symbol is the provided chain's default token symbol
|
|
37
28
|
*/
|
|
38
|
-
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex
|
|
39
|
-
/**
|
|
40
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
41
|
-
*
|
|
42
|
-
* @param address - The address to check
|
|
43
|
-
* @returns Whether the address is a native asset
|
|
44
|
-
*/
|
|
45
|
-
export declare const isNativeAddress: (address?: string | null) => boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
48
|
-
*
|
|
49
|
-
* @param chainId - The chainId to check
|
|
50
|
-
* @returns Whether the chainId is Solana
|
|
51
|
-
*/
|
|
52
|
-
export declare const isSolanaChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
|
|
29
|
+
export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex) => boolean;
|
|
53
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,47 +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
|
-
/**
|
|
15
|
-
* Returns the native assetType for a given chainId and native currency symbol
|
|
16
|
-
* Note that the return value is used as the assetId although it is a CaipAssetType
|
|
17
|
-
*
|
|
18
|
-
* @param chainId - The chainId to get the native assetType for
|
|
19
|
-
* @param nativeCurrencySymbol - The native currency symbol for the given chainId
|
|
20
|
-
* @returns The native assetType for the given chainId
|
|
21
|
-
*/
|
|
22
|
-
const getNativeAssetCaipAssetType = (chainId, nativeCurrencySymbol) => {
|
|
23
|
-
return `${formatChainIdToCaip(chainId)}/${SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Returns the native swaps or bridge asset for a given chainId
|
|
27
|
-
*
|
|
28
|
-
* @param chainId - The chainId to get the default token for
|
|
29
|
-
* @returns The native asset for the given chainId
|
|
30
|
-
* @throws If no native asset is defined for the given chainId
|
|
31
|
-
*/
|
|
32
|
-
export const getNativeAssetForChainId = (chainId) => {
|
|
33
|
-
const chainIdInCaip = formatChainIdToCaip(chainId);
|
|
34
|
-
const nativeToken = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[formatChainIdToCaip(chainId)] ??
|
|
35
|
-
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[formatChainIdToHex(chainId)];
|
|
36
|
-
if (!nativeToken) {
|
|
37
|
-
throw new Error(`No XChain Swaps native asset found for chainId: ${chainId}`);
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
...nativeToken,
|
|
41
|
-
chainId: formatChainIdToDec(chainId),
|
|
42
|
-
assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
9
|
/**
|
|
46
10
|
* A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
|
|
47
11
|
*
|
|
@@ -77,7 +41,8 @@ export const isSwapsDefaultTokenAddress = (address, chainId) => {
|
|
|
77
41
|
if (!address || !chainId) {
|
|
78
42
|
return false;
|
|
79
43
|
}
|
|
80
|
-
return address ===
|
|
44
|
+
return (address ===
|
|
45
|
+
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address);
|
|
81
46
|
};
|
|
82
47
|
/**
|
|
83
48
|
* Checks whether the provided symbol is strictly equal to the symbol for
|
|
@@ -91,29 +56,7 @@ export const isSwapsDefaultTokenSymbol = (symbol, chainId) => {
|
|
|
91
56
|
if (!symbol || !chainId) {
|
|
92
57
|
return false;
|
|
93
58
|
}
|
|
94
|
-
return symbol ===
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Checks whether the address is a native asset in any supported xchain swaps network
|
|
98
|
-
*
|
|
99
|
-
* @param address - The address to check
|
|
100
|
-
* @returns Whether the address is a native asset
|
|
101
|
-
*/
|
|
102
|
-
export const isNativeAddress = (address) => address === AddressZero || // bridge and swap apis set the native asset address to zero
|
|
103
|
-
address === '' || // assets controllers set the native asset address to an empty string
|
|
104
|
-
!address ||
|
|
105
|
-
address.endsWith('11111111111111111111111111111111') || // token-api and bridge-api use this as the solana native assetId
|
|
106
|
-
[getNativeAssetForChainId(ChainId.SOLANA).assetId].some((assetId) => assetId.includes(address) && !isStrictHexString(address)); // solana native assetId used in the extension client
|
|
107
|
-
/**
|
|
108
|
-
* Checks whether the chainId matches Solana in CaipChainId or number format
|
|
109
|
-
*
|
|
110
|
-
* @param chainId - The chainId to check
|
|
111
|
-
* @returns Whether the chainId is Solana
|
|
112
|
-
*/
|
|
113
|
-
export const isSolanaChainId = (chainId) => {
|
|
114
|
-
if (isCaipChainId(chainId)) {
|
|
115
|
-
return chainId === SolScope.Mainnet.toString();
|
|
116
|
-
}
|
|
117
|
-
return chainId.toString() === ChainId.SOLANA.toString();
|
|
59
|
+
return (symbol ===
|
|
60
|
+
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.symbol);
|
|
118
61
|
};
|
|
119
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,qBAAqB,GAAG,IAAI,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC;SACnE,SAAS,CAAC;IACb,MAAM,IAAI,GAAG,qBAAqB,CAAC,kBAAkB,CAAC,SAAS,EAAE;QAC/D,2BAA2B;QAC3B,GAAG;KACJ,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,OAAY,EAAE,OAAe,EAAE,EAAE,CACzD,OAAO,KAAK,SAAS,CAAC,OAAO;IAC7B,OAAO,CAAC,WAAW,EAAE,KAAK,gBAAgB,CAAC,WAAW,EAAE,CAAC;AAE3D,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAG,UAAoB,EAAO,EAAE;IACvD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;QAC3B,OAAO,KAAK,CAAC;KACd;IAED,MAAM,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1E,OAAO,KAAK,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;AACjC,CAAC,CAAC;AACF;;;;;;;GAOG;AAEH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,OAAe,EAAE,OAAY,EAAE,EAAE;IAC1E,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CACL,OAAO;QACP,+BAA+B,CAC7B,OAAuD,CACxD,EAAE,OAAO,CACX,CAAC;AACJ,CAAC,CAAC;AACF;;;;;;;GAOG;AAEH,MAAM,CAAC,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,+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);
|