1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-26 03:06:49 +02:00

fix wrong TVL on pool tab (#1281)

* remove multiplier from totalLiquidityInOcean

* Revert "remove multiplier from totalLiquidityInOcean"

This reverts commit e6b5cc15d6.

* restore totalLiquidityInOcean calculation

* getting user / owner liquidity from calcSingleOutGivenPoolIn + some clean up

* removed web3 hook in favor of dummyWeb3 connection

* remove calc on user liquidity (YVL)
This commit is contained in:
EnzoVezzaro 2022-04-04 09:29:31 -04:00 committed by GitHub
parent 0ade87c971
commit b574daee30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 82 deletions

View File

@ -1,4 +1,4 @@
import { LoggerInstance } from '@oceanprotocol/lib' import { LoggerInstance, Pool } from '@oceanprotocol/lib'
import { isValidNumber } from '@utils/numbers' import { isValidNumber } from '@utils/numbers'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import React, { import React, {
@ -16,6 +16,7 @@ import {
} from 'src/@types/subgraph/PoolData' } from 'src/@types/subgraph/PoolData'
import { useAsset } from '../Asset' import { useAsset } from '../Asset'
import { useWeb3 } from '../Web3' import { useWeb3 } from '../Web3'
import { calculateSharesVL } from '@utils/pool'
import { PoolProviderValue, PoolInfo, PoolInfoUser } from './_types' import { PoolProviderValue, PoolInfo, PoolInfoUser } from './_types'
import { getFee, getPoolData, getWeight } from './_utils' import { getFee, getPoolData, getWeight } from './_utils'
@ -37,7 +38,7 @@ const initialPoolInfoUser: Partial<PoolInfoUser> = {
const initialPoolInfoCreator: Partial<PoolInfoUser> = initialPoolInfoUser const initialPoolInfoCreator: Partial<PoolInfoUser> = initialPoolInfoUser
function PoolProvider({ children }: { children: ReactNode }): ReactElement { function PoolProvider({ children }: { children: ReactNode }): ReactElement {
const { accountId } = useWeb3() const { accountId, web3, chainId } = useWeb3()
const { isInPurgatory, asset, owner } = useAsset() const { isInPurgatory, asset, owner } = useAsset()
const [poolData, setPoolData] = useState<PoolDataPoolData>() const [poolData, setPoolData] = useState<PoolDataPoolData>()
@ -54,6 +55,8 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
const [hasUserAddedLiquidity, setUserHasAddedLiquidity] = useState(false) const [hasUserAddedLiquidity, setUserHasAddedLiquidity] = useState(false)
const [isRemoveDisabled, setIsRemoveDisabled] = useState(false) const [isRemoveDisabled, setIsRemoveDisabled] = useState(false)
// const [fetchInterval, setFetchInterval] = useState<NodeJS.Timeout>() // const [fetchInterval, setFetchInterval] = useState<NodeJS.Timeout>()
const [ownerPoolShares, setOwnerPoolShares] = useState('0')
const [userPoolShares, setUserPoolShares] = useState('0')
const fetchAllData = useCallback(async () => { const fetchAllData = useCallback(async () => {
if ( if (
@ -84,30 +87,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
LoggerInstance.log('[pool] Fetched pool snapshots:', response.poolSnapshots) LoggerInstance.log('[pool] Fetched pool snapshots:', response.poolSnapshots)
}, [asset?.chainId, asset?.accessDetails?.addressOrId, owner, accountId]) }, [asset?.chainId, asset?.accessDetails?.addressOrId, owner, accountId])
// Helper: start interval fetching
// const initFetchInterval = useCallback(() => {
// if (fetchInterval) return
// const newInterval = setInterval(() => {
// fetchAllData()
// LoggerInstance.log(
// `[pool] Refetch interval fired after ${refreshInterval / 1000}s`
// )
// }, refreshInterval)
// setFetchInterval(newInterval)
// // Having `accountId` as dependency is important for interval to
// // change after user account switch.
// // eslint-disable-next-line react-hooks/exhaustive-deps
// }, [fetchInterval, fetchAllData, accountId])
// useEffect(() => {
// return () => {
// clearInterval(fetchInterval)
// }
// }, [fetchInterval])
//
// 0 Fetch all the data on mount if we are on a pool. // 0 Fetch all the data on mount if we are on a pool.
// All further effects depend on the fetched data // All further effects depend on the fetched data
// and only do further data checking and manipulation. // and only do further data checking and manipulation.
@ -116,7 +95,6 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
if (asset?.accessDetails?.type !== 'dynamic') return if (asset?.accessDetails?.type !== 'dynamic') return
fetchAllData() fetchAllData()
// initFetchInterval()
}, [fetchAllData, asset?.accessDetails?.type]) }, [fetchAllData, asset?.accessDetails?.type])
// //
@ -125,12 +103,19 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
useEffect(() => { useEffect(() => {
if (!poolData) return if (!poolData) return
// once we have poolData, we need to get owner's pool shares (OVL)
calculateSharesVL(
poolData.id,
poolData.baseToken.address,
poolData.shares[0].shares,
asset.chainId
).then((shares) => {
setOwnerPoolShares(shares)
})
// Total Liquidity // Total Liquidity
const totalLiquidityInOcean = isValidNumber(poolData.spotPrice) const totalLiquidityInOcean = new Decimal(
? new Decimal(poolData.baseTokenLiquidity).add( poolData.baseTokenLiquidity * 2 || 0
new Decimal(poolData.datatokenLiquidity).mul(poolData.spotPrice) )
)
: new Decimal(0)
const newPoolInfo = { const newPoolInfo = {
liquidityProviderSwapFee: getFee(poolData.liquidityProviderSwapFee), liquidityProviderSwapFee: getFee(poolData.liquidityProviderSwapFee),
@ -145,48 +130,44 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
totalPoolTokens: poolData.totalShares, totalPoolTokens: poolData.totalShares,
totalLiquidityInOcean totalLiquidityInOcean
} }
setPoolInfo(newPoolInfo) setPoolInfo(newPoolInfo)
LoggerInstance.log('[pool] Created new pool info:', newPoolInfo) LoggerInstance.log('[pool] Created new pool info:', newPoolInfo)
}, [poolData]) }, [asset.chainId, chainId, poolData, web3])
// //
// 2 Pool Creator Info // 2 Pool Creator Info
// //
useEffect(() => { useEffect(() => {
if (!poolData || !poolInfo?.totalPoolTokens) return if (
!poolData ||
// Staking bot receives half the pool shares so for display purposes !poolInfo?.totalPoolTokens ||
// we can multiply by 2 as we have a hardcoded 50/50 pool weight. !poolInfo.totalLiquidityInOcean ||
const ownerPoolShares = new Decimal(poolData.shares[0]?.shares) ownerPoolShares === '0'
.mul(2) )
.toString() return
// Liquidity in base token, calculated from pool share tokens.
const liquidity =
isValidNumber(ownerPoolShares) &&
isValidNumber(poolInfo.totalPoolTokens) &&
isValidNumber(poolData.baseTokenLiquidity)
? new Decimal(ownerPoolShares)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(poolData.baseTokenLiquidity)
: new Decimal(0)
// Pool share tokens. // Pool share tokens.
const poolShare = const poolShare = new Decimal(ownerPoolShares)
isValidNumber(ownerPoolShares) && isValidNumber(poolInfo.totalPoolTokens) .dividedBy(poolInfo.totalLiquidityInOcean)
? new Decimal(ownerPoolShares) .mul(100)
.dividedBy(new Decimal(poolInfo.totalPoolTokens)) .toFixed(2)
.mul(100)
.toFixed(2) console.log(ownerPoolShares, poolShare)
: '0'
const newPoolOwnerInfo = { const newPoolOwnerInfo = {
liquidity, liquidity: new Decimal(ownerPoolShares), // liquidity in base token, values from from `calcSingleOutGivenPoolIn` method
poolShares: ownerPoolShares, poolShares: ownerPoolShares,
poolShare poolShare
} }
setPoolInfoOwner(newPoolOwnerInfo) setPoolInfoOwner(newPoolOwnerInfo)
LoggerInstance.log('[pool] Created new owner pool info:', newPoolOwnerInfo) LoggerInstance.log('[pool] Created new owner pool info:', newPoolOwnerInfo)
}, [poolData, poolInfo?.totalPoolTokens]) }, [
ownerPoolShares,
poolData,
poolInfo.totalLiquidityInOcean,
poolInfo.totalPoolTokens
])
// //
// 3 User Pool Info // 3 User Pool Info
@ -196,40 +177,34 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
!poolData || !poolData ||
!poolInfo?.totalPoolTokens || !poolInfo?.totalPoolTokens ||
!poolInfoUser?.poolShares || !poolInfoUser?.poolShares ||
!poolInfo?.totalLiquidityInOcean ||
!poolData?.baseTokenLiquidity ||
!asset?.chainId || !asset?.chainId ||
!accountId || !accountId ||
!poolInfoUser !poolInfoUser
) )
return return
// Staking bot receives half the pool shares so for display purposes
// we can multiply by 2 as we have a hardcoded 50/50 pool weight. // once we have poolData, we need to get user's pool shares (VL)
const userPoolShares = new Decimal(poolInfoUser.poolShares || 0) calculateSharesVL(
.mul(2) poolData.id,
.toString() poolData.baseToken.address,
poolInfoUser.poolShares,
asset.chainId
).then((shares) => {
setUserPoolShares(shares)
})
// Pool share in %. // Pool share in %.
const poolShare = const poolShare = new Decimal(userPoolShares)
isValidNumber(userPoolShares) && .dividedBy(new Decimal(poolInfo.totalLiquidityInOcean))
isValidNumber(poolInfo.totalPoolTokens) && .mul(100)
new Decimal(userPoolShares) .toFixed(2)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(100)
.toFixed(2)
setUserHasAddedLiquidity(Number(poolShare) > 0) setUserHasAddedLiquidity(Number(poolShare) > 0)
// Liquidity in base token, calculated from pool share tokens.
const liquidity =
isValidNumber(userPoolShares) &&
isValidNumber(poolInfo.totalPoolTokens) &&
isValidNumber(poolData.baseTokenLiquidity)
? new Decimal(userPoolShares)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(poolData.baseTokenLiquidity)
: new Decimal(0)
const newPoolInfoUser = { const newPoolInfoUser = {
liquidity, liquidity: new Decimal(userPoolShares), // liquidity in base token, values from from `calcSingleOutGivenPoolIn` method
poolShare poolShare
} }
setPoolInfoUser((prevState: PoolInfoUser) => ({ setPoolInfoUser((prevState: PoolInfoUser) => ({
@ -247,6 +222,7 @@ function PoolProvider({ children }: { children: ReactNode }): ReactElement {
poolData, poolData,
poolInfoUser?.poolShares, poolInfoUser?.poolShares,
accountId, accountId,
userPoolShares,
asset?.chainId, asset?.chainId,
owner, owner,
poolInfo?.totalPoolTokens poolInfo?.totalPoolTokens

View File

@ -114,3 +114,26 @@ export function calculateUserTVL(
const tvl = new Decimal(liquidity).mul(2) // we multiply by 2 because of 50/50 weight const tvl = new Decimal(liquidity).mul(2) // we multiply by 2 because of 50/50 weight
return tvl.toDecimalPlaces(MAX_DECIMALS).toString() return tvl.toDecimalPlaces(MAX_DECIMALS).toString()
} }
export async function calculateSharesVL(
pool: string,
tokenAddress: string,
shares: string,
chainId?: number
): Promise<string> {
if (!chainId) throw new Error("chainId can't be undefined at the same time!")
// we only use the dummyWeb3 connection here
const web3 = await getDummyWeb3(chainId)
const poolInstance = new Pool(web3)
// get shares VL in ocean
const amountOcean = await poolInstance.calcSingleOutGivenPoolIn(
pool,
tokenAddress,
shares
)
const tvl = new Decimal(amountOcean || 0).mul(2) // we multiply by 2 because of 50/50 weight
return tvl.toDecimalPlaces(MAX_DECIMALS).toString()
}