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

View File

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

View File

@ -81,6 +81,16 @@ export function transformChainIdsListToQuery(chainIds: number[]): string {
return chainQuery 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( export async function queryMetadata(
query: any, query: any,
cancelToken: CancelToken cancelToken: CancelToken
@ -227,6 +237,34 @@ export async function getAlgorithmDatasetsForCompute(
return datasets 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( export async function getPublishedAssets(
accountId: string, accountId: string,
chainIds: number[], 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( export async function getDownloadAssets(
didList: string[], didList: string[],
tokenOrders: OrdersData[], tokenOrders: OrdersData[],
@ -310,12 +311,12 @@ export async function getDownloadAssets(
const downloadedAssets: DownloadedAsset[] = [] const downloadedAssets: DownloadedAsset[] = []
try { try {
const queryResult = await getAssetsFromDidList( const queryResult = await retrieveDDOListByDIDs(
didList, didList,
chainIds, chainIds,
cancelToken cancelToken
) )
const ddoList = queryResult?.results const ddoList = queryResult
for (let i = 0; i < tokenOrders?.length; i++) { for (let i = 0; i < tokenOrders?.length; i++) {
const ddo = ddoList.filter( const ddo = ddoList.filter(