2021-04-11 01:13:43 +02:00
|
|
|
import { gql, DocumentNode, ApolloQueryResult } from '@apollo/client'
|
2021-04-09 11:55:18 +02:00
|
|
|
import { DDO } from '@oceanprotocol/lib'
|
|
|
|
import { getApolloClientInstance } from '../providers/ApolloClientProvider'
|
2021-04-11 01:13:43 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
2021-04-09 11:55:18 +02:00
|
|
|
|
2021-04-14 13:41:17 +02:00
|
|
|
export interface PriceList {
|
|
|
|
[key: string]: string
|
|
|
|
}
|
|
|
|
|
2021-04-09 11:55:18 +02:00
|
|
|
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
|
2021-04-23 15:44:31 +02:00
|
|
|
consumePrice
|
2021-04-09 11:55:18 +02:00
|
|
|
id
|
|
|
|
datatokenAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-04-11 01:13:43 +02:00
|
|
|
const previousOrderQuery = gql`
|
|
|
|
query AssetPreviousOrder($id: String!, $account: String!) {
|
|
|
|
tokenOrders(
|
|
|
|
first: 1
|
|
|
|
where: { datatokenId: $id, payer: $account }
|
|
|
|
orderBy: timestamp
|
|
|
|
orderDirection: desc
|
|
|
|
) {
|
|
|
|
timestamp
|
|
|
|
tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-04-09 11:55:18 +02:00
|
|
|
async function fetchData(
|
|
|
|
query: DocumentNode,
|
|
|
|
variables: any
|
2021-04-11 01:13:43 +02:00
|
|
|
): Promise<ApolloQueryResult<any>> {
|
2021-04-09 11:55:18 +02:00
|
|
|
try {
|
|
|
|
const client = getApolloClientInstance()
|
|
|
|
const response = await client.query({
|
|
|
|
query: query,
|
|
|
|
variables: variables
|
|
|
|
})
|
|
|
|
return response
|
|
|
|
} catch (error) {
|
2021-04-11 01:13:43 +02:00
|
|
|
console.error('Error fetchData: ', error.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPreviousOrders(
|
|
|
|
id: string,
|
|
|
|
account: string,
|
|
|
|
assetTimeout: string
|
|
|
|
): Promise<string> {
|
|
|
|
const variables = {
|
|
|
|
id: id,
|
|
|
|
account: account
|
|
|
|
}
|
2021-04-12 16:09:41 +02:00
|
|
|
const fetchedPreviousOrders: any = await fetchData(
|
2021-04-11 01:13:43 +02:00
|
|
|
previousOrderQuery,
|
|
|
|
variables
|
|
|
|
)
|
2021-04-12 16:09:41 +02:00
|
|
|
if (fetchedPreviousOrders.data?.tokenOrders?.length === 0) return null
|
|
|
|
if (assetTimeout === '0') {
|
|
|
|
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx
|
2021-04-11 01:13:43 +02:00
|
|
|
} else {
|
|
|
|
const expiry = new BigNumber(
|
2021-04-12 16:09:41 +02:00
|
|
|
fetchedPreviousOrders?.data?.tokenOrders[0]?.timestamp
|
2021-04-11 01:13:43 +02:00
|
|
|
).plus(assetTimeout)
|
|
|
|
const unixTime = new BigNumber(Math.floor(Date.now() / 1000))
|
|
|
|
if (unixTime.isLessThan(expiry)) {
|
2021-04-12 16:09:41 +02:00
|
|
|
return fetchedPreviousOrders?.data?.tokenOrders[0]?.tx
|
2021-04-11 01:13:43 +02:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 13:41:17 +02:00
|
|
|
export async function getAssetPrices(assets: DDO[]): Promise<PriceList> {
|
|
|
|
const priceList: PriceList = {}
|
2021-04-13 18:15:09 +02:00
|
|
|
const didDTMap: any = {}
|
|
|
|
const dataTokenList: string[] = []
|
|
|
|
|
2021-04-11 01:13:43 +02:00
|
|
|
for (const ddo of assets) {
|
2021-04-13 18:15:09 +02:00
|
|
|
didDTMap[ddo?.dataToken.toLowerCase()] = ddo.id
|
|
|
|
dataTokenList.push(ddo?.dataToken.toLowerCase())
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
const freVariables = {
|
2021-04-13 18:15:09 +02:00
|
|
|
datatoken_in: dataTokenList
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
const poolVariables = {
|
2021-04-13 18:15:09 +02:00
|
|
|
datatokenAddress_in: dataTokenList
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
const poolPriceResponse: any = await fetchData(poolQuery, poolVariables)
|
2021-04-12 12:52:09 +02:00
|
|
|
for (const poolPrice of poolPriceResponse.data?.pools) {
|
2021-04-23 15:44:31 +02:00
|
|
|
priceList[didDTMap[poolPrice.datatokenAddress]] =
|
|
|
|
poolPrice.consumePrice === '-1'
|
|
|
|
? poolPrice.spotPrice
|
|
|
|
: poolPrice.consumePrice
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
const frePriceResponse: any = await fetchData(freQuery, freVariables)
|
|
|
|
for (const frePrice of frePriceResponse.data?.fixedRateExchanges) {
|
2021-04-13 18:15:09 +02:00
|
|
|
priceList[didDTMap[frePrice.datatoken?.address]] = frePrice.rate
|
2021-04-09 11:55:18 +02:00
|
|
|
}
|
|
|
|
return priceList
|
|
|
|
}
|