thirdweb 5.99.2 → 5.100.0-nightly-713ebe0a2d6d8a409e41ce0f0317fc34b1f221bb-20250520000414

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 (201) hide show
  1. package/dist/cjs/bridge/Buy.js +16 -12
  2. package/dist/cjs/bridge/Buy.js.map +1 -1
  3. package/dist/cjs/bridge/Chains.js +2 -2
  4. package/dist/cjs/bridge/Chains.js.map +1 -1
  5. package/dist/cjs/bridge/Routes.js +2 -2
  6. package/dist/cjs/bridge/Routes.js.map +1 -1
  7. package/dist/cjs/bridge/Sell.js +17 -13
  8. package/dist/cjs/bridge/Sell.js.map +1 -1
  9. package/dist/cjs/bridge/Status.js +15 -2
  10. package/dist/cjs/bridge/Status.js.map +1 -1
  11. package/dist/cjs/bridge/Transfer.js +229 -0
  12. package/dist/cjs/bridge/Transfer.js.map +1 -0
  13. package/dist/cjs/bridge/index.js +2 -1
  14. package/dist/cjs/bridge/index.js.map +1 -1
  15. package/dist/cjs/bridge/types/Token.js +3 -0
  16. package/dist/cjs/bridge/types/Token.js.map +1 -0
  17. package/dist/cjs/exports/wallets.js +3 -1
  18. package/dist/cjs/exports/wallets.js.map +1 -1
  19. package/dist/cjs/pay/buyWithCrypto/getQuote.js +159 -53
  20. package/dist/cjs/pay/buyWithCrypto/getQuote.js.map +1 -1
  21. package/dist/cjs/pay/buyWithCrypto/getStatus.js +160 -14
  22. package/dist/cjs/pay/buyWithCrypto/getStatus.js.map +1 -1
  23. package/dist/cjs/pay/buyWithCrypto/getTransfer.js +95 -37
  24. package/dist/cjs/pay/buyWithCrypto/getTransfer.js.map +1 -1
  25. package/dist/cjs/pay/utils/definitions.js +1 -31
  26. package/dist/cjs/pay/utils/definitions.js.map +1 -1
  27. package/dist/cjs/react/core/hooks/transaction/useSendTransaction.js +23 -21
  28. package/dist/cjs/react/core/hooks/transaction/useSendTransaction.js.map +1 -1
  29. package/dist/cjs/react/web/ui/ConnectWallet/TransactionsScreen.js +2 -15
  30. package/dist/cjs/react/web/ui/ConnectWallet/TransactionsScreen.js.map +1 -1
  31. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js +1 -1
  32. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js.map +1 -1
  33. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.js +0 -135
  34. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.js.map +1 -1
  35. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/WalletRow.js +3 -1
  36. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/WalletRow.js.map +1 -1
  37. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.js +1 -1
  38. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.js.map +1 -1
  39. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.js +4 -4
  40. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.js.map +1 -1
  41. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.js +83 -34
  42. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.js.map +1 -1
  43. package/dist/cjs/react/web/ui/TransactionButton/ExecutingScreen.js +2 -2
  44. package/dist/cjs/react/web/ui/TransactionButton/ExecutingScreen.js.map +1 -1
  45. package/dist/cjs/utils/any-evm/zksync/constants.js +1 -0
  46. package/dist/cjs/utils/any-evm/zksync/constants.js.map +1 -1
  47. package/dist/cjs/utils/domains.js +3 -0
  48. package/dist/cjs/utils/domains.js.map +1 -1
  49. package/dist/cjs/version.js +1 -1
  50. package/dist/cjs/version.js.map +1 -1
  51. package/dist/cjs/wallets/injected/mipdStore.js +12 -0
  52. package/dist/cjs/wallets/injected/mipdStore.js.map +1 -1
  53. package/dist/esm/bridge/Buy.js +16 -12
  54. package/dist/esm/bridge/Buy.js.map +1 -1
  55. package/dist/esm/bridge/Chains.js +2 -2
  56. package/dist/esm/bridge/Chains.js.map +1 -1
  57. package/dist/esm/bridge/Routes.js +2 -2
  58. package/dist/esm/bridge/Routes.js.map +1 -1
  59. package/dist/esm/bridge/Sell.js +17 -13
  60. package/dist/esm/bridge/Sell.js.map +1 -1
  61. package/dist/esm/bridge/Status.js +15 -2
  62. package/dist/esm/bridge/Status.js.map +1 -1
  63. package/dist/esm/bridge/Transfer.js +226 -0
  64. package/dist/esm/bridge/Transfer.js.map +1 -0
  65. package/dist/esm/bridge/index.js +1 -0
  66. package/dist/esm/bridge/index.js.map +1 -1
  67. package/dist/esm/bridge/types/Token.js +2 -0
  68. package/dist/esm/bridge/types/Token.js.map +1 -0
  69. package/dist/esm/exports/wallets.js +1 -0
  70. package/dist/esm/exports/wallets.js.map +1 -1
  71. package/dist/esm/pay/buyWithCrypto/getQuote.js +159 -53
  72. package/dist/esm/pay/buyWithCrypto/getQuote.js.map +1 -1
  73. package/dist/esm/pay/buyWithCrypto/getStatus.js +160 -14
  74. package/dist/esm/pay/buyWithCrypto/getStatus.js.map +1 -1
  75. package/dist/esm/pay/buyWithCrypto/getTransfer.js +95 -37
  76. package/dist/esm/pay/buyWithCrypto/getTransfer.js.map +1 -1
  77. package/dist/esm/pay/utils/definitions.js +0 -25
  78. package/dist/esm/pay/utils/definitions.js.map +1 -1
  79. package/dist/esm/react/core/hooks/transaction/useSendTransaction.js +23 -21
  80. package/dist/esm/react/core/hooks/transaction/useSendTransaction.js.map +1 -1
  81. package/dist/esm/react/web/ui/ConnectWallet/TransactionsScreen.js +2 -15
  82. package/dist/esm/react/web/ui/ConnectWallet/TransactionsScreen.js.map +1 -1
  83. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js +1 -1
  84. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.js.map +1 -1
  85. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.js +1 -134
  86. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.js.map +1 -1
  87. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/WalletRow.js +3 -1
  88. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/WalletRow.js.map +1 -1
  89. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.js +1 -1
  90. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.js.map +1 -1
  91. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.js +1 -1
  92. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.js.map +1 -1
  93. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.js +84 -34
  94. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.js.map +1 -1
  95. package/dist/esm/react/web/ui/TransactionButton/ExecutingScreen.js +2 -2
  96. package/dist/esm/react/web/ui/TransactionButton/ExecutingScreen.js.map +1 -1
  97. package/dist/esm/utils/any-evm/zksync/constants.js +1 -0
  98. package/dist/esm/utils/any-evm/zksync/constants.js.map +1 -1
  99. package/dist/esm/utils/domains.js +3 -0
  100. package/dist/esm/utils/domains.js.map +1 -1
  101. package/dist/esm/version.js +1 -1
  102. package/dist/esm/version.js.map +1 -1
  103. package/dist/esm/wallets/injected/mipdStore.js +11 -0
  104. package/dist/esm/wallets/injected/mipdStore.js.map +1 -1
  105. package/dist/types/bridge/Buy.d.ts +10 -8
  106. package/dist/types/bridge/Buy.d.ts.map +1 -1
  107. package/dist/types/bridge/Sell.d.ts +12 -10
  108. package/dist/types/bridge/Sell.d.ts.map +1 -1
  109. package/dist/types/bridge/Status.d.ts.map +1 -1
  110. package/dist/types/bridge/Transfer.d.ts +199 -0
  111. package/dist/types/bridge/Transfer.d.ts.map +1 -0
  112. package/dist/types/bridge/index.d.ts +1 -0
  113. package/dist/types/bridge/index.d.ts.map +1 -1
  114. package/dist/types/bridge/types/Route.d.ts +3 -17
  115. package/dist/types/bridge/types/Route.d.ts.map +1 -1
  116. package/dist/types/bridge/types/Status.d.ts +15 -0
  117. package/dist/types/bridge/types/Status.d.ts.map +1 -1
  118. package/dist/types/bridge/types/Token.d.ts +10 -0
  119. package/dist/types/bridge/types/Token.d.ts.map +1 -0
  120. package/dist/types/exports/wallets.d.ts +1 -0
  121. package/dist/types/exports/wallets.d.ts.map +1 -1
  122. package/dist/types/pay/buyWithCrypto/commonTypes.d.ts +0 -9
  123. package/dist/types/pay/buyWithCrypto/commonTypes.d.ts.map +1 -1
  124. package/dist/types/pay/buyWithCrypto/getQuote.d.ts.map +1 -1
  125. package/dist/types/pay/buyWithCrypto/getStatus.d.ts +1 -1
  126. package/dist/types/pay/buyWithCrypto/getStatus.d.ts.map +1 -1
  127. package/dist/types/pay/buyWithCrypto/getTransfer.d.ts.map +1 -1
  128. package/dist/types/pay/utils/definitions.d.ts +0 -25
  129. package/dist/types/pay/utils/definitions.d.ts.map +1 -1
  130. package/dist/types/react/core/hooks/transaction/useSendTransaction.d.ts.map +1 -1
  131. package/dist/types/react/web/ui/ConnectWallet/TransactionsScreen.d.ts.map +1 -1
  132. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.d.ts +2 -13
  133. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.d.ts.map +1 -1
  134. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/swap/WalletRow.d.ts.map +1 -1
  135. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.d.ts +0 -1
  136. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.d.ts.map +1 -1
  137. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.d.ts +8 -4
  138. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.d.ts.map +1 -1
  139. package/dist/types/react/web/ui/MediaRenderer/MediaRenderer.d.ts +1 -1
  140. package/dist/types/react/web/ui/TransactionButton/ExecutingScreen.d.ts.map +1 -1
  141. package/dist/types/utils/any-evm/zksync/constants.d.ts.map +1 -1
  142. package/dist/types/utils/domains.d.ts +6 -0
  143. package/dist/types/utils/domains.d.ts.map +1 -1
  144. package/dist/types/version.d.ts +1 -1
  145. package/dist/types/version.d.ts.map +1 -1
  146. package/dist/types/wallets/injected/mipdStore.d.ts +7 -0
  147. package/dist/types/wallets/injected/mipdStore.d.ts.map +1 -1
  148. package/package.json +1 -1
  149. package/src/bridge/Buy.ts +18 -12
  150. package/src/bridge/Chains.ts +2 -2
  151. package/src/bridge/Routes.ts +2 -2
  152. package/src/bridge/Sell.ts +19 -13
  153. package/src/bridge/Status.ts +15 -2
  154. package/src/bridge/Transfer.test.ts +76 -0
  155. package/src/bridge/Transfer.ts +270 -0
  156. package/src/bridge/index.ts +1 -0
  157. package/src/bridge/types/Route.ts +3 -17
  158. package/src/bridge/types/Status.ts +15 -1
  159. package/src/bridge/types/Token.ts +10 -0
  160. package/src/exports/wallets.ts +1 -0
  161. package/src/extensions/prebuilts/deploy-modular-core.test.ts +2 -1
  162. package/src/pay/buyWithCrypto/commonTypes.ts +0 -10
  163. package/src/pay/buyWithCrypto/getQuote.ts +210 -100
  164. package/src/pay/buyWithCrypto/getStatus.ts +213 -20
  165. package/src/pay/buyWithCrypto/getTransfer.ts +129 -61
  166. package/src/pay/utils/definitions.ts +0 -34
  167. package/src/react/core/hooks/transaction/useSendTransaction.ts +27 -33
  168. package/src/react/web/ui/ConnectWallet/TransactionsScreen.tsx +29 -35
  169. package/src/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.tsx +3 -1
  170. package/src/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/useBuyTransactionsToShow.ts +2 -181
  171. package/src/react/web/ui/ConnectWallet/screens/Buy/swap/WalletRow.tsx +3 -1
  172. package/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx +1 -1
  173. package/src/react/web/ui/ConnectWallet/screens/Buy/swap/pendingSwapTx.ts +1 -3
  174. package/src/react/web/ui/ConnectWallet/screens/Buy/swap/useSwapSupportedChains.ts +118 -63
  175. package/src/react/web/ui/TransactionButton/ExecutingScreen.tsx +0 -2
  176. package/src/utils/any-evm/zksync/constants.ts +1 -0
  177. package/src/utils/domain.test.ts +1 -0
  178. package/src/utils/domains.ts +8 -0
  179. package/src/version.ts +1 -1
  180. package/src/wallets/injected/mipdStore.ts +14 -0
  181. package/dist/cjs/bridge/constants.js +0 -5
  182. package/dist/cjs/bridge/constants.js.map +0 -1
  183. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.js +0 -54
  184. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.js.map +0 -1
  185. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.js +0 -52
  186. package/dist/cjs/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.js.map +0 -1
  187. package/dist/esm/bridge/constants.js +0 -2
  188. package/dist/esm/bridge/constants.js.map +0 -1
  189. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.js +0 -51
  190. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.js.map +0 -1
  191. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.js +0 -48
  192. package/dist/esm/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.js.map +0 -1
  193. package/dist/types/bridge/constants.d.ts +0 -2
  194. package/dist/types/bridge/constants.d.ts.map +0 -1
  195. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.d.ts +0 -10
  196. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.d.ts.map +0 -1
  197. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.d.ts +0 -9
  198. package/dist/types/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.d.ts.map +0 -1
  199. package/src/bridge/constants.ts +0 -1
  200. package/src/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistory.tsx +0 -163
  201. package/src/react/web/ui/ConnectWallet/screens/Buy/pay-transactions/BuyTxHistoryButton.tsx +0 -127
