@@ -8,6 +8,8 @@ import { useAddMyActivity, TransactionTypes, unpackReceipt } from '@/app/store';
88import LoadingSVG from '@/assets/icons/loading.svg?react' ;
99import { Button } from '@/components' ;
1010
11+ import { useWatchProgramStateChange } from '../../lib' ;
12+
1113import styles from './top-up-exec-balance.module.scss' ;
1214
1315type 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