@b3dotfun/sdk 0.0.50 → 0.0.51-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,9 +44,18 @@ class BondkitToken {
44
44
  this.contract = (0, viem_1.getContract)({
45
45
  address: this.contractAddress,
46
46
  abi: abis_1.BondkitTokenABI,
47
- client: this.walletClientInstance,
47
+ client: {
48
+ public: this.publicClient,
49
+ wallet: this.walletClientInstance,
50
+ },
48
51
  });
49
- this.contract.read.tradingToken().then(tradingToken => {
52
+ (0, viem_1.getContract)({
53
+ address: this.contractAddress,
54
+ abi: abis_1.BondkitTokenABI,
55
+ client: this.publicClient, // Use public client for read operations
56
+ })
57
+ .read.tradingToken()
58
+ .then(tradingToken => {
50
59
  this.tradingToken = tradingToken;
51
60
  });
52
61
  }
@@ -66,7 +75,10 @@ class BondkitToken {
66
75
  this.contract = (0, viem_1.getContract)({
67
76
  address: this.contractAddress,
68
77
  abi: abis_1.BondkitTokenABI,
69
- client: this.walletClientInstance,
78
+ client: {
79
+ public: this.publicClient,
80
+ wallet: this.walletClientInstance,
81
+ },
70
82
  });
71
83
  return true;
72
84
  }
@@ -113,7 +125,10 @@ class BondkitToken {
113
125
  this.contract = (0, viem_1.getContract)({
114
126
  address: this.contractAddress,
115
127
  abi: abis_1.BondkitTokenABI,
116
- client: this.walletClientInstance,
128
+ client: {
129
+ public: this.publicClient,
130
+ wallet: this.walletClientInstance,
131
+ },
117
132
  });
118
133
  return true;
119
134
  }
@@ -426,7 +441,10 @@ class BondkitToken {
426
441
  this.contract = (0, viem_1.getContract)({
427
442
  address: this.contractAddress,
428
443
  abi: abis_1.BondkitTokenABI,
429
- client: this.walletClientInstance,
444
+ client: {
445
+ public: this.publicClient,
446
+ wallet: this.walletClientInstance,
447
+ },
430
448
  });
431
449
  }
432
450
  }
@@ -436,6 +454,19 @@ class BondkitToken {
436
454
  throw new Error("Wallet key not set or client not connected for write operation.");
437
455
  }
438
456
  }
457
+ // Only attempt chain switching for browser wallet providers
458
+ // Private key users cannot switch chains programmatically
459
+ if (this.connectedProvider && this.walletClientInstance.account) {
460
+ const walletChainId = await this.walletClientInstance.getChainId();
461
+ if (walletChainId !== this.chain.id) {
462
+ try {
463
+ await this.walletClientInstance.switchChain({ id: this.chain.id });
464
+ }
465
+ catch (switchErr) {
466
+ throw new Error(`Please switch your wallet to ${this.chain.name} (${this.chain.id}).`);
467
+ }
468
+ }
469
+ }
439
470
  const accountToUse = this.walletKey ? (0, accounts_1.privateKeyToAccount)(this.walletKey) : this.walletClientInstance.account;
440
471
  if (!accountToUse)
441
472
  throw new Error("Account for transaction could not be determined.");
