@metamask/bridge-controller 9.0.0 → 11.0.0

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/dist/bridge-controller.cjs +96 -25
  3. package/dist/bridge-controller.cjs.map +1 -1
  4. package/dist/bridge-controller.d.cts +5 -5
  5. package/dist/bridge-controller.d.cts.map +1 -1
  6. package/dist/bridge-controller.d.mts +5 -5
  7. package/dist/bridge-controller.d.mts.map +1 -1
  8. package/dist/bridge-controller.mjs +97 -26
  9. package/dist/bridge-controller.mjs.map +1 -1
  10. package/dist/constants/bridge.cjs +2 -1
  11. package/dist/constants/bridge.cjs.map +1 -1
  12. package/dist/constants/bridge.d.cts +2 -1
  13. package/dist/constants/bridge.d.cts.map +1 -1
  14. package/dist/constants/bridge.d.mts +2 -1
  15. package/dist/constants/bridge.d.mts.map +1 -1
  16. package/dist/constants/bridge.mjs +2 -1
  17. package/dist/constants/bridge.mjs.map +1 -1
  18. package/dist/constants/chains.cjs +1 -12
  19. package/dist/constants/chains.cjs.map +1 -1
  20. package/dist/constants/chains.d.cts +0 -2
  21. package/dist/constants/chains.d.cts.map +1 -1
  22. package/dist/constants/chains.d.mts +0 -2
  23. package/dist/constants/chains.d.mts.map +1 -1
  24. package/dist/constants/chains.mjs +0 -11
  25. package/dist/constants/chains.mjs.map +1 -1
  26. package/dist/constants/tokens.cjs +61 -38
  27. package/dist/constants/tokens.cjs.map +1 -1
  28. package/dist/constants/tokens.d.cts +100 -48
  29. package/dist/constants/tokens.d.cts.map +1 -1
  30. package/dist/constants/tokens.d.mts +100 -48
  31. package/dist/constants/tokens.d.mts.map +1 -1
  32. package/dist/constants/tokens.mjs +37 -14
  33. package/dist/constants/tokens.mjs.map +1 -1
  34. package/dist/index.cjs +21 -1
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.cts +16 -4
  37. package/dist/index.d.cts.map +1 -1
  38. package/dist/index.d.mts +16 -4
  39. package/dist/index.d.mts.map +1 -1
  40. package/dist/index.mjs +11 -2
  41. package/dist/index.mjs.map +1 -1
  42. package/dist/types.cjs +4 -3
  43. package/dist/types.cjs.map +1 -1
  44. package/dist/types.d.cts +108 -54
  45. package/dist/types.d.cts.map +1 -1
  46. package/dist/types.d.mts +108 -54
  47. package/dist/types.d.mts.map +1 -1
  48. package/dist/types.mjs +4 -3
  49. package/dist/types.mjs.map +1 -1
  50. package/dist/utils/balance.cjs +2 -2
  51. package/dist/utils/balance.cjs.map +1 -1
  52. package/dist/utils/balance.d.cts.map +1 -1
  53. package/dist/utils/balance.d.mts.map +1 -1
  54. package/dist/utils/balance.mjs +2 -2
  55. package/dist/utils/balance.mjs.map +1 -1
  56. package/dist/utils/bridge.cjs +65 -5
  57. package/dist/utils/bridge.cjs.map +1 -1
  58. package/dist/utils/bridge.d.cts +27 -4
  59. package/dist/utils/bridge.d.cts.map +1 -1
  60. package/dist/utils/bridge.d.mts +27 -4
  61. package/dist/utils/bridge.d.mts.map +1 -1
  62. package/dist/utils/bridge.mjs +62 -5
  63. package/dist/utils/bridge.mjs.map +1 -1
  64. package/dist/utils/caip-formatters.cjs +102 -0
  65. package/dist/utils/caip-formatters.cjs.map +1 -0
  66. package/dist/utils/caip-formatters.d.cts +31 -0
  67. package/dist/utils/caip-formatters.d.cts.map +1 -0
  68. package/dist/utils/caip-formatters.d.mts +31 -0
  69. package/dist/utils/caip-formatters.d.mts.map +1 -0
  70. package/dist/utils/caip-formatters.mjs +95 -0
  71. package/dist/utils/caip-formatters.mjs.map +1 -0
  72. package/dist/utils/fetch.cjs +34 -26
  73. package/dist/utils/fetch.cjs.map +1 -1
  74. package/dist/utils/fetch.d.cts +6 -5
  75. package/dist/utils/fetch.d.cts.map +1 -1
  76. package/dist/utils/fetch.d.mts +6 -5
  77. package/dist/utils/fetch.d.mts.map +1 -1
  78. package/dist/utils/fetch.mjs +32 -24
  79. package/dist/utils/fetch.mjs.map +1 -1
  80. package/dist/utils/quote.cjs +12 -2
  81. package/dist/utils/quote.cjs.map +1 -1
  82. package/dist/utils/quote.d.cts +2 -2
  83. package/dist/utils/quote.d.cts.map +1 -1
  84. package/dist/utils/quote.d.mts +2 -2
  85. package/dist/utils/quote.d.mts.map +1 -1
  86. package/dist/utils/quote.mjs +12 -2
  87. package/dist/utils/quote.mjs.map +1 -1
  88. package/dist/utils/validators.cjs +15 -16
  89. package/dist/utils/validators.cjs.map +1 -1
  90. package/dist/utils/validators.d.cts +2 -3
  91. package/dist/utils/validators.d.cts.map +1 -1
  92. package/dist/utils/validators.d.mts +2 -3
  93. package/dist/utils/validators.d.mts.map +1 -1
  94. package/dist/utils/validators.mjs +16 -17
  95. package/dist/utils/validators.mjs.map +1 -1
  96. package/package.json +9 -4