@@ -1,15 +1,16 @@
1
- import type { Hash } from "viem";
1
+ import { Value } from "ox";
2
+ import * as ox__AbiFunction from "ox/AbiFunction";
3
+ import * as Bridge from "../../bridge/index.js";
2
4
  import { getCachedChain } from "../../chains/utils.js";
3
5
  import type { ThirdwebClient } from "../../client/client.js";
6
+ import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
7
+ import { getContract } from "../../contract/contract.js";
8
+ import { decimals } from "../../extensions/erc20/read/decimals.js";
4
9
  import type { PrepareTransactionOptions } from "../../transaction/prepare-transaction.js";
5
- import { getClientFetch } from "../../utils/fetch.js";
6
- import { stringify } from "../../utils/json.js";
7
- import { getPayBuyWithCryptoQuoteEndpoint } from "../utils/definitions.js";
8
10
  import type {
9
11
  QuoteApprovalInfo,
10
12
  QuotePaymentToken,
11
13
  QuoteTokenInfo,
12
- QuoteTransactionRequest,
13
14
  } from "./commonTypes.js";
14
15
 
15
16
  /**
@@ -105,44 +106,6 @@ export type GetBuyWithCryptoQuoteParams = {
105
106
  }
106
107
  );
107
108
 
108
- /**
109
- * @buyCrypto
110
- */
111
- type BuyWithCryptoQuoteRouteResponse = {
112
- transactionRequest: QuoteTransactionRequest;
113
- approval?: QuoteApprovalInfo;
114
-
115
- fromAddress: string;
116
- toAddress: string;
117
-
118
- fromToken: QuoteTokenInfo;
119
- toToken: QuoteTokenInfo;
120
-
121
- fromAmountWei: string;
122
- fromAmount: string;
123
-
124
- toAmountMinWei: string;
125
- toAmountMin: string;
126
- toAmountWei: string;
127
- toAmount: string;
128
-
129
- paymentTokens: QuotePaymentToken[];
130
- processingFees: QuotePaymentToken[];
131
-
132
- estimated: {
133
- fromAmountUSDCents: number;
134
- toAmountMinUSDCents: number;
135
- toAmountUSDCents: number;
136
- slippageBPS: number;
137
- feesUSDCents: number;
138
- gasCostUSDCents?: number;
139
- durationSeconds?: number;
140
- };
141
-
142
- maxSlippageBPS: number;
143
- bridge?: string;
144
- };
145
-
146
109
  /**
147
110
  * @buyCrypto
148
111
  */
