Skip to content

Commit 5dd0c91

Browse files
committed
catch and handle error in TokenClaimButton (#5064)
Fixes: DASH-324
1 parent f8f90fd commit 5dd0c91

File tree

1 file changed

+71
-60
lines changed

1 file changed

+71
-60
lines changed

apps/dashboard/src/contract-ui/tabs/tokens/components/claim-button.tsx

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -101,78 +101,89 @@ export const TokenClaimButton: React.FC<TokenClaimButtonProps> = ({
101101
colorScheme="primary"
102102
isDisabled={!form.formState.isDirty || isPending}
103103
onClick={form.handleSubmit(async (d) => {
104-
if (!d.to) {
105-
return toast.error(
106-
"Need to speficy an address to receive tokens",
107-
);
108-
}
109-
trackEvent({
110-
category: "token",
111-
action: "claim",
112-
label: "attempt",
113-
});
114-
if (!account) {
115-
return toast.error("No account detected");
116-
}
117-
const transaction = ERC20Ext.claimTo({
118-
contract,
119-
to: d.to,
120-
quantity: d.amount,
121-
from: account.address,
122-
});
104+
try {
105+
if (!d.to) {
106+
return toast.error(
107+
"Need to specify an address to receive tokens",
108+
);
109+
}
110+
trackEvent({
111+
category: "token",
112+
action: "claim",
113+
label: "attempt",
114+
});
115+
if (!account) {
116+
return toast.error("No account detected");
117+
}
118+
const transaction = ERC20Ext.claimTo({
119+
contract,
120+
to: d.to,
121+
quantity: d.amount,
122+
from: account.address,
123+
});
124+
125+
const approveTx = await ERC20Ext.getApprovalForTransaction({
126+
transaction,
127+
account,
128+
});
129+
130+
if (approveTx) {
131+
const promise = sendAndConfirmTransaction.mutateAsync(
132+
approveTx,
133+
{
134+
onError: (error) => {
135+
console.error(error);
136+
},
137+
},
138+
);
139+
toast.promise(promise, {
140+
loading: "Approving ERC20 tokens for this claim",
141+
success: "Tokens approved successfully",
142+
error: "Failed to approve token",
143+
});
123144

124-
const approveTx = await ERC20Ext.getApprovalForTransaction({
125-
transaction,
126-
account,
127-
});
145+
await promise;
146+
}
128147

129-
if (approveTx) {
130148
const promise = sendAndConfirmTransaction.mutateAsync(
131-
approveTx,
149+
transaction,
132150
{
151+
onSuccess: () => {
152+
trackEvent({
153+
category: "token",
154+
action: "claim",
155+
label: "success",
156+
});
157+
form.reset({ amount: "0", to: account?.address });
158+
setOpen(false);
159+
},
133160
onError: (error) => {
161+
trackEvent({
162+
category: "token",
163+
action: "claim",
164+
label: "error",
165+
error,
166+
});
134167
console.error(error);
135168
},
136169
},
137170
);
171+
138172
toast.promise(promise, {
139-
loading: "Approving ERC20 tokens for this claim",
140-
success: "Tokens approved successfully",
141-
error: "Failed to approve token",
173+
loading: "Claiming tokens",
174+
success: "Token claimed successfully",
175+
error: "Failed to claim tokens",
176+
});
177+
} catch (error) {
178+
console.error(error);
179+
toast.error("Failed to claim tokens");
180+
trackEvent({
181+
category: "token",
182+
action: "claim",
183+
label: "error",
184+
error,
142185
});
143-
144-
await promise;
145186
}
146-
147-
const promise = sendAndConfirmTransaction.mutateAsync(
148-
transaction,
149-
{
150-
onSuccess: () => {
151-
trackEvent({
152-
category: "token",
153-
action: "claim",
154-
label: "success",
155-
});
156-
form.reset({ amount: "0", to: account?.address });
157-
setOpen(false);
158-
},
159-
onError: (error) => {
160-
trackEvent({
161-
category: "token",
162-
action: "claim",
163-
label: "error",
164-
error,
165-
});
166-
console.error(error);
167-
},
168-
},
169-
);
170-
171-
toast.promise(promise, {
172-
loading: "Claiming tokens",
173-
success: "Token claimed successfully",
174-
error: "Failed to claim tokens",
175-
});
176187
})}
177188
>
178189
Claim Tokens

0 commit comments

Comments
 (0)