@b3dotfun/sdk 0.0.83-test.0 → 0.0.84-alpha.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 (176) hide show
  1. package/dist/cjs/anyspend/constants/index.d.ts +1 -1
  2. package/dist/cjs/anyspend/constants/index.js +2 -2
  3. package/dist/cjs/anyspend/react/components/AnySpend.js +21 -12
  4. package/dist/cjs/anyspend/react/components/AnySpendCollectorClubPurchase.js +1 -1
  5. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +4 -6
  6. package/dist/cjs/anyspend/react/components/common/CryptoPaySection.js +2 -4
  7. package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +47 -177
  8. package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethodDisplay.d.ts +15 -0
  9. package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethodDisplay.js +28 -0
  10. package/dist/cjs/anyspend/react/components/common/InsufficientDepositPayment.js +1 -1
  11. package/dist/cjs/anyspend/react/components/common/OrderDetails.js +5 -4
  12. package/dist/cjs/anyspend/react/components/common/OrderToken.js +2 -2
  13. package/dist/cjs/anyspend/react/components/common/OrderTokenAmount.js +1 -1
  14. package/dist/cjs/anyspend/react/components/common/OrderTokenAmountFiat.js +1 -1
  15. package/dist/cjs/anyspend/react/components/common/OrderTokenAmountNew.js +2 -2
  16. package/dist/cjs/anyspend/react/components/common/PaymentStripeWeb2.js +4 -6
  17. package/dist/cjs/anyspend/react/components/common/PaymentVendorUI.js +41 -2
  18. package/dist/cjs/anyspend/react/components/common/TransferCryptoDetails.js +1 -1
  19. package/dist/cjs/anyspend/react/components/webview/WebviewOnrampPayment.js +2 -3
  20. package/dist/cjs/anyspend/react/hooks/index.d.ts +1 -0
  21. package/dist/cjs/anyspend/react/hooks/index.js +1 -0
  22. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +0 -3
  23. package/dist/cjs/anyspend/react/hooks/useConnectedUserProfile.d.ts +2 -1
  24. package/dist/cjs/anyspend/react/hooks/useConnectedUserProfile.js +5 -8
  25. package/dist/cjs/anyspend/react/hooks/useConnectedWalletDisplay.d.ts +0 -2
  26. package/dist/cjs/anyspend/react/hooks/useConnectedWalletDisplay.js +2 -12
  27. package/dist/cjs/anyspend/react/hooks/useHyperliquidTransfer.d.ts +37 -0
  28. package/dist/cjs/anyspend/react/hooks/useHyperliquidTransfer.js +133 -0
  29. package/dist/cjs/anyspend/react/providers/AnyspendProvider.js +1 -13
  30. package/dist/cjs/anyspend/types/chain.d.ts +10 -2
  31. package/dist/cjs/anyspend/types/chain.js +1 -0
  32. package/dist/cjs/anyspend/utils/address.d.ts +11 -0
  33. package/dist/cjs/anyspend/utils/address.js +15 -0
  34. package/dist/cjs/anyspend/utils/chain.d.ts +58 -1
  35. package/dist/cjs/anyspend/utils/chain.js +94 -4
  36. package/dist/cjs/anyspend/utils/token.d.ts +4 -0
  37. package/dist/cjs/anyspend/utils/token.js +35 -6
  38. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +1 -1
  39. package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +3 -4
  40. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +12 -57
  41. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +1 -1
  42. package/dist/cjs/global-account/react/hooks/index.d.ts +0 -2
  43. package/dist/cjs/global-account/react/hooks/index.js +1 -5
  44. package/dist/cjs/global-account/react/hooks/useAccountWallet.js +11 -10
  45. package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +2 -2
  46. package/dist/cjs/global-account/react/hooks/useAuthentication.js +63 -72
  47. package/dist/cjs/global-account/react/hooks/useTWAuth.d.ts +0 -3
  48. package/dist/cjs/global-account/react/hooks/useTWAuth.js +0 -8
  49. package/dist/cjs/global-account/react/hooks/useTurnkeyAuth.js +24 -54
  50. package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +1 -1
  51. package/dist/cjs/shared/utils/payment.utils.d.ts +1 -0
  52. package/dist/cjs/shared/utils/payment.utils.js +9 -0
  53. package/dist/esm/anyspend/constants/index.d.ts +1 -1
  54. package/dist/esm/anyspend/constants/index.js +1 -1
  55. package/dist/esm/anyspend/react/components/AnySpend.js +22 -13
  56. package/dist/esm/anyspend/react/components/AnySpendCollectorClubPurchase.js +1 -1
  57. package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -7
  58. package/dist/esm/anyspend/react/components/common/CryptoPaySection.js +4 -6
  59. package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +45 -175
  60. package/dist/esm/anyspend/react/components/common/CryptoPaymentMethodDisplay.d.ts +15 -0
  61. package/dist/esm/anyspend/react/components/common/CryptoPaymentMethodDisplay.js +25 -0
  62. package/dist/esm/anyspend/react/components/common/InsufficientDepositPayment.js +2 -2
  63. package/dist/esm/anyspend/react/components/common/OrderDetails.js +6 -5
  64. package/dist/esm/anyspend/react/components/common/OrderToken.js +3 -3
  65. package/dist/esm/anyspend/react/components/common/OrderTokenAmount.js +2 -2
  66. package/dist/esm/anyspend/react/components/common/OrderTokenAmountFiat.js +2 -2
  67. package/dist/esm/anyspend/react/components/common/OrderTokenAmountNew.js +3 -3
  68. package/dist/esm/anyspend/react/components/common/PaymentStripeWeb2.js +2 -4
  69. package/dist/esm/anyspend/react/components/common/PaymentVendorUI.js +9 -3
  70. package/dist/esm/anyspend/react/components/common/TransferCryptoDetails.js +2 -2
  71. package/dist/esm/anyspend/react/components/webview/WebviewOnrampPayment.js +3 -4
  72. package/dist/esm/anyspend/react/hooks/index.d.ts +1 -0
  73. package/dist/esm/anyspend/react/hooks/index.js +1 -0
  74. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +0 -3
  75. package/dist/esm/anyspend/react/hooks/useConnectedUserProfile.d.ts +2 -1
  76. package/dist/esm/anyspend/react/hooks/useConnectedUserProfile.js +6 -9
  77. package/dist/esm/anyspend/react/hooks/useConnectedWalletDisplay.d.ts +0 -2
  78. package/dist/esm/anyspend/react/hooks/useConnectedWalletDisplay.js +2 -12
  79. package/dist/esm/anyspend/react/hooks/useHyperliquidTransfer.d.ts +37 -0
  80. package/dist/esm/anyspend/react/hooks/useHyperliquidTransfer.js +127 -0
  81. package/dist/esm/anyspend/react/providers/AnyspendProvider.js +1 -13
  82. package/dist/esm/anyspend/types/chain.d.ts +10 -2
  83. package/dist/esm/anyspend/types/chain.js +1 -0
  84. package/dist/esm/anyspend/utils/address.d.ts +11 -0
  85. package/dist/esm/anyspend/utils/address.js +14 -0
  86. package/dist/esm/anyspend/utils/chain.d.ts +58 -1
  87. package/dist/esm/anyspend/utils/chain.js +91 -4
  88. package/dist/esm/anyspend/utils/token.d.ts +4 -0
  89. package/dist/esm/anyspend/utils/token.js +32 -6
  90. package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +2 -2
  91. package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +4 -5
  92. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +12 -57
  93. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +1 -1
  94. package/dist/esm/global-account/react/hooks/index.d.ts +0 -2
  95. package/dist/esm/global-account/react/hooks/index.js +0 -2
  96. package/dist/esm/global-account/react/hooks/useAccountWallet.js +11 -10
  97. package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +2 -2
  98. package/dist/esm/global-account/react/hooks/useAuthentication.js +63 -72
  99. package/dist/esm/global-account/react/hooks/useTWAuth.d.ts +0 -3
  100. package/dist/esm/global-account/react/hooks/useTWAuth.js +0 -8
  101. package/dist/esm/global-account/react/hooks/useTurnkeyAuth.js +24 -54
  102. package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +1 -1
  103. package/dist/esm/shared/utils/payment.utils.d.ts +1 -0
  104. package/dist/esm/shared/utils/payment.utils.js +9 -1
  105. package/dist/styles/index.css +1 -1
  106. package/dist/types/anyspend/constants/index.d.ts +1 -1
  107. package/dist/types/anyspend/react/components/common/CryptoPaymentMethodDisplay.d.ts +15 -0
  108. package/dist/types/anyspend/react/hooks/index.d.ts +1 -0
  109. package/dist/types/anyspend/react/hooks/useConnectedUserProfile.d.ts +2 -1
  110. package/dist/types/anyspend/react/hooks/useConnectedWalletDisplay.d.ts +0 -2
  111. package/dist/types/anyspend/react/hooks/useHyperliquidTransfer.d.ts +37 -0
  112. package/dist/types/anyspend/types/chain.d.ts +10 -2
  113. package/dist/types/anyspend/utils/address.d.ts +11 -0
  114. package/dist/types/anyspend/utils/chain.d.ts +58 -1
  115. package/dist/types/anyspend/utils/token.d.ts +4 -0
  116. package/dist/types/global-account/react/hooks/index.d.ts +0 -2
  117. package/dist/types/global-account/react/hooks/useAuthentication.d.ts +2 -2
  118. package/dist/types/global-account/react/hooks/useTWAuth.d.ts +0 -3
  119. package/dist/types/global-account/react/hooks/useUserQuery.d.ts +1 -1
  120. package/dist/types/shared/utils/payment.utils.d.ts +1 -0
  121. package/package.json +1 -1
  122. package/src/anyspend/constants/index.ts +1 -1
  123. package/src/anyspend/react/components/AnySpend.tsx +31 -13
  124. package/src/anyspend/react/components/AnySpendCollectorClubPurchase.tsx +1 -1
  125. package/src/anyspend/react/components/AnySpendCustom.tsx +9 -29
  126. package/src/anyspend/react/components/common/CryptoPaySection.tsx +7 -30
  127. package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +22 -253
  128. package/src/anyspend/react/components/common/CryptoPaymentMethodDisplay.tsx +64 -0
  129. package/src/anyspend/react/components/common/InsufficientDepositPayment.tsx +2 -2
  130. package/src/anyspend/react/components/common/OrderDetails.tsx +6 -5
  131. package/src/anyspend/react/components/common/OrderToken.tsx +5 -5
  132. package/src/anyspend/react/components/common/OrderTokenAmount.tsx +3 -3
  133. package/src/anyspend/react/components/common/OrderTokenAmountFiat.tsx +3 -3
  134. package/src/anyspend/react/components/common/OrderTokenAmountNew.tsx +6 -6
  135. package/src/anyspend/react/components/common/PaymentStripeWeb2.tsx +3 -5
  136. package/src/anyspend/react/components/common/PaymentVendorUI.tsx +26 -2
  137. package/src/anyspend/react/components/common/TransferCryptoDetails.tsx +2 -2
  138. package/src/anyspend/react/components/webview/WebviewOnrampPayment.tsx +3 -5
  139. package/src/anyspend/react/hooks/index.ts +1 -0
  140. package/src/anyspend/react/hooks/useAnyspendFlow.ts +0 -4
  141. package/src/anyspend/react/hooks/useConnectedUserProfile.ts +7 -10
  142. package/src/anyspend/react/hooks/useConnectedWalletDisplay.ts +2 -15
  143. package/src/anyspend/react/hooks/useHyperliquidTransfer.ts +152 -0
  144. package/src/anyspend/react/providers/AnyspendProvider.tsx +7 -22
  145. package/src/anyspend/types/chain.ts +10 -1
  146. package/src/anyspend/utils/address.ts +15 -0
  147. package/src/anyspend/utils/chain.ts +115 -5
  148. package/src/anyspend/utils/token.ts +36 -7
  149. package/src/global-account/react/components/B3Provider/B3Provider.tsx +6 -2
  150. package/src/global-account/react/components/SignInWithB3/SignIn.tsx +4 -6
  151. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +22 -77
  152. package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +1 -1
  153. package/src/global-account/react/hooks/index.ts +0 -2
  154. package/src/global-account/react/hooks/useAccountWallet.tsx +12 -11
  155. package/src/global-account/react/hooks/useAuthentication.ts +85 -88
  156. package/src/global-account/react/hooks/useTWAuth.tsx +0 -10
  157. package/src/global-account/react/hooks/useTurnkeyAuth.ts +26 -59
  158. package/src/shared/utils/payment.utils.ts +10 -1
  159. package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +0 -10
  160. package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +0 -73
  161. package/dist/cjs/global-account/react/hooks/useAuth.d.ts +0 -76
  162. package/dist/cjs/global-account/react/hooks/useAuth.js +0 -338
  163. package/dist/cjs/global-account/react/hooks/useBestTransactionPath.d.ts +0 -41
  164. package/dist/cjs/global-account/react/hooks/useBestTransactionPath.js +0 -148
  165. package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +0 -10
  166. package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +0 -70
  167. package/dist/esm/global-account/react/hooks/useAuth.d.ts +0 -76
  168. package/dist/esm/global-account/react/hooks/useAuth.js +0 -332
  169. package/dist/esm/global-account/react/hooks/useBestTransactionPath.d.ts +0 -41
  170. package/dist/esm/global-account/react/hooks/useBestTransactionPath.js +0 -145
  171. package/dist/types/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +0 -10
  172. package/dist/types/global-account/react/hooks/useAuth.d.ts +0 -76
  173. package/dist/types/global-account/react/hooks/useBestTransactionPath.d.ts +0 -41
  174. package/src/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.ts +0 -80
  175. package/src/global-account/react/hooks/useAuth.ts +0 -380
  176. package/src/global-account/react/hooks/useBestTransactionPath.tsx +0 -201
