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

Refine Pool Shares DDO fetching (#868)

* use elastic search to get all ddo's at once

* fix get ddo's list function and get names in one query

* reorder ddo list based on did list order

* remove inPurgatory filter

* fix broken aquarius query

* remove getAssetsFromDidList function and use retrieveDDOListByDIDs

* use RegExp
This commit is contained in:
Norbi 2021-10-08 14:15:02 +03:00 committed by GitHub
parent 024af8db81
commit 4d232e4b8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 54 deletions

View File

@ -5,7 +5,7 @@ import { Logger } from '@oceanprotocol/lib'
import Price from '../atoms/Price'
import Tooltip from '../atoms/Tooltip'
import AssetTitle from './AssetListTitle'
import { getAssetsFromDidList } from '../../utils/aquarius'
import { retrieveDDOListByDIDs } from '../../utils/aquarius'
import { getAssetsBestPrices, AssetListPrices } from '../../utils/subgraph'
import axios, { CancelToken } from 'axios'
import { useSiteMetadata } from '../../hooks/useSiteMetadata'
@ -17,7 +17,7 @@ async function getAssetsBookmarked(
cancelToken: CancelToken
) {
try {
const result = await getAssetsFromDidList(bookmarks, chainIds, cancelToken)
const result = await retrieveDDOListByDIDs(bookmarks, chainIds, cancelToken)
return result
} catch (error) {
Logger.error(error.message)
@ -80,7 +80,7 @@ export default function Bookmarks(): ReactElement {
newCancelToken()
)
const pinnedAssets: AssetListPrices[] = await getAssetsBestPrices(
resultPinned?.results
resultPinned
)
setPinned(pinnedAssets)
} catch (error) {

View File

@ -11,13 +11,14 @@ import web3 from 'web3'
import Token from '../../../organisms/AssetActions/Pool/Token'
import { calculateUserLiquidity } from '../../../../utils/subgraph'
import NetworkName from '../../../atoms/NetworkName'
import { retrieveDDOListByDIDs } from '../../../../utils/aquarius'
import { CancelToken } from 'axios'
import { retrieveDDO } from '../../../../utils/aquarius'
import { isValidNumber } from '../../../../utils/numberValidations'
import Decimal from 'decimal.js'
import { useProfile } from '../../../../providers/Profile'
import { DDO } from '@oceanprotocol/lib'
import { useCancelToken } from '../../../../hooks/useCancelToken'
import { useUserPreferences } from '../../../../providers/UserPreferences'
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
@ -131,24 +132,29 @@ const columns = [
async function getPoolSharesAssets(
data: PoolShare[],
chainIds: number[],
cancelToken: CancelToken
) {
if (data.length < 1) return
const assetList: Asset[] = []
const didList: string[] = []
for (let i = 0; i < data.length; i++) {
const did = web3.utils
.toChecksumAddress(data[i].poolId.datatokenAddress)
.replace('0x', 'did:op:')
const ddo = await retrieveDDO(did, cancelToken)
didList.push(did)
}
const ddoList = await retrieveDDOListByDIDs(didList, chainIds, cancelToken)
for (let i = 0; i < data.length; i++) {
const userLiquidity = calculateUserLiquidity(data[i])
ddo &&
assetList.push({
poolShare: data[i],
userLiquidity: userLiquidity,
networkId: ddo?.chainId,
networkId: ddoList[i].chainId,
createTime: data[i].poolId.createTime,
ddo
ddo: ddoList[i]
})
}
const assets = assetList.sort((a, b) => b.createTime - a.createTime)
@ -164,6 +170,7 @@ export default function PoolShares({
const [assets, setAssets] = useState<Asset[]>()
const [loading, setLoading] = useState<boolean>(false)
const [dataFetchInterval, setDataFetchInterval] = useState<NodeJS.Timeout>()
const { chainIds } = useUserPreferences()
const newCancelToken = useCancelToken()
const fetchPoolSharesAssets = useCallback(
@ -171,7 +178,11 @@ export default function PoolShares({
if (!poolShares || isPoolSharesLoading) return
try {
const assets = await getPoolSharesAssets(poolShares, cancelToken)
const assets = await getPoolSharesAssets(
poolShares,
chainIds,
cancelToken
)
setAssets(assets)
} catch (error) {
console.error('Error fetching pool shares: ', error.message)

View File

@ -81,6 +81,16 @@ export function transformChainIdsListToQuery(chainIds: number[]): string {
return chainQuery
}
export function transformDIDListToQuery(didList: string[] | DID[]): string {
let chainQuery = ''
const regex = new RegExp('(:)', 'g')
didList.forEach((did: any) => {
chainQuery += `id:${did.replace(regex, '\\:')} OR `
})
chainQuery = chainQuery.slice(0, chainQuery.length - 4)
return chainQuery
}
export async function queryMetadata(
query: any,
cancelToken: CancelToken
@ -227,6 +237,34 @@ export async function getAlgorithmDatasetsForCompute(
return datasets
}
export async function retrieveDDOListByDIDs(
didList: string[] | DID[],
chainIds: number[],
cancelToken: CancelToken
): Promise<DDO[]> {
try {
const orderedDDOListByDIDList: DDO[] = []
const query = {
size: didList.length,
query: {
query_string: {
query: `(${transformDIDListToQuery(
didList
)}) AND (${transformChainIdsListToQuery(chainIds)})`
}
}
}
const result = await queryMetadata(query, cancelToken)
didList.forEach((did: string | DID) => {
const ddo: DDO = result.results.find((ddo: DDO) => ddo.id === did)
orderedDDOListByDIDList.push(ddo)
})
return orderedDDOListByDIDList
} catch (error) {
Logger.error(error.message)
}
}
export async function getPublishedAssets(
accountId: string,
chainIds: number[],
@ -264,43 +302,6 @@ export async function getPublishedAssets(
}
}
export async function getAssetsFromDidList(
didList: string[],
chainIds: number[],
cancelToken: CancelToken
): Promise<any> {
try {
// TODO: figure out cleaner way to transform string[] into csv
const searchDids = JSON.stringify(didList)
.replace(/,/g, ' ')
.replace(/"/g, '')
.replace(/(\[|\])/g, '')
// for whatever reason ddo.id is not searchable, so use ddo.dataToken instead
.replace(/(did:op:)/g, '0x')
// safeguard against passed empty didList, preventing 500 from Aquarius
if (!searchDids) return
const query = {
query: {
query_string: {
query: `(${searchDids}) AND (${transformChainIdsListToQuery(
chainIds
)})`,
fields: ['dataToken'],
default_operator: 'OR'
}
},
sort: { created: 'desc' }
}
const queryResult = await queryMetadata(query, cancelToken)
return queryResult
} catch (error) {
Logger.error(error.message)
}
}
export async function getDownloadAssets(
didList: string[],
tokenOrders: OrdersData[],
@ -310,12 +311,12 @@ export async function getDownloadAssets(
const downloadedAssets: DownloadedAsset[] = []
try {
const queryResult = await getAssetsFromDidList(
const queryResult = await retrieveDDOListByDIDs(
didList,
chainIds,
cancelToken
)
const ddoList = queryResult?.results
const ddoList = queryResult
for (let i = 0; i < tokenOrders?.length; i++) {
const ddo = ddoList.filter(