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:
parent
aa54795c2b
commit
88b40e5985
@ -45,7 +45,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
|
||||
)
|
||||
const [poolSnapshots, setPoolSnapshots] = useState<PoolDataPoolSnapshots[]>()
|
||||
const [hasUserAddedLiquidity, setUserHasAddedLiquidity] = useState(false)
|
||||
// const [fetchInterval, setFetchInterval] = useState<NodeJS.Timeout>()
|
||||
|
||||
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 (
|
||||
<PoolContext.Provider
|
||||
value={
|
||||
|
Loading…
Reference in New Issue
Block a user