@@ -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");
6
5
  const contracts_1 = require("@ethersproject/contracts");
7
6
  const providers_1 = require("@ethersproject/providers");
8
7
  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 (tokenAddress === constants_1.AddressZero) {
20
+ if ((0, bridge_1.isNativeAddress)(tokenAddress)) {
21
21
  const ethersProvider = new providers_1.Web3Provider(provider);
22
22
  return await ethersProvider.getBalance((0, address_1.getAddress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AAuB,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
+ {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AE;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 { 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\nimport { isNativeAddress } from './bridge';\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 (isNativeAddress(tokenAddress)) {\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;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
+ {"version":3,"file":"balance.d.cts","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAiC;AAI1D,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAC7D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,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;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
+ {"version":3,"file":"balance.d.mts","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAiC;AAI1D,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAC7D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,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,8 +1,8 @@
1
1
  import { getAddress } from "@ethersproject/address";
2
- import { AddressZero } from "@ethersproject/constants";
3
2
  import { Contract } from "@ethersproject/contracts";
4
3
  import { Web3Provider } from "@ethersproject/providers";
5
4
  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 (tokenAddress === AddressZero) {
16
+ if (isNativeAddress(tokenAddress)) {
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,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"]}
1
+ {"version":3,"file":"balance.mjs","sourceRoot":"","sources":["../../src/utils/balance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,+BAA+B;AAEpD,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,YAAY,EAAE,iCAAiC;AACxD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAIvD,OAAO,EAAE,eAAe,EAAE,qBAAiB;AAE3C,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,eAAe,CAAC,YAAY,CAAC,EAAE;YACjC,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 { 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\nimport { isNativeAddress } from './bridge';\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 (isNativeAddress(tokenAddress)) {\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,15 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getDefaultBridgeControllerState = void 0;
3
+ exports.isSolanaChainId = exports.isNativeAddress = exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getNativeAssetForChainId = exports.getDefaultBridgeControllerState = void 0;
4
+ const constants_1 = require("@ethersproject/constants");
4
5
  const contracts_1 = require("@ethersproject/contracts");
6
+ const keyring_api_1 = require("@metamask/keyring-api");
5
7
  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");
6
10
  const bridge_1 = require("../constants/bridge.cjs");
7
11
  const chains_1 = require("../constants/chains.cjs");
8
12
  const tokens_1 = require("../constants/tokens.cjs");
13
+ const types_1 = require("../types.cjs");
9
14
  const getDefaultBridgeControllerState = () => {
10
15
  return bridge_1.DEFAULT_BRIDGE_CONTROLLER_STATE;
11
16
  };
12
17
  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;
13
50
  /**
14
51
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
15
52
  *
@@ -48,8 +85,7 @@ const isSwapsDefaultTokenAddress = (address, chainId) => {
48
85
  if (!address || !chainId) {
49
86
  return false;
50
87
  }
51
- return (address ===
52
- tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address);
88
+ return address === (0, exports.getNativeAssetForChainId)(chainId)?.address;
53
89
  };
54
90
  exports.isSwapsDefaultTokenAddress = isSwapsDefaultTokenAddress;
55
91
  /**
@@ -64,8 +100,32 @@ const isSwapsDefaultTokenSymbol = (symbol, chainId) => {
64
100
  if (!symbol || !chainId) {
65
101
  return false;
66
102
  }
67
- return (symbol ===
68
- tokens_1.SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.symbol);
103
+ return symbol === (0, exports.getNativeAssetForChainId)(chainId)?.symbol;
69
104
  };
70
105
  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;
71
131
  //# sourceMappingURL=bridge.cjs.map
@@ -1 +1 @@
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,m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AAG,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"]}
1
+ {"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,wDAAoD;AACpD,uDAAiD;AACjD,mEAAuD;AAEvD,2CAA6E;AAE7E,2DAI2B;AAC3B,oDAI6B;AAC7B,oDAAgD;AAChD,oDAI6B;AAE7B,wCAAmC;AAE5B,MAAM,+BAA+B,GAAG,GAA0B,EAAE;IACzE,OAAO,wCAA+B,CAAC;AACzC,CAAC,CAAC;AAFW,QAAA,+BAA+B,mCAE1C;AAEF;;;;;;;GAOG;AACH,MAAM,2BAA2B,GAAG,CAClC,OAAoB,EACpB,oBAAyD,EAC1C,EAAE;IACjB,OAAO,GAAG,IAAA,qCAAmB,EAAC,OAAO,CAAC,IAAI,6BAAoB,CAAC,oBAAoB,CAAC,EAAE,CAAC;AACzF,CAAC,CAAC;AAEF;;;;;;GAMG;AACI,MAAM,wBAAwB,GAAG,CACtC,OAA4C,EAC/B,EAAE;IACf,MAAM,aAAa,GAAG,IAAA,qC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ACI,MAAM,eAAe,GAAG,CAC7B,OAA4C,EAC5C,EAAE;IACF,IAAI,IAAA,qBAAa,EAAC,OAAO,CAAC,EAAE;QAC1B,OAAO,OAAO,KAAK,sBAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;KAChD;IACD,OAAO,OAAO,CAAC,QAAQ,EAAE,KAAK,eAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC1D,CAAC,CAAC;AAPW,QAAA,eAAe,mBAO1B","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { SolScope } from '@metamask/keyring-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { CaipAssetType, CaipChainId } from '@metamask/utils';\nimport { isCaipChainId, isStrictHexString, type Hex } from '@metamask/utils';\n\nimport {\n formatChainIdToCaip,\n formatChainIdToDec,\n formatChainIdToHex,\n} from './caip-formatters';\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 {\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n SYMBOL_TO_SLIP44_MAP,\n type SupportedSwapsNativeCurrencySymbols,\n} from '../constants/tokens';\nimport type { BridgeAsset, BridgeControllerState } from '../types';\nimport { ChainId } from '../types';\n\nexport const getDefaultBridgeControllerState = (): BridgeControllerState => {\n return DEFAULT_BRIDGE_CONTROLLER_STATE;\n};\n\n/**\n * Returns the native assetType for a given chainId and native currency symbol\n * Note that the return value is used as the assetId although it is a CaipAssetType\n *\n * @param chainId - The chainId to get the native assetType for\n * @param nativeCurrencySymbol - The native currency symbol for the given chainId\n * @returns The native assetType for the given chainId\n */\nconst getNativeAssetCaipAssetType = (\n chainId: CaipChainId,\n nativeCurrencySymbol: SupportedSwapsNativeCurrencySymbols,\n): CaipAssetType => {\n return `${formatChainIdToCaip(chainId)}/${SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;\n};\n\n/**\n * Returns the native swaps or bridge asset for a given chainId\n *\n * @param chainId - The chainId to get the default token for\n * @returns The native asset for the given chainId\n * @throws If no native asset is defined for the given chainId\n */\nexport const getNativeAssetForChainId = (\n chainId: string | number | Hex | CaipChainId,\n): BridgeAsset => {\n const chainIdInCaip = formatChainIdToCaip(chainId);\n const nativeToken =\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToCaip(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ] ??\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToHex(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ];\n\n if (!nativeToken) {\n throw new Error(\n `No XChain Swaps native asset found for chainId: ${chainId}`,\n );\n }\n\n return {\n ...nativeToken,\n chainId: formatChainIdToDec(chainId),\n assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),\n };\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 */\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/**\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 */\nexport const isSwapsDefaultTokenAddress = (\n address: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!address || !chainId) {\n return false;\n }\n\n return address === getNativeAssetForChainId(chainId)?.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 */\nexport const isSwapsDefaultTokenSymbol = (\n symbol: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!symbol || !chainId) {\n return false;\n }\n\n return symbol === getNativeAssetForChainId(chainId)?.symbol;\n};\n\n/**\n * Checks whether the address is a native asset in any supported xchain swaps network\n *\n * @param address - The address to check\n * @returns Whether the address is a native asset\n */\nexport const isNativeAddress = (address?: string | null) =>\n address === AddressZero || // bridge and swap apis set the native asset address to zero\n address === '' || // assets controllers set the native asset address to an empty string\n !address ||\n address.endsWith('11111111111111111111111111111111') || // token-api and bridge-api use this as the solana native assetId\n [getNativeAssetForChainId(ChainId.SOLANA).assetId].some(\n (assetId) => assetId.includes(address) && !isStrictHexString(address),\n ); // solana native assetId used in the extension client\n\n/**\n * Checks whether the chainId matches Solana in CaipChainId or number format\n *\n * @param chainId - The chainId to check\n * @returns Whether the chainId is Solana\n */\nexport const isSolanaChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === SolScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.SOLANA.toString();\n};\n"]}
@@ -1,6 +1,15 @@
1
- import type { Hex } from "@metamask/utils";
2
- import type { BridgeControllerState } from "../types.cjs";
1
+ import type { CaipChainId } from "@metamask/utils";
2
+ import { type Hex } from "@metamask/utils";
3
+ import type { BridgeAsset, BridgeControllerState } from "../types.cjs";
3
4
  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;
4
13
  /**
5
14
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
6
15
  *
@@ -17,7 +26,7 @@ export declare const sumHexes: (...hexStrings: string[]) => Hex;
17
26
  * @param chainId - The hex encoded chain ID of the default swaps token to check
18
27
  * @returns Whether the address is the provided chain's default token address
19
28
  */
20
- export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex) => boolean;
29
+ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex | CaipChainId) => boolean;
21
30
  /**
22
31
  * Checks whether the provided symbol is strictly equal to the symbol for
23
32
  * the default swaps token of the provided chain.
@@ -26,5 +35,19 @@ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex)
26
35
  * @param chainId - The hex encoded chain ID of the default swaps token to check
27
36
  * @returns Whether the symbol is the provided chain's default token symbol
28
37
  */
29
- export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex) => boolean;
38
+ export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex | CaipChainId) => boolean;
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;
30
53
  //# sourceMappingURL=bridge.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bridge.d.cts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"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AAG,WAAW,YAO3B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,aAAc,MAAM,GAAG,IAAI,YAOpD,CAAC;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YACjB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC"}
@@ -1,6 +1,15 @@
1
- import type { Hex } from "@metamask/utils";
2
- import type { BridgeControllerState } from "../types.mjs";
1
+ import type { CaipChainId } from "@metamask/utils";
2
+ import { type Hex } from "@metamask/utils";
3
+ import type { BridgeAsset, BridgeControllerState } from "../types.mjs";
3
4
  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;
4
13
  /**
5
14
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
6
15
  *
@@ -17,7 +26,7 @@ export declare const sumHexes: (...hexStrings: string[]) => Hex;
17
26
  * @param chainId - The hex encoded chain ID of the default swaps token to check
18
27
  * @returns Whether the address is the provided chain's default token address
19
28
  */
20
- export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex) => boolean;
29
+ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex | CaipChainId) => boolean;
21
30
  /**
22
31
  * Checks whether the provided symbol is strictly equal to the symbol for
23
32
  * the default swaps token of the provided chain.
@@ -26,5 +35,19 @@ export declare const isSwapsDefaultTokenAddress: (address: string, chainId: Hex)
26
35
  * @param chainId - The hex encoded chain ID of the default swaps token to check
27
36
  * @returns Whether the symbol is the provided chain's default token symbol
28
37
  */
29
- export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex) => boolean;
38
+ export declare const isSwapsDefaultTokenSymbol: (symbol: string, chainId: Hex | CaipChainId) => boolean;
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;
30
53
  //# sourceMappingURL=bridge.d.mts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"bridge.d.mts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAiB,WAAW,EAAE,wBAAwB;AAClE,OAAO,EAAoC,KAAK,GAAG,EAAE,wBAAwB;AAkB7E,OAAO,KAAK,EAAE,WAAW,EAAE,qBAAqB,EAAE,qBAAiB;AAGnE,eAAO,MAAM,+BAA+B,QAAO,qBAElD,CAAC;AAiBF;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,YAC1B,MAAM,GAAG,MAAM,GAAG,GAAG,GAAG,WAAW,KAC3C,WAyBF,CAAC;AAEF;;;;GAIG;AACH,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;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,0BAA0B,YAC5B,MAAM,WACN,GAAG,GAAG,WAAW,YAO3B,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,WAC5B,MAAM,WACL,GAAG,GAAG,WAAW,YAO3B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,aAAc,MAAM,GAAG,IAAI,YAOpD,CAAC;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YACjB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC"}
@@ -1,11 +1,47 @@
1
+ import { AddressZero } from "@ethersproject/constants";
1
2
  import { Contract } from "@ethersproject/contracts";
3
+ import { SolScope } from "@metamask/keyring-api";
2
4
  import { abiERC20 } from "@metamask/metamask-eth-abis";
5
+ import { isCaipChainId, isStrictHexString } from "@metamask/utils";
6
+ import { formatChainIdToCaip, formatChainIdToDec, formatChainIdToHex } from "./caip-formatters.mjs";
3
7
  import { DEFAULT_BRIDGE_CONTROLLER_STATE, ETH_USDT_ADDRESS, METABRIDGE_ETHEREUM_ADDRESS } from "../constants/bridge.mjs";
4
8
  import { CHAIN_IDS } from "../constants/chains.mjs";
5
- import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from "../constants/tokens.mjs";
9
+ import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP, SYMBOL_TO_SLIP44_MAP } from "../constants/tokens.mjs";
10
+ import { ChainId } from "../types.mjs";
6
11
  export const getDefaultBridgeControllerState = () => {
7
12
  return DEFAULT_BRIDGE_CONTROLLER_STATE;
8
13
  };
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
+ };
9
45
  /**
10
46
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
11
47
  *
@@ -41,8 +77,7 @@ export const isSwapsDefaultTokenAddress = (address, chainId) => {
41
77
  if (!address || !chainId) {
42
78
  return false;
43
79
  }
44
- return (address ===
45
- SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.address);
80
+ return address === getNativeAssetForChainId(chainId)?.address;
46
81
  };
47
82
  /**
48
83
  * Checks whether the provided symbol is strictly equal to the symbol for
@@ -56,7 +91,29 @@ export const isSwapsDefaultTokenSymbol = (symbol, chainId) => {
56
91
  if (!symbol || !chainId) {
57
92
  return false;
58
93
  }
59
- return (symbol ===
60
- SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId]?.symbol);
94
+ return symbol === getNativeAssetForChainId(chainId)?.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();
61
118
  };
62
119
  //# sourceMappingURL=bridge.mjs.map
@@ -1 +1 @@
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,qB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K,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"]}
1
+ {"version":3,"file":"bridge.mjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AACjD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAEvD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAY,wBAAwB;AAE7E,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EACnB,8BAA0B;AAC3B,OAAO,EACL,+BAA+B,EAC/B,gBAAgB,EAChB,2BAA2B,EAC5B,gCAA4B;AAC7B,OAAO,EAAE,SAAS,EAAE,gCAA4B;AAChD,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EAErB,gCAA4B;AAE7B,OAAO,EAAE,OAAO,EAAE,qBAAiB;AAEnC,MAAM,CAAC,MAAM,+BAA+B,GAAG,GAA0B,EAAE;IACzE,OAAO,+BAA+B,CAAC;AACzC,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,2BAA2B,GAAG,CAClC,OAAoB,EACpB,oBAAyD,EAC1C,EAAE;IACjB,OAAO,GAAG,mBAAmB,CAAC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AE1D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,OAA4C,EAC5C,EAAE;IACF,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE;QAC1B,OAAO,OAAO,KAAK,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;KAChD;IACD,OAAO,OAAO,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC1D,CAAC,CAAC","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { SolScope } from '@metamask/keyring-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { CaipAssetType, CaipChainId } from '@metamask/utils';\nimport { isCaipChainId, isStrictHexString, type Hex } from '@metamask/utils';\n\nimport {\n formatChainIdToCaip,\n formatChainIdToDec,\n formatChainIdToHex,\n} from './caip-formatters';\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 {\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n SYMBOL_TO_SLIP44_MAP,\n type SupportedSwapsNativeCurrencySymbols,\n} from '../constants/tokens';\nimport type { BridgeAsset, BridgeControllerState } from '../types';\nimport { ChainId } from '../types';\n\nexport const getDefaultBridgeControllerState = (): BridgeControllerState => {\n return DEFAULT_BRIDGE_CONTROLLER_STATE;\n};\n\n/**\n * Returns the native assetType for a given chainId and native currency symbol\n * Note that the return value is used as the assetId although it is a CaipAssetType\n *\n * @param chainId - The chainId to get the native assetType for\n * @param nativeCurrencySymbol - The native currency symbol for the given chainId\n * @returns The native assetType for the given chainId\n */\nconst getNativeAssetCaipAssetType = (\n chainId: CaipChainId,\n nativeCurrencySymbol: SupportedSwapsNativeCurrencySymbols,\n): CaipAssetType => {\n return `${formatChainIdToCaip(chainId)}/${SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;\n};\n\n/**\n * Returns the native swaps or bridge asset for a given chainId\n *\n * @param chainId - The chainId to get the default token for\n * @returns The native asset for the given chainId\n * @throws If no native asset is defined for the given chainId\n */\nexport const getNativeAssetForChainId = (\n chainId: string | number | Hex | CaipChainId,\n): BridgeAsset => {\n const chainIdInCaip = formatChainIdToCaip(chainId);\n const nativeToken =\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToCaip(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ] ??\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToHex(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ];\n\n if (!nativeToken) {\n throw new Error(\n `No XChain Swaps native asset found for chainId: ${chainId}`,\n );\n }\n\n return {\n ...nativeToken,\n chainId: formatChainIdToDec(chainId),\n assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),\n };\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 */\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/**\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 */\nexport const isSwapsDefaultTokenAddress = (\n address: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!address || !chainId) {\n return false;\n }\n\n return address === getNativeAssetForChainId(chainId)?.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 */\nexport const isSwapsDefaultTokenSymbol = (\n symbol: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!symbol || !chainId) {\n return false;\n }\n\n return symbol === getNativeAssetForChainId(chainId)?.symbol;\n};\n\n/**\n * Checks whether the address is a native asset in any supported xchain swaps network\n *\n * @param address - The address to check\n * @returns Whether the address is a native asset\n */\nexport const isNativeAddress = (address?: string | null) =>\n address === AddressZero || // bridge and swap apis set the native asset address to zero\n address === '' || // assets controllers set the native asset address to an empty string\n !address ||\n address.endsWith('11111111111111111111111111111111') || // token-api and bridge-api use this as the solana native assetId\n [getNativeAssetForChainId(ChainId.SOLANA).assetId].some(\n (assetId) => assetId.includes(address) && !isStrictHexString(address),\n ); // solana native assetId used in the extension client\n\n/**\n * Checks whether the chainId matches Solana in CaipChainId or number format\n *\n * @param chainId - The chainId to check\n * @returns Whether the chainId is Solana\n */\nexport const isSolanaChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === SolScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.SOLANA.toString();\n};\n"]}
@@ -0,0 +1,102 @@
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
@@ -0,0 +1 @@
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,M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ersproject/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"]}
@@ -0,0 +1,31 @@
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
@@ -0,0 +1 @@
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"}