Skip to content

Commit 27f50a8

Browse files
committed
[BLD-143] Dashboard: Fix distribute funds in split contract (#7907)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving the `useSplitDistributeFunds` hook and the `DistributeButton` component by refining the distribution logic and enhancing error handling. ### Detailed summary - Replaced direct comparison of `currency.name` with a check using `getAddress(currency.tokenAddress)` against `getAddress(NATIVE_TOKEN_ADDRESS)`. - Added `await` for the promise in the `useSplitDistributeFunds` function. - Renamed the mutation variable in `DistributeButton` from `mutation` to `distributeFundsMutation`. - Updated the `distributeFunds` function to use `distributeFundsMutation.mutateAsync()`. - Adjusted the `isPending` prop in `TransactionButton` to reference `distributeFundsMutation.isPending`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Bug Fixes - Correctly detects and handles the native token during fund distribution, ensuring accurate routing and transaction counts. - Ensures each distribution transaction completes before the next begins, reducing partial or overlapping operations. - Refactor - Streamlined distribution flow and loading state handling for a more consistent experience. - Removed toast-based error pop-ups during distribution; status is reflected via the ongoing operation state. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 157f4b9 commit 27f50a8

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

apps/dashboard/src/@/hooks/useSplit.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { toast } from "sonner";
88
import {
99
type Chain,
10+
getAddress,
1011
NATIVE_TOKEN_ADDRESS,
1112
sendAndConfirmTransaction,
1213
type ThirdwebClient,
@@ -98,7 +99,8 @@ export function useSplitDistributeFunds(contract: ThirdwebContract) {
9899
.filter((token) => token.value !== 0n)
99100
.map(async (currency) => {
100101
const transaction =
101-
currency.name === "Native Token"
102+
getAddress(currency.tokenAddress) ===
103+
getAddress(NATIVE_TOKEN_ADDRESS)
102104
? distribute({ contract })
103105
: distributeByToken({
104106
contract,
@@ -108,6 +110,7 @@ export function useSplitDistributeFunds(contract: ThirdwebContract) {
108110
account,
109111
transaction,
110112
});
113+
111114
toast.promise(promise, {
112115
error: (err) => ({
113116
message: `Error distributing ${currency.name}`,
@@ -116,6 +119,8 @@ export function useSplitDistributeFunds(contract: ThirdwebContract) {
116119
loading: `Distributing ${currency.name}`,
117120
success: `Successfully distributed ${currency.name}`,
118121
});
122+
123+
await promise;
119124
});
120125

121126
return await Promise.all(distributions);

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/split/components/distribute-button.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import { SplitIcon } from "lucide-react";
44
import { useMemo } from "react";
5-
import { toast } from "sonner";
65
import type { ThirdwebContract } from "thirdweb";
76
import type { GetBalanceResult } from "thirdweb/extensions/erc20";
87
import { TransactionButton } from "@/components/tx-button";
98
import { Button } from "@/components/ui/button";
109
import { ToolTipLabel } from "@/components/ui/tooltip";
1110
import { useSplitDistributeFunds } from "@/hooks/useSplit";
12-
import { parseError } from "@/utils/errorParser";
1311

1412
export const DistributeButton = ({
1513
contract,
@@ -25,6 +23,7 @@ export const DistributeButton = ({
2523
isLoggedIn: boolean;
2624
}) => {
2725
const validBalances = balances.filter((item) => item.value !== 0n);
26+
2827
const numTransactions = useMemo(() => {
2928
if (
3029
validBalances.length === 1 &&
@@ -38,28 +37,18 @@ export const DistributeButton = ({
3837
return validBalances?.filter((b) => b.value !== 0n).length;
3938
}, [validBalances, balancesIsPending]);
4039

41-
const mutation = useSplitDistributeFunds(contract);
40+
const distributeFundsMutation = useSplitDistributeFunds(contract);
4241

4342
const distributeFunds = () => {
44-
mutation.mutate(undefined, {
45-
onError: (error) => {
46-
toast.error("Failed to process transaction", {
47-
description: parseError(error),
48-
});
49-
console.error(error);
50-
},
51-
onSuccess: () => {
52-
toast.success("Funds splitted successfully");
53-
},
54-
});
43+
distributeFundsMutation.mutateAsync();
5544
};
5645

5746
if (balancesIsError) {
5847
return (
5948
<TransactionButton
6049
client={contract.client}
6150
isLoggedIn={isLoggedIn}
62-
isPending={mutation.isPending}
51+
isPending={distributeFundsMutation.isPending}
6352
onClick={distributeFunds}
6453
size="sm"
6554
transactionCount={undefined}
@@ -86,7 +75,7 @@ export const DistributeButton = ({
8675
<TransactionButton
8776
client={contract.client}
8877
isLoggedIn={isLoggedIn}
89-
isPending={mutation.isPending}
78+
isPending={distributeFundsMutation.isPending}
9079
onClick={distributeFunds}
9180
transactionCount={numTransactions === 1 ? undefined : numTransactions}
9281
txChainID={contract.chain.id}

0 commit comments

Comments
 (0)