Skip to content

Commit e4769ef

Browse files
committed
add use watch program state changed hook
1 parent b007670 commit e4769ef

File tree

3 files changed

+8
-27
lines changed

3 files changed

+8
-27
lines changed

idea/vara-eth/frontend/src/features/programs/lib/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { useInitProgram } from './use-init-program';
33
export { useReadContractState } from './use-read-contract-state';
44
export { useSendProgramMessage } from './use-send-program-message';
55
export { useSendInjectedTransaction } from './use-send-injected-transaction';
6+
export { useWatchProgramStateChange } from './use-watch-program-state-change';

idea/vara-eth/frontend/src/features/programs/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export {
44
useReadContractState,
55
useSendProgramMessage,
66
useSendInjectedTransaction,
7+
useWatchProgramStateChange,
78
} from './hooks';
89
export { useGetAllProgramsQuery, useGetProgramByIdQuery } from './queries';

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { useAddMyActivity, TransactionTypes, unpackReceipt } from '@/app/store';
88
import LoadingSVG from '@/assets/icons/loading.svg?react';
99
import { Button } from '@/components';
1010

11+
import { useWatchProgramStateChange } from '../../lib';
12+
1113
import styles from './top-up-exec-balance.module.scss';
1214

1315
type Props = {
@@ -43,34 +45,9 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
4345
return tx.getReceipt();
4446
};
4547

46-
const watchFn = async (value: bigint) => {
47-
if (!api) throw new Error('API is not intialized');
48-
if (!mirrorContract) throw new Error('Mirror contract is not found');
49-
50-
const currentStateHash = await mirrorContract.stateHash();
51-
const currentState = await api.query.program.readState(currentStateHash);
52-
53-
return new Promise<void>((resolve, reject) => {
54-
const unwatch = mirrorContract.watchStateChangedEvent((stateHash) => {
55-
api.query.program
56-
.readState(stateHash)
57-
.then((state) => {
58-
if (BigInt(state.executableBalance - currentState.executableBalance) === value) {
59-
unwatch();
60-
resolve();
61-
}
62-
})
63-
.catch((error) => {
64-
unwatch();
65-
reject(error instanceof Error ? error : new Error(String(error)));
66-
});
67-
});
68-
});
69-
};
70-
7148
const approve = useMutation({ mutationFn: approveFn });
7249
const topUp = useMutation({ mutationFn: topUpFn });
73-
const watch = useMutation({ mutationFn: watchFn });
50+
const watch = useWatchProgramStateChange(programId);
7451

7552
const handleTopUpClick = async () => {
7653
const value = parseUnits('1', 12);
@@ -94,7 +71,9 @@ const TopUpExecBalance = ({ programId, onSuccess }: Props) => {
9471
...unpackReceipt(topUpReceipt),
9572
});
9673

97-
await watch.mutateAsync(value);
74+
await watch.mutateAsync(
75+
(state, incomingState) => BigInt(incomingState.executableBalance - state.executableBalance) === value,
76+
);
9877

9978
onSuccess();
10079
};

0 commit comments

Comments
 (0)