Skip to content

Commit e226bec

Browse files
committed
Add total staked display in RewardsCard component and implement total staked fetching in useVesting hook
1 parent 9998f8f commit e226bec

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/pages/stakingPage/components/RewardsCard.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { GrixLogo } from '@/components/commons/Logo';
66
import { EthLogo } from '@/components/commons/Logo/EthLogo';
77
import { claim, compound } from '@/web3Config/staking/hooks';
88

9+
import { useVesting } from '../hooks/useVesting';
10+
911
type RewardsCardProps = {
1012
data: {
1113
claimable: string;
@@ -27,6 +29,7 @@ export const RewardsCard = ({ data, refetchData }: RewardsCardProps): JSX.Elemen
2729
const [isClaiming, setIsClaiming] = useState(false);
2830
const [isCompounding, setIsCompounding] = useState(false);
2931
const [grixPrice, setGrixPrice] = useState<number | null>(null);
32+
const { totalStaked } = useVesting();
3033
const toast = useToast();
3134

3235
// Fetch GRIX price from CoinGecko
@@ -143,6 +146,32 @@ export const RewardsCard = ({ data, refetchData }: RewardsCardProps): JSX.Elemen
143146
)}
144147
</Text>
145148
</HStack>
149+
150+
<HStack justify="space-between" pt={2} borderTop="1px solid" borderColor="gray.800">
151+
<HStack spacing={2}>
152+
<GrixLogo boxSize="14px" />
153+
<Text color="white" fontWeight="600" fontSize="sm" letterSpacing="-0.01em">
154+
Total Staked
155+
</Text>
156+
</HStack>
157+
<Text color="gray.400" fontSize="sm" fontWeight="500" flexShrink={0} textAlign="right">
158+
{Number(totalStaked).toLocaleString(undefined, {
159+
minimumFractionDigits: 2,
160+
maximumFractionDigits: 2,
161+
})}{' '}
162+
GRIX
163+
{grixPrice && Number(totalStaked) > 0 && (
164+
<Text as="span" color="green.300" fontWeight="600" fontSize="sm" letterSpacing="-0.01em">
165+
&nbsp;($
166+
{(Number(totalStaked) * grixPrice).toLocaleString(undefined, {
167+
minimumFractionDigits: 2,
168+
maximumFractionDigits: 2,
169+
})}
170+
)
171+
</Text>
172+
)}
173+
</Text>
174+
</HStack>
146175
<Button
147176
onClick={() => void handleClaim()}
148177
isLoading={isClaiming}
@@ -175,6 +204,7 @@ export const RewardsCard = ({ data, refetchData }: RewardsCardProps): JSX.Elemen
175204
>
176205
Compound
177206
</Button>
207+
178208
</VStack>
179209
</Box>
180210
);

src/pages/stakingPage/hooks/useVesting.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { useCallback, useEffect, useState } from 'react';
2-
import { parseEther } from 'viem';
2+
import { erc20Abi, formatEther, parseEther } from 'viem';
33
import { useAccount } from 'wagmi';
4+
import { readContract } from 'wagmi/actions';
45

6+
import { normalizeAddress } from '@/utils/web3Util';
7+
import { wagmiConfig } from '@/web3Config/reownConfig';
58
import { stakingContracts } from '@/web3Config/staking/config';
69
import {
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

Comments
 (0)