@@ -530,7 +561,10 @@ class BondkitToken {
530
561
  const tradingTokenContract = (0, viem_1.getContract)({
531
562
  address: this.tradingToken,
532
563
  abi: viem_1.erc20Abi,
533
- client: this.walletClientInstance,
564
+ client: {
565
+ public: this.publicClient,
566
+ wallet: this.walletClientInstance,
567
+ },
534
568
  });
535
569
  const currentAllowance = await tradingTokenContract.read.allowance([
536
570
  this.walletClientInstance.account?.address,
@@ -31,7 +31,10 @@ class BondkitTokenFactory {
31
31
  this.contract = (0, viem_1.getContract)({
32
32
  address: this.contractAddress,
33
33
  abi: abis_1.BondkitTokenFactoryABI,
34
- client: this.walletClientInstance,
34
+ client: {
35
+ public: this.publicClient,
36
+ wallet: this.walletClientInstance,
37
+ },
35
38
  });
36
39
  }
37
40
  connect(provider) {
@@ -46,7 +49,10 @@ class BondkitTokenFactory {
46
49
  this.contract = (0, viem_1.getContract)({
47
50
  address: this.contractAddress,
48
51
  abi: abis_1.BondkitTokenFactoryABI,
49
- client: this.walletClientInstance,
52
+ client: {
53
+ public: this.publicClient,
54
+ wallet: this.walletClientInstance,
55
+ },
50
56
  });
51
57
  this.publicClient = (0, viem_1.createPublicClient)({
52
58
  chain: this.chain,
@@ -95,7 +101,10 @@ class BondkitTokenFactory {
95
101
  this.contract = (0, viem_1.getContract)({
96
102
  address: this.contractAddress,
97
103
  abi: abis_1.BondkitTokenFactoryABI,
98
- client: this.walletClientInstance,
104
+ client: {
105
+ public: this.publicClient,
106
+ wallet: this.walletClientInstance,
107
+ },
99
108
  });
100
109
  return true;
101
110
  }
@@ -126,7 +135,10 @@ class BondkitTokenFactory {
126
135
  this.contract = (0, viem_1.getContract)({
127
136
  address: this.contractAddress,
128
137
  abi: abis_1.BondkitTokenFactoryABI,
129
- client: this.walletClientInstance,
138
+ client: {
139
+ public: this.publicClient,
140
+ wallet: this.walletClientInstance,
141
+ },
130
142
  });
131
143
  }
132
144
  }
@@ -164,7 +176,6 @@ class BondkitTokenFactory {
164
176
  account: accountToUse,
165
177
  chain: this.chain,
166
178
  });
167
- console.log("hash", hash);
168
179
  const receipt = await this.publicClient.waitForTransactionReceipt({ hash });
169
180
  for (const log of receipt.logs) {
170
181
  try {
@@ -16,6 +16,7 @@ const react_3 = require("thirdweb/react");
16
16
  const wallets_1 = require("thirdweb/wallets");
17
17
  const in_app_1 = require("thirdweb/wallets/in-app");
18
18
  const wagmi_1 = require("wagmi");
19
+ const useTWAuth_1 = require("./useTWAuth");
19
20
  const useUserQuery_1 = require("./useUserQuery");
20
21
  const useWagmiConfig_1 = require("./useWagmiConfig");
21
22
  const debug = (0, debug_1.debugB3React)("useAuthentication");
@@ -33,7 +34,7 @@ function useAuthentication(partnerId) {
33
34
  const setHasStartedConnecting = (0, react_1.useAuthStore)(state => state.setHasStartedConnecting);
34
35
  const setActiveWallet = (0, react_3.useSetActiveWallet)();
35
36
  const hasStartedConnecting = (0, react_1.useAuthStore)(state => state.hasStartedConnecting);
36
- const { authenticate } = (0, react_1.useSiwe)();
37
+ const { authenticate } = (0, useTWAuth_1.useTWAuth)();
37
38
  const { user, setUser } = (0, useUserQuery_1.useUserQuery)();
38
39
  const useAutoConnectLoadingPrevious = (0, react_2.useRef)(false);
39
40
  const wagmiConfig = (0, useWagmiConfig_1.useWagmiConfig)(partnerId);
@@ -106,10 +107,10 @@ function useAuthentication(partnerId) {
106
107
  }, [wallets, syncWagmi]);
107
108
  const authenticateUser = (0, react_2.useCallback)(async (wallet) => {
108
109
  setHasStartedConnecting(true);
109
- const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
110
- if (!account) {
111
- throw new Error("No account found during auto-connect");
110
+ if (!wallet) {
111
+ throw new Error("No wallet found during auto-connect");
112
112
  }
113
+ const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
113
114
  if (!account) {
114
115
  throw new Error("No account found during auto-connect");
115
116
  }
@@ -127,7 +128,7 @@ function useAuthentication(partnerId) {
127
128
  catch (error) {
128
129
  // If re-authentication fails, try fresh authentication
129
130
  debug("Re-authentication failed, attempting fresh authentication");
130
- const userAuth = await authenticate(account, partnerId);
131
+ const userAuth = await authenticate(wallet, partnerId);
131
132
  setUser(userAuth.user);
132
133
  setIsAuthenticated(true);
133
134
  setIsAuthenticating(false);
@@ -171,7 +172,7 @@ function useAuthentication(partnerId) {
171
172
  setIsConnected,
172
173
  setUser,
173
174
  ]);
174
- const logout = async (callback) => {
175
+ const logout = (0, react_2.useCallback)(async (callback) => {
175
176
  if (activeWallet) {
176
177
  debug("@@logout:activeWallet", activeWallet);
177
178
  disconnect(activeWallet);
@@ -188,6 +189,7 @@ function useAuthentication(partnerId) {
188
189
  localStorage.removeItem("thirdweb:connected-wallet-ids");
189
190
  localStorage.removeItem("wagmi.store");
190
191
  localStorage.removeItem("lastAuthProvider");
192
+ localStorage.removeItem("b3-user");
191
193
  }
192
194
  app_1.default.logout();
193
195
  debug("@@logout:loggedOut");
@@ -195,7 +197,7 @@ function useAuthentication(partnerId) {
195
197
  setIsConnected(false);
196
198
  setUser();
197
199
  callback?.();
198
- };
200
+ }, [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected]);
199
201
  const { isLoading: useAutoConnectLoading } = (0, react_3.useAutoConnect)({
200
202
  client: thirdweb_1.client,
201
203
  wallets: [wallet],
@@ -11,6 +11,7 @@ const useSearchParamsSSR_1 = require("./useSearchParamsSSR");
11
11
  function useSiwe() {
12
12
  const referralCode = (0, useSearchParamsSSR_1.useSearchParam)("referralCode");
13
13
  const authenticate = (0, react_1.useCallback)(async (account, partnerId) => {
14
+ console.warn("@@useSiwe is deprecated, use useTWAuth instead");
14
15
  if (!account || !account.signMessage)
15
16
  throw new Error("Account not found");
16
17
  console.log("@@useAuthenticate:referralCode", referralCode);
@@ -0,0 +1,4 @@
1
+ import { Wallet } from "thirdweb/wallets";
2
+ export declare function useTWAuth(): {
3
+ authenticate: (wallet: Wallet, partnerId: string) => Promise<import("@feathersjs/authentication").AuthenticationResult>;
4
+ };
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useTWAuth = useTWAuth;
7
+ const app_1 = __importDefault(require("../../../global-account/app"));
8
+ const debug_1 = __importDefault(require("../../../shared/utils/debug"));
9
+ const react_1 = require("react");
10
+ const useSearchParamsSSR_1 = require("./useSearchParamsSSR");
11
+ function useTWAuth() {
12
+ const referralCode = (0, useSearchParamsSSR_1.useSearchParam)("referralCode");
13
+ const authenticate = (0, react_1.useCallback)(async (wallet, partnerId) => {
14
+ if (!wallet || !wallet?.getAuthToken?.())
15
+ throw new Error("Wallet not found");
16
+ const authToken = wallet.getAuthToken();
17
+ (0, debug_1.default)("@@useTWSignIn:authToken", authToken);
18
+ (0, debug_1.default)("@@useTWSignIn:referralCode", referralCode);
19
+ // authenticate
20
+ const response = await app_1.default.authenticate({
21
+ strategy: "thirdweb-jwt",
22
+ accessToken: authToken,
23
+ // http://localhost:5173/?referralCode=GIO2
24
+ referralCode,
25
+ partnerId: partnerId,
26
+ });
27
+ (0, debug_1.default)("@@useTWSignIn:response", response);
28
+ return response;
29
+ }, [referralCode]);
30
+ return {
31
+ authenticate,
32
+ };
33
+ }
@@ -41,9 +41,18 @@ export class BondkitToken {
41
41
  this.contract = getContract({
42
42
  address: this.contractAddress,
43
43
  abi: BondkitTokenABI,
44
- client: this.walletClientInstance,
44
+ client: {
45
+ public: this.publicClient,
46
+ wallet: this.walletClientInstance,
47
+ },
45
48
  });
46
- this.contract.read.tradingToken().then(tradingToken => {
49
+ getContract({
50
+ address: this.contractAddress,
51
+ abi: BondkitTokenABI,
52
+ client: this.publicClient, // Use public client for read operations
53
+ })
54
+ .read.tradingToken()
55
+ .then(tradingToken => {
47
56
  this.tradingToken = tradingToken;
48
57
  });
49
58
  }
@@ -63,7 +72,10 @@ export class BondkitToken {
63
72
  this.contract = getContract({
64
73
  address: this.contractAddress,
65
74
  abi: BondkitTokenABI,
66
- client: this.walletClientInstance,
75
+ client: {
76
+ public: this.publicClient,
77
+ wallet: this.walletClientInstance,
78
+ },
67
79
  });
68
80
  return true;
69
81
  }
@@ -110,7 +122,10 @@ export class BondkitToken {
110
122
  this.contract = getContract({
111
123
  address: this.contractAddress,
112
124
  abi: BondkitTokenABI,
113
- client: this.walletClientInstance,
125
+ client: {
126
+ public: this.publicClient,
127
+ wallet: this.walletClientInstance,
128
+ },
114
129
  });
115
130
  return true;
116
131
  }
@@ -423,7 +438,10 @@ export class BondkitToken {
423
438
  this.contract = getContract({
424
439
  address: this.contractAddress,
425
440
  abi: BondkitTokenABI,
426
- client: this.walletClientInstance,
441
+ client: {
442
+ public: this.publicClient,
443
+ wallet: this.walletClientInstance,
444
+ },
427
445
  });
428
446
  }
429
447
  }
@@ -433,6 +451,19 @@ export class BondkitToken {
433
451
  throw new Error("Wallet key not set or client not connected for write operation.");
434
452
  }
435
453
  }
454
+ // Only attempt chain switching for browser wallet providers
455
+ // Private key users cannot switch chains programmatically
456
+ if (this.connectedProvider && this.walletClientInstance.account) {
457
+ const walletChainId = await this.walletClientInstance.getChainId();
458
+ if (walletChainId !== this.chain.id) {
459
+ try {
460
+ await this.walletClientInstance.switchChain({ id: this.chain.id });
461
+ }
462
+ catch (switchErr) {
463
+ throw new Error(`Please switch your wallet to ${this.chain.name} (${this.chain.id}).`);
464
+ }
465
+ }
466
+ }
436
467
  const accountToUse = this.walletKey ? privateKeyToAccount(this.walletKey) : this.walletClientInstance.account;
437
468
  if (!accountToUse)
438
469
  throw new Error("Account for transaction could not be determined.");
@@ -527,7 +558,10 @@ export class BondkitToken {
527
558
  const tradingTokenContract = getContract({
528
559
  address: this.tradingToken,
529
560
  abi: erc20Abi,
530
- client: this.walletClientInstance,
561
+ client: {
562
+ public: this.publicClient,
563
+ wallet: this.walletClientInstance,
564
+ },
531
565
  });
532
566
  const currentAllowance = await tradingTokenContract.read.allowance([
533
567
  this.walletClientInstance.account?.address,
@@ -28,7 +28,10 @@ export class BondkitTokenFactory {
28
28
  this.contract = getContract({
29
29
  address: this.contractAddress,
30
30
  abi: BondkitTokenFactoryABI,
31
- client: this.walletClientInstance,
31
+ client: {
32
+ public: this.publicClient,
33
+ wallet: this.walletClientInstance,
34
+ },
32
35
  });
33
36
  }
34
37
  connect(provider) {
@@ -43,7 +46,10 @@ export class BondkitTokenFactory {
43
46
  this.contract = getContract({
44
47
  address: this.contractAddress,
45
48
  abi: BondkitTokenFactoryABI,
46
- client: this.walletClientInstance,
49
+ client: {
50
+ public: this.publicClient,
51
+ wallet: this.walletClientInstance,
52
+ },
47
53
  });
48
54
  this.publicClient = createPublicClient({
49
55
  chain: this.chain,
@@ -92,7 +98,10 @@ export class BondkitTokenFactory {
92
98
  this.contract = getContract({
93
99
  address: this.contractAddress,
94
100
  abi: BondkitTokenFactoryABI,
95
- client: this.walletClientInstance,
101
+ client: {
102
+ public: this.publicClient,
103
+ wallet: this.walletClientInstance,
104
+ },
96
105
  });
97
106
  return true;
98
107
  }
@@ -123,7 +132,10 @@ export class BondkitTokenFactory {
123
132
  this.contract = getContract({
124
133
  address: this.contractAddress,
125
134
  abi: BondkitTokenFactoryABI,
126
- client: this.walletClientInstance,
135
+ client: {
136
+ public: this.publicClient,
137
+ wallet: this.walletClientInstance,
138
+ },
127
139
  });
128
140
  }
129
141
  }
@@ -161,7 +173,6 @@ export class BondkitTokenFactory {
161
173
  account: accountToUse,
162
174
  chain: this.chain,
163
175
  });
164
- console.log("hash", hash);
165
176
  const receipt = await this.publicClient.waitForTransactionReceipt({ hash });
166
177
  for (const log of receipt.logs) {
167
178
  try {
@@ -1,6 +1,6 @@
1
1
  import app from "../../../global-account/app.js";
2
2
  import { authenticateWithB3JWT } from "../../../global-account/bsmnt.js";
3
- import { useAuthStore, useSiwe } from "../../../global-account/react/index.js";
3
+ import { useAuthStore } from "../../../global-account/react/index.js";
4
4
  import { ecosystemWalletId } from "../../../shared/constants/index.js";
5
5
  import { debugB3React } from "../../../shared/utils/debug.js";
6
6
  import { client } from "../../../shared/utils/thirdweb.js";
@@ -10,6 +10,7 @@ import { useActiveWallet, useAutoConnect, useConnectedWallets, useDisconnect, us
10
10
  import { ecosystemWallet } from "thirdweb/wallets";
11
11
  import { preAuthenticate } from "thirdweb/wallets/in-app";
12
12
  import { useAccount, useConnect, useSwitchAccount } from "wagmi";
13
+ import { useTWAuth } from "./useTWAuth.js";
13
14
  import { useUserQuery } from "./useUserQuery.js";
14
15
  import { useWagmiConfig } from "./useWagmiConfig.js";
15
16
  const debug = debugB3React("useAuthentication");
@@ -27,7 +28,7 @@ export function useAuthentication(partnerId) {
27
28
  const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
28
29
  const setActiveWallet = useSetActiveWallet();
29
30
  const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
30
- const { authenticate } = useSiwe();
31
+ const { authenticate } = useTWAuth();
31
32
  const { user, setUser } = useUserQuery();
32
33
  const useAutoConnectLoadingPrevious = useRef(false);
33
34
  const wagmiConfig = useWagmiConfig(partnerId);
@@ -100,10 +101,10 @@ export function useAuthentication(partnerId) {
100
101
  }, [wallets, syncWagmi]);
101
102
  const authenticateUser = useCallback(async (wallet) => {
102
103
  setHasStartedConnecting(true);
103
- const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
104
- if (!account) {
105
- throw new Error("No account found during auto-connect");
104
+ if (!wallet) {
105
+ throw new Error("No wallet found during auto-connect");
106
106
  }
107
+ const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
107
108
  if (!account) {
108
109
  throw new Error("No account found during auto-connect");
109
110
  }
@@ -121,7 +122,7 @@ export function useAuthentication(partnerId) {
121
122
  catch (error) {
122
123
  // If re-authentication fails, try fresh authentication
123
124
  debug("Re-authentication failed, attempting fresh authentication");
124
- const userAuth = await authenticate(account, partnerId);
125
+ const userAuth = await authenticate(wallet, partnerId);
125
126
  setUser(userAuth.user);
126
127
  setIsAuthenticated(true);
127
128
  setIsAuthenticating(false);
@@ -165,7 +166,7 @@ export function useAuthentication(partnerId) {
165
166
  setIsConnected,
166
167
  setUser,
167
168
  ]);
168
- const logout = async (callback) => {
169
+ const logout = useCallback(async (callback) => {
169
170
  if (activeWallet) {
170
171
  debug("@@logout:activeWallet", activeWallet);
171
172
  disconnect(activeWallet);
@@ -182,6 +183,7 @@ export function useAuthentication(partnerId) {
182
183
  localStorage.removeItem("thirdweb:connected-wallet-ids");
183
184
  localStorage.removeItem("wagmi.store");
184
185
  localStorage.removeItem("lastAuthProvider");
186
+ localStorage.removeItem("b3-user");
185
187
  }
186
188
  app.logout();
187
189
  debug("@@logout:loggedOut");
@@ -189,7 +191,7 @@ export function useAuthentication(partnerId) {
189
191
  setIsConnected(false);
190
192
  setUser();
191
193
  callback?.();
192
- };
194
+ }, [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected]);
193
195
  const { isLoading: useAutoConnectLoading } = useAutoConnect({
194
196
  client,
195
197
  wallets: [wallet],
@@ -5,6 +5,7 @@ import { useSearchParam } from "./useSearchParamsSSR.js";
5
5
  export function useSiwe() {
6
6
  const referralCode = useSearchParam("referralCode");
7
7
  const authenticate = useCallback(async (account, partnerId) => {
8
+ console.warn("@@useSiwe is deprecated, use useTWAuth instead");
8
9
  if (!account || !account.signMessage)
9
10
  throw new Error("Account not found");
10
11
  console.log("@@useAuthenticate:referralCode", referralCode);
@@ -0,0 +1,4 @@
1
+ import { Wallet } from "thirdweb/wallets";
2
+ export declare function useTWAuth(): {
3
+ authenticate: (wallet: Wallet, partnerId: string) => Promise<import("@feathersjs/authentication").AuthenticationResult>;
4
+ };
@@ -0,0 +1,27 @@
1
+ import app from "../../../global-account/app.js";
2
+ import debug from "../../../shared/utils/debug.js";
3
+ import { useCallback } from "react";
4
+ import { useSearchParam } from "./useSearchParamsSSR.js";
5
+ export function useTWAuth() {
6
+ const referralCode = useSearchParam("referralCode");
7
+ const authenticate = useCallback(async (wallet, partnerId) => {
8
+ if (!wallet || !wallet?.getAuthToken?.())
9
+ throw new Error("Wallet not found");
10
+ const authToken = wallet.getAuthToken();
11
+ debug("@@useTWSignIn:authToken", authToken);
12
+ debug("@@useTWSignIn:referralCode", referralCode);
13
+ // authenticate
14
+ const response = await app.authenticate({
15
+ strategy: "thirdweb-jwt",
16
+ accessToken: authToken,
17
+ // http://localhost:5173/?referralCode=GIO2
18
+ referralCode,
19
+ partnerId: partnerId,
20
+ });
21
+ debug("@@useTWSignIn:response", response);
22
+ return response;
23
+ }, [referralCode]);
24
+ return {
25
+ authenticate,
26
+ };
27
+ }
@@ -0,0 +1,4 @@
1
+ import { Wallet } from "thirdweb/wallets";
2
+ export declare function useTWAuth(): {
3
+ authenticate: (wallet: Wallet, partnerId: string) => Promise<import("@feathersjs/authentication").AuthenticationResult>;
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b3dotfun/sdk",
3
- "version": "0.0.50",
3
+ "version": "0.0.51-alpha.1",
4
4
  "source": "src/index.ts",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "react-native": "./dist/cjs/index.native.js",
@@ -91,12 +91,21 @@ export class BondkitToken {
91
91
  this.contract = getContract({
92
92
  address: this.contractAddress,
93
93
  abi: BondkitTokenABI,
94
- client: this.walletClientInstance,
94
+ client: {
95
+ public: this.publicClient,
96
+ wallet: this.walletClientInstance,
97
+ },
95
98
  });
96
99
 
97
- this.contract.read.tradingToken().then(tradingToken => {
98
- this.tradingToken = tradingToken as Address;
99
- });
100
+ getContract({
101
+ address: this.contractAddress,
102
+ abi: BondkitTokenABI,
103
+ client: this.publicClient, // Use public client for read operations
104
+ })
105
+ .read.tradingToken()
106
+ .then(tradingToken => {
107
+ this.tradingToken = tradingToken as Address;
108
+ });
100
109
  }
101
110
 
102
111
  public connect(provider?: EIP1193Provider): boolean {
@@ -119,7 +128,10 @@ export class BondkitToken {
119
128
  this.contract = getContract({
120
129
  address: this.contractAddress,
121
130
  abi: BondkitTokenABI,
122
- client: this.walletClientInstance,
131
+ client: {
132
+ public: this.publicClient,
133
+ wallet: this.walletClientInstance,
134
+ },
123
135
  });
124
136
  return true;
125
137
  } catch (error) {
@@ -167,7 +179,10 @@ export class BondkitToken {
167
179
  this.contract = getContract({
168
180
  address: this.contractAddress,
169
181
  abi: BondkitTokenABI,
170
- client: this.walletClientInstance,
182
+ client: {
183
+ public: this.publicClient,
184
+ wallet: this.walletClientInstance,
185
+ },
171
186
  });
172
187
 
173
188
  return true;
@@ -505,7 +520,10 @@ export class BondkitToken {
505
520
  this.contract = getContract({
506
521
  address: this.contractAddress,
507
522
  abi: BondkitTokenABI,
508
- client: this.walletClientInstance,
523
+ client: {
524
+ public: this.publicClient,
525
+ wallet: this.walletClientInstance,
526
+ },
509
527
  });
510
528
  }
511
529
  } catch (_) {}
@@ -514,6 +532,19 @@ export class BondkitToken {
514
532
  throw new Error("Wallet key not set or client not connected for write operation.");
515
533
  }
516
534
  }
535
+
536
+ // Only attempt chain switching for browser wallet providers
537
+ // Private key users cannot switch chains programmatically
538
+ if (this.connectedProvider && this.walletClientInstance.account) {
539
+ const walletChainId = await this.walletClientInstance.getChainId();
540
+ if (walletChainId !== this.chain.id) {
541
+ try {
542
+ await this.walletClientInstance.switchChain({ id: this.chain.id });
543
+ } catch (switchErr) {
544
+ throw new Error(`Please switch your wallet to ${this.chain.name} (${this.chain.id}).`);
545
+ }
546
+ }
547
+ }
517
548
  const accountToUse = this.walletKey ? privateKeyToAccount(this.walletKey) : this.walletClientInstance.account;
518
549
  if (!accountToUse) throw new Error("Account for transaction could not be determined.");
519
550
 
@@ -624,7 +655,10 @@ export class BondkitToken {
624
655
  const tradingTokenContract = getContract({
625
656
  address: this.tradingToken as Address,
626
657
  abi: erc20Abi,
627
- client: this.walletClientInstance,
658
+ client: {
659
+ public: this.publicClient,
660
+ wallet: this.walletClientInstance,
661
+ },
628
662
  });
629
663
 
630
664
  const currentAllowance = await tradingTokenContract.read.allowance([
@@ -56,7 +56,10 @@ export class BondkitTokenFactory {
56
56
  this.contract = getContract({
57
57
  address: this.contractAddress,
58
58
  abi: BondkitTokenFactoryABI,
59
- client: this.walletClientInstance,
59
+ client: {
60
+ public: this.publicClient,
61
+ wallet: this.walletClientInstance,
62
+ },
60
63
  });
61
64
  }
62
65
 
@@ -75,7 +78,10 @@ export class BondkitTokenFactory {
75
78
  this.contract = getContract({
76
79
  address: this.contractAddress,
77
80
  abi: BondkitTokenFactoryABI,
78
- client: this.walletClientInstance,
81
+ client: {
82
+ public: this.publicClient,
83
+ wallet: this.walletClientInstance,
84
+ },
79
85
  });
80
86
 
81
87
  this.publicClient = createPublicClient({
@@ -127,7 +133,10 @@ export class BondkitTokenFactory {
127
133
  this.contract = getContract({
128
134
  address: this.contractAddress,
129
135
  abi: BondkitTokenFactoryABI,
130
- client: this.walletClientInstance,
136
+ client: {
137
+ public: this.publicClient,
138
+ wallet: this.walletClientInstance,
139
+ },
131
140
  });
132
141
 
133
142
  return true;
@@ -157,7 +166,10 @@ export class BondkitTokenFactory {
157
166
  this.contract = getContract({
158
167
  address: this.contractAddress,
159
168
  abi: BondkitTokenFactoryABI,
160
- client: this.walletClientInstance,
169
+ client: {
170
+ public: this.publicClient,
171
+ wallet: this.walletClientInstance,
172
+ },
161
173
  });
162
174
  }
163
175
  } catch (_) {}
@@ -201,7 +213,6 @@ export class BondkitTokenFactory {
201
213
  chain: this.chain,
202
214
  });
203
215
 
204
- console.log("hash", hash);
205
216
  const receipt: TransactionReceipt = await this.publicClient.waitForTransactionReceipt({ hash });
206
217
 
207
218
  for (const log of receipt.logs) {
@@ -1,6 +1,6 @@
1
1
  import app from "@b3dotfun/sdk/global-account/app";
2
2
  import { authenticateWithB3JWT } from "@b3dotfun/sdk/global-account/bsmnt";
3
- import { useAuthStore, useSiwe } from "@b3dotfun/sdk/global-account/react";
3
+ import { useAuthStore } from "@b3dotfun/sdk/global-account/react";
4
4
  import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
5
5
  import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
6
6
  import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
@@ -17,6 +17,7 @@ import {
17
17
  import { Wallet, ecosystemWallet } from "thirdweb/wallets";
18
18
  import { preAuthenticate } from "thirdweb/wallets/in-app";
19
19
  import { useAccount, useConnect, useSwitchAccount } from "wagmi";
20
+ import { useTWAuth } from "./useTWAuth";
20
21
  import { useUserQuery } from "./useUserQuery";
21
22
  import { useWagmiConfig } from "./useWagmiConfig";
22
23
 
@@ -36,7 +37,7 @@ export function useAuthentication(partnerId: string) {
36
37
  const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
37
38
  const setActiveWallet = useSetActiveWallet();
38
39
  const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
39
- const { authenticate } = useSiwe();
40
+ const { authenticate } = useTWAuth();
40
41
  const { user, setUser } = useUserQuery();
41
42
  const useAutoConnectLoadingPrevious = useRef(false);
42
43
  const wagmiConfig = useWagmiConfig(partnerId);
@@ -119,10 +120,11 @@ export function useAuthentication(partnerId: string) {
119
120
  async (wallet?: Wallet) => {
120
121
  setHasStartedConnecting(true);
121
122
 
122
- const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
123
- if (!account) {
124
- throw new Error("No account found during auto-connect");
123
+ if (!wallet) {
124
+ throw new Error("No wallet found during auto-connect");
125
125
  }
126
+
127
+ const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
126
128
  if (!account) {
127
129
  throw new Error("No account found during auto-connect");
128
130
  }
@@ -141,7 +143,7 @@ export function useAuthentication(partnerId: string) {
141
143
  } catch (error) {
142
144
  // If re-authentication fails, try fresh authentication
143
145
  debug("Re-authentication failed, attempting fresh authentication");
144
- const userAuth = await authenticate(account, partnerId);
146
+ const userAuth = await authenticate(wallet, partnerId);
145
147
  setUser(userAuth.user);
146
148
  setIsAuthenticated(true);
147
149
  setIsAuthenticating(false);
@@ -193,35 +195,39 @@ export function useAuthentication(partnerId: string) {
193
195
  ],
194
196
  );
195
197
 
196
- const logout = async (callback?: () => void) => {
197
- if (activeWallet) {
198
- debug("@@logout:activeWallet", activeWallet);
199
- disconnect(activeWallet);
200
- debug("@@logout:activeWallet", activeWallet);
201
- }
198
+ const logout = useCallback(
199
+ async (callback?: () => void) => {
200
+ if (activeWallet) {
201
+ debug("@@logout:activeWallet", activeWallet);
202
+ disconnect(activeWallet);
203
+ debug("@@logout:activeWallet", activeWallet);
204
+ }
202
205
 
203
- // Log out of each wallet
204
- wallets.forEach(wallet => {
205
- console.log("@@logging out", wallet);
206
- disconnect(wallet);
207
- });
206
+ // Log out of each wallet
207
+ wallets.forEach(wallet => {
208
+ console.log("@@logging out", wallet);
209
+ disconnect(wallet);
210
+ });
208
211
 
209
- // Delete localStorage thirdweb:connected-wallet-ids
210
- // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
211
- if (typeof localStorage !== "undefined") {
212
- localStorage.removeItem("thirdweb:connected-wallet-ids");
213
- localStorage.removeItem("wagmi.store");
214
- localStorage.removeItem("lastAuthProvider");
215
- }
212
+ // Delete localStorage thirdweb:connected-wallet-ids
213
+ // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
214
+ if (typeof localStorage !== "undefined") {
215
+ localStorage.removeItem("thirdweb:connected-wallet-ids");
216
+ localStorage.removeItem("wagmi.store");
217
+ localStorage.removeItem("lastAuthProvider");
218
+ localStorage.removeItem("b3-user");
219
+ }
216
220
 
217
- app.logout();
218
- debug("@@logout:loggedOut");
221
+ app.logout();
222
+ debug("@@logout:loggedOut");
219
223
 
220
- setIsAuthenticated(false);
221
- setIsConnected(false);
222
- setUser();
223
- callback?.();
224
- };
224
+ setIsAuthenticated(false);
225
+ setIsConnected(false);
226
+ setUser();
227
+ callback?.();
228
+ },
229
+ [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected],
230
+ );
225
231
 
226
232
  const { isLoading: useAutoConnectLoading } = useAutoConnect({
227
233
  client,
@@ -9,6 +9,7 @@ export function useSiwe() {
9
9
 
10
10
  const authenticate = useCallback(
11
11
  async (account: Account, partnerId: string) => {
12
+ console.warn("@@useSiwe is deprecated, use useTWAuth instead");
12
13
  if (!account || !account.signMessage) throw new Error("Account not found");
13
14
 
14
15
  console.log("@@useAuthenticate:referralCode", referralCode);
@@ -0,0 +1,36 @@
1
+ import app from "@b3dotfun/sdk/global-account/app";
2
+ import debug from "@b3dotfun/sdk/shared/utils/debug";
3
+ import { useCallback } from "react";
4
+ import { Wallet } from "thirdweb/wallets";
5
+ import { useSearchParam } from "./useSearchParamsSSR";
6
+
7
+ export function useTWAuth() {
8
+ const referralCode = useSearchParam("referralCode");
9
+
10
+ const authenticate = useCallback(
11
+ async (wallet: Wallet, partnerId: string) => {
12
+ if (!wallet || !wallet?.getAuthToken?.()) throw new Error("Wallet not found");
13
+
14
+ const authToken = wallet.getAuthToken();
15
+ debug("@@useTWSignIn:authToken", authToken);
16
+ debug("@@useTWSignIn:referralCode", referralCode);
17
+
18
+ // authenticate
19
+ const response = await app.authenticate({
20
+ strategy: "thirdweb-jwt",
21
+ accessToken: authToken,
22
+ // http://localhost:5173/?referralCode=GIO2
23
+ referralCode,
24
+ partnerId: partnerId,
25
+ });
26
+ debug("@@useTWSignIn:response", response);
27
+
28
+ return response;
29
+ },
30
+ [referralCode],
31
+ );
32
+
33
+ return {
34
+ authenticate,
35
+ };
36
+ }