11import { useCallback , useEffect , useState } from 'react' ;
2- import { parseEther } from 'viem' ;
2+ import { erc20Abi , formatEther , parseEther } from 'viem' ;
33import { useAccount } from 'wagmi' ;
4+ import { readContract } from 'wagmi/actions' ;
45
6+ import { normalizeAddress } from '@/utils/web3Util' ;
7+ import { wagmiConfig } from '@/web3Config/reownConfig' ;
58import { stakingContracts } from '@/web3Config/staking/config' ;
69import {
710 approveVesting ,
@@ -24,8 +27,26 @@ export const useVesting = () => {
2427 const [ esGrixBalance , setEsGrixBalance ] = useState ( '0' ) ;
2528 const [ grixBalance , setGrixBalance ] = useState ( '0' ) ;
2629 const [ vestingData , setVestingData ] = useState < VestingData | null > ( null ) ;
30+ const [ totalStaked , setTotalStaked ] = useState ( '0' ) ;
2731 const [ isLoading , setIsLoading ] = useState ( false ) ;
2832
33+ const fetchTotalStaked = useCallback ( async ( ) => {
34+ try {
35+ // Get total GRIX tokens staked in the vester contract
36+ const balance = await readContract ( wagmiConfig , {
37+ abi : erc20Abi ,
38+ address : normalizeAddress ( stakingContracts . grixToken . address ) ,
39+ functionName : 'balanceOf' ,
40+ args : [ normalizeAddress ( stakingContracts . rewardTracker . address ) ] ,
41+ } ) ;
42+
43+ const amount = formatEther ( balance ) ;
44+ setTotalStaked ( amount ) ;
45+ } catch ( error ) {
46+ setTotalStaked ( '0' ) ;
47+ }
48+ } , [ ] ) ;
49+
2950 const fetchVestingData = useCallback ( async ( ) => {
3051 if ( ! address ) return ;
3152
@@ -35,6 +56,7 @@ export const useVesting = () => {
3556 getTokenBalance ( stakingContracts . esGRIXToken . address , address ) ,
3657 getTokenBalance ( stakingContracts . grixToken . address , address ) ,
3758 getVestingData ( address ) ,
59+ fetchTotalStaked ( ) ,
3860 ] ) ;
3961
4062 setVestingAllowance ( allowance . toString ( ) ) ;
@@ -45,7 +67,7 @@ export const useVesting = () => {
4567 setVestingData ( null ) ;
4668 throw error ;
4769 }
48- } , [ address ] ) ;
70+ } , [ address , fetchTotalStaked ] ) ;
4971
5072 useEffect ( ( ) => {
5173 void fetchVestingData ( ) ;
@@ -80,6 +102,7 @@ export const useVesting = () => {
80102 esGrixBalance,
81103 grixBalance,
82104 vestingData,
105+ totalStaked,
83106 isLoading,
84107 handleVest,
85108 fetchVestingData,
0 commit comments