@@ -1,380 +0,0 @@
1
- import app from "@b3dotfun/sdk/global-account/app";
2
- import { authenticateWithB3JWT } from "@b3dotfun/sdk/global-account/bsmnt";
3
- import { useAuthStore } from "@b3dotfun/sdk/global-account/react";
4
- import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
5
- import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
6
- import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
7
- import { ConnectionOptions } from "@thirdweb-dev/wagmi-adapter";
8
- import { getConnectors } from "@wagmi/core";
9
- import { useCallback, useContext, useEffect, useRef } from "react";
10
- import {
11
- useActiveWallet,
12
- useAutoConnect,
13
- useConnectedWallets,
14
- useDisconnect,
15
- useSetActiveWallet,
16
- } from "thirdweb/react";
17
- import { Wallet, ecosystemWallet } from "thirdweb/wallets";
18
- import { preAuthenticate } from "thirdweb/wallets/in-app";
19
- import { useAccount, useConnect, useSwitchAccount } from "wagmi";
20
- import { LocalSDKContext } from "../components/B3Provider/LocalSDKProvider";
21
- import { useB3 } from "../components/B3Provider/useB3";
22
- import { createWagmiConfig } from "../utils/createWagmiConfig";
23
- import { useSearchParam } from "./useSearchParamsSSR";
24
- import { useUserQuery } from "./useUserQuery";
25
-
26
- const debug = debugB3React("useAuth");
27
-
28
- /**
29
- * Unified authentication hook that uses Turnkey for authentication
30
- * This replaces the previous Thirdweb-based authentication
31
- *
32
- * This hook provides 1:1 feature parity with useAuthentication.ts
33
- */
34
- export function useAuth() {
35
- const { onConnectCallback } = useContext(LocalSDKContext);
36
- const { disconnect } = useDisconnect();
37
- const wallets = useConnectedWallets();
38
- const activeWallet = useActiveWallet();
39
- const isAuthenticated = useAuthStore(state => state.isAuthenticated);
40
- const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
41
- const setIsConnected = useAuthStore(state => state.setIsConnected);
42
- const isConnecting = useAuthStore(state => state.isConnecting);
43
- const isConnected = useAuthStore(state => state.isConnected);
44
- const isAuthenticating = useAuthStore(state => state.isAuthenticating);
45
- const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
46
- const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
47
- const setActiveWallet = useSetActiveWallet();
48
- const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
49
- const useAutoConnectLoadingPrevious = useRef(false);
50
- const referralCode = useSearchParam("referralCode");
51
- const { partnerId } = useB3();
52
- const wagmiConfig = createWagmiConfig({ partnerId });
53
- const { connect } = useConnect();
54
- const activeWagmiAccount = useAccount();
55
- const { switchAccount } = useSwitchAccount();
56
- const { user, setUser } = useUserQuery();
57
- debug("@@activeWagmiAccount", activeWagmiAccount);
58
-
59
- const wallet = ecosystemWallet(ecosystemWalletId, {
60
- partnerId: partnerId,
61
- });
62
-
63
- /**
64
- * Re-authenticate using existing session
65
- * Also updates user state and authenticates with BSMNT
66
- */
67
- const reAuthenticate = useCallback(async () => {
68
- debug("Re-authenticating...");
69
- try {
70
- const response = await app.reAuthenticate();
71
- debug("Re-authentication successful", response);
72
-
73
- // Update user state if user data exists
74
- if (response.user) {
75
- setUser(response.user);
76
- debug("User state updated", response.user);
77
- }
78
-
79
- // Authenticate with BSMNT
80
- try {
81
- const b3Jwt = await authenticateWithB3JWT(response.accessToken);
82
- debug("BSMNT re-authentication successful", b3Jwt);
83
- } catch (bsmntError) {
84
- // BSMNT authentication failure shouldn't block the main auth flow
85
- debug("BSMNT re-authentication failed (non-critical)", bsmntError);
86
- }
87
-
88
- return response;
89
- } catch (err: any) {
90
- debug("Re-authentication failed", err);
91
- throw err;
92
- }
93
- }, [setUser]);
94
-
95
- const syncWagmi = useCallback(async () => {
96
- function syncWagmiFunc() {
97
- const connectors = getConnectors(wagmiConfig);
98
- debug("@@syncWagmi", {
99
- connectors,
100
- wallets,
101
- });
102
-
103
- // For each that matchs a TW wallet on wallets, connect to the wagmi connector
104
- // or, since ecosystem wallets is separate, connect those via in-app-wallet from wagmi
105
- connectors.forEach(async connector => {
106
- const twWallet = wallets.find(wallet => wallet.id === connector.id || connector.id === "in-app-wallet");
107
-
108
- // If no TW wallet, do not prompt the user to connect
109
- if (!twWallet) {
110
- return;
111
- }
112
-
113
- // Metamask will prompt to connect, we can just switch accounts here.
114
- if (connector.id === "io.metamask") {
115
- return switchAccount({ connector });
116
- }
117
-
118
- if (
119
- // If it's not an in-app wallet or it is the ecosystem wallet, connect
120
- connector.id !== "in-app-wallet" ||
121
- (connector.id === "in-app-wallet" && twWallet.id === ecosystemWalletId)
122
- ) {
123
- try {
124
- const options = {
125
- wallet: twWallet, // the connected wallet
126
- } satisfies ConnectionOptions;
127
- debug("@@syncWagmi:connecting", { twWallet, connector });
128
- connect({
129
- connector,
130
- ...options,
131
- });
132
- } catch (error) {
133
- console.error("@@syncWagmi:error", error);
134
- }
135
- } else {
136
- debug("@@syncWagmi:not-connecting", connector);
137
- }
138
- });
139
- }
140
- syncWagmiFunc();
141
- // wagmi config shouldn't change
142
- // eslint-disable-next-line react-hooks/exhaustive-deps
143
- }, [partnerId, wallets]);
144
-
145
- useEffect(() => {
146
- syncWagmi();
147
- }, [wallets, syncWagmi]);
148
-
149
- /**
150
- * Authenticate user using Turnkey
151
- * Note: This no longer requires a wallet for authentication.
152
- * Wallets are still used for signing transactions, but authentication is done via Turnkey email OTP.
153
- *
154
- * For backward compatibility, this function still accepts a wallet parameter,
155
- * but it's not used for authentication anymore.
156
- */
157
- const authenticateUser = useCallback(async () => {
158
- setHasStartedConnecting(true);
159
-
160
- // Try to re-authenticate first
161
- try {
162
- const userAuth = await reAuthenticate();
163
- setUser(userAuth.user);
164
- setIsAuthenticated(true);
165
- setIsAuthenticating(false);
166
- debug("Re-authenticated successfully", { userAuth });
167
-
168
- // Authenticate on BSMNT with B3 JWT
169
- const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
170
- debug("@@b3Jwt", b3Jwt);
171
-
172
- return userAuth;
173
- } catch (error) {
174
- // If re-authentication fails, user needs to authenticate via Turnkey
175
- // This should be handled by the Turnkey auth modal/flow
176
- debug("Re-authentication failed. User needs to authenticate via Turnkey.", error);
177
- setIsAuthenticated(false);
178
- setIsAuthenticating(false);
179
- throw new Error("Authentication required. Please authenticate via Turnkey.");
180
- }
181
- }, [reAuthenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
182
-
183
- /**
184
- * Authenticate with Turnkey using email OTP
185
- * This is the primary authentication method, replacing Thirdweb wallet-based auth
186
- *
187
- * This function:
188
- * 1. Authenticates with FeathersJS (persists session via cookies)
189
- * 2. Sets user state in the user store (persists to localStorage)
190
- * 3. Authenticates with BSMNT for basement integration
191
- */
192
- const authenticate = useCallback(
193
- async (turnkeySessionJwt: string, partnerId: string) => {
194
- if (!turnkeySessionJwt) {
195
- throw new Error("Turnkey session JWT is required");
196
- }
197
-
198
- debug("Authenticating with Turnkey JWT", { referralCode, partnerId });
199
-
200
- try {
201
- // Step 1: Authenticate with FeathersJS (session persisted via cookies)
202
- const response = await app.authenticate({
203
- strategy: "turnkey-jwt",
204
- accessToken: turnkeySessionJwt,
205
- referralCode,
206
- partnerId: partnerId,
207
- } as any);
208
-
209
- debug("Authentication successful", response);
210
-
211
- // Step 2: Set user state (persists to localStorage via Zustand)
212
- if (response.user) {
213
- setUser(response.user);
214
- debug("User state updated", response.user);
215
- }
216
-
217
- // Step 3: Authenticate with BSMNT for basement integration
218
- try {
219
- const b3Jwt = await authenticateWithB3JWT(response.accessToken);
220
- debug("BSMNT authentication successful", b3Jwt);
221
- } catch (bsmntError) {
222
- // BSMNT authentication failure shouldn't block the main auth flow
223
- debug("BSMNT authentication failed (non-critical)", bsmntError);
224
- }
225
-
226
- return response;
227
- } catch (err: any) {
228
- debug("Authentication failed", err);
229
- throw err;
230
- }
231
- },
232
- [referralCode, setUser],
233
- );
234
-
235
- /**
236
- * Handle wallet connection
237
- * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
238
- * not for authentication. Authentication should be done separately via Turnkey email OTP.
239
- */
240
- /**
241
- * Handle wallet connection
242
- * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
243
- * not for authentication. Authentication should be done separately via Turnkey email OTP.
244
- */
245
- const onConnect = useCallback(
246
- async (_walleAutoConnectedWith: Wallet, allConnectedWallets: Wallet[]) => {
247
- debug("@@useAuth:onConnect", { _walleAutoConnectedWith, allConnectedWallets });
248
-
249
- const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
250
-
251
- if (!wallet) {
252
- throw new Error("No smart wallet found during auto-connect");
253
- }
254
-
255
- debug("@@useAuth:onConnect", { wallet });
256
-
257
- try {
258
- setHasStartedConnecting(true);
259
- setIsConnected(true);
260
- setIsAuthenticating(true);
261
- await setActiveWallet(wallet);
262
-
263
- // Try to authenticate user (will use re-authenticate if session exists)
264
- // If no session exists, authentication will need to happen via Turnkey flow
265
- try {
266
- const userAuth = await authenticateUser();
267
-
268
- if (userAuth && onConnectCallback) {
269
- await onConnectCallback(wallet, userAuth.accessToken);
270
- }
271
- } catch (authError) {
272
- // Authentication failed - this is expected if user hasn't authenticated via Turnkey yet
273
- // The Turnkey auth modal should handle this
274
- debug("@@useAuth:onConnect:authFailed", { authError });
275
- // Don't set isAuthenticated to false here - let the Turnkey flow handle it
276
- }
277
- } catch (error) {
278
- debug("@@useAuth:onConnect:failed", { error });
279
- setIsAuthenticated(false);
280
- setUser(undefined);
281
- } finally {
282
- setIsAuthenticating(false);
283
- }
284
-
285
- debug({
286
- isAuthenticated,
287
- isAuthenticating,
288
- isConnected,
289
- });
290
- },
291
- [
292
- onConnectCallback,
293
- authenticateUser,
294
- isAuthenticated,
295
- isAuthenticating,
296
- isConnected,
297
- setActiveWallet,
298
- setHasStartedConnecting,
299
- setIsAuthenticated,
300
- setIsAuthenticating,
301
- setIsConnected,
302
- setUser,
303
- ],
304
- );
305
-
306
- const logout = useCallback(
307
- async (callback?: () => void) => {
308
- if (activeWallet) {
309
- debug("@@logout:activeWallet", activeWallet);
310
- disconnect(activeWallet);
311
- debug("@@logout:activeWallet", activeWallet);
312
- }
313
-
314
- // Log out of each wallet
315
- wallets.forEach(wallet => {
316
- console.log("@@logging out", wallet);
317
- disconnect(wallet);
318
- });
319
-
320
- // Delete localStorage thirdweb:connected-wallet-ids
321
- // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
322
- if (typeof localStorage !== "undefined") {
323
- localStorage.removeItem("thirdweb:connected-wallet-ids");
324
- localStorage.removeItem("wagmi.store");
325
- localStorage.removeItem("lastAuthProvider");
326
- localStorage.removeItem("b3-user");
327
- }
328
-
329
- app.logout();
330
- debug("@@logout:loggedOut");
331
-
332
- setIsAuthenticated(false);
333
- setIsConnected(false);
334
- setUser();
335
- callback?.();
336
- },
337
- [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected],
338
- );
339
-
340
- const { isLoading: useAutoConnectLoading } = useAutoConnect({
341
- client,
342
- wallets: [wallet],
343
- onConnect,
344
- onTimeout: () => {
345
- logout().catch(error => {
346
- debug("@@useAuth:logout on timeout failed", { error });
347
- });
348
- },
349
- });
350
-
351
- /**
352
- * useAutoConnectLoading starts as false
353
- */
354
- useEffect(() => {
355
- if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
356
- setIsAuthenticating(false);
357
- }
358
- useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
359
- }, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
360
-
361
- const isReady = isAuthenticated && !isAuthenticating;
362
-
363
- return {
364
- authenticate,
365
- reAuthenticate,
366
- logout,
367
- isAuthenticated,
368
- isReady,
369
- isConnecting,
370
- isConnected,
371
- wallet,
372
- preAuthenticate,
373
- connect: onConnect,
374
- isAuthenticating,
375
- onConnect,
376
- user,
377
- refetchUser: authenticateUser,
378
- setUser,
379
- };
380
- }
@@ -1,201 +0,0 @@
1
- "use client";
2
-
3
- import { isNativeToken } from "@b3dotfun/sdk/anyspend";
4
- import { useAccountWallet, useOneBalance, useTokenBalance } from "@b3dotfun/sdk/global-account/react";
5
- import { useMemo } from "react";
6
- import { formatUnits } from "viem";
7
- import { components } from "@b3dotfun/sdk/anyspend/types/api";
8
-
9
- export type TransactionType = "send" | "swap" | "bridge";
10
-
11
- const supportedSprinterTokenSymbols = ["ETH", "USDC", "WETH"] as const;
12
-
13
- export interface SprinterOption {
14
- chainId: number;
15
- balance: bigint;
16
- formattedBalance: string;
17
- }
18
-
19
- export interface TransactionPath {
20
- type: "native" | "sprinter" | "anyspend";
21
- hasNativePath: boolean;
22
- hasSprinterPath: boolean;
23
- availableBalance: bigint;
24
- totalCrossChainBalance: number;
25
- sprinterOptions?: SprinterOption[];
26
- }
27
-
28
- export interface TransactionPathResult extends TransactionPath {
29
- loading: boolean;
30
- }
31
-
32
- interface UseBestTransactionPathProps {
33
- amount: bigint;
34
- token: components["schemas"]["Token"];
35
- address?: string;
36
- transactionType?: TransactionType;
37
- }
38
-
39
- /**
40
- * Hook to determine the optimal transaction path based on user's balances across chains
41
- *
42
- * @param props.amount - Amount to transact
43
- * @param props.token - Token (a Token object)
44
- * @param props.address - Target address (optional, defaults to self)
45
- * @param props.transactionType - Type of transaction (optional, defaults to 'send')
46
- *
47
- * - type: The recommended path type ('native', 'sprinter', or 'anyspend')
48
- * - hasNativePath: Whether direct native token transfer is possible (1st best path)
49
- * - hasSprinterPath: Whether bridging via Sprinter is possible (2nd best path)
50
- * - availableBalance: The user's balance of this asset
51
- * - sprinterOptions: Available chains and balances for bridging
52
- * @returns TransactionPathResult object containing path information and loading state
53
- */
54
- export function useBestTransactionPath({
55
- amount,
56
- token,
57
- address,
58
- transactionType: _transactionType = "send",
59
- }: UseBestTransactionPathProps): TransactionPathResult {
60
- const account = useAccountWallet();
61
- const effectiveAddress = address || account?.address;
62
-
63
- const isSupportedSprinterToken = supportedSprinterTokenSymbols.includes(
64
- token.symbol.toUpperCase() as (typeof supportedSprinterTokenSymbols)[number],
65
- );
66
-
67
- // Get balances across all chains
68
- const { aggregatedBalances, aggregatedTokenBalances, loading: oneBalanceLoading } = useOneBalance();
69
-
70
- // Get the token balance using our new hook
71
- const { rawBalance, isLoading: tokenBalanceLoading } = useTokenBalance({
72
- token,
73
- address: effectiveAddress,
74
- });
75
-
76
- // Check if amount is available natively on destination chain
77
- const hasNativeBalance = useMemo(() => {
78
- if (!rawBalance || !amount) return false;
79
-
80
- return rawBalance >= amount;
81
- }, [amount, rawBalance]);
82
-
83
- // Calculate non-native token balance using the same logic as hasNativeBalance
84
- const nonNativeTokenBalance = useMemo(() => {
85
- if (isNativeToken(token.address)) return BigInt(0);
86
-
87
- // For sprinter supported tokens, use aggregated balances
88
- if (isSupportedSprinterToken && !oneBalanceLoading && aggregatedBalances) {
89
- const asset = aggregatedBalances.find(asset => asset.symbol === token.symbol);
90
- if (asset) {
91
- const chainBalance = asset.chainBalances.find(cb => cb.chainId === token.chainId);
92
- if (chainBalance) {
93
- return BigInt(chainBalance.balance);
94
- }
95
- }
96
- return BigInt(0);
97
- }
98
-
99
- // For non-sprinter tokens, use token balance from our new hook
100
- return rawBalance;
101
- }, [
102
- token.address,
103
- token.symbol,
104
- token.chainId,
105
- isSupportedSprinterToken,
106
- oneBalanceLoading,
107
- aggregatedBalances,
108
- rawBalance,
109
- ]);
110
-
111
- const totalCrossChainBalance = useMemo(() => {
112
- if (oneBalanceLoading || !aggregatedTokenBalances || !token.symbol) return 0;
113
- return aggregatedTokenBalances[token.symbol] || 0;
114
- }, [aggregatedTokenBalances, token.symbol, oneBalanceLoading]);
115
-
116
- // Calculate available sprinter options and determine if sprinter path is available
117
- const { hasSprinterPath, sprinterOptions } = useMemo(() => {
118
- if (!isSupportedSprinterToken || oneBalanceLoading || !aggregatedBalances || !token.symbol || !amount) {
119
- return { hasSprinterPath: false, sprinterOptions: [] };
120
- }
121
-
122
- const asset = aggregatedBalances.find(asset => asset.symbol === token.symbol);
123
- if (!asset) {
124
- return { hasSprinterPath: false, sprinterOptions: [] };
125
- }
126
-
127
- const options: SprinterOption[] = [];
128
-
129
- // Get all chains where user has sufficient balance
130
- for (const chainBalance of asset.chainBalances) {
131
- const balanceBi = BigInt(chainBalance.balance);
132
- if (balanceBi >= amount && chainBalance.chainId !== token.chainId) {
133
- options.push({
134
- chainId: chainBalance.chainId,
135
- balance: balanceBi,
136
- formattedBalance: formatUnits(balanceBi, chainBalance.tokenDecimals),
137
- });
138
- }
139
- }
140
-
141
- return {
142
- hasSprinterPath: options.length > 0,
143
- sprinterOptions: options,
144
- };
145
- }, [isSupportedSprinterToken, oneBalanceLoading, aggregatedBalances, token.symbol, token.chainId, amount]);
146
-
147
- // Determine the best path
148
- const path = useMemo(() => {
149
- const availableBalance = (isNativeToken(token.address) ? rawBalance : nonNativeTokenBalance) || BigInt(0);
150
-
151
- // Case 1: Native path if available
152
- if (hasNativeBalance) {
153
- return {
154
- type: "native" as const,
155
- hasNativePath: true,
156
- hasSprinterPath,
157
- availableBalance,
158
- totalCrossChainBalance,
159
- sprinterOptions,
160
- };
161
- }
162
-
163
- // Case 2: Sprinter path if available
164
- if (hasSprinterPath) {
165
- return {
166
- type: "sprinter" as const,
167
- hasNativePath: false,
168
- hasSprinterPath: true,
169
- availableBalance,
170
- totalCrossChainBalance,
171
- sprinterOptions,
172
- };
173
- }
174
-
175
- // Case 3: Fallback to anyspend
176
- return {
177
- type: "anyspend" as const,
178
- hasNativePath: false,
179
- hasSprinterPath: false,
180
- availableBalance,
181
- totalCrossChainBalance,
182
- sprinterOptions: [],
183
- };
184
- }, [
185
- token.address,
186
- rawBalance,
187
- nonNativeTokenBalance,
188
- hasNativeBalance,
189
- hasSprinterPath,
190
- totalCrossChainBalance,
191
- sprinterOptions,
192
- ]);
193
-
194
- // Combine all loading states
195
- const loading = oneBalanceLoading || tokenBalanceLoading;
196
-
197
- return {
198
- ...path,
199
- loading,
200
- };
201
- }