diff --git a/src/@context/Pool/index.tsx b/src/@context/Pool/index.tsx index 0f2ab5de8..6620a2c2b 100644 --- a/src/@context/Pool/index.tsx +++ b/src/@context/Pool/index.tsx @@ -45,7 +45,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement { ) const [poolSnapshots, setPoolSnapshots] = useState() const [hasUserAddedLiquidity, setUserHasAddedLiquidity] = useState(false) - // const [fetchInterval, setFetchInterval] = useState() const fetchAllData = useCallback(async () => { if (!asset?.chainId || !asset?.accessDetails?.addressOrId || !owner) return @@ -56,14 +55,36 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement { owner, accountId || '' ) - if (!response) return setPoolData(response.poolData) - setPoolInfoUser((prevState) => ({ + + // calculate pool info user + const poolInfoShares = response.poolDataUser?.shares[0]?.shares || '0' + const userLiquidity = calcSingleOutGivenPoolIn( + response.poolData.baseTokenLiquidity, + response.poolData.totalShares, + poolInfoShares + ) + + // Pool share in %. + const poolSharePercentage = new Decimal(poolInfoShares) + .dividedBy(new Decimal(response.poolData.totalShares)) + .mul(100) + .toFixed(2) + + setUserHasAddedLiquidity(Number(poolSharePercentage) > 0) + + const newPoolInfoUser: PoolInfoUser = { + liquidity: userLiquidity, + poolShares: poolInfoShares, + poolSharePercentage + } + setPoolInfoUser((prevState: PoolInfoUser) => ({ ...prevState, - poolShares: response.poolDataUser?.shares[0]?.shares || '0' + ...newPoolInfoUser })) + setPoolSnapshots(response.poolSnapshots) LoggerInstance.log('[pool] Fetched pool data:', response.poolData) LoggerInstance.log('[pool] Fetched user data:', response.poolDataUser) @@ -147,54 +168,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement { poolInfo?.totalPoolTokens ]) - // - // 3 User Pool Info - // - useEffect(() => { - if ( - !poolData || - !poolInfo?.totalPoolTokens || - !poolInfoUser?.poolShares || - !poolData?.baseTokenLiquidity || - !asset?.chainId - ) - return - - const userLiquidity = calcSingleOutGivenPoolIn( - poolData.baseTokenLiquidity, - poolData.totalShares, - poolInfoUser.poolShares - ) - - // Pool share in %. - const poolSharePercentage = new Decimal(poolInfoUser.poolShares) - .dividedBy(new Decimal(poolInfo.totalPoolTokens)) - .mul(100) - .toFixed(2) - - setUserHasAddedLiquidity(Number(poolSharePercentage) > 0) - - const newPoolInfoUser: PoolInfoUser = { - liquidity: userLiquidity, - poolShares: poolInfoUser.poolShares, - poolSharePercentage - } - setPoolInfoUser((prevState: PoolInfoUser) => ({ - ...prevState, - ...newPoolInfoUser - })) - - LoggerInstance.log('[pool] Created new user pool info:', { - ...newPoolInfoUser - }) - }, [ - poolData, - poolInfoUser?.poolShares, - asset?.chainId, - owner, - poolInfo?.totalPoolTokens - ]) - return (