Skip to content

Commit 7bf1ac9

Browse files
tractorssjaybuidl
authored andcommitted
refactor(web): update-wrap-with-toast-function
1 parent 6993b98 commit 7bf1ac9

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

web/src/pages/Resolver/NavigationButtons/SubmitDisputeButton.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { prepareArbitratorExtradata } from "utils/prepareArbitratorExtradata";
1111
import { usePublicClient } from "wagmi";
1212
import Popup, { PopupType } from "components/Popup";
1313
import DisputeIcon from "assets/svgs/icons/dispute.svg";
14-
import { DecodeEventLogParameters, decodeEventLog, parseAbi } from "viem";
14+
import { Log, decodeEventLog, parseAbi } from "viem";
1515
import { EnsureChain } from "components/EnsureChain";
16+
import { isUndefined } from "utils/index";
1617

1718
const StyledButton = styled(Button)``;
1819

@@ -56,14 +57,13 @@ const SubmitDisputeButton: React.FC = () => {
5657
setIsSubmittingCase(true);
5758
wrapWithToast(async () => await submitCase().then((response) => response.hash), publicClient)
5859
.then((res) => {
59-
if (res.status === "success") {
60-
const id = retrieveDisputeId(res.logs[1]);
60+
if (res.status && !isUndefined(res.result)) {
61+
const id = retrieveDisputeId(res.result.logs[1]);
6162
setDisputeId(Number(id));
6263
setCourtId(disputeData.courtId ?? "1");
6364
setIsPopupOpen(true);
65+
resetDisputeData();
6466
}
65-
66-
resetDisputeData();
6767
})
6868
.finally(() => {
6969
setIsSubmittingCase(false);
@@ -96,7 +96,7 @@ const isTemplateValid = (disputeTemplate: IDisputeTemplate) => {
9696
areVotingOptionsFilled) as boolean;
9797
};
9898

99-
const retrieveDisputeId = (eventLog: DecodeEventLogParameters) =>
99+
const retrieveDisputeId = (eventLog: Log) =>
100100
decodeEventLog({
101101
abi: parseAbi(["event DisputeCreation(uint256 indexed, address indexed)"]),
102102
data: eventLog.data,

web/src/utils/wrapWithToast.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ export const OPTIONS = {
1212
theme: "colored" as Theme,
1313
};
1414

15-
export async function wrapWithToast(contractWrite: () => Promise<`0x${string}`>, publicClient: any) {
15+
type WrapWithToastReturnType = {
16+
status: boolean;
17+
result?: TransactionReceipt;
18+
};
19+
20+
export async function wrapWithToast(
21+
contractWrite: () => Promise<`0x${string}`>,
22+
publicClient: any
23+
): Promise<WrapWithToastReturnType> {
1624
toast.info("Transaction initiated", OPTIONS);
17-
const hash = await contractWrite();
18-
return await publicClient
19-
.waitForTransactionReceipt({ hash, confirmations: 2 })
20-
.then((response: TransactionReceipt) => {
21-
toast.success("Transaction mined!", OPTIONS);
22-
return response;
23-
})
25+
return await contractWrite()
26+
.then(
27+
async (hash) =>
28+
await publicClient.waitForTransactionReceipt({ hash, confirmations: 2 }).then((res: TransactionReceipt) => {
29+
const status = res.status === "success";
30+
31+
if (status) toast.success("Transaction mined!", OPTIONS);
32+
else toast.error("Transaction reverted!", OPTIONS);
33+
34+
return { status, result: res };
35+
})
36+
)
2437
.catch((error) => {
25-
toast.error(error.message, OPTIONS);
38+
toast.error(error.shortMessage ?? error.message, OPTIONS);
39+
return { status: false };
2640
});
2741
}

0 commit comments

Comments
 (0)