mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
Fix Your Liquidity linked to wallet network (#825)
* get user pool tokens from subgraph * fix errors * replace consume price with spot price in liquidity calculations * fix dependency error
This commit is contained in:
parent
9b94ed9f6d
commit
3bf8be12e4
@ -67,6 +67,18 @@ const poolLiquidityQuery = gql`
|
||||
}
|
||||
`
|
||||
|
||||
const userPoolShareQuery = gql`
|
||||
query PoolShare($id: ID!, $shareId: ID) {
|
||||
pool(id: $id) {
|
||||
id
|
||||
shares(where: { id: $shareId }) {
|
||||
id
|
||||
balance
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
export default function Pool(): ReactElement {
|
||||
const data = useStaticQuery(contentQuery)
|
||||
const content = data.content.edges[0].node.childContentJson.pool
|
||||
@ -124,6 +136,21 @@ export default function Pool(): ReactElement {
|
||||
setdataLiquidity(queryResult?.data)
|
||||
}
|
||||
|
||||
async function getUserPoolShareBalance() {
|
||||
const queryContext = getQueryContext(ddo.chainId)
|
||||
const queryVariables = {
|
||||
id: price.address.toLowerCase(),
|
||||
shareId: `${price.address.toLowerCase()}-${accountId.toLowerCase()}`
|
||||
}
|
||||
|
||||
const queryResult: OperationResult<PoolLiquidity> = await fetchData(
|
||||
userPoolShareQuery,
|
||||
queryVariables,
|
||||
queryContext
|
||||
)
|
||||
return queryResult?.data.pool.shares[0].balance
|
||||
}
|
||||
|
||||
function refetchLiquidity() {
|
||||
if (!liquidityFetchInterval) {
|
||||
setLiquidityFetchInterval(
|
||||
@ -245,6 +272,7 @@ export default function Pool(): ReactElement {
|
||||
}, [isInPurgatory, owner, accountId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!dataLiquidity) return
|
||||
const poolShare =
|
||||
isValidNumber(poolTokens) &&
|
||||
isValidNumber(totalPoolTokens) &&
|
||||
@ -261,9 +289,11 @@ export default function Pool(): ReactElement {
|
||||
const totalUserLiquidityInOcean =
|
||||
isValidNumber(userLiquidity?.ocean) &&
|
||||
isValidNumber(userLiquidity?.datatoken) &&
|
||||
isValidNumber(price?.value)
|
||||
isValidNumber(dataLiquidity.pool.spotPrice)
|
||||
? new Decimal(userLiquidity?.ocean).add(
|
||||
new Decimal(userLiquidity?.datatoken).mul(price?.value)
|
||||
new Decimal(userLiquidity?.datatoken).mul(
|
||||
dataLiquidity.pool.spotPrice
|
||||
)
|
||||
)
|
||||
: new Decimal(0)
|
||||
|
||||
@ -272,9 +302,9 @@ export default function Pool(): ReactElement {
|
||||
const totalLiquidityInOcean =
|
||||
isValidNumber(price?.ocean) &&
|
||||
isValidNumber(price?.datatoken) &&
|
||||
isValidNumber(price?.value)
|
||||
isValidNumber(dataLiquidity.pool.spotPrice)
|
||||
? new Decimal(price?.ocean).add(
|
||||
new Decimal(price?.datatoken).mul(price?.value)
|
||||
new Decimal(price?.datatoken).mul(dataLiquidity.pool.spotPrice)
|
||||
)
|
||||
: new Decimal(0)
|
||||
|
||||
@ -282,16 +312,13 @@ export default function Pool(): ReactElement {
|
||||
}, [userLiquidity, price, poolTokens, totalPoolTokens])
|
||||
|
||||
useEffect(() => {
|
||||
if (!ocean || !accountId || !price) return
|
||||
if (!accountId || !price) return
|
||||
async function init() {
|
||||
try {
|
||||
//
|
||||
// Get everything the user has put into the pool
|
||||
//
|
||||
const poolTokens = await ocean.pool.sharesBalance(
|
||||
accountId,
|
||||
price.address
|
||||
)
|
||||
const poolTokens = await getUserPoolShareBalance()
|
||||
setPoolTokens(poolTokens)
|
||||
|
||||
// calculate user's provided liquidity based on pool tokens
|
||||
@ -326,7 +353,7 @@ export default function Pool(): ReactElement {
|
||||
}
|
||||
}
|
||||
init()
|
||||
}, [ocean, accountId, price, ddo, refreshPool, owner, totalPoolTokens])
|
||||
}, [accountId, price, ddo, refreshPool, owner, totalPoolTokens])
|
||||
|
||||
const refreshInfo = async () => {
|
||||
setRefreshPool(!refreshPool)
|
||||
|
Loading…
Reference in New Issue
Block a user