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 {
|
export default function Pool(): ReactElement {
|
||||||
const data = useStaticQuery(contentQuery)
|
const data = useStaticQuery(contentQuery)
|
||||||
const content = data.content.edges[0].node.childContentJson.pool
|
const content = data.content.edges[0].node.childContentJson.pool
|
||||||
@ -124,6 +136,21 @@ export default function Pool(): ReactElement {
|
|||||||
setdataLiquidity(queryResult?.data)
|
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() {
|
function refetchLiquidity() {
|
||||||
if (!liquidityFetchInterval) {
|
if (!liquidityFetchInterval) {
|
||||||
setLiquidityFetchInterval(
|
setLiquidityFetchInterval(
|
||||||
@ -245,6 +272,7 @@ export default function Pool(): ReactElement {
|
|||||||
}, [isInPurgatory, owner, accountId])
|
}, [isInPurgatory, owner, accountId])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!dataLiquidity) return
|
||||||
const poolShare =
|
const poolShare =
|
||||||
isValidNumber(poolTokens) &&
|
isValidNumber(poolTokens) &&
|
||||||
isValidNumber(totalPoolTokens) &&
|
isValidNumber(totalPoolTokens) &&
|
||||||
@ -261,9 +289,11 @@ export default function Pool(): ReactElement {
|
|||||||
const totalUserLiquidityInOcean =
|
const totalUserLiquidityInOcean =
|
||||||
isValidNumber(userLiquidity?.ocean) &&
|
isValidNumber(userLiquidity?.ocean) &&
|
||||||
isValidNumber(userLiquidity?.datatoken) &&
|
isValidNumber(userLiquidity?.datatoken) &&
|
||||||
isValidNumber(price?.value)
|
isValidNumber(dataLiquidity.pool.spotPrice)
|
||||||
? new Decimal(userLiquidity?.ocean).add(
|
? new Decimal(userLiquidity?.ocean).add(
|
||||||
new Decimal(userLiquidity?.datatoken).mul(price?.value)
|
new Decimal(userLiquidity?.datatoken).mul(
|
||||||
|
dataLiquidity.pool.spotPrice
|
||||||
|
)
|
||||||
)
|
)
|
||||||
: new Decimal(0)
|
: new Decimal(0)
|
||||||
|
|
||||||
@ -272,9 +302,9 @@ export default function Pool(): ReactElement {
|
|||||||
const totalLiquidityInOcean =
|
const totalLiquidityInOcean =
|
||||||
isValidNumber(price?.ocean) &&
|
isValidNumber(price?.ocean) &&
|
||||||
isValidNumber(price?.datatoken) &&
|
isValidNumber(price?.datatoken) &&
|
||||||
isValidNumber(price?.value)
|
isValidNumber(dataLiquidity.pool.spotPrice)
|
||||||
? new Decimal(price?.ocean).add(
|
? new Decimal(price?.ocean).add(
|
||||||
new Decimal(price?.datatoken).mul(price?.value)
|
new Decimal(price?.datatoken).mul(dataLiquidity.pool.spotPrice)
|
||||||
)
|
)
|
||||||
: new Decimal(0)
|
: new Decimal(0)
|
||||||
|
|
||||||
@ -282,16 +312,13 @@ export default function Pool(): ReactElement {
|
|||||||
}, [userLiquidity, price, poolTokens, totalPoolTokens])
|
}, [userLiquidity, price, poolTokens, totalPoolTokens])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ocean || !accountId || !price) return
|
if (!accountId || !price) return
|
||||||
async function init() {
|
async function init() {
|
||||||
try {
|
try {
|
||||||
//
|
//
|
||||||
// Get everything the user has put into the pool
|
// Get everything the user has put into the pool
|
||||||
//
|
//
|
||||||
const poolTokens = await ocean.pool.sharesBalance(
|
const poolTokens = await getUserPoolShareBalance()
|
||||||
accountId,
|
|
||||||
price.address
|
|
||||||
)
|
|
||||||
setPoolTokens(poolTokens)
|
setPoolTokens(poolTokens)
|
||||||
|
|
||||||
// calculate user's provided liquidity based on pool tokens
|
// calculate user's provided liquidity based on pool tokens
|
||||||
@ -326,7 +353,7 @@ export default function Pool(): ReactElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [ocean, accountId, price, ddo, refreshPool, owner, totalPoolTokens])
|
}, [accountId, price, ddo, refreshPool, owner, totalPoolTokens])
|
||||||
|
|
||||||
const refreshInfo = async () => {
|
const refreshInfo = async () => {
|
||||||
setRefreshPool(!refreshPool)
|
setRefreshPool(!refreshPool)
|
||||||
|
Loading…
Reference in New Issue
Block a user