Skip to content

Commit 04961f0

Browse files
feat: change getWalletBalance prop from account to address (#2576)
1 parent 37f8433 commit 04961f0

File tree

16 files changed

+112
-47
lines changed

16 files changed

+112
-47
lines changed

.changeset/khaki-maps-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Change getWalletBalance prop from account to address

packages/thirdweb/src/analytics/track.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@ const ANALYTICS_ENDPOINT = "https://c.thirdweb.com/event";
66
/**
77
* @internal
88
*/
9-
export function track(client: ThirdwebClient, data: object) {
9+
export function trackConnect(args: {
10+
client: ThirdwebClient;
11+
walletType: string;
12+
walletAddress: string;
13+
}) {
14+
const { client, walletType, walletAddress } = args;
15+
track(client, {
16+
source: "connectWallet",
17+
action: "connect",
18+
walletType,
19+
walletAddress,
20+
});
21+
}
22+
23+
/**
24+
* @internal
25+
*/
26+
function track(client: ThirdwebClient, data: object) {
1027
const fetch = getClientFetch(client);
1128

1229
fetch(ANALYTICS_ENDPOINT, {

packages/thirdweb/src/react/core/hooks/others/useWalletBalance.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import { useWalletConnectionCtx } from "./useWalletConnectionCtx.js";
1414
export function useWalletBalance(
1515
options: Omit<Partial<GetWalletBalanceOptions>, "client">,
1616
) {
17-
const { chain, account, tokenAddress } = options;
17+
const { chain, address, tokenAddress } = options;
1818
const { client } = useWalletConnectionCtx();
1919
const query = queryOptions({
2020
// eslint-disable-next-line @tanstack/query/exhaustive-deps
2121
queryKey: [
2222
"walletBalance",
2323
chain?.id || -1,
24-
account?.address || "0x0",
24+
address || "0x0",
2525
{ tokenAddress },
2626
] as const,
2727
queryFn: async () => {
@@ -31,12 +31,12 @@ export function useWalletBalance(
3131
if (!client) {
3232
throw new Error("client is required");
3333
}
34-
if (!account) {
35-
throw new Error("account is required");
34+
if (!address) {
35+
throw new Error("address is required");
3636
}
37-
return getWalletBalance({ chain, client, account, tokenAddress });
37+
return getWalletBalance({ chain, client, address, tokenAddress });
3838
},
39-
enabled: !!chain && !!client && !!account,
39+
enabled: !!chain && !!client && !!address,
4040
});
4141
return useQuery(query);
4242
}

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const ConnectedWalletDetails: React.FC<{
121121
const balanceQuery = useWalletBalance({
122122
chain: walletChain ? walletChain : undefined,
123123
tokenAddress,
124-
account: activeAccount,
124+
address: activeAccount?.address,
125125
});
126126

127127
const [screen, setScreen] = useState<WalletDetailsModalScreen>("main");

packages/thirdweb/src/react/web/ui/ConnectWallet/Modal/InjectedConnectUI.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Wallet } from "../../../../../wallets/interfaces/wallet.js";
66
import type { InjectedSupportedWalletIds } from "../../../../../wallets/__generated__/wallet-ids.js";
77
import { useWalletConnectionCtx } from "../../../../core/hooks/others/useWalletConnectionCtx.js";
88
import type { WalletInfo } from "../../../../../wallets/wallet-info.js";
9-
import { asyncLocalStorage } from "../../../../core/utils/asyncLocalStorage.js";
109

1110
/**
1211
* @internal
@@ -20,7 +19,7 @@ export const InjectedConnectUI = (props: {
2019
done: () => void;
2120
}) => {
2221
const { wallet, done } = props;
23-
const { chain } = useWalletConnectionCtx();
22+
const { client, chain } = useWalletConnectionCtx();
2423
const [errorConnecting, setErrorConnecting] = useState(false);
2524
const locale = props.locale;
2625

@@ -29,9 +28,8 @@ export const InjectedConnectUI = (props: {
2928
connectPrompted.current = true;
3029
setErrorConnecting(false);
3130
await wait(1000);
32-
// set storage for the wallet if it supports it
33-
wallet.setStorage?.(asyncLocalStorage);
3431
await wallet.connect({
32+
client,
3533
chain: chain,
3634
});
3735

@@ -40,7 +38,7 @@ export const InjectedConnectUI = (props: {
4038
setErrorConnecting(true);
4139
console.error(e);
4240
}
43-
}, [chain, done, wallet]);
41+
}, [client, chain, done, wallet]);
4442

4543
const connectPrompted = useRef(false);
4644
useEffect(() => {

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/SwapScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function SwapScreenContent(props: {
121121
const deferredTokenAmount = useDebouncedValue(tokenAmount, 300);
122122

123123
const fromTokenBalanceQuery = useWalletBalance({
124-
account: account,
124+
address: account.address,
125125
chain: fromChain,
126126
tokenAddress: isNativeToken(fromToken) ? undefined : fromToken.address,
127127
});

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/PayWithCrypto.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function PayWithCrypto(props: {
4141
const activeAccount = useActiveAccount();
4242

4343
const balanceQuery = useWalletBalance({
44-
account: activeAccount,
44+
address: activeAccount?.address,
4545
chain: props.chain,
4646
tokenAddress: isNativeToken(props.token) ? undefined : props.token.address,
4747
});

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/SendFunds.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function SendFundsForm(props: {
116116
const balanceQuery = useWalletBalance({
117117
chain,
118118
tokenAddress: tokenAddress,
119-
account: activeAccount,
119+
address: activeAccount?.address,
120120
});
121121

122122
const { receiverAddress, setReceiverAddress, amount, setAmount } = props;

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/TokenSelector.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function TokenSelector(props: {
3939
// if input is undefined, it loads the native token
4040
// otherwise it loads the token with given address
4141
const tokenQuery = useWalletBalance({
42-
account: activeAccount,
42+
address: activeAccount?.address,
4343
chain: chain,
4444
tokenAddress: input,
4545
});
@@ -171,7 +171,7 @@ function SelectTokenButton(props: {
171171
}) {
172172
const account = useActiveAccount();
173173
const tokenBalanceQuery = useWalletBalance({
174-
account,
174+
address: account?.address,
175175
chain: props.chain,
176176
tokenAddress: isNativeToken(props.token) ? undefined : props.token.address,
177177
});

packages/thirdweb/src/react/web/ui/hooks/useTrack.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/thirdweb/src/react/web/wallets/shared/CoinbaseSDKConnection.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ function CoinbaseSDKWalletConnectUI(props: {
2020
}) {
2121
const { onBack, done, wallet, walletInfo, onGetStarted, locale } = props;
2222
const [qrCodeUri, setQrCodeUri] = useState<string | undefined>();
23-
const { chain } = useWalletConnectionCtx();
23+
const { client, chain } = useWalletConnectionCtx();
2424

2525
const connect = useCallback(() => {
2626
wallet
2727
.connect({
28+
client,
2829
chain,
2930
onUri: (uri) => {
3031
setQrCodeUri(uri);
@@ -38,7 +39,7 @@ function CoinbaseSDKWalletConnectUI(props: {
3839
.catch((e) => {
3940
console.error(e);
4041
});
41-
}, [wallet, chain, done]);
42+
}, [client, wallet, chain, done]);
4243

4344
const scanStarted = useRef(false);
4445
useEffect(() => {

packages/thirdweb/src/wallets/coinbase/coinbaseSDKWallet.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ import {
2929
import { getAddress } from "../../utils/address.js";
3030
import { getDefaultAppMetadata } from "../utils/defaultDappMetadata.js";
3131
import type { WalletEmitter } from "../wallet-emitter.js";
32+
import type { ThirdwebClient } from "../../client/client.js";
3233

3334
/**
3435
* Options for connecting to the CoinbaseSDK Wallet
3536
*/
3637
export type CoinbaseSDKWalletConnectionOptions = {
38+
/**
39+
* The Thirdweb client object
40+
*/
41+
client: ThirdwebClient;
42+
3743
/**
3844
* Whether to use Dark theme in the Coinbase Wallet "Onboarding Overlay" popup.
3945
*

packages/thirdweb/src/wallets/create-wallet.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { injectedProvider } from "./injected/mipdStore.js";
99
import type { InjectedSupportedWalletIds } from "./__generated__/wallet-ids.js";
1010

1111
import { createWalletEmitter } from "./wallet-emitter.js";
12+
import { trackConnect } from "../analytics/track.js";
1213

1314
// TODO: figure out how to define the type without tuple args type and using function overloads
1415

@@ -113,6 +114,11 @@ export function createWallet<const ID extends WalletId>(
113114
chain = connectedChain;
114115
handleDisconnect = doDisconnect;
115116
handleSwitchChain = doSwitchChain;
117+
trackConnect({
118+
client: options.client,
119+
walletType: id,
120+
walletAddress: account.address,
121+
});
116122
// return account
117123
return account;
118124
}
@@ -131,6 +137,11 @@ export function createWallet<const ID extends WalletId>(
131137
chain = connectedChain;
132138
handleDisconnect = doDisconnect;
133139
handleSwitchChain = doSwitchChain;
140+
trackConnect({
141+
client: options.client,
142+
walletType: id,
143+
walletAddress: account.address,
144+
});
134145
// return account
135146
return account;
136147
}
@@ -152,6 +163,11 @@ export function createWallet<const ID extends WalletId>(
152163
chain = connectedChain;
153164
handleDisconnect = doDisconnect;
154165
handleSwitchChain = doSwitchChain;
166+
trackConnect({
167+
client: options.client,
168+
walletType: id,
169+
walletAddress: account.address,
170+
});
155171
// return account
156172
return account;
157173
}
@@ -175,6 +191,11 @@ export function createWallet<const ID extends WalletId>(
175191
chain = connectedChain;
176192
handleDisconnect = doDisconnect;
177193
handleSwitchChain = doSwitchChain;
194+
trackConnect({
195+
client: options.client,
196+
walletType: id,
197+
walletAddress: account.address,
198+
});
178199
// return account
179200
return account;
180201
}
@@ -233,6 +254,11 @@ export function smartWallet(
233254
// set the states
234255
account = connectedAccount;
235256
chain = connectedChain;
257+
trackConnect({
258+
client: options.client,
259+
walletType: "smart",
260+
walletAddress: account.address,
261+
});
236262
// return account
237263
return account;
238264
},
@@ -246,6 +272,11 @@ export function smartWallet(
246272
// set the states
247273
account = connectedAccount;
248274
chain = connectedChain;
275+
trackConnect({
276+
client: options.client,
277+
walletType: "smart",
278+
walletAddress: account.address,
279+
});
249280
// return account
250281
return account;
251282
},
@@ -304,6 +335,11 @@ export function embeddedWallet(
304335
// set the states
305336
account = connectedAccount;
306337
chain = connectedChain;
338+
trackConnect({
339+
client: options.client,
340+
walletType: "embedded",
341+
walletAddress: account.address,
342+
});
307343
// return only the account
308344
return account;
309345
},
@@ -317,6 +353,11 @@ export function embeddedWallet(
317353
// set the states
318354
account = connectedAccount;
319355
chain = connectedChain;
356+
trackConnect({
357+
client: options.client,
358+
walletType: "embedded",
359+
walletAddress: account.address,
360+
});
320361
// return only the account
321362
return account;
322363
},
@@ -383,6 +424,11 @@ function coinbaseWalletSDK(): Wallet<"com.coinbase.wallet"> {
383424
chain = connectedChain;
384425
handleDisconnect = doDisconnect;
385426
handleSwitchChain = doSwitchChain;
427+
trackConnect({
428+
client: options.client,
429+
walletType: "com.coinbase.wallet",
430+
walletAddress: account.address,
431+
});
386432
// return account
387433
return account;
388434
},
@@ -398,6 +444,11 @@ function coinbaseWalletSDK(): Wallet<"com.coinbase.wallet"> {
398444
chain = connectedChain;
399445
handleDisconnect = doDisconnect;
400446
handleSwitchChain = doSwitchChain;
447+
trackConnect({
448+
client: options.client,
449+
walletType: "com.coinbase.wallet",
450+
walletAddress: account.address,
451+
});
401452
// return account
402453
return account;
403454
},

packages/thirdweb/src/wallets/interfaces/wallet.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ export type Wallet<TWalletId extends WalletId = WalletId> = {
3636
disconnect(): Promise<void>;
3737
// chain management methods
3838
switchChain(chain: Chain): Promise<void>;
39-
// storage management (temporary and "internal")
40-
setStorage?(storage: any): void;
4139
// events
4240
subscribe: WalletEmitter<TWalletId>["subscribe"];
4341

packages/thirdweb/src/wallets/utils/getWalletBalance.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import { getContract } from "../../contract/contract.js";
99
import { eth_getBalance } from "../../rpc/actions/eth_getBalance.js";
1010
import { getRpcClient } from "../../rpc/rpc.js";
1111
import { toTokens } from "../../utils/units.js";
12-
import type { Account } from "../interfaces/wallet.js";
1312

1413
export type GetWalletBalanceOptions = {
15-
account: Pick<Account, "address">;
14+
address: string;
1615
client: ThirdwebClient;
1716
chain: Chain;
1817
/**
@@ -47,7 +46,7 @@ type GetWalletBalanceResult = {
4746
export async function getWalletBalance(
4847
options: GetWalletBalanceOptions,
4948
): Promise<GetWalletBalanceResult> {
50-
const { account, client, chain, tokenAddress } = options;
49+
const { address, client, chain, tokenAddress } = options;
5150
// erc20 case
5251
if (tokenAddress) {
5352
// load balanceOf dynamically to avoid circular dependency
@@ -56,7 +55,7 @@ export async function getWalletBalance(
5655
);
5756
return getBalance({
5857
contract: getContract({ client, chain, address: tokenAddress }),
59-
address: account.address,
58+
address,
6059
});
6160
}
6261
// native token case
@@ -67,7 +66,7 @@ export async function getWalletBalance(
6766
getChainSymbol(chain),
6867
getChainDecimals(chain),
6968
getChainNativeCurrencyName(chain),
70-
eth_getBalance(rpcRequest, { address: account.address }),
69+
eth_getBalance(rpcRequest, { address }),
7170
]);
7271

7372
return {

0 commit comments

Comments
 (0)