From 2cce93d502ad5830f7b0aa6becae628564e9bb90 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 9 Apr 2021 12:55:18 +0300 Subject: [PATCH 1/6] fetch asset list prices from the graph --- src/providers/ApolloClientProvider.tsx | 6 ++ src/utils/aquarius.ts | 56 +++++++++--------- src/utils/subgraph.ts | 78 ++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 src/utils/subgraph.ts 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 +} From c533d1ee0c6de8bdb72265d75e0d1b64e53b2f49 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Sun, 11 Apr 2021 02:13:43 +0300 Subject: [PATCH 2/6] get compute previous orders from subgraph util --- .../organisms/AssetActions/Compute/index.tsx | 10 +++- .../organisms/AssetActions/Consume.tsx | 1 + src/utils/aquarius.ts | 7 +-- src/utils/subgraph.ts | 58 ++++++++++++++++--- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/components/organisms/AssetActions/Compute/index.tsx b/src/components/organisms/AssetActions/Compute/index.tsx index 83a442ed3..3036fcdf6 100644 --- a/src/components/organisms/AssetActions/Compute/index.tsx +++ b/src/components/organisms/AssetActions/Compute/index.tsx @@ -38,6 +38,7 @@ import { gql, useQuery } from '@apollo/client' import { FrePrice } from '../../../../@types/apollo/FrePrice' import { PoolPrice } from '../../../../@types/apollo/PoolPrice' import { secondsToString } from '../../../../utils/metadata' +import { getPreviousOrders } from '../../../../utils/subgraph' const SuccessAction = () => (