2021-06-25 09:50:56 +02:00
|
|
|
import { gql, OperationResult, TypedDocumentNode, OperationContext } from 'urql'
|
2021-04-20 08:44:18 +02:00
|
|
|
import { DDO, BestPrice } from '@oceanprotocol/lib'
|
2021-06-22 07:52:49 +02:00
|
|
|
import { getUrqlClientInstance } from '../providers/UrqlProvider'
|
2021-06-25 09:50:56 +02:00
|
|
|
import { getOceanConfig } from './ocean'
|
2021-06-02 11:14:54 +02:00
|
|
|
import web3 from 'web3'
|
2021-07-09 10:36:43 +02:00
|
|
|
import {
|
|
|
|
AssetsPoolPrice,
|
|
|
|
AssetsPoolPrice_pools as AssetsPoolPricePools
|
|
|
|
} from '../@types/apollo/AssetsPoolPrice'
|
|
|
|
import {
|
|
|
|
AssetsFrePrice,
|
|
|
|
AssetsFrePrice_fixedRateExchanges as AssetsFrePriceFixedRateExchanges
|
|
|
|
} from '../@types/apollo/AssetsFrePrice'
|
|
|
|
import {
|
|
|
|
AssetsFreePrice,
|
|
|
|
AssetsFreePrice_dispensers as AssetFreePriceDispenser
|
|
|
|
} from '../@types/apollo/AssetsFreePrice'
|
2021-07-06 00:09:51 +02:00
|
|
|
import { AssetPreviousOrder } from '../@types/apollo/AssetPreviousOrder'
|
2021-07-15 17:25:53 +02:00
|
|
|
import {
|
|
|
|
HighestLiquidityAssets_pools as HighestLiquidityAssetsPools,
|
|
|
|
HighestLiquidityAssets as HighestLiquidityGraphAssets
|
|
|
|
} from '../@types/apollo/HighestLiquidityAssets'
|
2021-04-09 11:55:18 +02:00
|
|
|
|
2021-04-14 13:41:17 +02:00
|
|
|
export interface PriceList {
|
|
|
|
[key: string]: string
|
|
|
|
}
|
|
|
|
|
2021-05-17 16:08:15 +02:00
|
|
|
export interface AssetListPrices {
|
|
|
|
ddo: DDO
|
|
|
|
price: BestPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DidAndDatatokenMap {
|
|
|
|
[name: string]: string
|
|
|
|
}
|
|
|
|
|
2021-06-16 03:32:11 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-04-20 08:44:18 +02:00
|
|
|
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
|
|
|
|
datatoken {
|
|
|
|
id
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-04-20 08:44:18 +02:00
|
|
|
const AssetFreQuery = gql`
|
|
|
|
query AssetFrePrice($datatoken: String) {
|
|
|
|
fixedRateExchanges(orderBy: id, where: { datatoken: $datatoken }) {
|
|
|
|
rate
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const PoolQuery = gql`
|
|
|
|
query AssetsPoolPrice($datatokenAddress_in: [String!]) {
|
2021-04-09 11:55:18 +02:00
|
|
|
pools(where: { datatokenAddress_in: $datatokenAddress_in }) {
|
|
|
|
id
|
|
|
|
spotPrice
|
2021-04-23 15:44:31 +02:00
|
|
|
consumePrice
|
2021-04-09 11:55:18 +02:00
|
|
|
datatokenAddress
|
2021-05-17 16:08:15 +02:00
|
|
|
datatokenReserve
|
|
|
|
oceanReserve
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-04-20 08:44:18 +02:00
|
|
|
const AssetPoolPriceQuerry = gql`
|
|
|
|
query AssetPoolPrice($datatokenAddress: String) {
|
|
|
|
pools(where: { datatokenAddress: $datatokenAddress }) {
|
2021-04-09 11:55:18 +02:00
|
|
|
id
|
2021-04-20 08:44:18 +02:00
|
|
|
spotPrice
|
2021-04-22 13:03:58 +02:00
|
|
|
consumePrice
|
2021-04-09 11:55:18 +02:00
|
|
|
datatokenAddress
|
2021-04-20 08:44:18 +02:00
|
|
|
datatokenReserve
|
|
|
|
oceanReserve
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-04-20 08:44:18 +02:00
|
|
|
const PreviousOrderQuery = gql`
|
2021-04-11 01:13:43 +02:00
|
|
|
query AssetPreviousOrder($id: String!, $account: String!) {
|
|
|
|
tokenOrders(
|
|
|
|
first: 1
|
|
|
|
where: { datatokenId: $id, payer: $account }
|
|
|
|
orderBy: timestamp
|
|
|
|
orderDirection: desc
|
|
|
|
) {
|
|
|
|
timestamp
|
|
|
|
tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2021-06-02 11:14:54 +02:00
|
|
|
const HighestLiquidityAssets = gql`
|
2021-07-15 17:25:53 +02:00
|
|
|
query HighestLiquidityAssets {
|
2021-06-04 13:23:18 +02:00
|
|
|
pools(
|
|
|
|
where: { datatokenReserve_gte: 1 }
|
2021-07-15 17:25:53 +02:00
|
|
|
orderBy: oceanReserve
|
2021-06-04 13:23:18 +02:00
|
|
|
orderDirection: desc
|
|
|
|
first: 15
|
|
|
|
) {
|
2021-06-02 11:14:54 +02:00
|
|
|
id
|
|
|
|
datatokenAddress
|
|
|
|
valueLocked
|
2021-07-15 17:25:53 +02:00
|
|
|
oceanReserve
|
2021-06-02 11:14:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2021-04-11 01:13:43 +02:00
|
|
|
|
2021-07-22 13:02:32 +02:00
|
|
|
export function getSubgrahUri(chainId: number): string {
|
2021-06-25 09:50:56 +02:00
|
|
|
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: `${getSubgrahUri(
|
|
|
|
Number(chainId)
|
|
|
|
)}/subgraphs/name/oceanprotocol/ocean-subgraph`,
|
|
|
|
requestPolicy: 'network-only'
|
|
|
|
}
|
|
|
|
|
|
|
|
return queryContext
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:02:32 +02:00
|
|
|
export async function fetchData(
|
2021-06-22 07:52:49 +02:00
|
|
|
query: TypedDocumentNode,
|
2021-06-25 09:50:56 +02:00
|
|
|
variables: any,
|
|
|
|
context: OperationContext
|
2021-07-22 13:07:52 +02:00
|
|
|
): Promise<any> {
|
2021-04-09 11:55:18 +02:00
|
|
|
try {
|
2021-06-22 07:52:49 +02:00
|
|
|
const client = getUrqlClientInstance()
|
2021-06-25 09:50:56 +02:00
|
|
|
const response = await client.query(query, variables, context).toPromise()
|
2021-04-09 11:55:18 +02:00
|
|
|
return response
|
|
|
|
} catch (error) {
|
2021-04-11 01:13:43 +02:00
|
|
|
console.error('Error fetchData: ', error.message)
|
2021-07-22 13:02:32 +02:00
|
|
|
throw Error(error.message)
|
2021-04-11 01:13:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:46:25 +02:00
|
|
|
export async function fetchDataForMultipleChains(
|
|
|
|
query: TypedDocumentNode,
|
|
|
|
variables: any,
|
|
|
|
chainIds: number[]
|
|
|
|
): Promise<any[]> {
|
|
|
|
let datas: any[] = []
|
|
|
|
for (const chainId of chainIds) {
|
|
|
|
const context: OperationContext = {
|
|
|
|
url: `${getSubgrahUri(
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-11 01:13:43 +02:00
|
|
|
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> =
|
2021-06-25 09:50:56 +02:00
|
|
|
await fetchData(PreviousOrderQuery, variables, null)
|
2021-04-12 16:09:41 +02:00
|
|
|
if (fetchedPreviousOrders.data?.tokenOrders?.length === 0) return null
|
|
|
|
if (assetTimeout === '0') {
|
|
|
|
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx
|
2021-04-11 01:13:43 +02:00
|
|
|
} else {
|
2021-05-07 15:00:40 +02:00
|
|
|
const expiry =
|
|
|
|
fetchedPreviousOrders?.data?.tokenOrders[0]?.timestamp * 1000 +
|
|
|
|
Number(assetTimeout) * 1000
|
|
|
|
if (Date.now() <= expiry) {
|
2021-04-12 16:09:41 +02:00
|
|
|
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx
|
2021-04-11 01:13:43 +02:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 16:08:15 +02:00
|
|
|
function transformPriceToBestPrice(
|
|
|
|
frePrice: AssetsFrePriceFixedRateExchanges[],
|
2021-06-16 03:32:11 +02:00
|
|
|
poolPrice: AssetsPoolPricePools[],
|
|
|
|
freePrice: AssetFreePriceDispenser[]
|
2021-05-17 16:08:15 +02:00
|
|
|
) {
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
exchange_id: frePrice[0]?.id,
|
|
|
|
ocean: 0,
|
|
|
|
datatoken: 0,
|
|
|
|
pools: [],
|
|
|
|
isConsumable: 'true'
|
|
|
|
}
|
|
|
|
return price
|
2021-06-16 03:32:11 +02:00
|
|
|
} else if (freePrice?.length > 0) {
|
|
|
|
const price: BestPrice = {
|
|
|
|
type: 'free',
|
|
|
|
value: 0,
|
|
|
|
address: freePrice[0]?.datatoken.id,
|
|
|
|
exchange_id: '',
|
|
|
|
ocean: 0,
|
|
|
|
datatoken: 0,
|
|
|
|
pools: [],
|
|
|
|
isConsumable: 'true'
|
|
|
|
}
|
|
|
|
return price
|
2021-05-17 16:08:15 +02:00
|
|
|
} else {
|
|
|
|
const price: BestPrice = {
|
|
|
|
type: '',
|
|
|
|
value: 0,
|
|
|
|
address: '',
|
|
|
|
exchange_id: '',
|
|
|
|
ocean: 0,
|
|
|
|
datatoken: 0,
|
|
|
|
pools: [],
|
|
|
|
isConsumable: 'false'
|
|
|
|
}
|
|
|
|
return price
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAssetsPoolsExchangesAndDatatokenMap(
|
|
|
|
assets: DDO[]
|
|
|
|
): Promise<
|
|
|
|
[
|
2021-07-09 10:36:43 +02:00
|
|
|
AssetsPoolPricePools[],
|
|
|
|
AssetsFrePriceFixedRateExchanges[],
|
|
|
|
AssetFreePriceDispenser[],
|
2021-05-17 16:08:15 +02:00
|
|
|
DidAndDatatokenMap
|
|
|
|
]
|
|
|
|
> {
|
|
|
|
const didDTMap: DidAndDatatokenMap = {}
|
2021-06-25 09:50:56 +02:00
|
|
|
const chainAssetLists: any = {}
|
2021-04-13 18:15:09 +02:00
|
|
|
|
2021-04-11 01:13:43 +02:00
|
|
|
for (const ddo of assets) {
|
2021-04-13 18:15:09 +02:00
|
|
|
didDTMap[ddo?.dataToken.toLowerCase()] = ddo.id
|
2021-06-25 09:50:56 +02:00
|
|
|
// harcoded until we have chainId on assets
|
2021-07-08 00:07:12 +02:00
|
|
|
if (chainAssetLists[ddo.chainId]) {
|
|
|
|
chainAssetLists[ddo.chainId].push(ddo?.dataToken.toLowerCase())
|
2021-06-25 09:50:56 +02:00
|
|
|
} else {
|
2021-07-08 00:07:12 +02:00
|
|
|
chainAssetLists[ddo.chainId] = []
|
|
|
|
chainAssetLists[ddo.chainId].push(ddo?.dataToken.toLowerCase())
|
2021-06-25 09:50:56 +02:00
|
|
|
}
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
2021-06-25 09:50:56 +02:00
|
|
|
let poolPriceResponse: AssetsPoolPricePools[] = []
|
|
|
|
let frePriceResponse: AssetsFrePriceFixedRateExchanges[] = []
|
2021-07-05 13:27:43 +02:00
|
|
|
let freePriceResponse: AssetFreePriceDispenser[] = []
|
2021-05-17 16:08:15 +02:00
|
|
|
|
2021-06-25 09:50:56 +02:00
|
|
|
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-08 00:07:12 +02:00
|
|
|
|
2021-07-22 14:01:30 +02:00
|
|
|
const queryContext = getQueryContext(Number(chainKey))
|
2021-06-25 09:50:56 +02:00
|
|
|
|
|
|
|
const chainPoolPriceResponse: OperationResult<AssetsPoolPrice> =
|
|
|
|
await fetchData(PoolQuery, poolVariables, queryContext)
|
|
|
|
|
|
|
|
poolPriceResponse = poolPriceResponse.concat(
|
|
|
|
chainPoolPriceResponse.data.pools
|
|
|
|
)
|
|
|
|
const chainFrePriceResponse: OperationResult<AssetsFrePrice> =
|
|
|
|
await fetchData(FreQuery, freVariables, queryContext)
|
2021-07-08 00:07:12 +02:00
|
|
|
|
2021-06-25 09:50:56 +02:00
|
|
|
frePriceResponse = frePriceResponse.concat(
|
|
|
|
chainFrePriceResponse.data.fixedRateExchanges
|
|
|
|
)
|
2021-05-17 16:08:15 +02:00
|
|
|
|
2021-07-05 13:27:43 +02:00
|
|
|
const chainFreePriceResponse: OperationResult<AssetsFreePrice> =
|
|
|
|
await fetchData(FreeQuery, freeVariables, queryContext)
|
2021-07-08 00:07:12 +02:00
|
|
|
|
2021-07-05 13:27:43 +02:00
|
|
|
freePriceResponse = freePriceResponse.concat(
|
|
|
|
chainFreePriceResponse.data.dispensers
|
|
|
|
)
|
|
|
|
}
|
2021-06-16 03:32:11 +02:00
|
|
|
return [poolPriceResponse, frePriceResponse, freePriceResponse, didDTMap]
|
2021-05-17 16:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAssetsPriceList(assets: DDO[]): Promise<PriceList> {
|
|
|
|
const priceList: PriceList = {}
|
|
|
|
|
|
|
|
const values: [
|
2021-07-09 10:36:43 +02:00
|
|
|
AssetsPoolPricePools[],
|
|
|
|
AssetsFrePriceFixedRateExchanges[],
|
|
|
|
AssetFreePriceDispenser[],
|
2021-05-17 16:08:15 +02:00
|
|
|
DidAndDatatokenMap
|
|
|
|
] = await getAssetsPoolsExchangesAndDatatokenMap(assets)
|
|
|
|
const poolPriceResponse = values[0]
|
|
|
|
const frePriceResponse = values[1]
|
2021-06-16 03:32:11 +02:00
|
|
|
const freePriceResponse = values[2]
|
|
|
|
const didDTMap: DidAndDatatokenMap = values[3]
|
2021-05-17 16:08:15 +02:00
|
|
|
|
2021-06-25 09:50:56 +02:00
|
|
|
for (const poolPrice of poolPriceResponse) {
|
2021-04-23 15:44:31 +02:00
|
|
|
priceList[didDTMap[poolPrice.datatokenAddress]] =
|
|
|
|
poolPrice.consumePrice === '-1'
|
|
|
|
? poolPrice.spotPrice
|
|
|
|
: poolPrice.consumePrice
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
2021-06-25 09:50:56 +02:00
|
|
|
for (const frePrice of frePriceResponse) {
|
2021-04-13 18:15:09 +02:00
|
|
|
priceList[didDTMap[frePrice.datatoken?.address]] = frePrice.rate
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
2021-07-08 00:07:12 +02:00
|
|
|
for (const freePrice of freePriceResponse) {
|
2021-06-16 03:32:11 +02:00
|
|
|
priceList[didDTMap[freePrice.datatoken?.address]] = '0'
|
|
|
|
}
|
2021-04-09 11:55:18 +02:00
|
|
|
return priceList
|
|
|
|
}
|
2021-04-20 08:44:18 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2021-04-20 08:44:18 +02:00
|
|
|
const poolVariables = {
|
|
|
|
datatokenAddress: asset?.dataToken.toLowerCase()
|
|
|
|
}
|
2021-07-22 14:01:30 +02:00
|
|
|
const queryContext = getQueryContext(Number(asset.chainId))
|
2021-04-20 08:44:18 +02:00
|
|
|
|
2021-06-22 07:52:49 +02:00
|
|
|
const poolPriceResponse: OperationResult<AssetsPoolPrice> = await fetchData(
|
2021-04-20 08:44:18 +02:00
|
|
|
AssetPoolPriceQuerry,
|
2021-06-25 09:50:56 +02:00
|
|
|
poolVariables,
|
|
|
|
queryContext
|
2021-04-20 08:44:18 +02:00
|
|
|
)
|
2021-06-22 07:52:49 +02:00
|
|
|
const frePriceResponse: OperationResult<AssetsFrePrice> = await fetchData(
|
2021-05-17 16:08:15 +02:00
|
|
|
AssetFreQuery,
|
2021-06-25 09:50:56 +02:00
|
|
|
freVariables,
|
|
|
|
queryContext
|
2021-05-17 16:08:15 +02:00
|
|
|
)
|
2021-07-09 10:36:43 +02:00
|
|
|
const freePriceResponse: OperationResult<AssetsFreePrice> = await fetchData(
|
2021-06-16 03:32:11 +02:00
|
|
|
AssetFreeQuery,
|
2021-07-05 13:27:43 +02:00
|
|
|
freeVariables,
|
|
|
|
queryContext
|
2021-06-16 03:32:11 +02:00
|
|
|
)
|
2021-05-17 16:08:15 +02:00
|
|
|
|
|
|
|
const bestPrice: BestPrice = transformPriceToBestPrice(
|
|
|
|
frePriceResponse.data.fixedRateExchanges,
|
2021-06-16 03:32:11 +02:00
|
|
|
poolPriceResponse.data.pools,
|
|
|
|
freePriceResponse.data.dispensers
|
2021-05-17 16:08:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return bestPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAssetsBestPrices(
|
|
|
|
assets: DDO[]
|
|
|
|
): Promise<AssetListPrices[]> {
|
|
|
|
const assetsWithPrice: AssetListPrices[] = []
|
|
|
|
|
|
|
|
const values: [
|
2021-07-09 10:36:43 +02:00
|
|
|
AssetsPoolPricePools[],
|
|
|
|
AssetsFrePriceFixedRateExchanges[],
|
|
|
|
AssetFreePriceDispenser[],
|
2021-05-17 16:08:15 +02:00
|
|
|
DidAndDatatokenMap
|
|
|
|
] = await getAssetsPoolsExchangesAndDatatokenMap(assets)
|
2021-07-08 00:07:12 +02:00
|
|
|
|
2021-05-17 16:08:15 +02:00
|
|
|
const poolPriceResponse = values[0]
|
|
|
|
const frePriceResponse = values[1]
|
2021-06-16 03:32:11 +02:00
|
|
|
const freePriceResponse = values[2]
|
2021-05-17 16:08:15 +02:00
|
|
|
for (const ddo of assets) {
|
|
|
|
const dataToken = ddo.dataToken.toLowerCase()
|
|
|
|
const poolPrice: AssetsPoolPricePools[] = []
|
|
|
|
const frePrice: AssetsFrePriceFixedRateExchanges[] = []
|
2021-06-16 03:32:11 +02:00
|
|
|
const freePrice: AssetFreePriceDispenser[] = []
|
2021-07-08 00:07:12 +02:00
|
|
|
const pool = poolPriceResponse.find(
|
2021-05-17 16:08:15 +02:00
|
|
|
(pool: any) => pool.datatokenAddress === dataToken
|
|
|
|
)
|
|
|
|
pool && poolPrice.push(pool)
|
2021-06-25 09:50:56 +02:00
|
|
|
const fre = frePriceResponse.find(
|
2021-05-17 16:08:15 +02:00
|
|
|
(fre: any) => fre.datatoken.address === dataToken
|
|
|
|
)
|
|
|
|
fre && frePrice.push(fre)
|
2021-07-08 00:07:12 +02:00
|
|
|
const free = freePriceResponse.find(
|
2021-06-16 03:32:11 +02:00
|
|
|
(free: any) => free.datatoken.address === dataToken
|
|
|
|
)
|
|
|
|
free && freePrice.push(free)
|
|
|
|
const bestPrice = transformPriceToBestPrice(frePrice, poolPrice, freePrice)
|
2021-05-17 16:08:15 +02:00
|
|
|
assetsWithPrice.push({
|
|
|
|
ddo: ddo,
|
|
|
|
price: bestPrice
|
|
|
|
})
|
2021-04-20 08:44:18 +02:00
|
|
|
}
|
2021-05-17 16:08:15 +02:00
|
|
|
|
|
|
|
return assetsWithPrice
|
2021-04-20 08:44:18 +02:00
|
|
|
}
|
2021-06-02 11:14:54 +02:00
|
|
|
|
2021-07-08 00:07:12 +02:00
|
|
|
export async function getHighestLiquidityDIDs(
|
|
|
|
chainIds: number[]
|
|
|
|
): Promise<string> {
|
2021-06-02 11:14:54 +02:00
|
|
|
const didList: string[] = []
|
2021-07-15 17:25:53 +02:00
|
|
|
let highestLiquidiyAssets: HighestLiquidityAssetsPools[] = []
|
2021-07-08 00:07:12 +02:00
|
|
|
for (const chain of chainIds) {
|
2021-07-22 14:01:30 +02:00
|
|
|
const queryContext = getQueryContext(Number(chain))
|
2021-07-15 17:25:53 +02:00
|
|
|
const fetchedPools: OperationResult<HighestLiquidityGraphAssets, any> =
|
|
|
|
await fetchData(HighestLiquidityAssets, null, queryContext)
|
|
|
|
highestLiquidiyAssets = highestLiquidiyAssets.concat(
|
|
|
|
fetchedPools.data.pools
|
2021-07-08 00:07:12 +02:00
|
|
|
)
|
2021-07-15 17:25:53 +02:00
|
|
|
}
|
|
|
|
highestLiquidiyAssets
|
|
|
|
.sort((a, b) => a.oceanReserve - b.oceanReserve)
|
|
|
|
.reverse()
|
|
|
|
for (let i = 0; i < highestLiquidiyAssets.length; i++) {
|
|
|
|
if (!highestLiquidiyAssets[i].datatokenAddress) continue
|
|
|
|
const did = web3.utils
|
|
|
|
.toChecksumAddress(highestLiquidiyAssets[i].datatokenAddress)
|
|
|
|
.replace('0x', 'did:op:')
|
|
|
|
didList.push(did)
|
2021-06-02 11:14:54 +02:00
|
|
|
}
|
|
|
|
const searchDids = JSON.stringify(didList)
|
|
|
|
.replace(/,/g, ' ')
|
|
|
|
.replace(/"/g, '')
|
|
|
|
.replace(/(\[|\])/g, '')
|
|
|
|
.replace(/(did:op:)/g, '0x')
|
|
|
|
return searchDids
|
|
|
|
}
|