mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
more pool query refactor
This commit is contained in:
parent
f3c85ccb95
commit
606654b533
@ -1,4 +1,4 @@
|
|||||||
import React, { ReactElement, useEffect, useState } from 'react'
|
import React, { ReactElement, useCallback, useEffect, useState } from 'react'
|
||||||
import { LoggerInstance } from '@oceanprotocol/lib'
|
import { LoggerInstance } from '@oceanprotocol/lib'
|
||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import stylesActions from './Actions.module.css'
|
import stylesActions from './Actions.module.css'
|
||||||
@ -22,13 +22,11 @@ import { isValidNumber } from '@utils/numbers'
|
|||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
import content from '../../../../../content/price.json'
|
import content from '../../../../../content/price.json'
|
||||||
|
|
||||||
const REFETCH_INTERVAL = 5000
|
|
||||||
|
|
||||||
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
|
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
|
||||||
|
|
||||||
const poolLiquidityQuery = gql`
|
const poolLiquidityQuery = gql`
|
||||||
query PoolLiquidity($id: ID!, $shareId: ID) {
|
query PoolLiquidity($pool: ID!, $owner: ID) {
|
||||||
pool(id: $id, subgraphError: deny) {
|
pool(id: $pool) {
|
||||||
id
|
id
|
||||||
totalShares
|
totalShares
|
||||||
poolFee
|
poolFee
|
||||||
@ -47,8 +45,7 @@ const poolLiquidityQuery = gql`
|
|||||||
}
|
}
|
||||||
datatokenWeight
|
datatokenWeight
|
||||||
datatokenLiquidity
|
datatokenLiquidity
|
||||||
shares(where: { id: $shareId }) {
|
shares(where: { user: $owner }) {
|
||||||
id
|
|
||||||
shares
|
shares
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,11 +53,10 @@ const poolLiquidityQuery = gql`
|
|||||||
`
|
`
|
||||||
|
|
||||||
const userPoolShareQuery = gql`
|
const userPoolShareQuery = gql`
|
||||||
query PoolShare($id: ID!, $shareId: ID) {
|
query PoolShare($pool: ID!, $user: ID) {
|
||||||
pool(id: $id) {
|
pool(id: $pool) {
|
||||||
id
|
|
||||||
shares(where: { id: $shareId }) {
|
|
||||||
id
|
id
|
||||||
|
shares(where: { user: $user }) {
|
||||||
shares
|
shares
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,45 +102,40 @@ export default function Pool(): ReactElement {
|
|||||||
// the purpose of the value is just to trigger the effect
|
// the purpose of the value is just to trigger the effect
|
||||||
const [refreshPool, setRefreshPool] = useState(false)
|
const [refreshPool, setRefreshPool] = useState(false)
|
||||||
|
|
||||||
async function getPoolLiquidity() {
|
const getPoolLiquidity = useCallback(async () => {
|
||||||
const queryContext = getQueryContext(ddo.chainId)
|
if (!ddo?.chainId || !price?.address || !owner) return
|
||||||
const queryVariables = {
|
|
||||||
id: price.address.toLowerCase(),
|
|
||||||
shareId: `${price.address.toLowerCase()}-${ddo.nft.owner.toLowerCase()}`
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const queryVariables = {
|
||||||
|
pool: price.address.toLowerCase(),
|
||||||
|
owner: owner.toLowerCase()
|
||||||
|
}
|
||||||
const queryResult: OperationResult<PoolLiquidity> = await fetchData(
|
const queryResult: OperationResult<PoolLiquidity> = await fetchData(
|
||||||
poolLiquidityQuery,
|
poolLiquidityQuery,
|
||||||
queryVariables,
|
queryVariables,
|
||||||
queryContext
|
getQueryContext(ddo.chainId)
|
||||||
)
|
)
|
||||||
setdataLiquidity(queryResult?.data)
|
setdataLiquidity(queryResult?.data)
|
||||||
}
|
}, [ddo?.chainId, price?.address, owner])
|
||||||
|
|
||||||
async function getUserPoolShareBalance() {
|
async function getUserPoolShareBalance() {
|
||||||
const queryContext = getQueryContext(ddo.chainId)
|
|
||||||
const queryVariables = {
|
const queryVariables = {
|
||||||
id: price.address.toLowerCase(),
|
pool: price.address.toLowerCase(),
|
||||||
shareId: `${price.address.toLowerCase()}-${accountId.toLowerCase()}`
|
user: accountId.toLowerCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryResult: OperationResult<PoolLiquidity> = await fetchData(
|
const queryResult: OperationResult<PoolLiquidity> = await fetchData(
|
||||||
userPoolShareQuery,
|
userPoolShareQuery,
|
||||||
queryVariables,
|
queryVariables,
|
||||||
queryContext
|
getQueryContext(ddo.chainId)
|
||||||
)
|
)
|
||||||
return queryResult?.data.pool.shares[0]?.shares
|
return queryResult?.data.pool.shares[0]?.shares
|
||||||
}
|
}
|
||||||
|
|
||||||
function refetchLiquidity() {
|
const refetchLiquidity = useCallback(() => {
|
||||||
if (!liquidityFetchInterval) {
|
if (liquidityFetchInterval) return
|
||||||
setLiquidityFetchInterval(
|
|
||||||
setInterval(function () {
|
const newInterval = setInterval(() => getPoolLiquidity(), refreshInterval)
|
||||||
getPoolLiquidity()
|
setLiquidityFetchInterval(newInterval)
|
||||||
}, REFETCH_INTERVAL)
|
}, [liquidityFetchInterval, getPoolLiquidity, refreshInterval])
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
@ -249,7 +240,13 @@ export default function Pool(): ReactElement {
|
|||||||
refetchLiquidity()
|
refetchLiquidity()
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [dataLiquidity, ddo, price])
|
}, [
|
||||||
|
dataLiquidity,
|
||||||
|
price?.datatoken,
|
||||||
|
price?.ocean,
|
||||||
|
getPoolLiquidity,
|
||||||
|
refetchLiquidity
|
||||||
|
])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsRemoveDisabled(isInPurgatory && owner === accountId)
|
setIsRemoveDisabled(isInPurgatory && owner === accountId)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user