@metamask/bridge-controller 66.1.0 → 66.1.1
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 +9 -2
- package/dist/utils/caip-formatters.cjs +3 -3
- package/dist/utils/caip-formatters.cjs.map +1 -1
- package/dist/utils/caip-formatters.d.cts +1 -1
- package/dist/utils/caip-formatters.d.cts.map +1 -1
- package/dist/utils/caip-formatters.d.mts +1 -1
- package/dist/utils/caip-formatters.d.mts.map +1 -1
- package/dist/utils/caip-formatters.mjs +5 -5
- package/dist/utils/caip-formatters.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
## [66.1.
|
|
10
|
+
## [66.1.1]
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Return 0-prefixed hex string from `formatChainIdToHex` utility ([#7909](https://github.com/MetaMask/core/pull/7909))
|
|
15
|
+
|
|
16
|
+
## [66.1.0] [DEPRECATED]
|
|
11
17
|
|
|
12
18
|
### Added
|
|
13
19
|
|
|
@@ -1124,7 +1130,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
1124
1130
|
|
|
1125
1131
|
- Initial release ([#5317](https://github.com/MetaMask/core/pull/5317))
|
|
1126
1132
|
|
|
1127
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@66.1.
|
|
1133
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@66.1.1...HEAD
|
|
1134
|
+
[66.1.1]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@66.1.0...@metamask/bridge-controller@66.1.1
|
|
1128
1135
|
[66.1.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@66.0.0...@metamask/bridge-controller@66.1.0
|
|
1129
1136
|
[66.0.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@65.3.0...@metamask/bridge-controller@66.0.0
|
|
1130
1137
|
[65.3.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@65.2.0...@metamask/bridge-controller@65.3.0
|
|
@@ -32,7 +32,7 @@ const formatChainIdToCaip = (chainId) => {
|
|
|
32
32
|
if ((0, bridge_1.isTronChainId)(chainId)) {
|
|
33
33
|
return keyring_api_1.TrxScope.Mainnet;
|
|
34
34
|
}
|
|
35
|
-
return (0, multichain_network_controller_1.toEvmCaipChainId)((0,
|
|
35
|
+
return (0, multichain_network_controller_1.toEvmCaipChainId)((0, utils_1.numberToHex)(Number(chainId)));
|
|
36
36
|
};
|
|
37
37
|
exports.formatChainIdToCaip = formatChainIdToCaip;
|
|
38
38
|
/**
|
|
@@ -75,12 +75,12 @@ const formatChainIdToHex = (chainId) => {
|
|
|
75
75
|
return chainId;
|
|
76
76
|
}
|
|
77
77
|
if (typeof chainId === 'number' || parseInt(chainId, 10)) {
|
|
78
|
-
return (0,
|
|
78
|
+
return (0, utils_1.numberToHex)(Number(chainId));
|
|
79
79
|
}
|
|
80
80
|
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
81
81
|
const { reference } = (0, utils_1.parseCaipChainId)(chainId);
|
|
82
82
|
if ((0, utils_1.isCaipReference)(reference) && !isNaN(Number(reference))) {
|
|
83
|
-
return (0,
|
|
83
|
+
return (0, utils_1.numberToHex)(Number(reference));
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
// Throw an error if a non-evm chainId is passed to this function
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.cjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":";;;AAAA,qEAAqE;AACrE,oDAAoD;AACpD,wDAAuD;AACvD,
|
|
1
|
+
{"version":3,"file":"caip-formatters.cjs","sourceRoot":"","sources":["../../src/utils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QACrC,OAAO,2BAAmB,CAAC,MAAM,CAC/B,GAAG,WAAW,UAAU,gBAAgB,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED,IAAI,WAAW,KAAK,sBAAQ,CAAC,OAAO,EAAE,CAAC;QACrC,OAAO,2BAAmB,CAAC,MAAM,CAC/B,GAAG,WAAW,UAAU,gBAAgB,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED,aAAa;IACb,IAAI,CAAC,IAAA,yBAAiB,EAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,aAAa;IACb,MAAM,kBAAkB,GAAG,IAAA,uCAAoB,EAAC,gBAAgB,CAAC,CAAC;IAClE,OAAO,2BAAmB,CAAC,MAAM,CAC/B,GAAG,WAAW,UAAU,kBAAkB,EAAE,CAC7C,CAAC;AACJ,CAAC,CAAC;AArCW,QAAA,sBAAsB,0BAqCjC","sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\nimport { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport {\n convertHexToDecimal,\n toChecksumHexAddress,\n} from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport {\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n isCaipAssetType,\n CaipAssetTypeStruct,\n numberToHex,\n} from '@metamask/utils';\nimport type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\n\nimport {\n getNativeAssetForChainId,\n isBitcoinChainId,\n isNativeAddress,\n isSolanaChainId,\n isTronChainId,\n} from './bridge';\nimport type { GenericQuoteRequest } from '../types';\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 if (isBitcoinChainId(chainId)) {\n return BtcScope.Mainnet;\n }\n if (isTronChainId(chainId)) {\n return TrxScope.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 (chainId === BtcScope.Mainnet) {\n return ChainId.BTC;\n }\n if (chainId === TrxScope.Mainnet) {\n return ChainId.TRON;\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\n/**\n * Converts an address or assetId to a checksummed CaipAssetType\n *\n * @param addressOrAssetId - The address or assetId to convert\n * @param chainId - The chainId of the asset\n * @returns The CaipAssetType\n */\nexport const formatAddressToAssetId = (\n addressOrAssetId: Hex | CaipAssetType | string,\n chainId?: GenericQuoteRequest['srcChainId'],\n): CaipAssetType | undefined => {\n if (isCaipAssetType(addressOrAssetId)) {\n return addressOrAssetId;\n }\n if (!chainId) {\n return undefined;\n }\n\n const chainIdCaip = formatChainIdToCaip(chainId);\n if (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainIdCaip).assetId;\n }\n if (chainIdCaip === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(\n `${chainIdCaip}/token:${addressOrAssetId}`,\n );\n }\n\n if (chainIdCaip === TrxScope.Mainnet) {\n return CaipAssetTypeStruct.create(\n `${chainIdCaip}/trc20:${addressOrAssetId}`,\n );\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n\n // EVM assets\n const checksummedAddress = toChecksumHexAddress(addressOrAssetId);\n return CaipAssetTypeStruct.create(\n `${chainIdCaip}/erc20:${checksummedAddress}`,\n );\n};\n"]}
|
|
@@ -36,5 +36,5 @@ export declare const formatAddressToCaipReference: (address: string) => string;
|
|
|
36
36
|
* @param chainId - The chainId of the asset
|
|
37
37
|
* @returns The CaipAssetType
|
|
38
38
|
*/
|
|
39
|
-
export declare const formatAddressToAssetId: (addressOrAssetId
|
|
39
|
+
export declare const formatAddressToAssetId: (addressOrAssetId: Hex | CaipAssetType | string, chainId?: GenericQuoteRequest['srcChainId']) => CaipAssetType | undefined;
|
|
40
40
|
//# sourceMappingURL=caip-formatters.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AASvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAiBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAqB7C,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;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,
|
|
1
|
+
{"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AASvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAiBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAqB7C,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;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,qBACf,GAAG,GAAG,aAAa,GAAG,MAAM,YACpC,mBAAmB,CAAC,YAAY,CAAC,KAC1C,aAAa,GAAG,SAkClB,CAAC"}
|
|
@@ -36,5 +36,5 @@ export declare const formatAddressToCaipReference: (address: string) => string;
|
|
|
36
36
|
* @param chainId - The chainId of the asset
|
|
37
37
|
* @returns The CaipAssetType
|
|
38
38
|
*/
|
|
39
|
-
export declare const formatAddressToAssetId: (addressOrAssetId
|
|
39
|
+
export declare const formatAddressToAssetId: (addressOrAssetId: Hex | CaipAssetType | string, chainId?: GenericQuoteRequest['srcChainId']) => CaipAssetType | undefined;
|
|
40
40
|
//# sourceMappingURL=caip-formatters.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.d.mts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AASvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAiBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAqB7C,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;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,
|
|
1
|
+
{"version":3,"file":"caip-formatters.d.mts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AASvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAiBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAqB7C,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;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,qBACf,GAAG,GAAG,aAAa,GAAG,MAAM,YACpC,mBAAmB,CAAC,YAAY,CAAC,KAC1C,aAAa,GAAG,SAkClB,CAAC"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
2
2
|
import { getAddress } from "@ethersproject/address";
|
|
3
3
|
import { AddressZero } from "@ethersproject/constants";
|
|
4
|
-
import { convertHexToDecimal, toChecksumHexAddress
|
|
4
|
+
import { convertHexToDecimal, toChecksumHexAddress } from "@metamask/controller-utils";
|
|
5
5
|
import { BtcScope, SolScope, TrxScope } from "@metamask/keyring-api";
|
|
6
6
|
import { toEvmCaipChainId } from "@metamask/multichain-network-controller";
|
|
7
|
-
import { isCaipChainId, isStrictHexString, parseCaipChainId, isCaipReference, isCaipAssetType, CaipAssetTypeStruct } from "@metamask/utils";
|
|
7
|
+
import { isCaipChainId, isStrictHexString, parseCaipChainId, isCaipReference, isCaipAssetType, CaipAssetTypeStruct, numberToHex } from "@metamask/utils";
|
|
8
8
|
import { getNativeAssetForChainId, isBitcoinChainId, isNativeAddress, isSolanaChainId, isTronChainId } from "./bridge.mjs";
|
|
9
9
|
import { ChainId } from "../types.mjs";
|
|
10
10
|
/**
|
|
@@ -29,7 +29,7 @@ export const formatChainIdToCaip = (chainId) => {
|
|
|
29
29
|
if (isTronChainId(chainId)) {
|
|
30
30
|
return TrxScope.Mainnet;
|
|
31
31
|
}
|
|
32
|
-
return toEvmCaipChainId(
|
|
32
|
+
return toEvmCaipChainId(numberToHex(Number(chainId)));
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
35
|
* Converts a chainId to a decimal number that can be used for bridge-api requests
|
|
@@ -70,12 +70,12 @@ export const formatChainIdToHex = (chainId) => {
|
|
|
70
70
|
return chainId;
|
|
71
71
|
}
|
|
72
72
|
if (typeof chainId === 'number' || parseInt(chainId, 10)) {
|
|
73
|
-
return
|
|
73
|
+
return numberToHex(Number(chainId));
|
|
74
74
|
}
|
|
75
75
|
if (isCaipChainId(chainId)) {
|
|
76
76
|
const { reference } = parseCaipChainId(chainId);
|
|
77
77
|
if (isCaipReference(reference) && !isNaN(Number(reference))) {
|
|
78
|
-
return
|
|
78
|
+
return numberToHex(Number(reference));
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
// Throw an error if a non-evm chainId is passed to this function
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caip-formatters.mjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,EAAE,UAAU,EAAE,+BAA+B;AACpD,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EACL,mBAAmB,EACnB,oBAAoB,
|
|
1
|
+
{"version":3,"file":"caip-formatters.mjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,EAAE,UAAU,EAAE,+BAA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AI,WAAW,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC;QACrC,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,WAAW,UAAU,gBAAgB,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED,IAAI,WAAW,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC;QACrC,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,WAAW,UAAU,gBAAgB,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED,aAAa;IACb,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,aAAa;IACb,MAAM,kBAAkB,GAAG,oBAAoB,CAAC,gBAAgB,CAAC,CAAC;IAClE,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,WAAW,UAAU,kBAAkB,EAAE,CAC7C,CAAC;AACJ,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\nimport { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport {\n convertHexToDecimal,\n toChecksumHexAddress,\n} from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport {\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n isCaipAssetType,\n CaipAssetTypeStruct,\n numberToHex,\n} from '@metamask/utils';\nimport type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\n\nimport {\n getNativeAssetForChainId,\n isBitcoinChainId,\n isNativeAddress,\n isSolanaChainId,\n isTronChainId,\n} from './bridge';\nimport type { GenericQuoteRequest } from '../types';\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 if (isBitcoinChainId(chainId)) {\n return BtcScope.Mainnet;\n }\n if (isTronChainId(chainId)) {\n return TrxScope.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 (chainId === BtcScope.Mainnet) {\n return ChainId.BTC;\n }\n if (chainId === TrxScope.Mainnet) {\n return ChainId.TRON;\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\n/**\n * Converts an address or assetId to a checksummed CaipAssetType\n *\n * @param addressOrAssetId - The address or assetId to convert\n * @param chainId - The chainId of the asset\n * @returns The CaipAssetType\n */\nexport const formatAddressToAssetId = (\n addressOrAssetId: Hex | CaipAssetType | string,\n chainId?: GenericQuoteRequest['srcChainId'],\n): CaipAssetType | undefined => {\n if (isCaipAssetType(addressOrAssetId)) {\n return addressOrAssetId;\n }\n if (!chainId) {\n return undefined;\n }\n\n const chainIdCaip = formatChainIdToCaip(chainId);\n if (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainIdCaip).assetId;\n }\n if (chainIdCaip === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(\n `${chainIdCaip}/token:${addressOrAssetId}`,\n );\n }\n\n if (chainIdCaip === TrxScope.Mainnet) {\n return CaipAssetTypeStruct.create(\n `${chainIdCaip}/trc20:${addressOrAssetId}`,\n );\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n\n // EVM assets\n const checksummedAddress = toChecksumHexAddress(addressOrAssetId);\n return CaipAssetTypeStruct.create(\n `${chainIdCaip}/erc20:${checksummedAddress}`,\n );\n};\n"]}
|