Skip to content

Commit 00695ca

Browse files
committed
Add total staked and GRIX price fetching to StakingCard component
- Implemented functions to fetch total GRIX and esGRIX staked amounts. - Added state management for total staked amount and GRIX price. - Updated data fetching logic to include total staked and GRIX price. - Enhanced StakingCardContent to display total staked amount and its value in USD.
1 parent 7eb936d commit 00695ca

3 files changed

Lines changed: 101 additions & 3 deletions

File tree

src/pages/stakingPage/components/StakingCard.tsx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
getEsGrixStakedAmount,
1212
getStakedAmount,
1313
getTokenBalance,
14+
getTotalEsGrixStaked,
15+
getTotalGrixStaked,
1416
stakeEsGRIX,
1517
stakeGRIX,
1618
unstakeEsGRIX,
@@ -42,6 +44,8 @@ export const StakingCard: React.FC<StakingCardProps> = ({
4244
const [needsApproval, setNeedsApproval] = useState(true);
4345
const [availableBalance, setAvailableBalance] = useState('0');
4446
const [stakedAmount, setStakedAmount] = useState('0');
47+
const [totalStakedInProtocol, setTotalStakedInProtocol] = useState('0');
48+
const [grixPrice, setGrixPrice] = useState<number | null>(null);
4549
const [apr, setApr] = useState(0);
4650
const toast = useToast();
4751
const tokenAddress = type === 'gx' ? stakingContracts.grixToken.address : stakingContracts.esGRIXToken.address;
@@ -90,19 +94,53 @@ export const StakingCard: React.FC<StakingCardProps> = ({
9094
setApr(calculatedAPR);
9195
}, []);
9296

97+
const fetchTotalStaked = useCallback(async () => {
98+
try {
99+
if (type === 'gx') {
100+
const total = await getTotalGrixStaked();
101+
setTotalStakedInProtocol(total);
102+
} else {
103+
const total = await getTotalEsGrixStaked();
104+
setTotalStakedInProtocol(total);
105+
}
106+
} catch (error) {
107+
setTotalStakedInProtocol('0');
108+
}
109+
}, [type]);
110+
111+
const fetchGrixPrice = useCallback(async () => {
112+
try {
113+
const res = await fetch('https://z61hgkwkn8.execute-api.us-east-1.amazonaws.com/dev/assetprice?asset=GRIX', {
114+
headers: {
115+
'x-api-key': import.meta.env.VITE_GRIX_API_KEY,
116+
origin: 'https://app.grix.finance',
117+
},
118+
});
119+
const json = await res.json();
120+
const price = json.assetPrice;
121+
setGrixPrice(price);
122+
} catch {
123+
setGrixPrice(null);
124+
}
125+
}, []);
126+
93127
useEffect(() => {
94128
void fetchBalance();
95129
void fetchStakedAmount();
96130
void fetchAPR();
131+
void fetchTotalStaked();
132+
void fetchGrixPrice();
97133

98134
const interval = setInterval(() => {
99135
void fetchBalance();
100136
void fetchStakedAmount();
101137
void fetchAPR();
138+
void fetchTotalStaked();
139+
void fetchGrixPrice();
102140
}, 30000);
103141

104142
return () => clearInterval(interval);
105-
}, [fetchBalance, fetchStakedAmount, fetchAPR, refreshTrigger]);
143+
}, [fetchBalance, fetchStakedAmount, fetchAPR, fetchTotalStaked, fetchGrixPrice, refreshTrigger]);
106144

