Skip to content

Commit 78f700a

Browse files
committed
add watch program state change timeout
1 parent e4769ef commit 78f700a

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,55 @@ import { HexString, ProgramState } from '@vara-eth/api';
44
import { useMirrorContract } from '@/app/api';
55
import { useVaraEthApi } from '@/app/providers';
66

7+
const UNWATCH_TIMEOUT_MS = 60000;
8+
9+
type Params = {
10+
name: string;
11+
isChanged: (currentState: ProgramState, incomingState: ProgramState) => boolean;
12+
};
13+
714
const useWatchProgramStateChange = (programId: HexString) => {
815
const { api } = useVaraEthApi();
916
const { data: mirrorContract } = useMirrorContract(programId);
1017

11-
const watchFn = async (isChanged: (currentState: ProgramState, incomingState: ProgramState) => boolean) => {
18+
const watch = async ({ name, isChanged }: Params) => {
1219
if (!api) throw new Error('API is not intialized');
1320
if (!mirrorContract) throw new Error('Mirror contract is not found');
1421

1522
const currentStateHash = await mirrorContract.stateHash();
1623
const currentState = await api.query.program.readState(currentStateHash);
1724

1825
return new Promise<void>((resolve, reject) => {
19-
const unwatch = mirrorContract.watchStateChangedEvent((stateHash) => {
26+
let unwatch = () => {};
27+
28+
const timeoutId = setTimeout(() => {
29+
unwatch();
30+
reject(new Error(`No ${name} changes detected`));
31+
}, UNWATCH_TIMEOUT_MS);
32+
33+
const cleanup = () => {
34+
clearTimeout(timeoutId);
35+
unwatch();
36+
};
37+
38+
unwatch = mirrorContract.watchStateChangedEvent((stateHash) => {
2039
api.query.program
2140
.readState(stateHash)
2241
.then((state) => {
2342
if (isChanged(currentState, state)) {
24-
unwatch();
43+
cleanup();
2544
resolve();
2645
}
2746
})
2847
.catch((error) => {
29-
unwatch();
48+
cleanup();
3049
reject(error instanceof Error ? error : new Error(String(error)));
3150
});
3251
});
3352
});
3453
};
3554

36-
return useMutation({ mutationFn: watchFn });
55+
return useMutation({ mutationFn: watch });
3756
};
3857

3958
export { useWatchProgramStateChange };

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
2929

3030
const tx = await ethClient.wvara.approve(programId, value);
3131

32-
await tx.send();
33-
34-
return tx.getReceipt();
32+
return tx.sendAndWaitForReceipt();
3533
};
3634

3735
const topUpFn = async (value: bigint) => {
@@ -40,9 +38,7 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
4038

4139
const tx = await mirrorContract.executableBalanceTopUp(value);
4240

43-
await tx.send();
44-
45-
return tx.getReceipt();
41+
return tx.sendAndWaitForReceipt();
4642
};
4743

4844
const approve = useMutation({ mutationFn: approveFn });
@@ -54,7 +50,7 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
5450

5551
const approveReceipt = await approve.mutateAsync(value);
5652

57-
addMyActivity({
53+
await addMyActivity({
5854
type: TransactionTypes.approve,
5955
value: value.toString(),
6056
owner: approveReceipt.from,
@@ -64,16 +60,17 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
6460

6561
const topUpReceipt = await topUp.mutateAsync(value);
6662

67-
addMyActivity({
63+
await addMyActivity({
6864
type: TransactionTypes.executableBalanceTopUp,
6965
value: String(value),
7066
programId,
7167
...unpackReceipt(topUpReceipt),
7268
});
7369

74-
await watch.mutateAsync(
75-
(state, incomingState) => BigInt(incomingState.executableBalance - state.executableBalance) === value,
76-
);
70+
await watch.mutateAsync({
71+
name: 'executable program balance',
72+
isChanged: (current, incoming) => BigInt(incoming.executableBalance - current.executableBalance) === value,
73+
});
7774

7875
onSuccess();
7976
};

0 commit comments

Comments
 (0)