1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

calculate user liquidity with subgraph values (#1053)

This commit is contained in:
Matthias Kretschmann 2022-02-02 11:49:59 +00:00 committed by GitHub
parent 2fb7ed3516
commit ef6a5fda7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 65 deletions

View File

@ -1,5 +1,5 @@
interface PoolBalance {
ocean: string
baseToken: string
datatoken: string
}

View File

@ -35,7 +35,9 @@ export default function Output({
return
const newPoolSupply = new Decimal(totalPoolTokens).plus(newPoolTokens)
const ratio = new Decimal(newPoolTokens).div(newPoolSupply)
const newOceanReserve = new Decimal(totalBalance.ocean).plus(values.amount)
const newOceanReserve = new Decimal(totalBalance.baseToken).plus(
values.amount
)
const newDtReserve = new Decimal(totalBalance.datatoken)
const poolOcean = newOceanReserve.mul(ratio).toString()
const poolDatatoken = newDtReserve.mul(ratio).toString()

View File

@ -136,7 +136,7 @@ export default function Pool(): ReactElement {
// 1 General Pool Info
//
useEffect(() => {
if (!poolData || !price?.ocean || !price?.datatoken) return
if (!poolData) return
// Pool Fee (swap fee)
// poolFee is tricky: to get 0.1% you need to convert from 0.001
@ -145,14 +145,11 @@ export default function Pool(): ReactElement {
: '0'
// Total Liquidity
const totalLiquidityInOcean =
isValidNumber(price.ocean) &&
isValidNumber(price.datatoken) &&
isValidNumber(poolData.spotPrice)
? new Decimal(price.ocean).add(
new Decimal(price.datatoken).mul(poolData.spotPrice)
)
: new Decimal(0)
const totalLiquidityInOcean = isValidNumber(poolData.spotPrice)
? new Decimal(poolData.baseTokenLiquidity).add(
new Decimal(poolData.datatokenLiquidity).mul(poolData.spotPrice)
)
: new Decimal(0)
const newPoolInfo = {
poolFee,
@ -166,52 +163,46 @@ export default function Pool(): ReactElement {
}
setPoolInfo(newPoolInfo)
LoggerInstance.log('[pool] Created new pool info:', newPoolInfo)
}, [poolData, price?.datatoken, price?.ocean])
}, [poolData])
//
// 2 Pool Creator Info
//
useEffect(() => {
if (
!poolData ||
!poolInfo?.totalPoolTokens ||
!price?.ocean ||
!price?.datatoken
)
return
if (!poolData || !poolInfo?.totalPoolTokens) return
const ownerPoolTokens = poolData.shares[0]?.shares
const ownerOceanBalance =
const ownerBaseTokenBalance =
isValidNumber(ownerPoolTokens) &&
isValidNumber(poolInfo.totalPoolTokens) &&
isValidNumber(price.ocean)
isValidNumber(poolData.baseTokenLiquidity)
? new Decimal(ownerPoolTokens)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(price.ocean)
.mul(poolData.baseTokenLiquidity)
.toString()
: '0'
const ownerDtBalance =
isValidNumber(ownerPoolTokens) &&
isValidNumber(poolInfo.totalPoolTokens) &&
isValidNumber(price.datatoken)
isValidNumber(poolData.datatokenLiquidity)
? new Decimal(ownerPoolTokens)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(price.datatoken)
.mul(poolData.datatokenLiquidity)
.toString()
: '0'
const liquidity = {
ocean: ownerOceanBalance,
baseToken: ownerBaseTokenBalance,
datatoken: ownerDtBalance
}
const totalLiquidityInOcean =
isValidNumber(liquidity.ocean) &&
isValidNumber(liquidity.baseToken) &&
isValidNumber(liquidity.datatoken) &&
isValidNumber(poolData.spotPrice)
? new Decimal(liquidity.ocean).add(
? new Decimal(liquidity.baseToken).add(
new Decimal(liquidity.datatoken).mul(
new Decimal(poolData.spotPrice)
)
@ -236,20 +227,13 @@ export default function Pool(): ReactElement {
}
setPoolInfoOwner(newPoolOwnerInfo)
LoggerInstance.log('[pool] Created new owner pool info:', newPoolOwnerInfo)
}, [poolData, price?.ocean, price?.datatoken, poolInfo?.totalPoolTokens])
}, [poolData, poolInfo?.totalPoolTokens])
//
// 3 User Pool Info
//
useEffect(() => {
if (
!poolData?.spotPrice ||
!poolInfo?.totalPoolTokens ||
!ddo?.chainId ||
!accountId ||
!price?.ocean ||
!price?.datatoken
)
if (!poolData || !poolInfo?.totalPoolTokens || !ddo?.chainId || !accountId)
return
const poolShare =
@ -263,36 +247,36 @@ export default function Pool(): ReactElement {
setUserHasAddedLiquidity(Number(poolShare) > 0)
// calculate user's provided liquidity based on pool tokens
const userOceanBalance =
const userBaseTokenBalance =
isValidNumber(poolInfoUser.poolShares) &&
isValidNumber(poolInfo.totalPoolTokens) &&
isValidNumber(price.ocean)
isValidNumber(poolData.baseTokenLiquidity)
? new Decimal(poolInfoUser.poolShares)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(price.ocean)
.mul(poolData.baseTokenLiquidity)
.toString()
: '0'
const userDtBalance =
isValidNumber(poolInfoUser.poolShares) &&
isValidNumber(poolInfo.totalPoolTokens) &&
isValidNumber(price.datatoken)
isValidNumber(poolData.datatokenLiquidity)
? new Decimal(poolInfoUser.poolShares)
.dividedBy(new Decimal(poolInfo.totalPoolTokens))
.mul(price.datatoken)
.mul(poolData.datatokenLiquidity)
.toString()
: '0'
const liquidity = {
ocean: userOceanBalance,
baseToken: userBaseTokenBalance,
datatoken: userDtBalance
}
const totalLiquidityInOcean =
isValidNumber(liquidity.ocean) &&
isValidNumber(liquidity.baseToken) &&
isValidNumber(liquidity.datatoken) &&
isValidNumber(poolData.spotPrice)
? new Decimal(liquidity.ocean).add(
? new Decimal(liquidity.baseToken).add(
new Decimal(liquidity.datatoken).mul(poolData.spotPrice)
)
: new Decimal(0)
@ -312,10 +296,9 @@ export default function Pool(): ReactElement {
...newPoolInfoUser
})
}, [
poolData?.spotPrice,
poolData,
poolInfoUser?.poolShares,
accountId,
price,
ddo?.chainId,
owner,
poolInfo?.totalPoolTokens
@ -337,8 +320,8 @@ export default function Pool(): ReactElement {
poolAddress={price?.address}
totalPoolTokens={poolInfo?.totalPoolTokens}
totalBalance={{
ocean: new Decimal(price?.ocean).toString(),
datatoken: new Decimal(price?.datatoken).toString()
baseToken: new Decimal(poolData?.baseTokenLiquidity).toString(),
datatoken: new Decimal(poolData?.datatokenLiquidity).toString()
}}
swapFee={poolInfo?.poolFee}
datatokenSymbol={poolInfo?.datatokenSymbol}
@ -361,7 +344,7 @@ export default function Pool(): ReactElement {
<div className={styles.dataToken}>
<PriceUnit price="1" symbol={poolInfo?.datatokenSymbol} /> ={' '}
<PriceUnit
price={`${price?.value}`}
price={`${poolData?.spotPrice}`}
symbol={poolInfo?.baseTokenSymbol}
/>
<Tooltip content={content.pool.tooltips.price} />
@ -397,7 +380,7 @@ export default function Pool(): ReactElement {
/>
</>
}
baseTokenValue={`${poolInfoUser?.liquidity?.ocean}`}
baseTokenValue={`${poolInfoUser?.liquidity?.baseToken}`}
baseTokenSymbol={poolInfo?.baseTokenSymbol}
datatokenValue={`${poolInfoUser?.liquidity?.datatoken}`}
datatokenSymbol={poolInfo?.datatokenSymbol}
@ -414,7 +397,7 @@ export default function Pool(): ReactElement {
<TokenList
title="Pool Creator Statistics"
baseTokenValue={`${poolInfoOwner?.liquidity?.ocean}`}
baseTokenValue={`${poolInfoOwner?.liquidity?.baseToken}`}
baseTokenSymbol={poolInfo?.baseTokenSymbol}
datatokenValue={`${poolInfoOwner?.liquidity?.datatoken}`}
datatokenSymbol={poolInfo?.datatokenSymbol}
@ -443,9 +426,9 @@ export default function Pool(): ReactElement {
<Graph poolSnapshots={poolSnapshots} />
</>
}
baseTokenValue={`${price?.ocean}`}
baseTokenValue={`${poolData?.baseTokenLiquidity}`}
baseTokenSymbol={poolInfo?.baseTokenSymbol}
datatokenValue={`${price?.datatoken}`}
datatokenValue={`${poolData?.datatokenLiquidity}`}
datatokenSymbol={poolInfo?.datatokenSymbol}
poolShares={poolInfo?.totalPoolTokens}
conversion={poolInfo?.totalLiquidityInOcean}

View File

@ -139,7 +139,7 @@ export default function FormTrade({
!isWarningAccepted ||
!isAssetNetwork ||
values.datatoken === undefined ||
values.ocean === undefined
values.baseToken === undefined
}
isLoading={isSubmitting}
loaderMessage="Swapping tokens..."
@ -150,8 +150,8 @@ export default function FormTrade({
? values.datatoken
? `${values.datatoken}`
: undefined
: values.ocean
? `${values.ocean}`
: values.baseToken
? `${values.baseToken}`
: undefined
}
action={submitForm}

View File

@ -40,8 +40,8 @@ export default function Output({
const value =
values.type === 'buy'
? isValidNumber(swapFee) && isValidNumber(values.ocean)
? new Decimal(swapFee).mul(new Decimal(values.ocean))
? isValidNumber(swapFee) && isValidNumber(values.baseToken)
? new Decimal(swapFee).mul(new Decimal(values.baseToken))
: 0
: isValidNumber(swapFee) && isValidNumber(values.datatoken)
? new Decimal(swapFee).mul(new Decimal(values.datatoken))
@ -49,7 +49,7 @@ export default function Output({
setSwapFeeValue(value.toString())
}
getSwapFee()
}, [poolAddress, values, isAssetNetwork])
}, [poolAddress, values, isAssetNetwork, swapFee])
// Get output values
useEffect(() => {
@ -66,8 +66,8 @@ export default function Output({
.mul(new Decimal(maxImpact))
.toString()
: '0'
: isValidNumber(values.ocean) && isValidNumber(maxImpact)
? new Decimal(values.ocean).mul(new Decimal(maxImpact)).toString()
: isValidNumber(values.baseToken) && isValidNumber(maxImpact)
? new Decimal(values.baseToken).mul(new Decimal(maxImpact)).toString()
: '0'
setMaxOutput(maxPrice)

View File

@ -68,7 +68,7 @@ export default function Swap({
: new Decimal(balance.datatoken)
const amountOcean =
values.type === 'buy'
? new Decimal(balance.ocean)
? new Decimal(balance.baseToken)
: new Decimal(maxOcean)
// const maxBuyOcean = await ocean.pool.getOceanReceived(

View File

@ -1,7 +1,7 @@
import { FormTradeData } from './_types'
export const initialValues: FormTradeData = {
ocean: undefined,
baseToken: undefined,
datatoken: undefined,
type: 'buy',
slippage: '5'

View File

@ -36,7 +36,7 @@ export default function Trade(): ReactElement {
accountId
)
setTokenBalance({
ocean: new Decimal(balance.ocean).toString(),
baseToken: new Decimal(balance.ocean).toString(),
datatoken: new Decimal(dtBalance).toString()
})
}

View File

@ -8,7 +8,7 @@ import { getQueryContext } from '@utils/subgraph'
import styles from './EditHistory.module.css'
const getReceipts = gql`
query ReceiptData($address: ID!) {
query ReceiptData($address: String!) {
nftUpdates(
where: { nft: $address }
orderBy: timestamp