market/src/utils/subgraph.ts

633 lines
16 KiB
TypeScript
Raw Normal View History

import { gql, OperationResult, TypedDocumentNode, OperationContext } from 'urql'
import { DDO } from '@oceanprotocol/lib'
2021-06-22 07:52:49 +02:00
import { getUrqlClientInstance } from '../providers/UrqlProvider'
import { getOceanConfig } from './ocean'
import web3 from 'web3'
import {
AssetsPoolPrice,
2021-09-01 17:48:54 +02:00
AssetsPoolPrice_pools as AssetsPoolPricePool
} from '../@types/apollo/AssetsPoolPrice'
import {
AssetsFrePrice,
2021-09-01 17:48:54 +02:00
AssetsFrePrice_fixedRateExchanges as AssetsFrePriceFixedRateExchange
} from '../@types/apollo/AssetsFrePrice'
import {
AssetsFreePrice,
AssetsFreePrice_dispensers as AssetFreePriceDispenser
} from '../@types/apollo/AssetsFreePrice'
import { AssetPreviousOrder } from '../@types/apollo/AssetPreviousOrder'
import {
2021-09-01 17:48:54 +02:00
HighestLiquidityAssets_pools as HighestLiquidityAssetsPool,
HighestLiquidityAssets as HighestLiquidityGraphAssets
} from '../@types/apollo/HighestLiquidityAssets'
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
import {
PoolShares as PoolSharesList,
PoolShares_poolShares as PoolShare
} from '../@types/apollo/PoolShares'
import { BestPrice } from '../models/BestPrice'
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
export interface UserTVL {
price: string
oceanBalance: string
}
2021-04-09 11:55:18 +02:00
export interface PriceList {
[key: string]: string
}
export interface AssetListPrices {
ddo: DDO
price: BestPrice
}
interface DidAndDatatokenMap {
[name: string]: string
}
const FreeQuery = gql`
query AssetsFreePrice($datatoken_in: [String!]) {
dispensers(orderBy: id, where: { datatoken_in: $datatoken_in }) {
datatoken {
id
address
}
}
}
`
const AssetFreeQuery = gql`
query AssetFreePrice($datatoken: String) {
dispensers(orderBy: id, where: { datatoken: $datatoken }) {
active
owner {
id
}
minterApproved
isTrueMinter
maxTokens
maxBalance
balance
datatoken {
id
}
}
}
`
const FreQuery = gql`
query AssetsFrePrice($datatoken_in: [String!]) {
2021-04-09 11:55:18 +02:00
fixedRateExchanges(orderBy: id, where: { datatoken_in: $datatoken_in }) {
rate
id
baseTokenSymbol
2021-04-09 11:55:18 +02:00
datatoken {
id
address
symbol
2021-04-09 11:55:18 +02:00
}
}
}
`
const AssetFreQuery = gql`
query AssetFrePrice($datatoken: String) {
fixedRateExchanges(orderBy: id, where: { datatoken: $datatoken }) {
rate
id
baseTokenSymbol
datatoken {
id
address
symbol
}
}
}
`
const PoolQuery = gql`
query AssetsPoolPrice($datatokenAddress_in: [String!]) {
2021-04-09 11:55:18 +02:00
pools(where: { datatokenAddress_in: $datatokenAddress_in }) {
id
spotPrice
consumePrice
2021-04-09 11:55:18 +02:00
datatokenAddress
datatokenReserve
oceanReserve
tokens(where: { isDatatoken: false }) {
isDatatoken
symbol
}
2021-04-09 11:55:18 +02:00
}
}
`
2021-09-01 17:48:54 +02:00
const AssetPoolPriceQuery = gql`
query AssetPoolPrice($datatokenAddress: String) {
pools(where: { datatokenAddress: $datatokenAddress }) {
2021-04-09 11:55:18 +02:00
id
spotPrice
consumePrice
2021-04-09 11:55:18 +02:00
datatokenAddress
datatokenReserve
oceanReserve
tokens {
symbol
}
2021-04-09 11:55:18 +02:00
}
}
`
const PreviousOrderQuery = gql`
query AssetPreviousOrder($id: String!, $account: String!) {
tokenOrders(
first: 1
where: { datatokenId: $id, payer: $account }
orderBy: timestamp
orderDirection: desc
) {
timestamp
tx
}
}
`
const HighestLiquidityAssets = gql`
query HighestLiquidityAssets {
pools(
where: { datatokenReserve_gte: 1 }
orderBy: oceanReserve
orderDirection: desc
first: 15
) {
id
datatokenAddress
valueLocked
oceanReserve
}
}
`
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
const TotalAccountOrders = gql`
query TotalAccountOrders($datatokenId_in: [String!]) {
tokenOrders(where: { datatokenId_in: $datatokenId_in }) {
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
payer {
id
}
datatokenId {
id
}
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
}
}
`
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
const UserSharesQuery = gql`
query UserSharesQuery($user: String, $pools: [String!]) {
poolShares(where: { userAddress: $user, poolId_in: $pools }) {
id
balance
userAddress {
id
}
poolId {
id
datatokenAddress
valueLocked
tokens {
tokenId {
symbol
}
}
oceanReserve
datatokenReserve
totalShares
consumePrice
spotPrice
createTime
}
}
}
`
export function getSubgraphUri(chainId: number): string {
const config = getOceanConfig(chainId)
return config.subgraphUri
}
2021-07-22 14:01:30 +02:00
export function getQueryContext(chainId: number): OperationContext {
const queryContext: OperationContext = {
url: `${getSubgraphUri(
2021-07-22 14:01:30 +02:00
Number(chainId)
)}/subgraphs/name/oceanprotocol/ocean-subgraph`,
requestPolicy: 'cache-and-network'
2021-07-22 14:01:30 +02:00
}
return queryContext
}
export async function fetchData(
2021-06-22 07:52:49 +02:00
query: TypedDocumentNode,
variables: any,
context: OperationContext
): Promise<any> {
2021-04-09 11:55:18 +02:00
try {
2021-06-22 07:52:49 +02:00
const client = getUrqlClientInstance()
const response = await client.query(query, variables, context).toPromise()
2021-04-09 11:55:18 +02:00
return response
} catch (error) {
console.error('Error fetchData: ', error.message)
throw Error(error.message)
}
}
export async function fetchDataForMultipleChains(
query: TypedDocumentNode,
variables: any,
chainIds: number[]
): Promise<any[]> {
let datas: any[] = []
for (const chainId of chainIds) {
const context: OperationContext = {
url: `${getSubgraphUri(
chainId
)}/subgraphs/name/oceanprotocol/ocean-subgraph`,
requestPolicy: 'network-only'
}
try {
const response = await fetchData(query, variables, context)
datas = datas.concat(response.data)
} catch (error) {
console.error('Error fetchData: ', error.message)
}
}
return datas
}
export async function getPreviousOrders(
id: string,
account: string,
assetTimeout: string
): Promise<string> {
const variables = {
id: id,
account: account
}
2021-06-22 07:52:49 +02:00
const fetchedPreviousOrders: OperationResult<AssetPreviousOrder> =
await fetchData(PreviousOrderQuery, variables, null)
if (fetchedPreviousOrders.data?.tokenOrders?.length === 0) return null
if (assetTimeout === '0') {
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx
} else {
const expiry =
fetchedPreviousOrders?.data?.tokenOrders[0]?.timestamp * 1000 +
Number(assetTimeout) * 1000
if (Date.now() <= expiry) {
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx
} else {
return null
}
2021-04-09 11:55:18 +02:00
}
}
function transformPriceToBestPrice(
2021-09-01 17:48:54 +02:00
frePrice: AssetsFrePriceFixedRateExchange[],
poolPrice: AssetsPoolPricePool[],
freePrice: AssetFreePriceDispenser[]
) {
if (poolPrice?.length > 0) {
const price: BestPrice = {
type: 'pool',
address: poolPrice[0]?.id,
value:
poolPrice[0]?.consumePrice === '-1'
? poolPrice[0]?.spotPrice
: poolPrice[0]?.consumePrice,
ocean: poolPrice[0]?.oceanReserve,
oceanSymbol: poolPrice[0]?.tokens[0]?.symbol,
datatoken: poolPrice[0]?.datatokenReserve,
pools: [poolPrice[0]?.id],
isConsumable: poolPrice[0]?.consumePrice === '-1' ? 'false' : 'true'
}
return price
} else if (frePrice?.length > 0) {
// TODO Hacky hack, temporary™: set isConsumable to true for fre assets.
// isConsumable: 'true'
const price: BestPrice = {
type: 'exchange',
value: frePrice[0]?.rate,
address: frePrice[0]?.id,
exchangeId: frePrice[0]?.id,
oceanSymbol: frePrice[0]?.baseTokenSymbol,
ocean: 0,
datatoken: 0,
pools: [],
isConsumable: 'true'
}
return price
} else if (freePrice?.length > 0) {
const price: BestPrice = {
type: 'free',
value: 0,
address: freePrice[0]?.datatoken.id,
exchangeId: '',
ocean: 0,
datatoken: 0,
pools: [],
isConsumable: 'true'
}
return price
} else {
const price: BestPrice = {
type: '',
value: 0,
address: '',
exchangeId: '',
ocean: 0,
datatoken: 0,
pools: [],
isConsumable: 'false'
}
return price
}
}
async function getAssetsPoolsExchangesAndDatatokenMap(
assets: DDO[]
): Promise<
[
2021-09-01 17:48:54 +02:00
AssetsPoolPricePool[],
AssetsFrePriceFixedRateExchange[],
AssetFreePriceDispenser[],
DidAndDatatokenMap
]
> {
const didDTMap: DidAndDatatokenMap = {}
const chainAssetLists: any = {}
for (const ddo of assets) {
didDTMap[ddo?.dataToken.toLowerCase()] = ddo.id
// harcoded until we have chainId on assets
if (chainAssetLists[ddo.chainId]) {
chainAssetLists[ddo.chainId].push(ddo?.dataToken.toLowerCase())
} else {
chainAssetLists[ddo.chainId] = []
chainAssetLists[ddo.chainId].push(ddo?.dataToken.toLowerCase())
}
2021-04-09 11:55:18 +02:00
}
2021-09-01 17:48:54 +02:00
let poolPriceResponse: AssetsPoolPricePool[] = []
let frePriceResponse: AssetsFrePriceFixedRateExchange[] = []
2021-07-05 13:27:43 +02:00
let freePriceResponse: AssetFreePriceDispenser[] = []
for (const chainKey in chainAssetLists) {
const freVariables = {
datatoken_in: chainAssetLists[chainKey]
}
const poolVariables = {
datatokenAddress_in: chainAssetLists[chainKey]
}
2021-07-05 13:27:43 +02:00
const freeVariables = {
datatoken_in: chainAssetLists[chainKey]
}
2021-07-22 14:01:30 +02:00
const queryContext = getQueryContext(Number(chainKey))
const chainPoolPriceResponse: OperationResult<AssetsPoolPrice> =
await fetchData(PoolQuery, poolVariables, queryContext)
poolPriceResponse = poolPriceResponse.concat(
chainPoolPriceResponse.data.pools
)
const chainFrePriceResponse: OperationResult<AssetsFrePrice> =
await fetchData(FreQuery, freVariables, queryContext)
frePriceResponse = frePriceResponse.concat(
chainFrePriceResponse.data.fixedRateExchanges
)
2021-07-05 13:27:43 +02:00
const chainFreePriceResponse: OperationResult<AssetsFreePrice> =
await fetchData(FreeQuery, freeVariables, queryContext)
2021-07-05 13:27:43 +02:00
freePriceResponse = freePriceResponse.concat(
chainFreePriceResponse.data.dispensers
)
}
return [poolPriceResponse, frePriceResponse, freePriceResponse, didDTMap]
}
export async function getAssetsPriceList(assets: DDO[]): Promise<PriceList> {
const priceList: PriceList = {}
const values: [
2021-09-01 17:48:54 +02:00
AssetsPoolPricePool[],
AssetsFrePriceFixedRateExchange[],
AssetFreePriceDispenser[],
DidAndDatatokenMap
] = await getAssetsPoolsExchangesAndDatatokenMap(assets)
const poolPriceResponse = values[0]
const frePriceResponse = values[1]
const freePriceResponse = values[2]
const didDTMap: DidAndDatatokenMap = values[3]
for (const poolPrice of poolPriceResponse) {
priceList[didDTMap[poolPrice.datatokenAddress]] =
poolPrice.consumePrice === '-1'
? poolPrice.spotPrice
: poolPrice.consumePrice
2021-04-09 11:55:18 +02:00
}
for (const frePrice of frePriceResponse) {
priceList[didDTMap[frePrice.datatoken?.address]] = frePrice.rate
2021-04-09 11:55:18 +02:00
}
for (const freePrice of freePriceResponse) {
priceList[didDTMap[freePrice.datatoken?.address]] = '0'
}
2021-04-09 11:55:18 +02:00
return priceList
}
export async function getPrice(asset: DDO): Promise<BestPrice> {
const freVariables = {
datatoken: asset?.dataToken.toLowerCase()
}
2021-07-05 13:27:43 +02:00
const freeVariables = {
datatoken: asset?.dataToken.toLowerCase()
}
const poolVariables = {
datatokenAddress: asset?.dataToken.toLowerCase()
}
2021-07-22 14:01:30 +02:00
const queryContext = getQueryContext(Number(asset.chainId))
2021-06-22 07:52:49 +02:00
const poolPriceResponse: OperationResult<AssetsPoolPrice> = await fetchData(
2021-09-01 17:48:54 +02:00
AssetPoolPriceQuery,
poolVariables,
queryContext
)
2021-06-22 07:52:49 +02:00
const frePriceResponse: OperationResult<AssetsFrePrice> = await fetchData(
AssetFreQuery,
freVariables,
queryContext
)
const freePriceResponse: OperationResult<AssetsFreePrice> = await fetchData(
AssetFreeQuery,
2021-07-05 13:27:43 +02:00
freeVariables,
queryContext
)
const bestPrice: BestPrice = transformPriceToBestPrice(
frePriceResponse.data.fixedRateExchanges,
poolPriceResponse.data.pools,
freePriceResponse.data.dispensers
)
return bestPrice
}
export async function getSpotPrice(asset: DDO): Promise<number> {
const poolVariables = {
datatokenAddress: asset?.dataToken.toLowerCase()
}
const queryContext = getQueryContext(Number(asset.chainId))
const poolPriceResponse: OperationResult<AssetsPoolPrice> = await fetchData(
2021-09-01 17:48:54 +02:00
AssetPoolPriceQuery,
poolVariables,
queryContext
)
return poolPriceResponse.data.pools[0].spotPrice
}
export async function getAssetsBestPrices(
assets: DDO[]
): Promise<AssetListPrices[]> {
const assetsWithPrice: AssetListPrices[] = []
const values: [
2021-09-01 17:48:54 +02:00
AssetsPoolPricePool[],
AssetsFrePriceFixedRateExchange[],
AssetFreePriceDispenser[],
DidAndDatatokenMap
] = await getAssetsPoolsExchangesAndDatatokenMap(assets)
const poolPriceResponse = values[0]
const frePriceResponse = values[1]
const freePriceResponse = values[2]
for (const ddo of assets) {
const dataToken = ddo.dataToken.toLowerCase()
2021-09-01 17:48:54 +02:00
const poolPrice: AssetsPoolPricePool[] = []
const frePrice: AssetsFrePriceFixedRateExchange[] = []
const freePrice: AssetFreePriceDispenser[] = []
const pool = poolPriceResponse.find(
2021-09-01 17:48:54 +02:00
(pool: AssetsPoolPricePool) => pool.datatokenAddress === dataToken
)
pool && poolPrice.push(pool)
const fre = frePriceResponse.find(
2021-09-01 17:48:54 +02:00
(fre: AssetsFrePriceFixedRateExchange) =>
fre.datatoken.address === dataToken
)
fre && frePrice.push(fre)
const free = freePriceResponse.find(
2021-09-01 17:48:54 +02:00
(free: AssetFreePriceDispenser) => free.datatoken.address === dataToken
)
free && freePrice.push(free)
const bestPrice = transformPriceToBestPrice(frePrice, poolPrice, freePrice)
assetsWithPrice.push({
ddo: ddo,
price: bestPrice
})
}
return assetsWithPrice
}
export async function getHighestLiquidityDIDs(
chainIds: number[]
): Promise<[string, number]> {
const didList: string[] = []
2021-09-01 17:48:54 +02:00
let highestLiquidityAssets: HighestLiquidityAssetsPool[] = []
for (const chain of chainIds) {
2021-07-22 14:01:30 +02:00
const queryContext = getQueryContext(Number(chain))
const fetchedPools: OperationResult<HighestLiquidityGraphAssets, any> =
await fetchData(HighestLiquidityAssets, null, queryContext)
2021-09-01 17:48:54 +02:00
highestLiquidityAssets = highestLiquidityAssets.concat(
fetchedPools.data.pools
)
}
2021-09-01 17:48:54 +02:00
highestLiquidityAssets
.sort((a, b) => a.oceanReserve - b.oceanReserve)
.reverse()
2021-09-01 17:48:54 +02:00
for (let i = 0; i < highestLiquidityAssets.length; i++) {
if (!highestLiquidityAssets[i].datatokenAddress) continue
const did = web3.utils
2021-09-01 17:48:54 +02:00
.toChecksumAddress(highestLiquidityAssets[i].datatokenAddress)
.replace('0x', 'did:op:')
didList.push(did)
}
const searchDids = JSON.stringify(didList)
.replace(/,/g, ' ')
.replace(/"/g, '')
.replace(/(\[|\])/g, '')
.replace(/(did:op:)/g, '0x')
return [searchDids, didList.length]
}
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
export async function getAccountNumberOfOrders(
assets: DDO[],
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
chainIds: number[]
): Promise<number> {
const datatokens: string[] = []
assets.forEach((ddo) => {
datatokens.push(ddo?.dataToken?.toLowerCase())
})
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
const queryVariables = {
datatokenId_in: datatokens
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
}
const results = await fetchDataForMultipleChains(
TotalAccountOrders,
queryVariables,
chainIds
)
let numberOfOrders = 0
for (const result of results) {
numberOfOrders += result?.tokenOrders?.length
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
}
return numberOfOrders
}
export function calculateUserLiquidity(poolShare: PoolShare): number {
Account metadata header (#776) * get all neded data for the header from 3box, aqua and subgraph * fix tvl display error * WIP metadata header styling * added more styling for the header * make page title optional so we can remove it on account page * stroke change for svg images and default values * more styling added to the header * fixed linter * added ocean balance to tvl * update styling for statistcs * fixed eror for go to my account from another account page * updated styling for mobile use * wip show more on explorer links and description * properly display read more for explorer links and description * replaced show more with 3box redirect on description * change accounts default picture and check links length before display element * use optional on links * grid cleanup, new number unit, split up stats * rename all the things, more profile header styling * visual hierarchy, improve image loading experience * layout flow & visual tweaks * more description * replaced account route with profile when accesing a profile by the eth address * use account id from url if exists when fetching data * bump @oceanprotocol/art to v3.2.0 * styling, fallbacks, edge case fixes * clean up Publisher atom, link to profile page * fixed issue when switching to my profile from another profile * output accountId, make it copyable, remove stats icons * render tweaks, markup cleanup * add 3box reference * mobile tabs spacing tweaks * text flow and spacing tweaks Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-09-01 13:56:34 +02:00
const ocean =
(poolShare.balance / poolShare.poolId.totalShares) *
poolShare.poolId.oceanReserve
const datatokens =
(poolShare.balance / poolShare.poolId.totalShares) *
poolShare.poolId.datatokenReserve
const totalLiquidity = ocean + datatokens * poolShare.poolId.consumePrice
return totalLiquidity
}
export async function getAccountLiquidityInOwnAssets(
accountId: string,
chainIds: number[],
pools: string[]
): Promise<UserTVL> {
const queryVariables = {
user: accountId.toLowerCase(),
pools: pools
}
const results: PoolSharesList[] = await fetchDataForMultipleChains(
UserSharesQuery,
queryVariables,
chainIds
)
let totalLiquidity = 0
let totalOceanLiquidity = 0
for (const result of results) {
for (const poolShare of result.poolShares) {
const userShare = poolShare.balance / poolShare.poolId.totalShares
const userBalance = userShare * poolShare.poolId.oceanReserve
totalOceanLiquidity += userBalance
const poolLiquidity = calculateUserLiquidity(poolShare)
totalLiquidity += poolLiquidity
}
}
return {
price: totalLiquidity.toString(),
oceanBalance: totalOceanLiquidity.toString()
}
}