@@ -215,76 +178,223 @@ export async function getBuyWithCryptoQuote(
215
178
  params: GetBuyWithCryptoQuoteParams,
216
179
  ): Promise<BuyWithCryptoQuote> {
217
180
  try {
218
- const clientFetch = getClientFetch(params.client);
219
-
220
- const response = await clientFetch(getPayBuyWithCryptoQuoteEndpoint(), {
221
- method: "POST",
222
- headers: {
223
- Accept: "application/json",
224
- "Content-Type": "application/json",
225
- },
226
- body: stringify({
227
- fromAddress: params.fromAddress,
228
- toAddress: params.toAddress,
229
- fromChainId: params.fromChainId.toString(),
230
- fromTokenAddress: params.fromTokenAddress,
231
- toChainId: params.toChainId.toString(),
232
- toTokenAddress: params.toTokenAddress,
233
- fromAmount: params.fromAmount,
234
- toAmount: params.toAmount,
235
- maxSlippageBPS: params.maxSlippageBPS,
236
- intentId: params.intentId,
237
- purchaseData: params.purchaseData,
238
- }),
239
- });
240
-
241
- // Assuming the response directly matches the SwapResponse interface
242
- if (!response.ok) {
243
- const errorObj = await response.json();
244
- if (errorObj && "error" in errorObj) {
245
- throw errorObj;
181
+ const quote = await (async () => {
182
+ if (params.toAmount) {
183
+ const destinationTokenContract = getContract({
184
+ address: params.toTokenAddress,
185
+ chain: getCachedChain(params.toChainId),
186
+ client: params.client,
187
+ });
188
+ const tokenDecimals =
189
+ destinationTokenContract.address.toLowerCase() ===
190
+ NATIVE_TOKEN_ADDRESS
191
+ ? 18
192
+ : await decimals({
193
+ contract: destinationTokenContract,
194
+ });
195
+ const amount = Value.from(params.toAmount, tokenDecimals);
196
+ return Bridge.Buy.prepare({
197
+ sender: params.fromAddress,
198
+ receiver: params.toAddress,
199
+ originChainId: params.fromChainId,
200
+ originTokenAddress: params.fromTokenAddress,
201
+ destinationChainId: params.toChainId,
202
+ destinationTokenAddress: params.toTokenAddress,
203
+ amount: amount,
204
+ purchaseData: params.purchaseData,
205
+ client: params.client,
206
+ });
207
+ } else if (params.fromAmount) {
208
+ const originTokenContract = getContract({
209
+ address: params.fromTokenAddress,
210
+ chain: getCachedChain(params.fromChainId),
211
+ client: params.client,
212
+ });
213
+ const tokenDecimals = await decimals({
214
+ contract: originTokenContract,
215
+ });
216
+ const amount = Value.from(params.fromAmount, tokenDecimals);
217
+ return Bridge.Sell.prepare({
218
+ sender: params.fromAddress,
219
+ receiver: params.toAddress,
220
+ originChainId: params.fromChainId,
221
+ originTokenAddress: params.fromTokenAddress,
222
+ destinationChainId: params.toChainId,
223
+ destinationTokenAddress: params.toTokenAddress,
224
+ amount: amount,
225
+ purchaseData: params.purchaseData,
226
+ client: params.client,
227
+ });
246
228
  }
247
- throw new Error(`HTTP error! status: ${response.status}`);
229
+ throw new Error(
230
+ "Invalid quote request, must provide either `fromAmount` or `toAmount`",
231
+ );
232
+ })();
233
+
234
+ // check if the fromAddress already has approval for the given amount
235
+ const firstStep = quote.steps[0];
236
+ if (!firstStep) {
237
+ throw new Error(
238
+ "This quote is incompatible with getBuyWithCryptoQuote. Please use Bridge.Buy.prepare instead.",
239
+ );
240
+ }
241
+ const approvalTxs = firstStep.transactions.filter(
242
+ (tx) => tx.action === "approval",
243
+ );
244
+ if (approvalTxs.length > 1) {
245
+ throw new Error(
246
+ "This quote is incompatible with getBuyWithCryptoQuote. Please use Bridge.Buy.prepare instead.",
247
+ );
248
248
  }
249
+ const approvalTx = approvalTxs[0];
249
250
 
250
- const data: BuyWithCryptoQuoteRouteResponse = (await response.json())
251
- .result;
251
+ const txs = firstStep.transactions.filter((tx) => tx.action !== "approval");
252
+ if (txs.length > 1) {
253
+ throw new Error(
254
+ "This quote is incompatible with getBuyWithCryptoQuote. Please use Bridge.Buy.prepare instead.",
255
+ );
256
+ }
257
+ const tx = txs[0];
258
+ if (!tx) {
259
+ throw new Error(
260
+ "This quote is incompatible with getBuyWithCryptoQuote. Please use Bridge.Buy.prepare instead.",
261
+ );
262
+ }
252
263
 
253
- // check if the fromAddress already has approval for the given amount
254
- const approvalData = data.approval;
264
+ let approvalData: QuoteApprovalInfo | undefined;
265
+ if (approvalTx) {
266
+ const abiFunction = ox__AbiFunction.from([
267
+ "function approve(address spender, uint256 amount)",
268
+ ]);
269
+ const [spender, amount] = ox__AbiFunction.decodeData(
270
+ abiFunction,
271
+ approvalTx.data,
272
+ );
273
+ approvalData = {
274
+ chainId: firstStep.originToken.chainId,
275
+ tokenAddress: firstStep.originToken.address,
276
+ spenderAddress: spender,
277
+ amountWei: amount.toString(),
278
+ };
279
+ }
255
280
 
256
281
  const swapRoute: BuyWithCryptoQuote = {
257
282
  transactionRequest: {
258
- chain: getCachedChain(data.transactionRequest.chainId),
259
- client: params.client,
260
- data: data.transactionRequest.data as Hash,
261
- to: data.transactionRequest.to,
262
- value: BigInt(data.transactionRequest.value),
283
+ ...tx,
263
284
  extraGas: 50000n, // extra gas buffer
264
285
  },
265
286
  approvalData,
266
287
  swapDetails: {
267
- fromAddress: data.fromAddress,
268
- toAddress: data.toAddress,
269
-
270
- fromToken: data.fromToken,
271
- toToken: data.toToken,
272
-
273
- fromAmount: data.fromAmount,
274
- fromAmountWei: data.fromAmountWei,
275
-
276
- toAmountMinWei: data.toAmountMinWei,
277
- toAmountMin: data.toAmountMin,
278
-
279
- toAmountWei: data.toAmountWei,
280
- toAmount: data.toAmount,
281
- estimated: data.estimated,
282
-
283
- maxSlippageBPS: data.maxSlippageBPS,
288
+ fromAddress: quote.intent.sender,
289
+ toAddress: quote.intent.receiver,
290
+
291
+ fromToken: {
292
+ tokenAddress: firstStep.originToken.address,
293
+ chainId: firstStep.originToken.chainId,
294
+ decimals: firstStep.originToken.decimals,
295
+ symbol: firstStep.originToken.symbol,
296
+ name: firstStep.originToken.name,
297
+ priceUSDCents: firstStep.originToken.priceUsd * 100,
298
+ },
299
+ toToken: {
300
+ tokenAddress: firstStep.destinationToken.address,
301
+ chainId: firstStep.destinationToken.chainId,
302
+ decimals: firstStep.destinationToken.decimals,
303
+ symbol: firstStep.destinationToken.symbol,
304
+ name: firstStep.destinationToken.name,
305
+ priceUSDCents: firstStep.destinationToken.priceUsd * 100,
306
+ },
307
+
308
+ fromAmount: Value.format(
309
+ quote.originAmount,
310
+ firstStep.originToken.decimals,
311
+ ).toString(),
312
+ fromAmountWei: quote.originAmount.toString(),
313
+
314
+ toAmountMinWei: quote.destinationAmount.toString(),
315
+ toAmountMin: Value.format(
316
+ quote.destinationAmount,
317
+ firstStep.destinationToken.decimals,
318
+ ).toString(),
319
+
320
+ toAmountWei: quote.destinationAmount.toString(),
321
+ toAmount: Value.format(
322
+ quote.destinationAmount,
323
+ firstStep.destinationToken.decimals,
324
+ ).toString(),
325
+ estimated: {
326
+ fromAmountUSDCents:
327
+ Number(
328
+ Value.format(quote.originAmount, firstStep.originToken.decimals),
329
+ ) *
330
+ firstStep.originToken.priceUsd *
331
+ 100,
332
+ toAmountMinUSDCents:
333
+ Number(
334
+ Value.format(
335
+ quote.destinationAmount,
336
+ firstStep.destinationToken.decimals,
337
+ ),
338
+ ) *
339
+ firstStep.destinationToken.priceUsd *
340
+ 100,
341
+ toAmountUSDCents:
342
+ Number(
343
+ Value.format(
344
+ quote.destinationAmount,
345
+ firstStep.destinationToken.decimals,
346
+ ),
347
+ ) *
348
+ firstStep.destinationToken.priceUsd *
349
+ 100,
350
+ slippageBPS: 0,
351
+ feesUSDCents: 0,
352
+ gasCostUSDCents: 0,
353
+ durationSeconds: firstStep.estimatedExecutionTimeMs / 1000,
354
+ },
355
+
356
+ maxSlippageBPS: 0,
284
357
  },
285
358
 
286
- paymentTokens: data.paymentTokens,
287
- processingFees: data.processingFees,
359
+ paymentTokens: [
360
+ {
361
+ token: {
362
+ tokenAddress: firstStep.originToken.address,
363
+ chainId: firstStep.originToken.chainId,
364
+ decimals: firstStep.originToken.decimals,
365
+ symbol: firstStep.originToken.symbol,
366
+ name: firstStep.originToken.name,
367
+ priceUSDCents: firstStep.originToken.priceUsd * 100,
368
+ },
369
+ amountWei: quote.originAmount.toString(),
370
+ amount: Value.format(
371
+ quote.originAmount,
372
+ firstStep.originToken.decimals,
373
+ ).toString(),
374
+ amountUSDCents:
375
+ Number(
376
+ Value.format(quote.originAmount, firstStep.originToken.decimals),
377
+ ) *
378
+ firstStep.originToken.priceUsd *
379
+ 100,
380
+ },
381
+ ],
382
+ // TODO (UB): add develope and platform fees in API
383
+ processingFees: [
384
+ {
385
+ token: {
386
+ tokenAddress: firstStep.originToken.address,
387
+ chainId: firstStep.originToken.chainId,
388
+ decimals: firstStep.originToken.decimals,
389
+ symbol: firstStep.originToken.symbol,
390
+ name: firstStep.originToken.name,
391
+ priceUSDCents: firstStep.originToken.priceUsd * 100,
392
+ },
393
+ amountUSDCents: 0,
394
+ amountWei: "0",
395
+ amount: "0",
396
+ },
397
+ ],
288
398
  client: params.client,
289
399
  };
290
400
 
@@ -1,10 +1,12 @@
1
+ import { type Status, status as bridgeStatus } from "../../bridge/index.js";
2
+ import type { Token } from "../../bridge/types/Token.js";
1
3
  import type { ThirdwebClient } from "../../client/client.js";
2
- import { getClientFetch } from "../../utils/fetch.js";
4
+ import type { Hex } from "../../utils/encoding/hex.js";
5
+ import { toTokens } from "../../utils/units.js";
3
6
  import type {
4
7
  PayOnChainTransactionDetails,
5
8
  PayTokenInfo,
6
9
  } from "../utils/commonTypes.js";
7
- import { getPayBuyWithCryptoStatusUrl } from "../utils/definitions.js";
8
10
 
9
11
  // TODO: add JSDoc description for all properties
10
12
 
@@ -123,7 +125,7 @@ export type ValidBuyWithCryptoStatus = Exclude<
123
125
  * }});
124
126
  * ```
125
127
  * @returns Object of type [`BuyWithCryptoStatus`](https://portal.thirdweb.com/references/typescript/v5/BuyWithCryptoStatus)
126
- * @deprecated
128
+ * @deprecated use Bridge.status instead
127
129
  * @buyCrypto
128
130
  */
129
131
  export async function getBuyWithCryptoStatus(
@@ -133,26 +135,217 @@ export async function getBuyWithCryptoStatus(
133
135
  if (!buyWithCryptoTransaction.transactionHash) {
134
136
  throw new Error("Transaction hash is required");
135
137
  }
136
- const queryString = new URLSearchParams({
137
- transactionHash: buyWithCryptoTransaction.transactionHash,
138
- chainId: buyWithCryptoTransaction.chainId.toString(),
139
- }).toString();
140
- const url = `${getPayBuyWithCryptoStatusUrl()}?${queryString}`;
141
-
142
- const response = await getClientFetch(buyWithCryptoTransaction.client)(url);
143
-
144
- // Assuming the response directly matches the BuyWithCryptoStatus interface
145
- if (!response.ok) {
146
- const error = await response.text().catch(() => null);
147
- throw new Error(
148
- `HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
149
- );
150
- }
138
+ const result = await bridgeStatus({
139
+ transactionHash: buyWithCryptoTransaction.transactionHash as Hex,
140
+ chainId: buyWithCryptoTransaction.chainId,
141
+ client: buyWithCryptoTransaction.client,
142
+ });
143
+
144
+ switch (result.status) {
145
+ case "COMPLETED": {
146
+ const originTransaction = result.transactions?.find(
147
+ (tx) => tx.chainId === buyWithCryptoTransaction.chainId,
148
+ );
149
+ const destinationTransaction = result.transactions?.find(
150
+ (tx) => tx.chainId !== buyWithCryptoTransaction.chainId,
151
+ );
151
152
 
152
- const data: BuyWithCryptoStatus = (await response.json()).result;
153
- return data;
153
+ return toBuyWithCryptoStatus({
154
+ originTransaction,
155
+ destinationTransaction,
156
+ originAmount: result.originAmount,
157
+ destinationAmount: result.destinationAmount,
158
+ originTokenAddress: result.originTokenAddress,
159
+ destinationTokenAddress: result.destinationTokenAddress,
160
+ originChainId: result.originChainId,
161
+ destinationChainId: result.destinationChainId,
162
+ status: result.status,
163
+ sender: result.sender,
164
+ receiver: result.receiver,
165
+ paymentId: result.paymentId,
166
+ originToken: result.originToken,
167
+ destinationToken: result.destinationToken,
168
+ purchaseData: result.purchaseData as object | undefined,
169
+ });
170
+ }
171
+ case "PENDING": {
172
+ return toBuyWithCryptoStatus({
173
+ originAmount: result.originAmount,
174
+ originTokenAddress: result.originTokenAddress,
175
+ destinationTokenAddress: result.destinationTokenAddress,
176
+ originChainId: result.originChainId,
177
+ destinationChainId: result.destinationChainId,
178
+ status: result.status,
179
+ sender: result.sender,
180
+ receiver: result.receiver,
181
+ paymentId: result.paymentId,
182
+ originToken: result.originToken,
183
+ destinationToken: result.destinationToken,
184
+ purchaseData: result.purchaseData as object | undefined,
185
+ });
186
+ }
187
+ case "FAILED": {
188
+ const originTransaction = result.transactions?.find(
189
+ (tx) => tx.chainId === buyWithCryptoTransaction.chainId,
190
+ );
191
+ const destinationTransaction = result.transactions?.find(
192
+ (tx) => tx.chainId !== buyWithCryptoTransaction.chainId,
193
+ );
194
+ return toBuyWithCryptoStatus({
195
+ originTransaction,
196
+ destinationTransaction,
197
+ originAmount: BigInt(0), // TODO: get from API
198
+ originTokenAddress: "", // TODO: get from API
199
+ destinationTokenAddress: "", // TODO: get from API
200
+ originChainId: 0, // TODO: get from API
201
+ destinationChainId: 0, // TODO: get from API
202
+ status: result.status,
203
+ sender: "",
204
+ receiver: "",
205
+ paymentId: "",
206
+ originToken: undefined,
207
+ destinationToken: undefined,
208
+ purchaseData: result.purchaseData as object | undefined,
209
+ });
210
+ }
211
+ default: {
212
+ return {
213
+ status: "NOT_FOUND",
214
+ };
215
+ }
216
+ }
154
217
  } catch (error) {
155
218
  console.error("Fetch error:", error);
156
219
  throw new Error(`Fetch failed: ${error}`);
157
220
  }
