Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/pages/stakingPage/components/RewardsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { useAccount } from 'wagmi';

import { GrixLogo } from '@/components/commons/Logo';
import { EthLogo } from '@/components/commons/Logo/EthLogo';
import { AssetPriceResponse } from '@/types/api';
import { claim, compound } from '@/web3Config/staking/hooks';

import { useVesting } from '../hooks/useVesting';

type RewardsCardProps = {
data: {
claimable: string;
Expand All @@ -16,26 +19,26 @@ type RewardsCardProps = {
refetchData: () => Promise<void>;
};

type DexScreenerResponse = {
grix: {
usd: number;
};
};

export const RewardsCard = ({ data, refetchData }: RewardsCardProps): JSX.Element => {
const { address } = useAccount();
const [isClaiming, setIsClaiming] = useState(false);
const [isCompounding, setIsCompounding] = useState(false);
const [grixPrice, setGrixPrice] = useState<number | null>(null);
const { totalStaked } = useVesting();
const toast = useToast();

// Fetch GRIX price from CoinGecko
useEffect(() => {
const fetchPrice = async () => {
try {
const res = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=grix&vs_currencies=usd');
const json = (await res.json()) as DexScreenerResponse;
const price = json.grix.usd;
const res = await fetch('https://z61hgkwkn8.execute-api.us-east-1.amazonaws.com/dev/assetprice?asset=GRIX', {
headers: {
'x-api-key': import.meta.env.VITE_GRIX_API_KEY,
origin: 'https://app.grix.finance',
},
});
const json = (await res.json()) as AssetPriceResponse;
const price = json.assetPrice;
setGrixPrice(price);
} catch {
setGrixPrice(null);
Expand Down Expand Up @@ -143,6 +146,32 @@ export const RewardsCard = ({ data, refetchData }: RewardsCardProps): JSX.Elemen
)}
</Text>
</HStack>

<HStack justify="space-between" pt={2} borderTop="1px solid" borderColor="gray.800">
<HStack spacing={2}>
<GrixLogo boxSize="14px" />
<Text color="white" fontWeight="600" fontSize="sm" letterSpacing="-0.01em">
Total Staked
</Text>
</HStack>
<Text color="gray.400" fontSize="sm" fontWeight="500" flexShrink={0} textAlign="right">
{Number(totalStaked).toLocaleString(undefined, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
})}{' '}
GRIX
{grixPrice && Number(totalStaked) > 0 && (
<Text as="span" color="green.300" fontWeight="600" fontSize="sm" letterSpacing="-0.01em">
&nbsp;($
{(Number(totalStaked) * grixPrice).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
)
</Text>
)}
</Text>
</HStack>
<Button
onClick={() => void handleClaim()}
isLoading={isClaiming}
Expand Down
47 changes: 44 additions & 3 deletions src/pages/stakingPage/components/StakingCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import { parseEther } from 'viem';
import { useAccount } from 'wagmi';

import { AssetPriceResponse } from '@/types/api';
import { stakingContracts } from '@/web3Config/staking/config';
import {
approveStaking,
Expand All @@ -11,6 +12,8 @@ import {
getEsGrixStakedAmount,
getStakedAmount,
getTokenBalance,
getTotalEsGrixStaked,
getTotalGrixStaked,
stakeEsGRIX,
stakeGRIX,
unstakeEsGRIX,
Expand Down Expand Up @@ -42,6 +45,8 @@ export const StakingCard: React.FC<StakingCardProps> = ({
const [needsApproval, setNeedsApproval] = useState(true);
const [availableBalance, setAvailableBalance] = useState('0');
const [stakedAmount, setStakedAmount] = useState('0');
const [totalStakedInProtocol, setTotalStakedInProtocol] = useState('0');
const [grixPrice, setGrixPrice] = useState<number | null>(null);
const [apr, setApr] = useState(0);
const toast = useToast();
const tokenAddress = type === 'gx' ? stakingContracts.grixToken.address : stakingContracts.esGRIXToken.address;
Expand Down Expand Up @@ -90,19 +95,53 @@ export const StakingCard: React.FC<StakingCardProps> = ({
setApr(calculatedAPR);
}, []);

const fetchTotalStaked = useCallback(async () => {
try {
if (type === 'gx') {
const total = await getTotalGrixStaked();
setTotalStakedInProtocol(total);
} else {
const total = await getTotalEsGrixStaked();
setTotalStakedInProtocol(total);
}
} catch (error) {
setTotalStakedInProtocol('0');
}
}, [type]);

const fetchGrixPrice = useCallback(async () => {
try {
const res = await fetch('https://z61hgkwkn8.execute-api.us-east-1.amazonaws.com/dev/assetprice?asset=GRIX', {
headers: {
'x-api-key': import.meta.env.VITE_GRIX_API_KEY,
origin: 'https://app.grix.finance',
},
});
const json = (await res.json()) as AssetPriceResponse;
const price = json.assetPrice;
setGrixPrice(price);
} catch {
setGrixPrice(null);
}
}, []);

useEffect(() => {
void fetchBalance();
void fetchStakedAmount();
void fetchAPR();
void fetchTotalStaked();
void fetchGrixPrice();

const interval = setInterval(() => {
void fetchBalance();
void fetchStakedAmount();
void fetchAPR();
void fetchTotalStaked();
void fetchGrixPrice();
}, 30000);

return () => clearInterval(interval);
}, [fetchBalance, fetchStakedAmount, fetchAPR, refreshTrigger]);
}, [fetchBalance, fetchStakedAmount, fetchAPR, fetchTotalStaked, fetchGrixPrice, refreshTrigger]);

const isAmountValid = useCallback(() => {
if (!amount) return false;
Expand Down Expand Up @@ -155,12 +194,12 @@ export const StakingCard: React.FC<StakingCardProps> = ({

const refreshAllData = useCallback(
async (triggerParentRefresh = true) => {
await Promise.all([fetchBalance(), fetchStakedAmount(), fetchAPR()]);
await Promise.all([fetchBalance(), fetchStakedAmount(), fetchAPR(), fetchTotalStaked(), fetchGrixPrice()]);
if (triggerParentRefresh) {
onActionComplete();
}
},
[fetchBalance, fetchStakedAmount, fetchAPR, onActionComplete]
[fetchBalance, fetchStakedAmount, fetchAPR, fetchTotalStaked, fetchGrixPrice, onActionComplete]
);

useEffect(() => {
Expand Down Expand Up @@ -238,6 +277,8 @@ export const StakingCard: React.FC<StakingCardProps> = ({
description={description}
stakedAmount={stakedAmount}
availableBalance={availableBalance}
totalStakedInProtocol={totalStakedInProtocol}
grixPrice={grixPrice}
apr={apr}
amount={amount}
handleInputChange={handleInputChange}
Expand Down
28 changes: 28 additions & 0 deletions src/pages/stakingPage/components/StakingCardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type StakingCardContentProps = {
description: string;
stakedAmount: string;
availableBalance: string;
totalStakedInProtocol: string;
grixPrice: number | null;
apr: number;
amount: string;
handleInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
Expand All @@ -33,6 +35,8 @@ export const StakingCardContent: React.FC<StakingCardContentProps> = ({
description,
stakedAmount,
availableBalance,
totalStakedInProtocol,
grixPrice,
apr,
amount,
handleInputChange,
Expand Down Expand Up @@ -115,6 +119,30 @@ export const StakingCardContent: React.FC<StakingCardContentProps> = ({
{apr.toFixed(2)}%
</Text>
</VStack>

<VStack align="stretch">
<Text fontSize="sm" color="gray.500" mb={1}>
Total Staked
</Text>
<VStack align="flex-start" spacing={1}>
<Text fontSize="xl" fontWeight="700" color="white">
{Number(totalStakedInProtocol).toLocaleString(undefined, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
})}
</Text>
{grixPrice && Number(totalStakedInProtocol) > 0 && (
<Text fontSize="sm" color="green.300" fontWeight="600">
($
{(Number(totalStakedInProtocol) * grixPrice).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
)
</Text>
)}
</VStack>
</VStack>
</SimpleGrid>

<HStack spacing={4}>
Expand Down
18 changes: 9 additions & 9 deletions src/pages/stakingPage/components/VestingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useEffect, useState } from 'react';
import { FaEquals, FaPlus } from 'react-icons/fa';

import { GrixLogo } from '@/components/commons/Logo';
import { AssetPriceResponse } from '@/types/api';

type VestingModalProps = {
isOpen: boolean;
Expand All @@ -29,12 +30,6 @@ type VestingModalProps = {
claimableRewards: string;
};

type DexScreenerResponse = {
grix: {
usd: number;
};
};

export const VestingModal = ({
isOpen,
onClose,
Expand All @@ -52,9 +47,14 @@ export const VestingModal = ({
useEffect(() => {
const fetchPrice = async () => {
try {
const res = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=grix&vs_currencies=usd');
const json = (await res.json()) as DexScreenerResponse;
const price = json.grix.usd;
const res = await fetch('https://z61hgkwkn8.execute-api.us-east-1.amazonaws.com/dev/assetprice?asset=GRIX', {
headers: {
'x-api-key': import.meta.env.VITE_GRIX_API_KEY,
origin: 'https://app.grix.finance',
},
});
const json = (await res.json()) as AssetPriceResponse;
const price = json.assetPrice;
setGrixPrice(price);
} catch {
setGrixPrice(null);
Expand Down
27 changes: 25 additions & 2 deletions src/pages/stakingPage/hooks/useVesting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useCallback, useEffect, useState } from 'react';
import { parseEther } from 'viem';
import { erc20Abi, formatEther, parseEther } from 'viem';
import { useAccount } from 'wagmi';
import { readContract } from 'wagmi/actions';

import { normalizeAddress } from '@/utils/web3Util';
import { wagmiConfig } from '@/web3Config/reownConfig';
import { stakingContracts } from '@/web3Config/staking/config';
import {
approveVesting,
Expand All @@ -24,8 +27,26 @@ export const useVesting = () => {
const [esGrixBalance, setEsGrixBalance] = useState('0');
const [grixBalance, setGrixBalance] = useState('0');
const [vestingData, setVestingData] = useState<VestingData | null>(null);
const [totalStaked, setTotalStaked] = useState('0');
const [isLoading, setIsLoading] = useState(false);

const fetchTotalStaked = useCallback(async () => {
try {
// Get total GRIX tokens staked in the vester contract
const balance = await readContract(wagmiConfig, {
abi: erc20Abi,
address: normalizeAddress(stakingContracts.grixToken.address),
functionName: 'balanceOf',
args: [normalizeAddress(stakingContracts.rewardTracker.address)],
});

const amount = formatEther(balance);
setTotalStaked(amount);
} catch (error) {
setTotalStaked('0');
}
}, []);

const fetchVestingData = useCallback(async () => {
if (!address) return;

Expand All @@ -35,6 +56,7 @@ export const useVesting = () => {
getTokenBalance(stakingContracts.esGRIXToken.address, address),
getTokenBalance(stakingContracts.grixToken.address, address),
getVestingData(address),
fetchTotalStaked(),
]);

setVestingAllowance(allowance.toString());
Expand All @@ -45,7 +67,7 @@ export const useVesting = () => {
setVestingData(null);
throw error;
}
}, [address]);
}, [address, fetchTotalStaked]);

useEffect(() => {
void fetchVestingData();
Expand Down Expand Up @@ -80,6 +102,7 @@ export const useVesting = () => {
esGrixBalance,
grixBalance,
vestingData,
totalStaked,
isLoading,
handleVest,
fetchVestingData,
Expand Down
5 changes: 5 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// API Response Types

export type AssetPriceResponse = {
assetPrice: number;
};
Loading