@metamask/bridge-controller 63.2.0 → 64.1.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 (110) hide show
  1. package/CHANGELOG.md +41 -1
  2. package/dist/bridge-controller.cjs +60 -34
  3. package/dist/bridge-controller.cjs.map +1 -1
  4. package/dist/bridge-controller.d.cts +2 -10
  5. package/dist/bridge-controller.d.cts.map +1 -1
  6. package/dist/bridge-controller.d.mts +2 -10
  7. package/dist/bridge-controller.d.mts.map +1 -1
  8. package/dist/bridge-controller.mjs +62 -36
  9. package/dist/bridge-controller.mjs.map +1 -1
  10. package/dist/constants/swaps.cjs +57 -58
  11. package/dist/constants/swaps.cjs.map +1 -1
  12. package/dist/constants/swaps.d.cts +0 -1
  13. package/dist/constants/swaps.d.cts.map +1 -1
  14. package/dist/constants/swaps.d.mts +0 -1
  15. package/dist/constants/swaps.d.mts.map +1 -1
  16. package/dist/constants/swaps.mjs +6 -7
  17. package/dist/constants/swaps.mjs.map +1 -1
  18. package/dist/constants/tokens.cjs +15 -16
  19. package/dist/constants/tokens.cjs.map +1 -1
  20. package/dist/constants/tokens.d.cts +3 -3
  21. package/dist/constants/tokens.d.cts.map +1 -1
  22. package/dist/constants/tokens.d.mts +3 -3
  23. package/dist/constants/tokens.d.mts.map +1 -1
  24. package/dist/constants/tokens.mjs +2 -3
  25. package/dist/constants/tokens.mjs.map +1 -1
  26. package/dist/index.cjs +4 -4
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +4 -4
  29. package/dist/index.d.cts.map +1 -1
  30. package/dist/index.d.mts +4 -4
  31. package/dist/index.d.mts.map +1 -1
  32. package/dist/index.mjs +4 -4
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/selectors.cjs +11 -2
  35. package/dist/selectors.cjs.map +1 -1
  36. package/dist/selectors.d.cts +900 -18
  37. package/dist/selectors.d.cts.map +1 -1
  38. package/dist/selectors.d.mts +900 -18
  39. package/dist/selectors.d.mts.map +1 -1
  40. package/dist/selectors.mjs +11 -2
  41. package/dist/selectors.mjs.map +1 -1
  42. package/dist/types.cjs +2 -1
  43. package/dist/types.cjs.map +1 -1
  44. package/dist/types.d.cts +12 -3
  45. package/dist/types.d.cts.map +1 -1
  46. package/dist/types.d.mts +12 -3
  47. package/dist/types.d.mts.map +1 -1
  48. package/dist/types.mjs +2 -1
  49. package/dist/types.mjs.map +1 -1
  50. package/dist/utils/assets.cjs.map +1 -1
  51. package/dist/utils/assets.d.cts.map +1 -1
  52. package/dist/utils/assets.d.mts.map +1 -1
  53. package/dist/utils/assets.mjs.map +1 -1
  54. package/dist/utils/bridge.cjs +28 -23
  55. package/dist/utils/bridge.cjs.map +1 -1
  56. package/dist/utils/bridge.d.cts +12 -12
  57. package/dist/utils/bridge.d.cts.map +1 -1
  58. package/dist/utils/bridge.d.mts +12 -12
  59. package/dist/utils/bridge.d.mts.map +1 -1
  60. package/dist/utils/bridge.mjs +26 -21
  61. package/dist/utils/bridge.mjs.map +1 -1
  62. package/dist/utils/caip-formatters.cjs.map +1 -1
  63. package/dist/utils/caip-formatters.d.cts +1 -2
  64. package/dist/utils/caip-formatters.d.cts.map +1 -1
  65. package/dist/utils/caip-formatters.d.mts +1 -2
  66. package/dist/utils/caip-formatters.d.mts.map +1 -1
  67. package/dist/utils/caip-formatters.mjs.map +1 -1
  68. package/dist/utils/fetch.cjs +22 -1
  69. package/dist/utils/fetch.cjs.map +1 -1
  70. package/dist/utils/fetch.d.cts.map +1 -1
  71. package/dist/utils/fetch.d.mts.map +1 -1
  72. package/dist/utils/fetch.mjs +22 -1
  73. package/dist/utils/fetch.mjs.map +1 -1
  74. package/dist/utils/metrics/properties.cjs.map +1 -1
  75. package/dist/utils/metrics/properties.d.cts +1 -2
  76. package/dist/utils/metrics/properties.d.cts.map +1 -1
  77. package/dist/utils/metrics/properties.d.mts +1 -2
  78. package/dist/utils/metrics/properties.d.mts.map +1 -1
  79. package/dist/utils/metrics/properties.mjs.map +1 -1
  80. package/dist/utils/metrics/types.cjs.map +1 -1
  81. package/dist/utils/metrics/types.d.cts +1 -1
  82. package/dist/utils/metrics/types.d.cts.map +1 -1
  83. package/dist/utils/metrics/types.d.mts +1 -1
  84. package/dist/utils/metrics/types.d.mts.map +1 -1
  85. package/dist/utils/metrics/types.mjs.map +1 -1
  86. package/dist/utils/quote-fees.cjs +1 -4
  87. package/dist/utils/quote-fees.cjs.map +1 -1
  88. package/dist/utils/quote-fees.d.cts +1 -1
  89. package/dist/utils/quote-fees.d.cts.map +1 -1
  90. package/dist/utils/quote-fees.d.mts +1 -1
  91. package/dist/utils/quote-fees.d.mts.map +1 -1
  92. package/dist/utils/quote-fees.mjs +1 -4
  93. package/dist/utils/quote-fees.mjs.map +1 -1
  94. package/dist/utils/quote.cjs +13 -12
  95. package/dist/utils/quote.cjs.map +1 -1
  96. package/dist/utils/quote.d.cts +4 -5
  97. package/dist/utils/quote.d.cts.map +1 -1
  98. package/dist/utils/quote.d.mts +4 -5
  99. package/dist/utils/quote.d.mts.map +1 -1
  100. package/dist/utils/quote.mjs +13 -12
  101. package/dist/utils/quote.mjs.map +1 -1
  102. package/dist/utils/swaps.cjs +45 -1
  103. package/dist/utils/swaps.cjs.map +1 -1
  104. package/dist/utils/swaps.d.cts +20 -0
  105. package/dist/utils/swaps.d.cts.map +1 -1
  106. package/dist/utils/swaps.d.mts +20 -0
  107. package/dist/utils/swaps.d.mts.map +1 -1
  108. package/dist/utils/swaps.mjs +43 -0
  109. package/dist/utils/swaps.mjs.map +1 -1
  110. package/package.json +5 -5
