Skip to content

Commit a9e3693

Browse files
Preload wallet balances on pay embed
1 parent 1985de8 commit a9e3693

File tree

4 files changed

+91
-48
lines changed

4 files changed

+91
-48
lines changed

.changeset/sour-flies-post.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+
Preload wallet balances on pay embed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { SwapFlow } from "./swap/SwapFlow.js";
6363
import { SwapScreenContent } from "./swap/SwapScreenContent.js";
6464
import { TokenSelectorScreen } from "./swap/TokenSelectorScreen.js";
6565
import { TransferFlow } from "./swap/TransferFlow.js";
66+
import { useWalletsAndBalances } from "./swap/fetchBalancesForWallet.js";
6667
import {
6768
type SupportedChainAndTokens,
6869
useBuySupportedDestinations,
@@ -218,6 +219,16 @@ function BuyScreenContent(props: BuyScreenContentProps) {
218219
);
219220
}, [props.supportedTokens, supportedSourcesQuery.data, payOptions]);
220221

222+
// preload wallets and balances
223+
useWalletsAndBalances({
224+
client: props.client,
225+
sourceSupportedTokens: sourceSupportedTokens || [],
226+
toChain: toChain,
227+
toToken: toToken,
228+
tokenAmount: tokenAmount,
229+
mode: payOptions.mode,
230+
});
231+
221232
const { fromChain, setFromChain, fromToken, setFromToken } =
222233
useFromTokenSelectionStates({
223234
payOptions,

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

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ChevronRightIcon,
55
Cross2Icon,
66
} from "@radix-ui/react-icons";
7-
import { useQuery } from "@tanstack/react-query";
87
import { trackPayEvent } from "../../../../../../../analytics/track/pay.js";
98
import type { Chain } from "../../../../../../../chains/types.js";
109
import type { ThirdwebClient } from "../../../../../../../client/client.js";
@@ -43,14 +42,9 @@ import { FiatValue } from "./FiatValue.js";
4342
import { WalletRow } from "./WalletRow.js";
4443
import {
4544
type TokenBalance,
46-
fetchBalancesForWallet,
45+
useWalletsAndBalances,
4746
} from "./fetchBalancesForWallet.js";
4847

49-
type WalletKey = {
50-
id: WalletId;
51-
address: string;
52-
};
53-
5448
export function TokenSelectorScreen(props: {
5549
client: ThirdwebClient;
5650
sourceTokens: SupportedTokens | undefined;
@@ -71,46 +65,13 @@ export function TokenSelectorScreen(props: {
7165
const chainInfo = useChainMetadata(props.toChain);
7266
const theme = useCustomTheme();
7367

74-
const walletsAndBalances = useQuery({
75-
queryKey: [
76-
"wallets-and-balances",
77-
props.sourceSupportedTokens,
78-
props.toChain.id,
79-
props.toToken,
80-
props.tokenAmount,
81-
props.mode,
82-
activeAccount?.address,
83-
connectedWallets.map((w) => w.getAccount()?.address),
84-
],
85-
enabled: !!props.sourceSupportedTokens && !!chainInfo.data,
86-
queryFn: async () => {
87-
const entries = await Promise.all(
88-
connectedWallets.map(async (wallet) => {
89-
const balances = await fetchBalancesForWallet({
90-
wallet,
91-
accountAddress: activeAccount?.address,
92-
sourceSupportedTokens: props.sourceSupportedTokens || [],
93-
toChain: props.toChain,
94-
toToken: props.toToken,
95-
tokenAmount: props.tokenAmount,
96-
mode: props.mode,
97-
client: props.client,
98-
});
99-
return [
100-
{
101-
id: wallet.id,
102-
address: wallet.getAccount()?.address || "",
103-
} as WalletKey,
104-
balances,
105-
] as const;
106-
}),
107-
);
108-
const map = new Map<WalletKey, TokenBalance[]>();
109-
for (const entry of entries) {
110-
map.set(entry[0], entry[1]);
111-
}
112-
return map;
113-
},
68+
const walletsAndBalances = useWalletsAndBalances({
69+
client: props.client,
70+
sourceSupportedTokens: props.sourceSupportedTokens || [],
71+
toChain: props.toChain,
72+
toToken: props.toToken,
73+
tokenAmount: props.tokenAmount,
74+
mode: props.mode,
11475
});
11576

11677
if (
Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useQuery } from "@tanstack/react-query";
12
import type { Chain } from "../../../../../../../chains/types.js";
23
import { getCachedChain } from "../../../../../../../chains/utils.js";
34
import type { ThirdwebClient } from "../../../../../../../client/client.js";
@@ -9,7 +10,11 @@ import {
910
type GetWalletBalanceResult,
1011
getWalletBalance,
1112
} from "../../../../../../../wallets/utils/getWalletBalance.js";
13+
import type { WalletId } from "../../../../../../../wallets/wallet-types.js";
1214
import type { PayUIOptions } from "../../../../../../core/hooks/connection/ConnectButtonProps.js";
15+
import { useChainMetadata } from "../../../../../../core/hooks/others/useChainQuery.js";
16+
import { useActiveAccount } from "../../../../../../core/hooks/wallets/useActiveAccount.js";
17+
import { useConnectedWallets } from "../../../../../../core/hooks/wallets/useConnectedWallets.js";
1318
import type {
1419
SupportedTokens,
1520
TokenInfo,
@@ -43,7 +48,68 @@ export type TokenBalance = {
4348
token: TokenInfo;
4449
};
4550

46-
export async function fetchBalancesForWallet({
51+
type WalletKey = {
52+
id: WalletId;
53+
address: string;
54+
};
55+
56+
export function useWalletsAndBalances(props: {
57+
sourceSupportedTokens: SupportedTokens;
58+
toChain: Chain;
59+
toToken: ERC20OrNativeToken;
60+
tokenAmount: string;
61+
mode: PayUIOptions["mode"];
62+
client: ThirdwebClient;
63+
}) {
64+
const activeAccount = useActiveAccount();
65+
const connectedWallets = useConnectedWallets();
66+
const chainInfo = useChainMetadata(props.toChain);
67+
68+
return useQuery({
69+
queryKey: [
70+
"wallets-and-balances",
71+
props.sourceSupportedTokens,
72+
props.toChain.id,
73+
props.toToken,
74+
props.tokenAmount,
75+
props.mode,
76+
activeAccount?.address,
77+
connectedWallets.map((w) => w.getAccount()?.address),
78+
],
79+
enabled:
80+
!!props.sourceSupportedTokens && !!chainInfo.data && !!activeAccount,
81+
queryFn: async () => {
82+
const entries = await Promise.all(
83+
connectedWallets.map(async (wallet) => {
84+
const balances = await fetchBalancesForWallet({
85+
wallet,
86+
accountAddress: activeAccount?.address,
87+
sourceSupportedTokens: props.sourceSupportedTokens || [],
88+
toChain: props.toChain,
89+
toToken: props.toToken,
90+
tokenAmount: props.tokenAmount,
91+
mode: props.mode,
92+
client: props.client,
93+
});
94+
return [
95+
{
96+
id: wallet.id,
97+
address: wallet.getAccount()?.address || "",
98+
} as WalletKey,
99+
balances,
100+
] as const;
101+
}),
102+
);
103+
const map = new Map<WalletKey, TokenBalance[]>();
104+
for (const entry of entries) {
105+
map.set(entry[0], entry[1]);
106+
}
107+
return map;
108+
},
109+
});
110+
}
111+
112+
async function fetchBalancesForWallet({
47113
wallet,
48114
accountAddress,
49115
sourceSupportedTokens,

0 commit comments

Comments
 (0)