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

migrate data in subgraph.ts

This commit is contained in:
Matthias Kretschmann 2022-01-13 21:41:19 +00:00
parent 829333e1e1
commit d78676fa3b
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -23,11 +23,8 @@ import {
PoolShares as PoolSharesList, PoolShares as PoolSharesList,
PoolShares_poolShares as PoolShare PoolShares_poolShares as PoolShare
} from '../@types/apollo/PoolShares' } from '../@types/apollo/PoolShares'
import { OrdersData_tokenOrders as OrdersData } from '../@types/apollo/OrdersData' import { OrdersData_orders as OrdersData } from '../@types/apollo/OrdersData'
import { import { UserSalesQuery as UsersSalesList } from '../@types/apollo/UserSalesQuery'
UserSalesQuery_users as UserSales,
UserSalesQuery as UsersSalesList
} from '../@types/apollo/UserSalesQuery'
export interface UserLiquidity { export interface UserLiquidity {
price: string price: string
@ -118,6 +115,10 @@ const PoolQuery = gql`
address address
symbol symbol
} }
baseToken {
address
symbol
}
datatokenLiquidity datatokenLiquidity
baseTokenLiquidity baseTokenLiquidity
} }
@ -167,6 +168,9 @@ const HighestLiquidityAssets = gql`
datatoken { datatoken {
address address
} }
baseToken {
symbol
}
baseTokenLiquidity baseTokenLiquidity
} }
} }
@ -342,15 +346,15 @@ export async function getPreviousOrders(
} }
const fetchedPreviousOrders: OperationResult<AssetPreviousOrder> = const fetchedPreviousOrders: OperationResult<AssetPreviousOrder> =
await fetchData(PreviousOrderQuery, variables, null) await fetchData(PreviousOrderQuery, variables, null)
if (fetchedPreviousOrders.data?.tokenOrders?.length === 0) return null if (fetchedPreviousOrders.data?.orders?.length === 0) return null
if (assetTimeout === '0') { if (assetTimeout === '0') {
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx return fetchedPreviousOrders?.data?.orders[0]?.tx
} else { } else {
const expiry = const expiry =
fetchedPreviousOrders?.data?.tokenOrders[0]?.timestamp * 1000 + fetchedPreviousOrders?.data?.orders[0]?.createdTimestamp * 1000 +
Number(assetTimeout) * 1000 Number(assetTimeout) * 1000
if (Date.now() <= expiry) { if (Date.now() <= expiry) {
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx return fetchedPreviousOrders?.data?.orders[0]?.tx
} else { } else {
return null return null
} }
@ -366,15 +370,12 @@ function transformPriceToBestPrice(
const price: BestPrice = { const price: BestPrice = {
type: 'dynamic', type: 'dynamic',
address: poolPrice[0]?.id, address: poolPrice[0]?.id,
value: value: poolPrice[0]?.spotPrice,
poolPrice[0]?.consumePrice === '-1' ocean: poolPrice[0]?.baseTokenLiquidity,
? poolPrice[0]?.spotPrice oceanSymbol: poolPrice[0]?.baseToken.symbol,
: poolPrice[0]?.consumePrice, datatoken: poolPrice[0]?.datatokenLiquidity,
ocean: poolPrice[0]?.oceanReserve,
oceanSymbol: poolPrice[0]?.tokens[0]?.symbol,
datatoken: poolPrice[0]?.datatokenReserve,
pools: [poolPrice[0]?.id], pools: [poolPrice[0]?.id],
isConsumable: poolPrice[0]?.consumePrice === '-1' ? 'false' : 'true' isConsumable: poolPrice[0]?.spotPrice === '-1' ? 'false' : 'true'
} }
return price return price
} else if (frePrice?.length > 0) { } else if (frePrice?.length > 0) {
@ -382,10 +383,10 @@ function transformPriceToBestPrice(
// isConsumable: 'true' // isConsumable: 'true'
const price: BestPrice = { const price: BestPrice = {
type: 'fixed', type: 'fixed',
value: frePrice[0]?.rate, value: frePrice[0]?.price,
address: frePrice[0]?.id, address: frePrice[0]?.id,
exchangeId: frePrice[0]?.id, exchangeId: frePrice[0]?.id,
oceanSymbol: frePrice[0]?.baseTokenSymbol, oceanSymbol: frePrice[0]?.baseToken.symbol,
ocean: 0, ocean: 0,
datatoken: 0, datatoken: 0,
pools: [], pools: [],
@ -396,7 +397,7 @@ function transformPriceToBestPrice(
const price: BestPrice = { const price: BestPrice = {
type: 'free', type: 'free',
value: 0, value: 0,
address: freePrice[0]?.datatoken.id, address: freePrice[0]?.token.id,
exchangeId: '', exchangeId: '',
ocean: 0, ocean: 0,
datatoken: 0, datatoken: 0,
@ -501,16 +502,13 @@ export async function getAssetsPriceList(assets: Asset[]): Promise<PriceList> {
const didDTMap: DidAndDatatokenMap = values[3] const didDTMap: DidAndDatatokenMap = values[3]
for (const poolPrice of poolPriceResponse) { for (const poolPrice of poolPriceResponse) {
priceList[didDTMap[poolPrice.datatokenAddress]] = priceList[didDTMap[poolPrice.datatoken.address]] = poolPrice.spotPrice
poolPrice.consumePrice === '-1'
? poolPrice.spotPrice
: poolPrice.consumePrice
} }
for (const frePrice of frePriceResponse) { for (const frePrice of frePriceResponse) {
priceList[didDTMap[frePrice.datatoken?.address]] = frePrice.rate priceList[didDTMap[frePrice.datatoken?.address]] = frePrice.price
} }
for (const freePrice of freePriceResponse) { for (const freePrice of freePriceResponse) {
priceList[didDTMap[freePrice.datatoken?.address]] = '0' priceList[didDTMap[freePrice.token?.address]] = '0'
} }
return priceList return priceList
} }
@ -588,7 +586,7 @@ export async function getAssetsBestPrices(
const frePrice: AssetsFrePriceFixedRateExchange[] = [] const frePrice: AssetsFrePriceFixedRateExchange[] = []
const freePrice: AssetFreePriceDispenser[] = [] const freePrice: AssetFreePriceDispenser[] = []
const pool = poolPriceResponse.find( const pool = poolPriceResponse.find(
(pool: AssetsPoolPricePool) => pool.datatokenAddress === dataToken (pool: AssetsPoolPricePool) => pool.datatoken.address === dataToken
) )
pool && poolPrice.push(pool) pool && poolPrice.push(pool)
const fre = frePriceResponse.find( const fre = frePriceResponse.find(
@ -597,7 +595,7 @@ export async function getAssetsBestPrices(
) )
fre && frePrice.push(fre) fre && frePrice.push(fre)
const free = freePriceResponse.find( const free = freePriceResponse.find(
(free: AssetFreePriceDispenser) => free.datatoken.address === dataToken (free: AssetFreePriceDispenser) => free.token.address === dataToken
) )
free && freePrice.push(free) free && freePrice.push(free)
const bestPrice = transformPriceToBestPrice(frePrice, poolPrice, freePrice) const bestPrice = transformPriceToBestPrice(frePrice, poolPrice, freePrice)
@ -623,22 +621,24 @@ export async function getHighestLiquidityDatatokens(
fetchedPools.data.pools fetchedPools.data.pools
) )
} }
highestLiquidityAssets.sort((a, b) => b.oceanReserve - a.oceanReserve) highestLiquidityAssets.sort(
(a, b) => b.baseTokenLiquidity - a.baseTokenLiquidity
)
for (let i = 0; i < highestLiquidityAssets.length; i++) { for (let i = 0; i < highestLiquidityAssets.length; i++) {
if (!highestLiquidityAssets[i].datatokenAddress) continue if (!highestLiquidityAssets[i].datatoken.address) continue
dtList.push(highestLiquidityAssets[i].datatokenAddress) dtList.push(highestLiquidityAssets[i].datatoken.address)
} }
return dtList return dtList
} }
export function calculateUserLiquidity(poolShare: PoolShare): number { export function calculateUserLiquidity(poolShare: PoolShare): number {
const ocean = const ocean =
(poolShare.balance / poolShare.poolId.totalShares) * (poolShare.shares / poolShare.pool.totalShares) *
poolShare.poolId.oceanReserve poolShare.pool.baseTokenLiquidity
const datatokens = const datatokens =
(poolShare.balance / poolShare.poolId.totalShares) * (poolShare.shares / poolShare.pool.totalShares) *
poolShare.poolId.datatokenReserve poolShare.pool.datatokenLiquidity
const totalLiquidity = ocean + datatokens * poolShare.poolId.spotPrice const totalLiquidity = ocean + datatokens * poolShare.pool.spotPrice
return totalLiquidity return totalLiquidity
} }
@ -661,8 +661,8 @@ export async function getAccountLiquidityInOwnAssets(
for (const result of results) { for (const result of results) {
for (const poolShare of result.poolShares) { for (const poolShare of result.poolShares) {
const userShare = poolShare.balance / poolShare.poolId.totalShares const userShare = poolShare.shares / poolShare.pool.totalShares
const userBalance = userShare * poolShare.poolId.oceanReserve const userBalance = userShare * poolShare.pool.baseTokenLiquidity
totalOceanLiquidity += userBalance totalOceanLiquidity += userBalance
const poolLiquidity = calculateUserLiquidity(poolShare) const poolLiquidity = calculateUserLiquidity(poolShare)
totalLiquidity += poolLiquidity totalLiquidity += poolLiquidity
@ -762,7 +762,7 @@ export async function getTopAssetsPublishers(
if (publishersIndex === -1) { if (publishersIndex === -1) {
const publisher: AccountTeaserVM = { const publisher: AccountTeaserVM = {
address: fetchedUsers.data.users[i].id, address: fetchedUsers.data.users[i].id,
nrSales: fetchedUsers.data.users[i].nrSales nrSales: fetchedUsers.data.users[i].orders.length
} }
publisherSales.push(publisher) publisherSales.push(publisher)
} else { } else {