1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

fix pool context (#1416)

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2022-05-10 22:10:16 +03:00 committed by GitHub
parent aa54795c2b
commit 88b40e5985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,7 +45,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
) )
const [poolSnapshots, setPoolSnapshots] = useState<PoolDataPoolSnapshots[]>() const [poolSnapshots, setPoolSnapshots] = useState<PoolDataPoolSnapshots[]>()
const [hasUserAddedLiquidity, setUserHasAddedLiquidity] = useState(false) const [hasUserAddedLiquidity, setUserHasAddedLiquidity] = useState(false)
// const [fetchInterval, setFetchInterval] = useState<NodeJS.Timeout>()
const fetchAllData = useCallback(async () => { const fetchAllData = useCallback(async () => {
if (!asset?.chainId || !asset?.accessDetails?.addressOrId || !owner) return if (!asset?.chainId || !asset?.accessDetails?.addressOrId || !owner) return
@ -56,14 +55,36 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
owner, owner,
accountId || '' accountId || ''
) )
if (!response) return if (!response) return
setPoolData(response.poolData) 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, ...prevState,
poolShares: response.poolDataUser?.shares[0]?.shares || '0' ...newPoolInfoUser
})) }))
setPoolSnapshots(response.poolSnapshots) setPoolSnapshots(response.poolSnapshots)
LoggerInstance.log('[pool] Fetched pool data:', response.poolData) LoggerInstance.log('[pool] Fetched pool data:', response.poolData)
LoggerInstance.log('[pool] Fetched user data:', response.poolDataUser) LoggerInstance.log('[pool] Fetched user data:', response.poolDataUser)
@ -147,54 +168,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
poolInfo?.totalPoolTokens 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 ( return (
<PoolContext.Provider <PoolContext.Provider
value={ value={