diff --git a/src/providers/ApolloClientProvider.tsx b/src/providers/ApolloClientProvider.tsx index 4576c1a26..2ae677db9 100644 --- a/src/providers/ApolloClientProvider.tsx +++ b/src/providers/ApolloClientProvider.tsx @@ -10,6 +10,7 @@ import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHel import { useOcean } from './Ocean' import fetch from 'cross-fetch' import React, { useState, useEffect, ReactNode, ReactElement } from 'react' +let apolloClient: ApolloClient function createClient(subgraphUri: string) { const client = new ApolloClient({ @@ -23,6 +24,10 @@ function createClient(subgraphUri: string) { return client } +export function getApolloClientInstance(): ApolloClient { + return apolloClient +} + export default function ApolloClientProvider({ children }: { @@ -40,6 +45,7 @@ export default function ApolloClientProvider({ } const newClient = createClient((config as ConfigHelperConfig).subgraphUri) + apolloClient = newClient setClient(newClient) }, [config]) diff --git a/src/utils/aquarius.ts b/src/utils/aquarius.ts index 8378cfdc7..74d14d19d 100644 --- a/src/utils/aquarius.ts +++ b/src/utils/aquarius.ts @@ -10,8 +10,8 @@ import { SearchQuery } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache' import { AssetSelectionAsset } from '../components/molecules/FormFields/AssetSelection' +import { getAssetPrice } from './subgraph' import axios, { CancelToken, AxiosResponse } from 'axios' -import web3 from 'web3' // TODO: import directly from ocean.js somehow. // Transforming Aquarius' direct response is needed for getting actual DDOs @@ -113,37 +113,41 @@ export async function transformDDOToAssetSelection( ): Promise { const source = axios.CancelToken.source() const didList: string[] = [] - const priceList: any = {} + const priceList: any = await getAssetPrice(ddoList) + console.log(priceList) const symbolList: any = {} - ddoList.forEach((ddo: DDO) => { + for (const ddo: DDO of ddoList) { didList.push(ddo.id) - priceList[ddo.id] = ddo.price.value symbolList[ddo.id] = ddo.dataTokenInfo.symbol - }) + } const ddoNames = await getAssetsNames(didList, metadataCacheUri, source.token) const algorithmList: AssetSelectionAsset[] = [] didList?.forEach((did: string) => { - let selected = false - selectedAlgorithms?.forEach((algorithm: PublisherTrustedAlgorithm) => { - if (algorithm.did === did) { - selected = true - } - }) - selected - ? algorithmList.unshift({ - did: did, - name: ddoNames[did], - price: priceList[did], - checked: selected, - symbol: symbolList[did] - }) - : algorithmList.push({ - did: did, - name: ddoNames[did], - price: priceList[did], - checked: selected, - symbol: symbolList[did] - }) + console.log(did) + console.log(priceList[did]) + if (priceList[did] != '') { + let selected = false + selectedAlgorithms?.forEach((algorithm: PublisherTrustedAlgorithm) => { + if (algorithm.did === did) { + selected = true + } + }) + selected + ? algorithmList.unshift({ + did: did, + name: ddoNames[did], + price: priceList[did], + checked: selected, + symbol: symbolList[did] + }) + : algorithmList.push({ + did: did, + name: ddoNames[did], + price: priceList[did], + checked: selected, + symbol: symbolList[did] + }) + } }) return algorithmList } diff --git a/src/utils/subgraph.ts b/src/utils/subgraph.ts new file mode 100644 index 000000000..14acbcfd0 --- /dev/null +++ b/src/utils/subgraph.ts @@ -0,0 +1,78 @@ +import { gql, DocumentNode } from '@apollo/client' +import { FrePrice } from '../@types/apollo/FrePrice' +import { PoolPrice } from '../@types/apollo/PoolPrice' +import { DDO } from '@oceanprotocol/lib' +import { getApolloClientInstance } from '../providers/ApolloClientProvider' + +const freQuery = gql` + query AssetFrePrice($datatoken_in: [String!]) { + fixedRateExchanges(orderBy: id, where: { datatoken_in: $datatoken_in }) { + rate + id + datatoken { + id + address + } + } + } +` + +const poolQuery = gql` + query AssetPoolPrice($datatokenAddress_in: [String!]) { + pools(where: { datatokenAddress_in: $datatokenAddress_in }) { + spotPrice + id + datatokenAddress + } + } +` + +async function fetchData( + query: DocumentNode, + variables: any +): Promise { + try { + const client = getApolloClientInstance() + const response = await client.query({ + query: query, + variables: variables + }) + return response + } catch (error) { + console.error('Error parsing json: ', error) + } +} + +export async function getAssetPrice(assets: DDO[]): Promise { + const priceList: any = {} + const poolPriceAssets: string[] = [] + const poolDTadressDID: any = {} + const frePriceAssets: string[] = [] + const freDTadressDID: any = {} + for (const ddo: DDO of assets) { + if (ddo.price?.type === 'pool') { + poolDTadressDID[ddo?.dataToken.toLowerCase()] = ddo.id + poolPriceAssets.push(ddo?.dataToken.toLowerCase()) + } else if (ddo.price?.type === 'exchange') { + freDTadressDID[ddo?.dataToken.toLowerCase()] = ddo.id + frePriceAssets.push(ddo?.dataToken.toLowerCase()) + } + } + const freVariables = { + datatoken_in: frePriceAssets + } + const poolVariables = { + datatokenAddress_in: poolPriceAssets + } + const poolPriceResponse: any = await fetchData(poolQuery, poolVariables) + console.log('poolPriceResponse', poolPriceResponse) + for (const poolPirce of poolPriceResponse.data?.pools) { + priceList[poolDTadressDID[poolPirce.datatokenAddress]] = poolPirce.spotPrice + } + const frePriceResponse: any = await fetchData(freQuery, freVariables) + console.log('frePriceResponse', frePriceResponse) + for (const frePrice of frePriceResponse.data?.fixedRateExchanges) { + priceList[freDTadressDID[frePrice.datatoken?.address]] = frePrice.rate + } + return priceList +}