Skip to content

Commit 1e61a9e

Browse files
committed
[TOOL-3238] Fix Engine backend wallet balances not updating on chain change (#6059)
1 parent d03e687 commit 1e61a9e

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

apps/dashboard/src/@/components/blocks/NetworkSelectors.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export function SingleNetworkSelector(props: {
168168
onValueChange={(chainId) => {
169169
props.onChange(Number(chainId));
170170
}}
171+
closeOnSelect={true}
171172
placeholder={isLoadingChains ? "Loading Chains..." : "Select Chain"}
172173
overrideSearchFn={searchFn}
173174
renderOption={renderOption}

apps/dashboard/src/@/components/blocks/select-with-search.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface SelectWithSearchProps
3333
popoverContentClassName?: string;
3434
side?: "left" | "right" | "top" | "bottom";
3535
align?: "center" | "start" | "end";
36+
closeOnSelect?: boolean;
3637
}
3738

3839
export const SelectWithSearch = React.forwardRef<
@@ -50,6 +51,7 @@ export const SelectWithSearch = React.forwardRef<
5051
overrideSearchFn,
5152
popoverContentClassName,
5253
searchPlaceholder,
54+
closeOnSelect,
5355
...props
5456
},
5557
ref,
@@ -177,7 +179,12 @@ export const SelectWithSearch = React.forwardRef<
177179
key={option.value}
178180
role="option"
179181
aria-selected={isSelected}
180-
onClick={() => onValueChange(option.value)}
182+
onClick={() => {
183+
onValueChange(option.value);
184+
if (closeOnSelect) {
185+
setIsPopoverOpen(false);
186+
}
187+
}}
181188
variant="ghost"
182189
className="flex w-full cursor-pointer justify-start gap-3 rounded-sm px-3 py-2 text-left"
183190
ref={

apps/dashboard/src/@3rdweb-sdk/react/hooks/useEngine.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import type { ResultItem } from "app/team/[team_slug]/(team)/~/engine/(instance)/[engineId]/metrics/components/StatusCodes";
1111
import type { EngineBackendWalletType } from "lib/engine";
1212
import { useState } from "react";
13-
import { useActiveAccount, useActiveWalletChain } from "thirdweb/react";
13+
import { useActiveAccount } from "thirdweb/react";
1414
import invariant from "tiny-invariant";
1515
import type { EngineStatus } from "../../../app/team/[team_slug]/(team)/~/engine/(instance)/[engineId]/overview/components/transactions-table";
1616
import { engineKeys } from "../cache-keys";
@@ -469,12 +469,12 @@ export function useEngineBackendWalletBalance(params: {
469469
instanceUrl: string;
470470
address: string;
471471
authToken: string;
472+
chainId: number;
472473
}) {
473-
const { instanceUrl, address, authToken } = params;
474-
const chainId = useActiveWalletChain()?.id;
474+
const { instanceUrl, address, authToken, chainId } = params;
475475

476476
return useQuery({
477-
queryKey: engineKeys.backendWalletBalance(address, chainId || 1),
477+
queryKey: engineKeys.backendWalletBalance(address, chainId),
478478
queryFn: async () => {
479479
invariant(chainId, "chainId is required");
480480

apps/dashboard/src/app/team/[team_slug]/(team)/~/engine/(instance)/[engineId]/overview/components/backend-wallets-table.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Badge } from "@/components/ui/badge";
55
import { Button } from "@/components/ui/button";
66
import { Checkbox, CheckboxWithLabel } from "@/components/ui/checkbox";
77
import { FormItem } from "@/components/ui/form";
8+
import { Skeleton } from "@/components/ui/skeleton";
89
import {
910
type BackendWallet,
1011
useEngineBackendWalletBalance,
@@ -34,11 +35,9 @@ import { type ColumnDef, createColumnHelper } from "@tanstack/react-table";
3435
import { ChainIcon } from "components/icons/ChainIcon";
3536
import { TWTable } from "components/shared/TWTable";
3637
import { useTrack } from "hooks/analytics/useTrack";
38+
import { useAllChainsData } from "hooks/chains/allChains";
3739
import { EngineBackendWalletOptions } from "lib/engine";
38-
import {
39-
useActiveChainAsDashboardChain,
40-
useV5DashboardChain,
41-
} from "lib/v5-adapter";
40+
import { useV5DashboardChain } from "lib/v5-adapter";
4241
import {
4342
DownloadIcon,
4443
PencilIcon,
@@ -52,7 +51,6 @@ import { useForm } from "react-hook-form";
5251
import { toast } from "sonner";
5352
import { getAddress } from "thirdweb";
5453
import { shortenAddress } from "thirdweb/utils";
55-
import invariant from "tiny-invariant";
5654
import { FormHelperText, FormLabel, LinkButton, Text } from "tw-components";
5755
import { prettyPrintCurrency } from "./utils";
5856

@@ -89,21 +87,21 @@ const BackendWalletBalanceCell: React.FC<BackendWalletBalanceCellProps> = ({
8987
instanceUrl: instanceUrl,
9088
address,
9189
authToken,
90+
chainId,
9291
});
9392
const chain = useV5DashboardChain(chainId);
94-
if (!chain || !backendWalletBalance) {
95-
return;
93+
94+
if (!backendWalletBalance) {
95+
return <Skeleton className="h-5 w-32 rounded-lg" />;
9696
}
9797

9898
const balanceDisplay = prettyPrintCurrency({
9999
amount: backendWalletBalance.displayValue,
100-
symbol: backendWalletBalance.symbol,
100+
symbol: backendWalletBalance.symbol || chain.nativeCurrency?.symbol,
101101
});
102102

103103
const balanceComponent = (
104-
<Text fontWeight={backendWalletBalance.value === "0" ? "light" : "bold"}>
105-
{balanceDisplay}
106-
</Text>
104+
<div className="text-muted-foreground">{balanceDisplay}</div>
107105
);
108106

109107
const explorer = chain.blockExplorers?.[0];
@@ -250,6 +248,7 @@ export const BackendWalletsTable: React.FC<BackendWalletsTableProps> = ({
250248
disclosure={sendDisclosure}
251249
instanceUrl={instanceUrl}
252250
authToken={authToken}
251+
chainId={chainId}
253252
/>
254253
)}
255254
{selectedBackendWallet && deleteDisclosure.isOpen && (
@@ -425,36 +424,38 @@ const SendFundsModal = ({
425424
disclosure,
426425
instanceUrl,
427426
authToken,
427+
chainId,
428428
}: {
429429
fromWallet: BackendWallet;
430430
backendWallets: BackendWallet[];
431431
disclosure: UseDisclosureReturn;
432432
instanceUrl: string;
433433
authToken: string;
434+
chainId: number;
434435
}) => {
435-
const chain = useActiveChainAsDashboardChain();
436436
const form = useForm<SendFundsInput>();
437437
const sendTokens = useEngineSendTokens({
438438
instanceUrl,
439439
authToken,
440440
});
441+
const { idToChain } = useAllChainsData();
441442
const { data: backendWalletBalance } = useEngineBackendWalletBalance({
442443
instanceUrl,
443444
address: fromWallet.address,
444445
authToken,
446+
chainId: chainId,
445447
});
448+
const chain = idToChain.get(chainId);
446449
const toWalletDisclosure = useDisclosure();
447450

448451
if (!backendWalletBalance) {
449452
return null;
450453
}
451454

452455
const onSubmit = async (data: SendFundsInput) => {
453-
invariant(chain, "chain is required");
454-
455456
const promise = sendTokens.mutateAsync(
456457
{
457-
chainId: chain.chainId,
458+
chainId: chainId,
458459
fromAddress: fromWallet.address,
459460
toAddress: data.toAddress,
460461
amount: data.amount,

apps/dashboard/src/app/team/[team_slug]/(team)/~/engine/(instance)/[engineId]/overview/components/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export const prettyPrintCurrency = ({
33
symbol,
44
}: {
55
amount?: string | number;
6-
symbol: string;
6+
symbol?: string;
77
}): string => {
88
const amountNumber =
99
typeof amount === "string" ? Number.parseFloat(amount) : (amount ?? 0);
10-
return `${amountNumber.toFixed(6)} ${symbol}`;
10+
return `${amountNumber.toFixed(6)} ${symbol || ""}`;
1111
};

0 commit comments

Comments
 (0)