158
221
  }
222
+
223
+ function toBuyWithCryptoStatus(args: {
224
+ originTransaction?: Status["transactions"][number];
225
+ destinationTransaction?: Status["transactions"][number];
226
+ originAmount: bigint;
227
+ originTokenAddress: string;
228
+ destinationAmount?: bigint;
229
+ destinationTokenAddress: string;
230
+ originChainId: number;
231
+ destinationChainId: number;
232
+ status: Status["status"];
233
+ purchaseData?: object;
234
+ sender: string;
235
+ receiver: string;
236
+ paymentId: string;
237
+ originToken?: Token;
238
+ destinationToken?: Token;
239
+ }): BuyWithCryptoStatus {
240
+ const {
241
+ originTransaction,
242
+ destinationTransaction,
243
+ status,
244
+ purchaseData,
245
+ originAmount,
246
+ destinationAmount,
247
+ originTokenAddress,
248
+ destinationTokenAddress,
249
+ originChainId,
250
+ destinationChainId,
251
+ sender,
252
+ receiver,
253
+ originToken,
254
+ destinationToken,
255
+ } = args;
256
+ return {
257
+ fromAddress: sender,
258
+ toAddress: receiver,
259
+ quote: {
260
+ createdAt: new Date().toISOString(),
261
+ estimated: {
262
+ fromAmountUSDCents: 0,
263
+ toAmountMinUSDCents: 0,
264
+ toAmountUSDCents: 0,
265
+ slippageBPS: 0,
266
+ feesUSDCents: 0,
267
+ gasCostUSDCents: 0,
268
+ durationSeconds: 0,
269
+ },
270
+ fromAmount: originToken
271
+ ? toTokens(originAmount, originToken.decimals).toString()
272
+ : "",
273
+ fromAmountWei: originAmount.toString(),
274
+ toAmount:
275
+ destinationToken && destinationAmount
276
+ ? toTokens(destinationAmount, destinationToken.decimals).toString()
277
+ : "",
278
+ toAmountWei: destinationAmount ? destinationAmount.toString() : "",
279
+ toAmountMin: destinationToken
280
+ ? toTokens(
281
+ destinationAmount ?? BigInt(0),
282
+ destinationToken.decimals,
283
+ ).toString()
284
+ : "",
285
+ toAmountMinWei: destinationAmount ? destinationAmount.toString() : "",
286
+ fromToken: {
287
+ tokenAddress: originTokenAddress,
288
+ chainId: originChainId,
289
+ decimals: originToken?.decimals ?? 18,
290
+ name: originToken?.name ?? "",
291
+ symbol: originToken?.symbol ?? "",
292
+ priceUSDCents: 0,
293
+ },
294
+ toToken: {
295
+ tokenAddress: destinationTokenAddress,
296
+ chainId: destinationChainId,
297
+ decimals: destinationToken?.decimals ?? 18,
298
+ name: destinationToken?.name ?? "",
299
+ symbol: destinationToken?.symbol ?? "",
300
+ priceUSDCents: 0,
301
+ },
302
+ },
303
+ swapType:
304
+ originTransaction?.chainId === destinationTransaction?.chainId
305
+ ? "SAME_CHAIN"
306
+ : "CROSS_CHAIN", // TODO transfer type?
307
+ status: status,
308
+ subStatus: status === "COMPLETED" ? "SUCCESS" : "NONE",
309
+ purchaseData: purchaseData as object | undefined,
310
+ bridge: "STARPORT",
311
+ destination: {
312
+ amount: destinationToken
313
+ ? toTokens(
314
+ destinationAmount ?? BigInt(0),
315
+ destinationToken.decimals,
316
+ ).toString()
317
+ : "",
318
+ amountWei: destinationAmount?.toString() ?? "",
319
+ token: {
320
+ tokenAddress: destinationTokenAddress,
321
+ chainId: destinationChainId,
322
+ decimals: destinationToken?.decimals ?? 18,
323
+ name: destinationToken?.name ?? "",
324
+ symbol: destinationToken?.symbol ?? "",
325
+ priceUSDCents: 0,
326
+ },
327
+ amountUSDCents: 0,
328
+ completedAt: new Date().toISOString(),
329
+ explorerLink: "",
330
+ transactionHash: destinationTransaction?.transactionHash ?? "",
331
+ },
332
+ source: {
333
+ amount: originToken
334
+ ? toTokens(originAmount, originToken.decimals).toString()
335
+ : "",
336
+ amountWei: originAmount.toString(),
337
+ token: {
338
+ tokenAddress: originTokenAddress,
339
+ chainId: originChainId,
340
+ decimals: originToken?.decimals ?? 18,
341
+ name: originToken?.name ?? "",
342
+ symbol: originToken?.symbol ?? "",
343
+ priceUSDCents: 0,
344
+ },
345
+ amountUSDCents: 0,
346
+ completedAt: new Date().toISOString(),
347
+ explorerLink: "",
348
+ transactionHash: originTransaction?.transactionHash ?? "",
349
+ },
350
+ };
351
+ }