mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
fetch asset list prices from the graph
This commit is contained in:
parent
118bfb93a9
commit
2cce93d502
@ -10,6 +10,7 @@ import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHel
|
|||||||
import { useOcean } from './Ocean'
|
import { useOcean } from './Ocean'
|
||||||
import fetch from 'cross-fetch'
|
import fetch from 'cross-fetch'
|
||||||
import React, { useState, useEffect, ReactNode, ReactElement } from 'react'
|
import React, { useState, useEffect, ReactNode, ReactElement } from 'react'
|
||||||
|
let apolloClient: ApolloClient<NormalizedCacheObject>
|
||||||
|
|
||||||
function createClient(subgraphUri: string) {
|
function createClient(subgraphUri: string) {
|
||||||
const client = new ApolloClient({
|
const client = new ApolloClient({
|
||||||
@ -23,6 +24,10 @@ function createClient(subgraphUri: string) {
|
|||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getApolloClientInstance(): ApolloClient<NormalizedCacheObject> {
|
||||||
|
return apolloClient
|
||||||
|
}
|
||||||
|
|
||||||
export default function ApolloClientProvider({
|
export default function ApolloClientProvider({
|
||||||
children
|
children
|
||||||
}: {
|
}: {
|
||||||
@ -40,6 +45,7 @@ export default function ApolloClientProvider({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const newClient = createClient((config as ConfigHelperConfig).subgraphUri)
|
const newClient = createClient((config as ConfigHelperConfig).subgraphUri)
|
||||||
|
apolloClient = newClient
|
||||||
setClient(newClient)
|
setClient(newClient)
|
||||||
}, [config])
|
}, [config])
|
||||||
|
|
||||||
|
@ -10,8 +10,8 @@ import {
|
|||||||
SearchQuery
|
SearchQuery
|
||||||
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
||||||
import { AssetSelectionAsset } from '../components/molecules/FormFields/AssetSelection'
|
import { AssetSelectionAsset } from '../components/molecules/FormFields/AssetSelection'
|
||||||
|
import { getAssetPrice } from './subgraph'
|
||||||
import axios, { CancelToken, AxiosResponse } from 'axios'
|
import axios, { CancelToken, AxiosResponse } from 'axios'
|
||||||
import web3 from 'web3'
|
|
||||||
|
|
||||||
// TODO: import directly from ocean.js somehow.
|
// TODO: import directly from ocean.js somehow.
|
||||||
// Transforming Aquarius' direct response is needed for getting actual DDOs
|
// Transforming Aquarius' direct response is needed for getting actual DDOs
|
||||||
@ -113,37 +113,41 @@ export async function transformDDOToAssetSelection(
|
|||||||
): Promise<AssetSelectionAsset[]> {
|
): Promise<AssetSelectionAsset[]> {
|
||||||
const source = axios.CancelToken.source()
|
const source = axios.CancelToken.source()
|
||||||
const didList: string[] = []
|
const didList: string[] = []
|
||||||
const priceList: any = {}
|
const priceList: any = await getAssetPrice(ddoList)
|
||||||
|
console.log(priceList)
|
||||||
const symbolList: any = {}
|
const symbolList: any = {}
|
||||||
ddoList.forEach((ddo: DDO) => {
|
for (const ddo: DDO of ddoList) {
|
||||||
didList.push(ddo.id)
|
didList.push(ddo.id)
|
||||||
priceList[ddo.id] = ddo.price.value
|
|
||||||
symbolList[ddo.id] = ddo.dataTokenInfo.symbol
|
symbolList[ddo.id] = ddo.dataTokenInfo.symbol
|
||||||
})
|
}
|
||||||
const ddoNames = await getAssetsNames(didList, metadataCacheUri, source.token)
|
const ddoNames = await getAssetsNames(didList, metadataCacheUri, source.token)
|
||||||
const algorithmList: AssetSelectionAsset[] = []
|
const algorithmList: AssetSelectionAsset[] = []
|
||||||
didList?.forEach((did: string) => {
|
didList?.forEach((did: string) => {
|
||||||
let selected = false
|
console.log(did)
|
||||||
selectedAlgorithms?.forEach((algorithm: PublisherTrustedAlgorithm) => {
|
console.log(priceList[did])
|
||||||
if (algorithm.did === did) {
|
if (priceList[did] != '') {
|
||||||
selected = true
|
let selected = false
|
||||||
}
|
selectedAlgorithms?.forEach((algorithm: PublisherTrustedAlgorithm) => {
|
||||||
})
|
if (algorithm.did === did) {
|
||||||
selected
|
selected = true
|
||||||
? algorithmList.unshift({
|
}
|
||||||
did: did,
|
})
|
||||||
name: ddoNames[did],
|
selected
|
||||||
price: priceList[did],
|
? algorithmList.unshift({
|
||||||
checked: selected,
|
did: did,
|
||||||
symbol: symbolList[did]
|
name: ddoNames[did],
|
||||||
})
|
price: priceList[did],
|
||||||
: algorithmList.push({
|
checked: selected,
|
||||||
did: did,
|
symbol: symbolList[did]
|
||||||
name: ddoNames[did],
|
})
|
||||||
price: priceList[did],
|
: algorithmList.push({
|
||||||
checked: selected,
|
did: did,
|
||||||
symbol: symbolList[did]
|
name: ddoNames[did],
|
||||||
})
|
price: priceList[did],
|
||||||
|
checked: selected,
|
||||||
|
symbol: symbolList[did]
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return algorithmList
|
return algorithmList
|
||||||
}
|
}
|
||||||
|
78
src/utils/subgraph.ts
Normal file
78
src/utils/subgraph.ts
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user