Skip to content

Commit 08975ee

Browse files
committed
fix typos and improve error handling
1 parent 3645717 commit 08975ee

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

idea/vara-eth/frontend/src/features/programs/lib/hooks/use-watch-program-state-change.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ const useWatchProgramStateChange = (programId: HexString) => {
1919
const cleanupRef = useRef(() => {});
2020

2121
const watch = async ({ name, isChanged }: Params) => {
22-
if (!api) throw new Error('API is not intialized');
22+
if (!api) throw new Error('API is not initialized');
2323
if (!mirrorContract) throw new Error('Mirror contract is not found');
2424

25+
// mutation not intended to be used simultaneously or by multiple watchers per one hook,
26+
// clean up just in case
27+
cleanupRef.current();
28+
2529
const currentStateHash = await mirrorContract.stateHash();
2630
const currentState = await api.query.program.readState(currentStateHash);
2731

idea/vara-eth/frontend/src/features/programs/ui/top-up-exec-balance/top-up-exec-balance.tsx

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
2828
};
2929

3030
const topUpFn = async (value: bigint) => {
31-
if (!api) throw new Error('API is not intialized');
31+
if (!api) throw new Error('API is not initialized');
3232
if (!mirrorContract) throw new Error('Mirror contract is not found');
3333

3434
const tx = await mirrorContract.executableBalanceTopUp(value);
@@ -39,29 +39,31 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
3939
const approve = useMutation({ mutationFn: approveFn });
4040
const topUp = useMutation({ mutationFn: topUpFn });
4141

42-
const handleTopUpClick = async () => {
43-
const value = parseUnits('1', 12);
44-
45-
const approveReceipt = await approve.mutateAsync(value);
46-
47-
await addMyActivity({
48-
type: TransactionTypes.approve,
49-
value: value.toString(),
50-
owner: approveReceipt.from,
51-
spender: programId,
52-
...unpackReceipt(approveReceipt),
53-
});
54-
55-
const topUpReceipt = await topUp.mutateAsync(value);
56-
57-
await addMyActivity({
58-
type: TransactionTypes.executableBalanceTopUp,
59-
value: String(value),
60-
programId,
61-
...unpackReceipt(topUpReceipt),
62-
});
63-
64-
onSuccess(value);
42+
const handleClick = () => {
43+
const value = parseUnits('10', 12);
44+
45+
approve
46+
.mutateAsync(value)
47+
.then((receipt) =>
48+
addMyActivity({
49+
type: TransactionTypes.approve,
50+
value: value.toString(),
51+
owner: receipt.from,
52+
spender: programId,
53+
...unpackReceipt(receipt),
54+
}),
55+
)
56+
.then(() => topUp.mutateAsync(value))
57+
.then((receipt) =>
58+
addMyActivity({
59+
type: TransactionTypes.executableBalanceTopUp,
60+
value: String(value),
61+
programId,
62+
...unpackReceipt(receipt),
63+
}),
64+
)
65+
.then(() => onSuccess(value))
66+
.catch((error) => console.error(error));
6567
};
6668

6769
const isLoading = !api || !ethClient || !mirrorContract || approve.isPending || topUp.isPending;
@@ -74,7 +76,7 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
7476
};
7577

7678
return (
77-
<Button size="xs" onClick={handleTopUpClick} loadingPosition="start" isLoading={isLoading} variant="secondary">
79+
<Button size="xs" onClick={handleClick} loadingPosition="start" isLoading={isLoading} variant="secondary">
7880
{getButtonText()}
7981
</Button>
8082
);

idea/vara-eth/frontend/src/pages/program/program.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
gap: 8px;
2424
}
2525

26+
// TODO: spinner component?
2627
@keyframes spin {
2728
to {
2829
transform: rotate(360deg);

idea/vara-eth/frontend/src/pages/program/program.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const Program = () => {
5858
watchBalance
5959
.mutateAsync({
6060
name: 'executable program balance',
61-
isChanged: (current, incoming) => BigInt(incoming.executableBalance - current.executableBalance) === value,
61+
isChanged: (current, incoming) =>
62+
BigInt(incoming.executableBalance) - BigInt(current.executableBalance) === value,
6263
})
6364
.then(() => refetch())
6465
.catch((error) => console.error(error));

0 commit comments

Comments
 (0)