@@ -1 +1 @@
1
- {"version":3,"file":"assets.cjs","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":";;;AAEA,yCAAoD;AACpD,2DAA2D;AAGpD,MAAM,mBAAmB,GAAG,CACjC,YAAoD,EACpD,OAA0C,EAC1C,EAAE;IACF,MAAM,eAAe,GAAoB,EAAE,CAAC;IAE5C,MAAM,OAAO,GAAG,IAAA,wCAAsB,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAC9D,IAAI,OAAO,EAAE,CAAC;QACZ,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,IAAA,iCAAwB,EAAC,OAAO,CAAC,EAAE,OAAO;YACxC,eAAe,CAAC,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;IACpE,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC;AAdW,QAAA,mBAAmB,uBAc9B;AAEK,MAAM,eAAe,GAAG,CAC7B,QAAgB,EAChB,eAEC,EACD,EAAE;IACF,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,MAAM,CAC1D,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,EAAE;QACzB,IAAI,MAAM,EAAE,CAAC;YACX,GAAG,CAAC,OAAwB,CAAC,GAAG;gBAC9B,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;gBAC9B,eAAe,EAAE,MAAM,CAAC,GAAG;aAC5B,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAyC,CAC1C,CAAC;IACF,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC;AAnBW,QAAA,eAAe,mBAmB1B","sourcesContent":["import type { CaipAssetType } from '@metamask/utils';\n\nimport { getNativeAssetForChainId } from './bridge';\nimport { formatAddressToAssetId } from './caip-formatters';\nimport type { ExchangeRate, GenericQuoteRequest } from '../types';\n\nexport const getAssetIdsForToken = (\n tokenAddress: GenericQuoteRequest['srcTokenAddress'],\n chainId: GenericQuoteRequest['srcChainId'],\n) => {\n const assetIdsToFetch: CaipAssetType[] = [];\n\n const assetId = formatAddressToAssetId(tokenAddress, chainId);\n if (assetId) {\n assetIdsToFetch.push(assetId);\n getNativeAssetForChainId(chainId)?.assetId &&\n assetIdsToFetch.push(getNativeAssetForChainId(chainId).assetId);\n }\n\n return assetIdsToFetch;\n};\n\nexport const toExchangeRates = (\n currency: string,\n pricesByAssetId: {\n [assetId: CaipAssetType]: { [currency: string]: string } | undefined;\n },\n) => {\n const exchangeRates = Object.entries(pricesByAssetId).reduce(\n (acc, [assetId, prices]) => {\n if (prices) {\n acc[assetId as CaipAssetType] = {\n exchangeRate: prices[currency],\n usdExchangeRate: prices.usd,\n };\n }\n return acc;\n },\n {} as Record<CaipAssetType, ExchangeRate>,\n );\n return exchangeRates;\n};\n"]}
1
+ {"version":3,"file":"assets.cjs","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":";;;AAEA,yCAAoD;AACpD,2DAA2D;AAGpD,MAAM,mBAAmB,GAAG,CACjC,YAAoD,EACpD,OAA0C,EAC1C,EAAE;IACF,MAAM,eAAe,GAAoB,EAAE,CAAC;IAE5C,MAAM,OAAO,GAAG,IAAA,wCAAsB,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAC9D,IAAI,OAAO,EAAE,CAAC;QACZ,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,IAAA,iCAAwB,EAAC,OAAO,CAAC,EAAE,OAAO;YACxC,eAAe,CAAC,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;IACpE,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC;AAdW,QAAA,mBAAmB,uBAc9B;AAEK,MAAM,eAAe,GAAG,CAC7B,QAAgB,EAChB,eAEC,EACD,EAAE;IACF,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,MAAM,CAE1D,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,EAAE;QAC3B,IAAI,MAAM,EAAE,CAAC;YACX,GAAG,CAAC,OAAwB,CAAC,GAAG;gBAC9B,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;gBAC9B,eAAe,EAAE,MAAM,CAAC,GAAG;aAC5B,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACP,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC;AAlBW,QAAA,eAAe,mBAkB1B","sourcesContent":["import type { CaipAssetType } from '@metamask/utils';\n\nimport { getNativeAssetForChainId } from './bridge';\nimport { formatAddressToAssetId } from './caip-formatters';\nimport type { ExchangeRate, GenericQuoteRequest } from '../types';\n\nexport const getAssetIdsForToken = (\n tokenAddress: GenericQuoteRequest['srcTokenAddress'],\n chainId: GenericQuoteRequest['srcChainId'],\n) => {\n const assetIdsToFetch: CaipAssetType[] = [];\n\n const assetId = formatAddressToAssetId(tokenAddress, chainId);\n if (assetId) {\n assetIdsToFetch.push(assetId);\n getNativeAssetForChainId(chainId)?.assetId &&\n assetIdsToFetch.push(getNativeAssetForChainId(chainId).assetId);\n }\n\n return assetIdsToFetch;\n};\n\nexport const toExchangeRates = (\n currency: string,\n pricesByAssetId: {\n [assetId: CaipAssetType]: { [currency: string]: string } | undefined;\n },\n) => {\n const exchangeRates = Object.entries(pricesByAssetId).reduce<\n Record<CaipAssetType, ExchangeRate>\n >((acc, [assetId, prices]) => {\n if (prices) {\n acc[assetId as CaipAssetType] = {\n exchangeRate: prices[currency],\n usdExchangeRate: prices.usd,\n };\n }\n return acc;\n }, {});\n return exchangeRates;\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"assets.d.cts","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAIrD,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,qBAAiB;AAElE,eAAO,MAAM,mBAAmB,iBAChB,mBAAmB,CAAC,iBAAiB,CAAC,WAC3C,mBAAmB,CAAC,YAAY,CAAC,gDAY3C,CAAC;AAEF,eAAO,MAAM,eAAe,aAChB,MAAM;;;;qEAkBjB,CAAC"}
1
+ {"version":3,"file":"assets.d.cts","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAIrD,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,qBAAiB;AAElE,eAAO,MAAM,mBAAmB,iBAChB,mBAAmB,CAAC,iBAAiB,CAAC,WAC3C,mBAAmB,CAAC,YAAY,CAAC,gDAY3C,CAAC;AAEF,eAAO,MAAM,eAAe,aAChB,MAAM;;;;qEAiBjB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"assets.d.mts","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAIrD,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,qBAAiB;AAElE,eAAO,MAAM,mBAAmB,iBAChB,mBAAmB,CAAC,iBAAiB,CAAC,WAC3C,mBAAmB,CAAC,YAAY,CAAC,gDAY3C,CAAC;AAEF,eAAO,MAAM,eAAe,aAChB,MAAM;;;;qEAkBjB,CAAC"}
1
+ {"version":3,"file":"assets.d.mts","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAIrD,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,qBAAiB;AAElE,eAAO,MAAM,mBAAmB,iBAChB,mBAAmB,CAAC,iBAAiB,CAAC,WAC3C,mBAAmB,CAAC,YAAY,CAAC,gDAY3C,CAAC;AAEF,eAAO,MAAM,eAAe,aAChB,MAAM;;;;qEAiBjB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"assets.mjs","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,wBAAwB,EAAE,qBAAiB;AACpD,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAG3D,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,YAAoD,EACpD,OAA0C,EAC1C,EAAE;IACF,MAAM,eAAe,GAAoB,EAAE,CAAC;IAE5C,MAAM,OAAO,GAAG,sBAAsB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAC9D,IAAI,OAAO,EAAE,CAAC;QACZ,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,wBAAwB,CAAC,OAAO,CAAC,EAAE,OAAO;YACxC,eAAe,CAAC,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;IACpE,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,QAAgB,EAChB,eAEC,EACD,EAAE;IACF,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,MAAM,CAC1D,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,EAAE;QACzB,IAAI,MAAM,EAAE,CAAC;YACX,GAAG,CAAC,OAAwB,CAAC,GAAG;gBAC9B,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;gBAC9B,eAAe,EAAE,MAAM,CAAC,GAAG;aAC5B,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAyC,CAC1C,CAAC;IACF,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC","sourcesContent":["import type { CaipAssetType } from '@metamask/utils';\n\nimport { getNativeAssetForChainId } from './bridge';\nimport { formatAddressToAssetId } from './caip-formatters';\nimport type { ExchangeRate, GenericQuoteRequest } from '../types';\n\nexport const getAssetIdsForToken = (\n tokenAddress: GenericQuoteRequest['srcTokenAddress'],\n chainId: GenericQuoteRequest['srcChainId'],\n) => {\n const assetIdsToFetch: CaipAssetType[] = [];\n\n const assetId = formatAddressToAssetId(tokenAddress, chainId);\n if (assetId) {\n assetIdsToFetch.push(assetId);\n getNativeAssetForChainId(chainId)?.assetId &&\n assetIdsToFetch.push(getNativeAssetForChainId(chainId).assetId);\n }\n\n return assetIdsToFetch;\n};\n\nexport const toExchangeRates = (\n currency: string,\n pricesByAssetId: {\n [assetId: CaipAssetType]: { [currency: string]: string } | undefined;\n },\n) => {\n const exchangeRates = Object.entries(pricesByAssetId).reduce(\n (acc, [assetId, prices]) => {\n if (prices) {\n acc[assetId as CaipAssetType] = {\n exchangeRate: prices[currency],\n usdExchangeRate: prices.usd,\n };\n }\n return acc;\n },\n {} as Record<CaipAssetType, ExchangeRate>,\n );\n return exchangeRates;\n};\n"]}
1
+ {"version":3,"file":"assets.mjs","sourceRoot":"","sources":["../../src/utils/assets.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,wBAAwB,EAAE,qBAAiB;AACpD,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAG3D,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,YAAoD,EACpD,OAA0C,EAC1C,EAAE;IACF,MAAM,eAAe,GAAoB,EAAE,CAAC;IAE5C,MAAM,OAAO,GAAG,sBAAsB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAC9D,IAAI,OAAO,EAAE,CAAC;QACZ,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,wBAAwB,CAAC,OAAO,CAAC,EAAE,OAAO;YACxC,eAAe,CAAC,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;IACpE,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,QAAgB,EAChB,eAEC,EACD,EAAE;IACF,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,MAAM,CAE1D,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,EAAE;QAC3B,IAAI,MAAM,EAAE,CAAC;YACX,GAAG,CAAC,OAAwB,CAAC,GAAG;gBAC9B,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;gBAC9B,eAAe,EAAE,MAAM,CAAC,GAAG;aAC5B,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACP,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC","sourcesContent":["import type { CaipAssetType } from '@metamask/utils';\n\nimport { getNativeAssetForChainId } from './bridge';\nimport { formatAddressToAssetId } from './caip-formatters';\nimport type { ExchangeRate, GenericQuoteRequest } from '../types';\n\nexport const getAssetIdsForToken = (\n tokenAddress: GenericQuoteRequest['srcTokenAddress'],\n chainId: GenericQuoteRequest['srcChainId'],\n) => {\n const assetIdsToFetch: CaipAssetType[] = [];\n\n const assetId = formatAddressToAssetId(tokenAddress, chainId);\n if (assetId) {\n assetIdsToFetch.push(assetId);\n getNativeAssetForChainId(chainId)?.assetId &&\n assetIdsToFetch.push(getNativeAssetForChainId(chainId).assetId);\n }\n\n return assetIdsToFetch;\n};\n\nexport const toExchangeRates = (\n currency: string,\n pricesByAssetId: {\n [assetId: CaipAssetType]: { [currency: string]: string } | undefined;\n },\n) => {\n const exchangeRates = Object.entries(pricesByAssetId).reduce<\n Record<CaipAssetType, ExchangeRate>\n >((acc, [assetId, prices]) => {\n if (prices) {\n acc[assetId as CaipAssetType] = {\n exchangeRate: prices[currency],\n usdExchangeRate: prices.usd,\n };\n }\n return acc;\n }, {});\n return exchangeRates;\n};\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isCrossChain = exports.isEvmQuoteResponse = exports.isNonEvmChainId = exports.isTronChainId = exports.isBitcoinChainId = exports.isSolanaChainId = exports.isNativeAddress = exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getNativeAssetForChainId = exports.getDefaultBridgeControllerState = void 0;
3
+ exports.isEvmQuoteResponse = exports.isNonEvmChainId = exports.isTronChainId = exports.isBitcoinChainId = exports.isSolanaChainId = exports.isNativeAddress = exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getNativeAssetForChainId = exports.getDefaultBridgeControllerState = exports.isCrossChain = void 0;
4
4
  const constants_1 = require("@ethersproject/constants");
5
5
  const contracts_1 = require("@ethersproject/contracts");
6
6
  const keyring_api_1 = require("@metamask/keyring-api");
@@ -9,8 +9,28 @@ const utils_1 = require("@metamask/utils");
9
9
  const caip_formatters_1 = require("./caip-formatters.cjs");
10
10
  const bridge_1 = require("../constants/bridge.cjs");
11
11
  const chains_1 = require("../constants/chains.cjs");
12
+ const swaps_1 = require("../constants/swaps.cjs");
12
13
  const tokens_1 = require("../constants/tokens.cjs");
13
14
  const types_1 = require("../types.cjs");
15
+ /**
16
+ * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
17
+ *
18
+ * @param srcChainId - The source chainId
19
+ * @param destChainId - The destination chainId
20
+ * @returns Whether the transaction is a cross-chain transaction
21
+ */
22
+ const isCrossChain = (srcChainId, destChainId) => {
23
+ try {
24
+ if (!destChainId) {
25
+ return false;
26
+ }
27
+ return (0, caip_formatters_1.formatChainIdToCaip)(srcChainId) !== (0, caip_formatters_1.formatChainIdToCaip)(destChainId);
28
+ }
29
+ catch {
30
+ return false;
31
+ }
32
+ };
33
+ exports.isCrossChain = isCrossChain;
14
34
  const getDefaultBridgeControllerState = () => {
15
35
  return bridge_1.DEFAULT_BRIDGE_CONTROLLER_STATE;
16
36
  };
@@ -50,19 +70,23 @@ exports.getNativeAssetForChainId = getNativeAssetForChainId;
50
70
  /**
51
71
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
52
72
  *
73
+ * @param destChainId - The destination chain ID
53
74
  * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API
54
75
  */
55
- const getEthUsdtResetData = () => {
76
+ const getEthUsdtResetData = (destChainId) => {
77
+ const spenderAddress = (0, exports.isCrossChain)(chains_1.CHAIN_IDS.MAINNET, destChainId)
78
+ ? bridge_1.METABRIDGE_ETHEREUM_ADDRESS
79
+ : swaps_1.SWAPS_CONTRACT_ADDRESSES[chains_1.CHAIN_IDS.MAINNET];
56
80
  const UsdtContractInterface = new contracts_1.Contract(bridge_1.ETH_USDT_ADDRESS, metamask_eth_abis_1.abiERC20)
57
81
  .interface;
58
82
  const data = UsdtContractInterface.encodeFunctionData('approve', [
59
- bridge_1.METABRIDGE_ETHEREUM_ADDRESS,
83
+ spenderAddress,
60
84
  '0',
61
85
  ]);
62
86
  return data;
63
87
  };
64
88
  exports.getEthUsdtResetData = getEthUsdtResetData;
65
- const isEthUsdt = (chainId, address) => chainId === chains_1.CHAIN_IDS.MAINNET &&
89
+ const isEthUsdt = (chainId, address) => (0, caip_formatters_1.formatChainIdToDec)(chainId) === types_1.ChainId.ETH &&
66
90
  address.toLowerCase() === bridge_1.ETH_USDT_ADDRESS.toLowerCase();
67
91
  exports.isEthUsdt = isEthUsdt;
68
92
  const sumHexes = (...hexStrings) => {
@@ -161,23 +185,4 @@ const isEvmQuoteResponse = (quoteResponse) => {
161
185
  return !(0, exports.isNonEvmChainId)(quoteResponse.quote.srcChainId);
162
186
  };
163
187
  exports.isEvmQuoteResponse = isEvmQuoteResponse;
164
- /**
165
- * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
166
- *
167
- * @param srcChainId - The source chainId
168
- * @param destChainId - The destination chainId
169
- * @returns Whether the transaction is a cross-chain transaction
170
- */
171
- const isCrossChain = (srcChainId, destChainId) => {
172
- try {
173
- if (!destChainId) {
174
- return false;
175
- }
176
- return (0, caip_formatters_1.formatChainIdToCaip)(srcChainId) !== (0, caip_formatters_1.formatChainIdToCaip)(destChainId);
177
- }
178
- catch {
179
- return false;
180
- }
181
- };
182
- exports.isCrossChain = isCrossChain;
183
188
  //# sourceMappingURL=bridge.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,wDAAoD;AACpD,uDAAqE;AACrE,mEAAuD;AAEvD,2CAA6E;AAE7E,2DAI2B;AAC3B,oDAI6B;AAC7B,oDAAgD;AAChD,oDAI6B;AAQ7B,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,qCAAmB,EAAC,OAAO,CAAC,CAAC;IACnD,MAAM,WAAW,GACf,wCAA+B,CAC7B,IAAA,qCAAmB,EACjB,OAAO,CACwC,CAClD;QACD,wCAA+B,CAC7B,IAAA,oCAAkB,EAChB,OAAO,CACwC,CAClD,CAAC;IAEJ,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CACb,mDAAmD,OAAO,EAAE,CAC7D,CAAC;IACJ,CAAC;IAED,OAAO;QACL,GAAG,WAAW;QACd,OAAO,EAAE,IAAA,oCAAkB,EAAC,OAAO,CAAC;QACpC,OAAO,EAAE,2BAA2B,CAAC,aAAa,EAAE,WAAW,CAAC,MAAM,CAAC;KACxE,CAAC;AACJ,CAAC,CAAC;AA3BW,QAAA,wBAAwB,4BA2BnC;AAEF;;;;GAIG;AACI,MAAM,mBAAmB,GAAG,GA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esContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope, TrxScope } 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 {\n BridgeAsset,\n BridgeControllerState,\n GenericQuoteRequest,\n QuoteResponse,\n TxData,\n} 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 (!isStrictHexString(address) &&\n Object.values(SYMBOL_TO_SLIP44_MAP).some(\n // check if it matches any supported SLIP44 references\n (reference) => address.includes(reference) || reference.endsWith(address),\n ));\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\nexport const isBitcoinChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === BtcScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.BTC.toString();\n};\n\nexport const isTronChainId = (chainId: Hex | number | CaipChainId | string) => {\n if (isCaipChainId(chainId)) {\n return chainId === TrxScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.TRON.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana, Bitcoin and Tron\n *\n * @param chainId - The chain ID to check\n * @returns True if the chain is a supported non-EVM chain, false otherwise\n */\nexport const isNonEvmChainId = (\n chainId: GenericQuoteRequest['srcChainId'],\n): boolean => {\n return (\n isSolanaChainId(chainId) ||\n isBitcoinChainId(chainId) ||\n isTronChainId(chainId)\n );\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData, TxData> => {\n return !isNonEvmChainId(quoteResponse.quote.srcChainId);\n};\n\n/**\n * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds\n *\n * @param srcChainId - The source chainId\n * @param destChainId - The destination chainId\n * @returns Whether the transaction is a cross-chain transaction\n */\nexport const isCrossChain = (\n srcChainId: GenericQuoteRequest['srcChainId'],\n destChainId?: GenericQuoteRequest['destChainId'],\n) => {\n try {\n if (!destChainId) {\n return false;\n }\n return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);\n } catch {\n return false;\n }\n};\n"]}
1
+ {"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,wDAAoD;AACpD,uDAAqE;AACrE,mEAAuD;AACvD,2CAAmE;AAGnE,2DAI2B;AAC3B,oDAI6B;AAC7B,oDAAgD;AAChD,kDAA8D;AAC9D,oDAG6B;AAS7B,wCAAmC;AAEnC;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAC1B,UAA6C,EAC7C,WAAgD,EAChD,EAAE;IACF,IAAI,CAAC;QACH,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,IAAA,qCAAmB,EAAC,UAAU,CAAC,KAAK,IAAA,qCAAmB,EAAC,WAAW,CAAC,CAAC;IAC9E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAZW,QAAA,YAAY,gBAYvB;AAEK,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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,sBAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjD,CAAC;IACD,OAAO,OAAO,CAAC,QAAQ,EAAE,KAAK,eAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;AACxD,CAAC,CAAC;AALW,QAAA,aAAa,iBAKxB;AAEF;;;;;;GAMG;AACI,MAAM,eAAe,GAAG,CAC7B,OAA0C,EACjC,EAAE;IACX,OAAO,CACL,IAAA,uBAAe,EAAC,OAAO,CAAC;QACxB,IAAA,wBAAgB,EAAC,OAAO,CAAC;QACzB,IAAA,qBAAa,EAAC,OAAO,CAAC,CACvB,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,eAAe,mBAQ1B;AAEK,MAAM,kBAAkB,GAAG,CAChC,aAA4B,EACoB,EAAE;IAClD,OAAO,CAAC,IAAA,uBAAe,EAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC,CAAC;AAJW,QAAA,kBAAkB,sBAI7B","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { isCaipChainId, isStrictHexString } from '@metamask/utils';\nimport type { CaipAssetType, CaipChainId, 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 { SWAPS_CONTRACT_ADDRESSES } from '../constants/swaps';\nimport {\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n SYMBOL_TO_SLIP44_MAP,\n} from '../constants/tokens';\nimport type { SupportedSwapsNativeCurrencySymbols } from '../constants/tokens';\nimport type {\n BridgeAsset,\n BridgeControllerState,\n GenericQuoteRequest,\n QuoteResponse,\n TxData,\n} from '../types';\nimport { ChainId } from '../types';\n\n/**\n * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds\n *\n * @param srcChainId - The source chainId\n * @param destChainId - The destination chainId\n * @returns Whether the transaction is a cross-chain transaction\n */\nexport const isCrossChain = (\n srcChainId: GenericQuoteRequest['srcChainId'],\n destChainId?: GenericQuoteRequest['destChainId'],\n) => {\n try {\n if (!destChainId) {\n return false;\n }\n return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);\n } catch {\n return false;\n }\n};\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 * @param destChainId - The destination chain ID\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 destChainId: GenericQuoteRequest['destChainId'],\n) => {\n const spenderAddress = isCrossChain(CHAIN_IDS.MAINNET, destChainId)\n ? METABRIDGE_ETHEREUM_ADDRESS\n : SWAPS_CONTRACT_ADDRESSES[CHAIN_IDS.MAINNET];\n const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n .interface;\n const data = UsdtContractInterface.encodeFunctionData('approve', [\n spenderAddress,\n '0',\n ]);\n\n return data;\n};\n\nexport const isEthUsdt = (\n chainId: GenericQuoteRequest['srcChainId'],\n address: string,\n) =>\n formatChainIdToDec(chainId) === ChainId.ETH &&\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 (!isStrictHexString(address) &&\n Object.values(SYMBOL_TO_SLIP44_MAP).some(\n // check if it matches any supported SLIP44 references\n (reference) => address.includes(reference) || reference.endsWith(address),\n ));\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\nexport const isBitcoinChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === BtcScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.BTC.toString();\n};\n\nexport const isTronChainId = (chainId: Hex | number | CaipChainId | string) => {\n if (isCaipChainId(chainId)) {\n return chainId === TrxScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.TRON.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana, Bitcoin and Tron\n *\n * @param chainId - The chain ID to check\n * @returns True if the chain is a supported non-EVM chain, false otherwise\n */\nexport const isNonEvmChainId = (\n chainId: GenericQuoteRequest['srcChainId'],\n): boolean => {\n return (\n isSolanaChainId(chainId) ||\n isBitcoinChainId(chainId) ||\n isTronChainId(chainId)\n );\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData, TxData> => {\n return !isNonEvmChainId(quoteResponse.quote.srcChainId);\n};\n"]}
@@ -1,6 +1,13 @@
1
- import type { CaipChainId } from "@metamask/utils";
2
- import { type Hex } from "@metamask/utils";
1
+ import type { CaipChainId, Hex } from "@metamask/utils";
3
2
  import type { BridgeAsset, BridgeControllerState, GenericQuoteRequest, QuoteResponse } from "../types.cjs";
3
+ /**
4
+ * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
5
+ *
6
+ * @param srcChainId - The source chainId
7
+ * @param destChainId - The destination chainId
8
+ * @returns Whether the transaction is a cross-chain transaction
9
+ */
10
+ export declare const isCrossChain: (srcChainId: GenericQuoteRequest['srcChainId'], destChainId?: GenericQuoteRequest['destChainId']) => boolean;
4
11
  export declare const getDefaultBridgeControllerState: () => BridgeControllerState;
5
12
  /**
6
13
  * Returns the native swaps or bridge asset for a given chainId
@@ -13,10 +20,11 @@ export declare const getNativeAssetForChainId: (chainId: string | number | Hex |
13
20
  /**
14
21
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
15
22
  *
23
+ * @param destChainId - The destination chain ID
16
24
  * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API
17
25
  */
18
- export declare const getEthUsdtResetData: () => string;
19
- export declare const isEthUsdt: (chainId: Hex, address: string) => boolean;
26
+ export declare const getEthUsdtResetData: (destChainId: GenericQuoteRequest['destChainId']) => string;
27
+ export declare const isEthUsdt: (chainId: GenericQuoteRequest['srcChainId'], address: string) => boolean;
20
28
  export declare const sumHexes: (...hexStrings: string[]) => Hex;
21
29
  /**
22
30
  * Checks whether the provided address is strictly equal to the address for
@@ -77,12 +85,4 @@ export declare const isEvmQuoteResponse: (quoteResponse: QuoteResponse) => quote
77
85
  gasLimit: number | null;
78
86
  effectiveGas?: number | undefined;
79
87
  }>;
80
- /**
81
- * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
82
- *
83
- * @param srcChainId - The source chainId
84
- * @param destChainId - The destination chainId
85
- * @returns Whether the transaction is a cross-chain transaction
86
- */
87
- export declare const isCrossChain: (srcChainId: GenericQuoteRequest['srcChainId'], destChainId?: GenericQuoteRequest['destChainId']) => boolean;
88
88
  //# sourceMappingURL=bridge.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bridge.d.cts","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,EACV,WAAW,EACX,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EAEd,qBAAiB;AAGlB,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,YAQjD,CAAC;AAEP;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YACjB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF,eAAO,MAAM,gBAAgB,YAClB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAE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AAyB,WAC5B,MAAM,WACL,GAAG,GAAG,WAAW,YAO3B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,aAAc,MAAM,GAAG,IAAI,YAQjD,CAAC;AAEP;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YACjB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF,eAAO,MAAM,gBAAgB,YAClB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF,eAAO,MAAM,aAAa,YAAa,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAKzE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,YACjB,mBAAmB,CAAC,YAAY,CAAC,KACzC,OAMF,CAAC;AAEF,eAAO,MAAM,kBAAkB,kBACd,aAAa;;;;;;;;;;;;;;;;EAG7B,CAAC"}
@@ -1,6 +1,13 @@
1
- import type { CaipChainId } from "@metamask/utils";
2
- import { type Hex } from "@metamask/utils";
1
+ import type { CaipChainId, Hex } from "@metamask/utils";
3
2
  import type { BridgeAsset, BridgeControllerState, GenericQuoteRequest, QuoteResponse } from "../types.mjs";
3
+ /**
4
+ * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
5
+ *
6
+ * @param srcChainId - The source chainId
7
+ * @param destChainId - The destination chainId
8
+ * @returns Whether the transaction is a cross-chain transaction
9
+ */
10
+ export declare const isCrossChain: (srcChainId: GenericQuoteRequest['srcChainId'], destChainId?: GenericQuoteRequest['destChainId']) => boolean;
4
11
  export declare const getDefaultBridgeControllerState: () => BridgeControllerState;
5
12
  /**
6
13
  * Returns the native swaps or bridge asset for a given chainId
@@ -13,10 +20,11 @@ export declare const getNativeAssetForChainId: (chainId: string | number | Hex |
13
20
  /**
14
21
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
15
22
  *
23
+ * @param destChainId - The destination chain ID
16
24
  * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API
17
25
  */
18
- export declare const getEthUsdtResetData: () => string;
19
- export declare const isEthUsdt: (chainId: Hex, address: string) => boolean;
26
+ export declare const getEthUsdtResetData: (destChainId: GenericQuoteRequest['destChainId']) => string;
27
+ export declare const isEthUsdt: (chainId: GenericQuoteRequest['srcChainId'], address: string) => boolean;
20
28
  export declare const sumHexes: (...hexStrings: string[]) => Hex;
21
29
  /**
22
30
  * Checks whether the provided address is strictly equal to the address for
@@ -77,12 +85,4 @@ export declare const isEvmQuoteResponse: (quoteResponse: QuoteResponse) => quote
77
85
  gasLimit: number | null;
78
86
  effectiveGas?: number | undefined;
79
87
  }>;
80
- /**
81
- * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
82
- *
83
- * @param srcChainId - The source chainId
84
- * @param destChainId - The destination chainId
85
- * @returns Whether the transaction is a cross-chain transaction
86
- */
87
- export declare const isCrossChain: (srcChainId: GenericQuoteRequest['srcChainId'], destChainId?: GenericQuoteRequest['destChainId']) => boolean;
88
88
  //# sourceMappingURL=bridge.d.mts.map
@@ -1 +1 @@
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,EACV,WAAW,EACX,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EAEd,qBAAiB;AAGlB,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,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,CAAC;AAiBF;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,YAC1B,MAAM,GAAG,MAAM,GAAG,GAAG,GAAG,WAAW,KAC3C,WAyBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,gBACjB,mBAAmB,CAAC,aAAa,CAAC,WAahD,CAAC;AAEF,eAAO,MAAM,SAAS,YACX,mBAAmB,CAAC,YAAY,CAAC,WACjC,MAAM,YAGyC,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,YAQjD,CAAC;AAEP;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YACjB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF,eAAO,MAAM,gBAAgB,YAClB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF,eAAO,MAAM,aAAa,YAAa,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAKzE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,YACjB,mBAAmB,CAAC,YAAY,CAAC,KACzC,OAMF,CAAC;AAEF,eAAO,MAAM,kBAAkB,kBACd,aAAa;;;;;;;;;;;;;;;;EAG7B,CAAC"}
@@ -6,8 +6,27 @@ import { isCaipChainId, isStrictHexString } from "@metamask/utils";
6
6
  import { formatChainIdToCaip, formatChainIdToDec, formatChainIdToHex } from "./caip-formatters.mjs";
7
7
  import { DEFAULT_BRIDGE_CONTROLLER_STATE, ETH_USDT_ADDRESS, METABRIDGE_ETHEREUM_ADDRESS } from "../constants/bridge.mjs";
8
8
  import { CHAIN_IDS } from "../constants/chains.mjs";
9
+ import { SWAPS_CONTRACT_ADDRESSES } from "../constants/swaps.mjs";
9
10
  import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP, SYMBOL_TO_SLIP44_MAP } from "../constants/tokens.mjs";
10
11
  import { ChainId } from "../types.mjs";
12
+ /**
13
+ * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
14
+ *
15
+ * @param srcChainId - The source chainId
16
+ * @param destChainId - The destination chainId
17
+ * @returns Whether the transaction is a cross-chain transaction
18
+ */
19
+ export const isCrossChain = (srcChainId, destChainId) => {
20
+ try {
21
+ if (!destChainId) {
22
+ return false;
23
+ }
24
+ return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ };
11
30
  export const getDefaultBridgeControllerState = () => {
12
31
  return DEFAULT_BRIDGE_CONTROLLER_STATE;
13
32
  };
@@ -45,18 +64,22 @@ export const getNativeAssetForChainId = (chainId) => {
45
64
  /**
46
65
  * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum
47
66
  *
67
+ * @param destChainId - The destination chain ID
48
68
  * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API
49
69
  */
50
- export const getEthUsdtResetData = () => {
70
+ export const getEthUsdtResetData = (destChainId) => {
71
+ const spenderAddress = isCrossChain(CHAIN_IDS.MAINNET, destChainId)
72
+ ? METABRIDGE_ETHEREUM_ADDRESS
73
+ : SWAPS_CONTRACT_ADDRESSES[CHAIN_IDS.MAINNET];
51
74
  const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)
52
75
  .interface;
53
76
  const data = UsdtContractInterface.encodeFunctionData('approve', [
54
- METABRIDGE_ETHEREUM_ADDRESS,
77
+ spenderAddress,
55
78
  '0',
56
79
  ]);
57
80
  return data;
58
81
  };
59
- export const isEthUsdt = (chainId, address) => chainId === CHAIN_IDS.MAINNET &&
82
+ export const isEthUsdt = (chainId, address) => formatChainIdToDec(chainId) === ChainId.ETH &&
60
83
  address.toLowerCase() === ETH_USDT_ADDRESS.toLowerCase();
61
84
  export const sumHexes = (...hexStrings) => {
62
85
  if (hexStrings.length === 0) {
@@ -145,22 +168,4 @@ export const isNonEvmChainId = (chainId) => {
145
168
  export const isEvmQuoteResponse = (quoteResponse) => {
146
169
  return !isNonEvmChainId(quoteResponse.quote.srcChainId);
147
170
  };
148
- /**
149
- * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
150
- *
151
- * @param srcChainId - The source chainId
152
- * @param destChainId - The destination chainId
153
- * @returns Whether the transaction is a cross-chain transaction
154
- */
155
- export const isCrossChain = (srcChainId, destChainId) => {
156
- try {
157
- if (!destChainId) {
158
- return false;
159
- }
160
- return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);
161
- }
162
- catch {
163
- return false;
164
- }
165
- };
166
171
  //# sourceMappingURL=bridge.mjs.map
@@ -1 +1 @@
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,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AACrE,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;AAQ7B,OAAO,EAAE,OAAO,EAAE,q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X,OAAO,CACL,eAAe,CAAC,OAAO,CAAC;QACxB,gBAAgB,CAAC,OAAO,CAAC;QACzB,aAAa,CAAC,OAAO,CAAC,CACvB,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,aAA4B,EACoB,EAAE;IAClD,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,UAA6C,EAC7C,WAAgD,EAChD,EAAE;IACF,IAAI,CAAC;QACH,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,mBAAmB,CAAC,UAAU,CAAC,KAAK,mBAAmB,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope, TrxScope } 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 {\n BridgeAsset,\n BridgeControllerState,\n GenericQuoteRequest,\n QuoteResponse,\n TxData,\n} 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 (!isStrictHexString(address) &&\n Object.values(SYMBOL_TO_SLIP44_MAP).some(\n // check if it matches any supported SLIP44 references\n (reference) => address.includes(reference) || reference.endsWith(address),\n ));\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\nexport const isBitcoinChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === BtcScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.BTC.toString();\n};\n\nexport const isTronChainId = (chainId: Hex | number | CaipChainId | string) => {\n if (isCaipChainId(chainId)) {\n return chainId === TrxScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.TRON.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana, Bitcoin and Tron\n *\n * @param chainId - The chain ID to check\n * @returns True if the chain is a supported non-EVM chain, false otherwise\n */\nexport const isNonEvmChainId = (\n chainId: GenericQuoteRequest['srcChainId'],\n): boolean => {\n return (\n isSolanaChainId(chainId) ||\n isBitcoinChainId(chainId) ||\n isTronChainId(chainId)\n );\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData, TxData> => {\n return !isNonEvmChainId(quoteResponse.quote.srcChainId);\n};\n\n/**\n * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds\n *\n * @param srcChainId - The source chainId\n * @param destChainId - The destination chainId\n * @returns Whether the transaction is a cross-chain transaction\n */\nexport const isCrossChain = (\n srcChainId: GenericQuoteRequest['srcChainId'],\n destChainId?: GenericQuoteRequest['destChainId'],\n) => {\n try {\n if (!destChainId) {\n return false;\n }\n return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);\n } catch {\n return false;\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;AACp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,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC,CAAC","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { isCaipChainId, isStrictHexString } from '@metamask/utils';\nimport type { CaipAssetType, CaipChainId, 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 { SWAPS_CONTRACT_ADDRESSES } from '../constants/swaps';\nimport {\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n SYMBOL_TO_SLIP44_MAP,\n} from '../constants/tokens';\nimport type { SupportedSwapsNativeCurrencySymbols } from '../constants/tokens';\nimport type {\n BridgeAsset,\n BridgeControllerState,\n GenericQuoteRequest,\n QuoteResponse,\n TxData,\n} from '../types';\nimport { ChainId } from '../types';\n\n/**\n * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds\n *\n * @param srcChainId - The source chainId\n * @param destChainId - The destination chainId\n * @returns Whether the transaction is a cross-chain transaction\n */\nexport const isCrossChain = (\n srcChainId: GenericQuoteRequest['srcChainId'],\n destChainId?: GenericQuoteRequest['destChainId'],\n) => {\n try {\n if (!destChainId) {\n return false;\n }\n return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);\n } catch {\n return false;\n }\n};\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 * @param destChainId - The destination chain ID\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 destChainId: GenericQuoteRequest['destChainId'],\n) => {\n const spenderAddress = isCrossChain(CHAIN_IDS.MAINNET, destChainId)\n ? METABRIDGE_ETHEREUM_ADDRESS\n : SWAPS_CONTRACT_ADDRESSES[CHAIN_IDS.MAINNET];\n const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n .interface;\n const data = UsdtContractInterface.encodeFunctionData('approve', [\n spenderAddress,\n '0',\n ]);\n\n return data;\n};\n\nexport const isEthUsdt = (\n chainId: GenericQuoteRequest['srcChainId'],\n address: string,\n) =>\n formatChainIdToDec(chainId) === ChainId.ETH &&\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 (!isStrictHexString(address) &&\n Object.values(SYMBOL_TO_SLIP44_MAP).some(\n // check if it matches any supported SLIP44 references\n (reference) => address.includes(reference) || reference.endsWith(address),\n ));\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\nexport const isBitcoinChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === BtcScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.BTC.toString();\n};\n\nexport const isTronChainId = (chainId: Hex | number | CaipChainId | string) => {\n if (isCaipChainId(chainId)) {\n return chainId === TrxScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.TRON.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana, Bitcoin and Tron\n *\n * @param chainId - The chain ID to check\n * @returns True if the chain is a supported non-EVM chain, false otherwise\n */\nexport const isNonEvmChainId = (\n chainId: GenericQuoteRequest['srcChainId'],\n): boolean => {\n return (\n isSolanaChainId(chainId) ||\n isBitcoinChainId(chainId) ||\n isTronChainId(chainId)\n );\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData, TxData> => {\n return !isNonEvmChainId(quoteResponse.quote.srcChainId);\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"caip-formatters.cjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;AACpD,wDAAuD;AACvD,iEAAiE;AACjE,uDAAqE;AACrE,2FAA2E;AAE3E,2CAUyB;AAEzB,yCAMkB;AAElB,wCAAmC;AAEnC;;;;;GAKG;AACI,MAAM,mBAAmB,GAAG,CACjC,OAA4C,EAC/B,EAAE;IACf,IAAI,IAAA,qBAAa,EAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,IAAI,IAAA,yBAAiB,EAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,OAAO,IAAA,gDAAgB,EAAC,OAAO,CAAC,CAAC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oBAAoB,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,2EAA2E;IAC3E,oDAAoD;IACpD,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC;AAhBW,QAAA,4BAA4B,gCAgBvC;AAEF;;;;;;GAMG;AACI,MAAM,sBAAsB,GAAG,CACpC,gBAA8C,EAC9C,OAA0C,EACf,EAAE;IAC7B,IAAI,IAAA,uBAAe,EAAC,gBAAgB,CAAC,EAAE,CAAC;QACtC,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IACD,IAAI,IAAA,wBAAe,EAAC,gBAAgB,CAAC,EAAE,CAAC;QACtC,OAAO,IAAA,iCAAwB,EAAC,OAAO,CAAC,CAAC,OAAO,CAAC;IACnD,CAAC;IACD,IAAI,OAAO,KAAK,sBAAQ,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,2BAAmB,CAAC,MAAM,CAAC,GAAG,OAAO,UAAU,gBAAgB,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED,aAAa;IACb,IAAI,CAAC,IAAA,yBAAiB,EAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,2BAAmB,CAAC,MAAM,CAC/B,GAAG,IAAA,2BAAmB,EAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,sBAAsB,0BAqBjC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\n} 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 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 (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
1
+ {"version":3,"file":"caip-formatters.cjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":";;;AA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O,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,2BAAmB,CAAC,MAAM,CAC/B,GAAG,IAAA,2BAAmB,EAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,sBAAsB,0BAqBjC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } 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 numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\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 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 (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
@@ -1,5 +1,4 @@
1
- import type { CaipAssetType } from "@metamask/utils";
2
- import { type Hex, type CaipChainId } from "@metamask/utils";
1
+ import type { CaipAssetType, CaipChainId, Hex } from "@metamask/utils";
3
2
  import type { GenericQuoteRequest } from "../types.cjs";
4
3
  /**
5
4
  * Converts a chainId to a CaipChainId
@@ -1 +1 @@
1
- {"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AACrD,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAQjB,wBAAwB;AASzB,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,WACrC,mBAAmB,CAAC,YAAY,CAAC,KACzC,aAAa,GAAG,SAkBlB,CAAC"}
1
+ {"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAcA,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,WACrC,mBAAmB,CAAC,YAAY,CAAC,KACzC,aAAa,GAAG,SAkBlB,CAAC"}
@@ -1,5 +1,4 @@
1
- import type { CaipAssetType } from "@metamask/utils";
2
- import { type Hex, type CaipChainId } from "@metamask/utils";
1
+ import type { CaipAssetType, CaipChainId, Hex } from "@metamask/utils";
3
2
  import type { GenericQuoteRequest } from "../types.mjs";
4
3
  /**
5
4
  * Converts a chainId to a CaipChainId
@@ -1 +1 @@
1
- {"version":3,"file":"caip-formatters.d.mts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AACrD,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAQjB,wBAAwB;AASzB,OAAO,KAAK,EAAE,mBAAmB,EAAE,qB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AO,CAAC,CAAC,OAAO,CAAC;IACnD,CAAC;IACD,IAAI,OAAO,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,mBAAmB,CAAC,MAAM,CAAC,GAAG,OAAO,UAAU,gBAAgB,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED,aAAa;IACb,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,mBAAmB,CAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\n} 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 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 (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
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,iCAAiC;AACvD,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AACjE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AACrE,OAAO,EAAE,gBAAgB,EAAE,gDAAgD;AAC3E,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,eAAe,EACf,mBAAmB,EACpB,wBAAwB;AAGzB,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,aAAa,EACd,qBAAiB;AAElB,OAAO,EAAE,OAAO,EAAE,qBAAiB;AAEnC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,OAA4C,EAC/B,EAAE;IACf,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC;IACj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C,CAAC,CAAC;IACvD,2EAA2E;IAC3E,oDAAoD;IACpD,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,gBAA8C,EAC9C,OAA0C,EACf,EAAE;IAC7B,IAAI,eAAe,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACtC,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IACD,IAAI,eAAe,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACtC,OAAO,wBAAwB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;IACnD,CAAC;IACD,IAAI,OAAO,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,mBAAmB,CAAC,MAAM,CAAC,GAAG,OAAO,UAAU,gBAAgB,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED,aAAa;IACb,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,mBAAmB,CAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } 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 numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\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 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 (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
@@ -2,8 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchBridgeQuoteStream = exports.fetchAssetPrices = exports.fetchBridgeQuotes = exports.fetchBridgeTokens = exports.getClientHeaders = void 0;
4
4
  const superstruct_1 = require("@metamask/superstruct");
5
+ const bridge_1 = require("./bridge.cjs");
5
6
  const caip_formatters_1 = require("./caip-formatters.cjs");
6
7
  const fetch_server_events_1 = require("./fetch-server-events.cjs");
8
+ const trade_utils_1 = require("./trade-utils.cjs");
7
9
  const validators_1 = require("./validators.cjs");
8
10
  const getClientHeaders = (clientId, clientVersion) => ({
9
11
  'X-Client-Id': clientId,
@@ -121,6 +123,14 @@ async function fetchBridgeQuotes(request, signal, clientId, fetchFn, bridgeApiBa
121
123
  .map((quote) => ({
122
124
  ...quote,
123
125
  featureId: featureId ?? undefined,
126
+ // Append the reset approval data to the quote response if the request
127
+ // has resetApproval set to true and the quote has an approval
128
+ resetApproval: request.resetApproval && quote.approval && (0, trade_utils_1.isEvmTxData)(quote.approval)
129
+ ? {
130
+ ...quote.approval,
131
+ data: (0, bridge_1.getEthUsdtResetData)(request.destChainId),
132
+ }
133
+ : undefined,
124
134
  }));
125
135
  const validationFailures = Array.from(uniqueValidationFailures);
126
136
  if (uniqueValidationFailures.size > 0) {
@@ -213,7 +223,18 @@ async function fetchBridgeQuoteStream(fetchFn, request, signal, clientId, bridge
213
223
  const uniqueValidationFailures = new Set([]);
214
224
  try {
215
225
  if ((0, validators_1.validateQuoteResponse)(quoteResponse)) {
216
- return await serverEventHandlers.onValidQuoteReceived(quoteResponse);
226
+ return await serverEventHandlers.onValidQuoteReceived({
227
+ ...quoteResponse,
228
+ // Append the reset approval data to the quote response if the request has resetApproval set to true and the quote has an approval
229
+ resetApproval: request.resetApproval &&
230
+ quoteResponse.approval &&
231
+ (0, trade_utils_1.isEvmTxData)(quoteResponse.approval)
232
+ ? {
233
+ ...quoteResponse.approval,
234
+ data: (0, bridge_1.getEthUsdtResetData)(request.destChainId),
235
+ }
236
+ : undefined,
237
+ });
217
238
  }
218
239
  }
219
240
  catch (error) {