1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-26 11:16:51 +02:00

fetch asset list prices from the graph

This commit is contained in:
Bogdan Fazakas 2021-04-09 12:55:18 +03:00
parent 118bfb93a9
commit 2cce93d502
3 changed files with 114 additions and 26 deletions

View File

@ -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<NormalizedCacheObject>
function createClient(subgraphUri: string) {
const client = new ApolloClient({
@ -23,6 +24,10 @@ function createClient(subgraphUri: string) {
return client
}
export function getApolloClientInstance(): ApolloClient<NormalizedCacheObject> {
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])

View File

@ -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<AssetSelectionAsset[]> {
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
}

78
src/utils/subgraph.ts Normal file
View File

@ -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<ApolloQueryResult> {
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<any> {
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
}