import _ from 'lodash' import Link from 'next/link' import { useEffect, useMemo, useState, memo } from 'react' import { useAccount } from 'wagmi' import { type Address } from 'viem' import { LoaderIcon, toast } from 'react-hot-toast' import { useTranslations } from 'next-intl' import { useAppSelector } from '@/store/hooks' import { usePortfolioState } from '@/slices/portfolio/hooks' import { getSinglePoolData } from '@utils/actions/getPoolData' import { getPoolApyData } from '@utils/actions/getPoolApyData' import { getStakedBalance } from '@/utils/actions/getStakedBalance' import { formatBigIntToString } from '@/utils/bigint/format' import { PoolActionOptions } from '../../components/Pools/PoolActionOptions' import { PoolHeader } from '../../components/Pools/PoolHeader' import { PoolCardBody } from '../../components/Pools/PoolCardBody' import { STAKE_PATH, getPoolUrl } from '@urls' import { Token } from '@types' const PoolCard = memo(({ pool, address }: { pool: Token; address: string }) => { const [isClient, setIsClient] = useState(false) const [poolData, setPoolData] = useState(undefined) const [poolApyData, setPoolApyData] = useState(undefined) const [stakedBalance, setStakedBalance] = useState({ amount: 0n, reward: 0n, }) const { isDisconnected } = useAccount() const { synPrices, ethPrice, avaxPrice, metisPrice } = useAppSelector( (state) => state.priceData ) const prices = { synPrices, ethPrice, avaxPrice, metisPrice } let popup: string useEffect(() => { setIsClient(true) }, []) useEffect(() => { if (pool && isClient) { // TODO - separate the apy and tvl so they load async. getSinglePoolData(pool.chainId, pool, prices) .then((res) => { setPoolData(res) }) .catch((err) => { console.log('Could not get Pool Data: ', err) }) getPoolApyData(pool.chainId, pool, prices) .then((res) => { setPoolApyData(res) }) .catch((err) => { console.log('Could not get Pool APY Data: ', err) }) } }, [pool, isClient]) useEffect(() => { if (address && isClient) { getStakedBalance( address as Address, pool.chainId, pool.poolId[pool.chainId], pool ) .then((res) => { setStakedBalance(res) }) .catch((err) => { console.log('Could not get staked balances: ', err) }) } else { setStakedBalance({ amount: 0n, reward: 0n }) } }, [address, isClient]) /* useEffect triggers: address, isDisconnected, popup - will dismiss toast asking user to connect wallet once wallet has been connected */ useEffect(() => { if (address && !isDisconnected && popup) { toast.dismiss(popup) } }, [address, isDisconnected, popup]) return (
{pool && } {pool && poolData && poolApyData && poolData.tokens && poolData.totalLockedUSD ? ( ) : (
)} {pool && ( <> )}
) }) const ManageLp = ({ pool, stakedBalance, address }) => { const { poolTokenBalances } = usePortfolioState() const { amount, reward } = stakedBalance const t = useTranslations('Pools') const lpTokenBalance = useMemo(() => { if (!address) { return null } const token = _(poolTokenBalances[pool.chainId]) .pickBy( (value, _key) => value.tokenAddress === pool.addresses[pool.chainId] && value.balance > 0n ) .value() if (Object.keys(token).length === 0) { return null } else { return token[0] } }, [pool, poolTokenBalances, address]) if (!lpTokenBalance && amount === 0n && reward === 0n) { return null } return (
) } export const DisplayBalances = ({ pool, stakedBalance, showIcon, address }) => { const { poolTokenBalances } = usePortfolioState() const { amount, reward } = stakedBalance const t = useTranslations('Pools') const lpTokenBalance = useMemo(() => { if (!address) { return null } const token = _(poolTokenBalances[pool.chainId]) .pickBy( (value, _key) => value.tokenAddress === pool.addresses[pool.chainId] && value.balance > 0n ) .value() if (Object.keys(token).length === 0) { return null } else { return token[0] } }, [pool, poolTokenBalances, address]) const sum = useMemo(() => { const b = lpTokenBalance && lpTokenBalance.balance ? lpTokenBalance.balance : 0n return amount + b }, [lpTokenBalance, amount]) if (!lpTokenBalance && amount === 0n && reward === 0n) { return null } return (
{showIcon && ( )}
{formatBigIntToString(amount, pool.decimals[pool.chainId], 5)} {' '} /{' '} {formatBigIntToString(sum, pool.decimals[pool.chainId], 5)}
{pool.symbol}
{reward > 0n && (
{t('Earned')}: {formatBigIntToString(reward, 18, 5)}{' '} {pool?.customRewardToken ?? 'SYN'}
)}
) } export default PoolCard