107145
const isAmountValid = useCallback(() => {
108146
if (!amount) return false;
@@ -155,12 +193,12 @@ export const StakingCard: React.FC<StakingCardProps> = ({
155193

156194
const refreshAllData = useCallback(
157195
async (triggerParentRefresh = true) => {
158-
await Promise.all([fetchBalance(), fetchStakedAmount(), fetchAPR()]);
196+
await Promise.all([fetchBalance(), fetchStakedAmount(), fetchAPR(), fetchTotalStaked(), fetchGrixPrice()]);
159197
if (triggerParentRefresh) {
160198
onActionComplete();
161199
}
162200
},
163-
[fetchBalance, fetchStakedAmount, fetchAPR, onActionComplete]
201+
[fetchBalance, fetchStakedAmount, fetchAPR, fetchTotalStaked, fetchGrixPrice, onActionComplete]
164202
);
165203

166204
useEffect(() => {
@@ -238,6 +276,8 @@ export const StakingCard: React.FC<StakingCardProps> = ({
238276
description={description}
239277
stakedAmount={stakedAmount}
240278
availableBalance={availableBalance}
279+
totalStakedInProtocol={totalStakedInProtocol}
280+
grixPrice={grixPrice}
241281
apr={apr}
242282
amount={amount}
243283
handleInputChange={handleInputChange}

src/pages/stakingPage/components/StakingCardContent.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type StakingCardContentProps = {
1212
description: string;
1313
stakedAmount: string;
1414
availableBalance: string;
15+
totalStakedInProtocol: string;
16+
grixPrice: number | null;
1517
apr: number;
1618
amount: string;
1719
handleInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
@@ -33,6 +35,8 @@ export const StakingCardContent: React.FC<StakingCardContentProps> = ({
3335
description,
3436
stakedAmount,
3537
availableBalance,
38+
totalStakedInProtocol,
39+
grixPrice,
3640
apr,
3741
amount,
3842
handleInputChange,
@@ -115,6 +119,30 @@ export const StakingCardContent: React.FC<StakingCardContentProps> = ({
115119
{apr.toFixed(2)}%
116120
</Text>
117121
</VStack>
122+
123+
<VStack align="stretch">
124+
<Text fontSize="sm" color="gray.500" mb={1}>
125+
Total Staked
126+
</Text>
127+
<VStack align="flex-start" spacing={1}>
128+
<Text fontSize="xl" fontWeight="700" color="white">
129+
{Number(totalStakedInProtocol).toLocaleString(undefined, {
130+
minimumFractionDigits: 1,
131+
maximumFractionDigits: 1,
132+
})}
133+
</Text>
134+
{grixPrice && Number(totalStakedInProtocol) > 0 && (
135+
<Text fontSize="sm" color="green.300" fontWeight="600">
136+
($
137+
{(Number(totalStakedInProtocol) * grixPrice).toLocaleString(undefined, {
138+
minimumFractionDigits: 2,
139+
maximumFractionDigits: 2,
140+
})}
141+
)
142+
</Text>
143+
)}
144+
</VStack>
145+
</VStack>
118146
</SimpleGrid>
119147

120148
<HStack spacing={4}>

src/web3Config/staking/hooks.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,36 @@ export const getStakedAmount = async (userAddress: string) => {
185185
}
186186
};
187187

188+
// Get total GRIX staked in the protocol
189+
export const getTotalGrixStaked = async () => {
190+
try {
191+
const balance = await readContract(wagmiConfig, {
192+
abi: erc20Abi,
193+
address: normalizeAddress(stakingContracts.grixToken.address),
194+
functionName: 'balanceOf',
195+
args: [normalizeAddress(stakingContracts.rewardTracker.address)],
196+
});
197+
return formatEther(balance);
198+
} catch (error) {
199+
return '0';
200+
}
201+
};
202+
203+
// Get total esGRIX staked in the protocol
204+
export const getTotalEsGrixStaked = async () => {
205+
try {
206+
const balance = await readContract(wagmiConfig, {
207+
abi: erc20Abi,
208+
address: normalizeAddress(stakingContracts.esGRIXToken.address),
209+
functionName: 'balanceOf',
210+
args: [normalizeAddress(stakingContracts.rewardTracker.address)],
211+
});
212+
return formatEther(balance);
213+
} catch (error) {
214+
return '0';
215+
}
216+
};
217+
188218
// Update the getEsGrixStakedAmount function to correctly fetch esGRIX staked amount
189219
export const getEsGrixStakedAmount = async (userAddress: string) => {
190220
try {

0 commit comments

Comments
 (0)