@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
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatAddressToCaipReference = exports.formatChainIdToHex = exports.formatChainIdToDec = exports.formatChainIdToCaip = void 0;
|
|
4
|
-
const address_1 = require("@ethersproject/address");
|
|
5
|
-
const constants_1 = require("@ethersproject/constants");
|
|
6
|
-
const controller_utils_1 = require("@metamask/controller-utils");
|
|
7
|
-
const keyring_api_1 = require("@metamask/keyring-api");
|
|
8
|
-
const multichain_network_controller_1 = require("@metamask/multichain-network-controller");
|
|
9
|
-
const utils_1 = require("@metamask/utils");
|
|
10
|
-
const bridge_1 = require("./bridge.cjs");
|
|
11
|
-
const types_1 = require("../types.cjs");
|
|
12
|
-
/**
|
|
13
|
-
* Converts a chainId to a CaipChainId
|
|
14
|
-
*
|
|
15
|
-
* @param chainId - The chainId to convert
|
|
16
|
-
* @returns The CaipChainId
|
|
17
|
-
*/
|
|
18
|
-
const formatChainIdToCaip = (chainId) => {
|
|
19
|
-
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
20
|
-
return chainId;
|
|
21
|
-
}
|
|
22
|
-
if ((0, utils_1.isStrictHexString)(chainId)) {
|
|
23
|
-
return (0, multichain_network_controller_1.toEvmCaipChainId)(chainId);
|
|
24
|
-
}
|
|
25
|
-
if ((0, bridge_1.isSolanaChainId)(chainId)) {
|
|
26
|
-
return keyring_api_1.SolScope.Mainnet;
|
|
27
|
-
}
|
|
28
|
-
return (0, multichain_network_controller_1.toEvmCaipChainId)((0, utils_1.numberToHex)(Number(chainId)));
|
|
29
|
-
};
|
|
30
|
-
exports.formatChainIdToCaip = formatChainIdToCaip;
|
|
31
|
-
/**
|
|
32
|
-
* Converts a chainId to a decimal number that can be used for bridge-api requests
|
|
33
|
-
*
|
|
34
|
-
* @param chainId - The chainId to convert
|
|
35
|
-
* @returns The decimal number
|
|
36
|
-
*/
|
|
37
|
-
const formatChainIdToDec = (chainId) => {
|
|
38
|
-
if ((0, utils_1.isStrictHexString)(chainId)) {
|
|
39
|
-
return (0, controller_utils_1.convertHexToDecimal)(chainId);
|
|
40
|
-
}
|
|
41
|
-
if (chainId === keyring_api_1.SolScope.Mainnet) {
|
|
42
|
-
return types_1.ChainId.SOLANA;
|
|
43
|
-
}
|
|
44
|
-
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
45
|
-
return Number(chainId.split(':').at(-1));
|
|
46
|
-
}
|
|
47
|
-
if (typeof chainId === 'string') {
|
|
48
|
-
return parseInt(chainId, 10);
|
|
49
|
-
}
|
|
50
|
-
return chainId;
|
|
51
|
-
};
|
|
52
|
-
exports.formatChainIdToDec = formatChainIdToDec;
|
|
53
|
-
/**
|
|
54
|
-
* Converts a chainId to a hex string used to read controller data within the app
|
|
55
|
-
* Hex chainIds are also used for fetching exchange rates
|
|
56
|
-
*
|
|
57
|
-
* @param chainId - The chainId to convert
|
|
58
|
-
* @returns The hex string
|
|
59
|
-
*/
|
|
60
|
-
const formatChainIdToHex = (chainId) => {
|
|
61
|
-
if ((0, utils_1.isStrictHexString)(chainId)) {
|
|
62
|
-
return chainId;
|
|
63
|
-
}
|
|
64
|
-
if (typeof chainId === 'number' || parseInt(chainId, 10)) {
|
|
65
|
-
return (0, utils_1.numberToHex)(Number(chainId));
|
|
66
|
-
}
|
|
67
|
-
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
68
|
-
const { reference } = (0, utils_1.parseCaipChainId)(chainId);
|
|
69
|
-
if ((0, utils_1.isCaipReference)(reference) && !isNaN(Number(reference))) {
|
|
70
|
-
return (0, utils_1.numberToHex)(Number(reference));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
// Throw an error if a non-evm chainId is passed to this function
|
|
74
|
-
// This should never happen, but it's a sanity check
|
|
75
|
-
throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);
|
|
76
|
-
};
|
|
77
|
-
exports.formatChainIdToHex = formatChainIdToHex;
|
|
78
|
-
/**
|
|
79
|
-
* Converts an asset or account address to a string that can be used for bridge-api requests
|
|
80
|
-
*
|
|
81
|
-
* @param address - The address to convert
|
|
82
|
-
* @returns The converted address
|
|
83
|
-
*/
|
|
84
|
-
const formatAddressToCaipReference = (address) => {
|
|
85
|
-
if ((0, utils_1.isStrictHexString)(address)) {
|
|
86
|
-
return (0, address_1.getAddress)(address);
|
|
87
|
-
}
|
|
88
|
-
// If the address looks like a native token, return the zero address because it's
|
|
89
|
-
// what bridge-api uses to represent a native asset
|
|
90
|
-
if ((0, bridge_1.isNativeAddress)(address)) {
|
|
91
|
-
return constants_1.AddressZero;
|
|
92
|
-
}
|
|
93
|
-
const addressWithoutPrefix = address.split(':').at(-1);
|
|
94
|
-
// If the address is not a valid hex string or CAIP address, throw an error
|
|
95
|
-
// This should never happen, but it's a sanity check
|
|
96
|
-
if (!addressWithoutPrefix) {
|
|
97
|
-
throw new Error('Invalid address');
|
|
98
|
-
}
|
|
99
|
-
return addressWithoutPrefix;
|
|
100
|
-
};
|
|
101
|
-
exports.formatAddressToCaipReference = formatAddressToCaipReference;
|
|
102
|
-
//# sourceMappingURL=caip-formatters.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.cjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;AACpD,wDAAuD;AACvD,iEAAiE;AACjE,uDAAiD;AACjD,2FAA2E;AAC3E,2CAQyB;AAEzB,yCAA4D;AAC5D,wCAAmC;AAEnC;;;;;GAKG;AACI,MAAM,mBAAmB,GAAG,CACjC,OAA4C,EAC/B,EAAE;IACf,IAAI,IAAA,qBAAa,EAAC,OAAO,CAAC,EAAE;QAC1B,OAAO,OAAO,CAAC;KAChB;IACD,IAAI,IAAA,yBAAiB,EAAC,OA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AI,IAAA,qBAAa,EAAC,OAAO,CAAC,EAAE;QAC1B,MAAM,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,OAAO,CAAC,CAAC;QAChD,IAAI,IAAA,uBAAe,EAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE;YAC3D,OAAO,IAAA,mBAAW,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;SACvC;KACF;IACD,iEAAiE;IACjE,oDAAoD;IACpD,MAAM,IAAI,KAAK,CAAC,sCAAsC,OAAO,EAAE,CAAC,CAAC;AACnE,CAAC,CAAC;AAlBW,QAAA,kBAAkB,sBAkB7B;AAEF;;;;;GAKG;AACI,MAAM,4BAA4B,GAAG,CAAC,OAAe,EAAE,EAAE;IAC9D,IAAI,IAAA,yBAAiB,EAAC,OAAO,CAAC,EAAE;QAC9B,OAAO,IAAA,oBAAU,EAAC,OAAO,CAAC,CAAC;KAC5B;IACD,iFAAiF;IACjF,mDAAmD;IACnD,IAAI,IAAA,wBAAe,EAAC,OAAO,CAAC,EAAE;QAC5B,OAAO,uBAAW,CAAC;KACpB;IACD,MAAM,oBAAoB,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,2EAA2E;IAC3E,oDAAoD;IACpD,IAAI,CAAC,oBAAoB,EAAE;QACzB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;KACpC;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC;AAhBW,QAAA,4BAA4B,gCAgBvC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { SolScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n} from '@metamask/utils';\n\nimport { isNativeAddress, isSolanaChainId } from './bridge';\nimport { ChainId } from '../types';\n\n/**\n * Converts a chainId to a CaipChainId\n *\n * @param chainId - The chainId to convert\n * @returns The CaipChainId\n */\nexport const formatChainIdToCaip = (\n chainId: Hex | number | CaipChainId | string,\n): CaipChainId => {\n if (isCaipChainId(chainId)) {\n return chainId;\n }\n if (isStrictHexString(chainId)) {\n return toEvmCaipChainId(chainId);\n }\n if (isSolanaChainId(chainId)) {\n return SolScope.Mainnet;\n }\n return toEvmCaipChainId(numberToHex(Number(chainId)));\n};\n\n/**\n * Converts a chainId to a decimal number that can be used for bridge-api requests\n *\n * @param chainId - The chainId to convert\n * @returns The decimal number\n */\nexport const formatChainIdToDec = (\n chainId: number | Hex | CaipChainId | string,\n) => {\n if (isStrictHexString(chainId)) {\n return convertHexToDecimal(chainId);\n }\n if (chainId === SolScope.Mainnet) {\n return ChainId.SOLANA;\n }\n if (isCaipChainId(chainId)) {\n return Number(chainId.split(':').at(-1));\n }\n if (typeof chainId === 'string') {\n return parseInt(chainId, 10);\n }\n return chainId;\n};\n\n/**\n * Converts a chainId to a hex string used to read controller data within the app\n * Hex chainIds are also used for fetching exchange rates\n *\n * @param chainId - The chainId to convert\n * @returns The hex string\n */\nexport const formatChainIdToHex = (\n chainId: Hex | CaipChainId | string | number,\n): Hex => {\n if (isStrictHexString(chainId)) {\n return chainId;\n }\n if (typeof chainId === 'number' || parseInt(chainId, 10)) {\n return numberToHex(Number(chainId));\n }\n if (isCaipChainId(chainId)) {\n const { reference } = parseCaipChainId(chainId);\n if (isCaipReference(reference) && !isNaN(Number(reference))) {\n return numberToHex(Number(reference));\n }\n }\n // Throw an error if a non-evm chainId is passed to this function\n // This should never happen, but it's a sanity check\n throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);\n};\n\n/**\n * Converts an asset or account address to a string that can be used for bridge-api requests\n *\n * @param address - The address to convert\n * @returns The converted address\n */\nexport const formatAddressToCaipReference = (address: string) => {\n if (isStrictHexString(address)) {\n return getAddress(address);\n }\n // If the address looks like a native token, return the zero address because it's\n // what bridge-api uses to represent a native asset\n if (isNativeAddress(address)) {\n return AddressZero;\n }\n const addressWithoutPrefix = address.split(':').at(-1);\n // If the address is not a valid hex string or CAIP address, throw an error\n // This should never happen, but it's a sanity check\n if (!addressWithoutPrefix) {\n throw new Error('Invalid address');\n }\n return addressWithoutPrefix;\n};\n"]}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type Hex, type CaipChainId } from "@metamask/utils";
|
|
2
|
-
/**
|
|
3
|
-
* Converts a chainId to a CaipChainId
|
|
4
|
-
*
|
|
5
|
-
* @param chainId - The chainId to convert
|
|
6
|
-
* @returns The CaipChainId
|
|
7
|
-
*/
|
|
8
|
-
export declare const formatChainIdToCaip: (chainId: Hex | number | CaipChainId | string) => CaipChainId;
|
|
9
|
-
/**
|
|
10
|
-
* Converts a chainId to a decimal number that can be used for bridge-api requests
|
|
11
|
-
*
|
|
12
|
-
* @param chainId - The chainId to convert
|
|
13
|
-
* @returns The decimal number
|
|
14
|
-
*/
|
|
15
|
-
export declare const formatChainIdToDec: (chainId: number | Hex | CaipChainId | string) => number;
|
|
16
|
-
/**
|
|
17
|
-
* Converts a chainId to a hex string used to read controller data within the app
|
|
18
|
-
* Hex chainIds are also used for fetching exchange rates
|
|
19
|
-
*
|
|
20
|
-
* @param chainId - The chainId to convert
|
|
21
|
-
* @returns The hex string
|
|
22
|
-
*/
|
|
23
|
-
export declare const formatChainIdToHex: (chainId: Hex | CaipChainId | string | number) => Hex;
|
|
24
|
-
/**
|
|
25
|
-
* Converts an asset or account address to a string that can be used for bridge-api requests
|
|
26
|
-
*
|
|
27
|
-
* @param address - The address to convert
|
|
28
|
-
* @returns The converted address
|
|
29
|
-
*/
|
|
30
|
-
export declare const formatAddressToCaipReference: (address: string) => string;
|
|
31
|
-
//# sourceMappingURL=caip-formatters.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAMjB,wBAAwB;AAKzB;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAWF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAe7C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YACpB,GAAG,GAAG,WAAW,GAAG,MAAM,GAAG,MAAM,KAC3C,GAgBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,YAAa,MAAM,WAgB3D,CAAC"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type Hex, type CaipChainId } from "@metamask/utils";
|
|
2
|
-
/**
|
|
3
|
-
* Converts a chainId to a CaipChainId
|
|
4
|
-
*
|
|
5
|
-
* @param chainId - The chainId to convert
|
|
6
|
-
* @returns The CaipChainId
|
|
7
|
-
*/
|
|
8
|
-
export declare const formatChainIdToCaip: (chainId: Hex | number | CaipChainId | string) => CaipChainId;
|
|
9
|
-
/**
|
|
10
|
-
* Converts a chainId to a decimal number that can be used for bridge-api requests
|
|
11
|
-
*
|
|
12
|
-
* @param chainId - The chainId to convert
|
|
13
|
-
* @returns The decimal number
|
|
14
|
-
*/
|
|
15
|
-
export declare const formatChainIdToDec: (chainId: number | Hex | CaipChainId | string) => number;
|
|
16
|
-
/**
|
|
17
|
-
* Converts a chainId to a hex string used to read controller data within the app
|
|
18
|
-
* Hex chainIds are also used for fetching exchange rates
|
|
19
|
-
*
|
|
20
|
-
* @param chainId - The chainId to convert
|
|
21
|
-
* @returns The hex string
|
|
22
|
-
*/
|
|
23
|
-
export declare const formatChainIdToHex: (chainId: Hex | CaipChainId | string | number) => Hex;
|
|
24
|
-
/**
|
|
25
|
-
* Converts an asset or account address to a string that can be used for bridge-api requests
|
|
26
|
-
*
|
|
27
|
-
* @param address - The address to convert
|
|
28
|
-
* @returns The converted address
|
|
29
|
-
*/
|
|
30
|
-
export declare const formatAddressToCaipReference: (address: string) => string;
|
|
31
|
-
//# sourceMappingURL=caip-formatters.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.d.mts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAMjB,wBAAwB;AAKzB;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAWF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAe7C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YACpB,GAAG,GAAG,WAAW,GAAG,MAAM,GAAG,MAAM,KAC3C,GAgBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,YAAa,MAAM,WAgB3D,CAAC"}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { getAddress } from "@ethersproject/address";
|
|
2
|
-
import { AddressZero } from "@ethersproject/constants";
|
|
3
|
-
import { convertHexToDecimal } from "@metamask/controller-utils";
|
|
4
|
-
import { SolScope } from "@metamask/keyring-api";
|
|
5
|
-
import { toEvmCaipChainId } from "@metamask/multichain-network-controller";
|
|
6
|
-
import { isCaipChainId, isStrictHexString, parseCaipChainId, isCaipReference, numberToHex } from "@metamask/utils";
|
|
7
|
-
import { isNativeAddress, isSolanaChainId } from "./bridge.mjs";
|
|
8
|
-
import { ChainId } from "../types.mjs";
|
|
9
|
-
/**
|
|
10
|
-
* Converts a chainId to a CaipChainId
|
|
11
|
-
*
|
|
12
|
-
* @param chainId - The chainId to convert
|
|
13
|
-
* @returns The CaipChainId
|
|
14
|
-
*/
|
|
15
|
-
export const formatChainIdToCaip = (chainId) => {
|
|
16
|
-
if (isCaipChainId(chainId)) {
|
|
17
|
-
return chainId;
|
|
18
|
-
}
|
|
19
|
-
if (isStrictHexString(chainId)) {
|
|
20
|
-
return toEvmCaipChainId(chainId);
|
|
21
|
-
}
|
|
22
|
-
if (isSolanaChainId(chainId)) {
|
|
23
|
-
return SolScope.Mainnet;
|
|
24
|
-
}
|
|
25
|
-
return toEvmCaipChainId(numberToHex(Number(chainId)));
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Converts a chainId to a decimal number that can be used for bridge-api requests
|
|
29
|
-
*
|
|
30
|
-
* @param chainId - The chainId to convert
|
|
31
|
-
* @returns The decimal number
|
|
32
|
-
*/
|
|
33
|
-
export const formatChainIdToDec = (chainId) => {
|
|
34
|
-
if (isStrictHexString(chainId)) {
|
|
35
|
-
return convertHexToDecimal(chainId);
|
|
36
|
-
}
|
|
37
|
-
if (chainId === SolScope.Mainnet) {
|
|
38
|
-
return ChainId.SOLANA;
|
|
39
|
-
}
|
|
40
|
-
if (isCaipChainId(chainId)) {
|
|
41
|
-
return Number(chainId.split(':').at(-1));
|
|
42
|
-
}
|
|
43
|
-
if (typeof chainId === 'string') {
|
|
44
|
-
return parseInt(chainId, 10);
|
|
45
|
-
}
|
|
46
|
-
return chainId;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Converts a chainId to a hex string used to read controller data within the app
|
|
50
|
-
* Hex chainIds are also used for fetching exchange rates
|
|
51
|
-
*
|
|
52
|
-
* @param chainId - The chainId to convert
|
|
53
|
-
* @returns The hex string
|
|
54
|
-
*/
|
|
55
|
-
export const formatChainIdToHex = (chainId) => {
|
|
56
|
-
if (isStrictHexString(chainId)) {
|
|
57
|
-
return chainId;
|
|
58
|
-
}
|
|
59
|
-
if (typeof chainId === 'number' || parseInt(chainId, 10)) {
|
|
60
|
-
return numberToHex(Number(chainId));
|
|
61
|
-
}
|
|
62
|
-
if (isCaipChainId(chainId)) {
|
|
63
|
-
const { reference } = parseCaipChainId(chainId);
|
|
64
|
-
if (isCaipReference(reference) && !isNaN(Number(reference))) {
|
|
65
|
-
return numberToHex(Number(reference));
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
// Throw an error if a non-evm chainId is passed to this function
|
|
69
|
-
// This should never happen, but it's a sanity check
|
|
70
|
-
throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);
|
|
71
|
-
};
|
|
72
|
-
/**
|
|
73
|
-
* Converts an asset or account address to a string that can be used for bridge-api requests
|
|
74
|
-
*
|
|
75
|
-
* @param address - The address to convert
|
|
76
|
-
* @returns The converted address
|
|
77
|
-
*/
|
|
78
|
-
export const formatAddressToCaipReference = (address) => {
|
|
79
|
-
if (isStrictHexString(address)) {
|
|
80
|
-
return getAddress(address);
|
|
81
|
-
}
|
|
82
|
-
// If the address looks like a native token, return the zero address because it's
|
|
83
|
-
// what bridge-api uses to represent a native asset
|
|
84
|
-
if (isNativeAddress(address)) {
|
|
85
|
-
return AddressZero;
|
|
86
|
-
}
|
|
87
|
-
const addressWithoutPrefix = address.split(':').at(-1);
|
|
88
|
-
// If the address is not a valid hex string or CAIP address, throw an error
|
|
89
|
-
// This should never happen, but it's a sanity check
|
|
90
|
-
if (!addressWithoutPrefix) {
|
|
91
|
-
throw new Error('Invalid address');
|
|
92
|
-
}
|
|
93
|
-
return addressWithoutPrefix;
|
|
94
|
-
};
|
|
95
|
-
//# sourceMappingURL=caip-formatters.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.mjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,+BAA+B;AACpD,OAAO,EAAE,WAAW,EAAE,i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I,KAAK,CAAC,iBAAiB,CAAC,CAAC;KACpC;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { SolScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n} from '@metamask/utils';\n\nimport { isNativeAddress, isSolanaChainId } from './bridge';\nimport { ChainId } from '../types';\n\n/**\n * Converts a chainId to a CaipChainId\n *\n * @param chainId - The chainId to convert\n * @returns The CaipChainId\n */\nexport const formatChainIdToCaip = (\n chainId: Hex | number | CaipChainId | string,\n): CaipChainId => {\n if (isCaipChainId(chainId)) {\n return chainId;\n }\n if (isStrictHexString(chainId)) {\n return toEvmCaipChainId(chainId);\n }\n if (isSolanaChainId(chainId)) {\n return SolScope.Mainnet;\n }\n return toEvmCaipChainId(numberToHex(Number(chainId)));\n};\n\n/**\n * Converts a chainId to a decimal number that can be used for bridge-api requests\n *\n * @param chainId - The chainId to convert\n * @returns The decimal number\n */\nexport const formatChainIdToDec = (\n chainId: number | Hex | CaipChainId | string,\n) => {\n if (isStrictHexString(chainId)) {\n return convertHexToDecimal(chainId);\n }\n if (chainId === SolScope.Mainnet) {\n return ChainId.SOLANA;\n }\n if (isCaipChainId(chainId)) {\n return Number(chainId.split(':').at(-1));\n }\n if (typeof chainId === 'string') {\n return parseInt(chainId, 10);\n }\n return chainId;\n};\n\n/**\n * Converts a chainId to a hex string used to read controller data within the app\n * Hex chainIds are also used for fetching exchange rates\n *\n * @param chainId - The chainId to convert\n * @returns The hex string\n */\nexport const formatChainIdToHex = (\n chainId: Hex | CaipChainId | string | number,\n): Hex => {\n if (isStrictHexString(chainId)) {\n return chainId;\n }\n if (typeof chainId === 'number' || parseInt(chainId, 10)) {\n return numberToHex(Number(chainId));\n }\n if (isCaipChainId(chainId)) {\n const { reference } = parseCaipChainId(chainId);\n if (isCaipReference(reference) && !isNaN(Number(reference))) {\n return numberToHex(Number(reference));\n }\n }\n // Throw an error if a non-evm chainId is passed to this function\n // This should never happen, but it's a sanity check\n throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);\n};\n\n/**\n * Converts an asset or account address to a string that can be used for bridge-api requests\n *\n * @param address - The address to convert\n * @returns The converted address\n */\nexport const formatAddressToCaipReference = (address: string) => {\n if (isStrictHexString(address)) {\n return getAddress(address);\n }\n // If the address looks like a native token, return the zero address because it's\n // what bridge-api uses to represent a native asset\n if (isNativeAddress(address)) {\n return AddressZero;\n }\n const addressWithoutPrefix = address.split(':').at(-1);\n // If the address is not a valid hex string or CAIP address, throw an error\n // This should never happen, but it's a sanity check\n if (!addressWithoutPrefix) {\n throw new Error('Invalid address');\n }\n return addressWithoutPrefix;\n};\n"]}
|