mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
get compute previous orders from subgraph util
This commit is contained in:
parent
2cce93d502
commit
c533d1ee0c
@ -38,6 +38,7 @@ import { gql, useQuery } from '@apollo/client'
|
|||||||
import { FrePrice } from '../../../../@types/apollo/FrePrice'
|
import { FrePrice } from '../../../../@types/apollo/FrePrice'
|
||||||
import { PoolPrice } from '../../../../@types/apollo/PoolPrice'
|
import { PoolPrice } from '../../../../@types/apollo/PoolPrice'
|
||||||
import { secondsToString } from '../../../../utils/metadata'
|
import { secondsToString } from '../../../../utils/metadata'
|
||||||
|
import { getPreviousOrders } from '../../../../utils/subgraph'
|
||||||
|
|
||||||
const SuccessAction = () => (
|
const SuccessAction = () => (
|
||||||
<Button style="text" to="/history" size="small">
|
<Button style="text" to="/history" size="small">
|
||||||
@ -122,7 +123,14 @@ export default function Compute({
|
|||||||
const hasDatatoken = Number(dtBalance) >= 1
|
const hasDatatoken = Number(dtBalance) >= 1
|
||||||
|
|
||||||
async function checkPreviousOrders(ddo: DDO, serviceType: ServiceType) {
|
async function checkPreviousOrders(ddo: DDO, serviceType: ServiceType) {
|
||||||
const orderId = await checkPreviousOrder(ocean, accountId, ddo, serviceType)
|
const { timeout } = (
|
||||||
|
ddo.findServiceByType('access') || ddo.findServiceByType('compute')
|
||||||
|
).attributes.main
|
||||||
|
const orderId = await getPreviousOrders(
|
||||||
|
ddo.dataToken?.toLowerCase(),
|
||||||
|
accountId?.toLowerCase(),
|
||||||
|
secondsToString(timeout)
|
||||||
|
)
|
||||||
const assetType = ddo.findServiceByType('metadata').attributes.main.type
|
const assetType = ddo.findServiceByType('metadata').attributes.main.type
|
||||||
if (assetType === 'algorithm') {
|
if (assetType === 'algorithm') {
|
||||||
setPreviousAlgorithmOrderId(orderId)
|
setPreviousAlgorithmOrderId(orderId)
|
||||||
|
@ -69,6 +69,7 @@ export default function Consume({
|
|||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
console.log('data', data)
|
||||||
if (!data || !assetTimeout || data.tokenOrders.length === 0) return
|
if (!data || !assetTimeout || data.tokenOrders.length === 0) return
|
||||||
|
|
||||||
const lastOrder = data.tokenOrders[0]
|
const lastOrder = data.tokenOrders[0]
|
||||||
|
@ -114,18 +114,15 @@ export async function transformDDOToAssetSelection(
|
|||||||
const source = axios.CancelToken.source()
|
const source = axios.CancelToken.source()
|
||||||
const didList: string[] = []
|
const didList: string[] = []
|
||||||
const priceList: any = await getAssetPrice(ddoList)
|
const priceList: any = await getAssetPrice(ddoList)
|
||||||
console.log(priceList)
|
|
||||||
const symbolList: any = {}
|
const symbolList: any = {}
|
||||||
for (const ddo: DDO of ddoList) {
|
for (const ddo of ddoList) {
|
||||||
didList.push(ddo.id)
|
didList.push(ddo.id)
|
||||||
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) => {
|
||||||
console.log(did)
|
if (priceList[did] !== 'none') {
|
||||||
console.log(priceList[did])
|
|
||||||
if (priceList[did] != '') {
|
|
||||||
let selected = false
|
let selected = false
|
||||||
selectedAlgorithms?.forEach((algorithm: PublisherTrustedAlgorithm) => {
|
selectedAlgorithms?.forEach((algorithm: PublisherTrustedAlgorithm) => {
|
||||||
if (algorithm.did === did) {
|
if (algorithm.did === did) {
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { gql, DocumentNode } from '@apollo/client'
|
import { gql, DocumentNode, ApolloQueryResult } from '@apollo/client'
|
||||||
import { FrePrice } from '../@types/apollo/FrePrice'
|
|
||||||
import { PoolPrice } from '../@types/apollo/PoolPrice'
|
|
||||||
import { DDO } from '@oceanprotocol/lib'
|
import { DDO } from '@oceanprotocol/lib'
|
||||||
import { getApolloClientInstance } from '../providers/ApolloClientProvider'
|
import { getApolloClientInstance } from '../providers/ApolloClientProvider'
|
||||||
|
import BigNumber from 'bignumber.js'
|
||||||
|
|
||||||
const freQuery = gql`
|
const freQuery = gql`
|
||||||
query AssetFrePrice($datatoken_in: [String!]) {
|
query AssetFrePrice($datatoken_in: [String!]) {
|
||||||
@ -27,10 +26,24 @@ const poolQuery = gql`
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const previousOrderQuery = gql`
|
||||||
|
query AssetPreviousOrder($id: String!, $account: String!) {
|
||||||
|
tokenOrders(
|
||||||
|
first: 1
|
||||||
|
where: { datatokenId: $id, payer: $account }
|
||||||
|
orderBy: timestamp
|
||||||
|
orderDirection: desc
|
||||||
|
) {
|
||||||
|
timestamp
|
||||||
|
tx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
async function fetchData(
|
async function fetchData(
|
||||||
query: DocumentNode,
|
query: DocumentNode,
|
||||||
variables: any
|
variables: any
|
||||||
): Promise<ApolloQueryResult> {
|
): Promise<ApolloQueryResult<any>> {
|
||||||
try {
|
try {
|
||||||
const client = getApolloClientInstance()
|
const client = getApolloClientInstance()
|
||||||
const response = await client.query({
|
const response = await client.query({
|
||||||
@ -39,7 +52,36 @@ async function fetchData(
|
|||||||
})
|
})
|
||||||
return response
|
return response
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error parsing json: ', error)
|
console.error('Error fetchData: ', error.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getPreviousOrders(
|
||||||
|
id: string,
|
||||||
|
account: string,
|
||||||
|
assetTimeout: string
|
||||||
|
): Promise<string> {
|
||||||
|
const variables = {
|
||||||
|
id: id,
|
||||||
|
account: account
|
||||||
|
}
|
||||||
|
const fatchedPreviousOrders: any = await fetchData(
|
||||||
|
previousOrderQuery,
|
||||||
|
variables
|
||||||
|
)
|
||||||
|
if (fatchedPreviousOrders.data?.tokenOrders?.length === 0) return null
|
||||||
|
if (assetTimeout === 'Forever') {
|
||||||
|
return fatchedPreviousOrders?.data?.tokenOrders[0]?.tx
|
||||||
|
} else {
|
||||||
|
const expiry = new BigNumber(
|
||||||
|
fatchedPreviousOrders?.data?.tokenOrders[0]?.timestamp
|
||||||
|
).plus(assetTimeout)
|
||||||
|
const unixTime = new BigNumber(Math.floor(Date.now() / 1000))
|
||||||
|
if (unixTime.isLessThan(expiry)) {
|
||||||
|
return fatchedPreviousOrders?.data?.tokenOrders[0]?.tx
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,13 +91,15 @@ export async function getAssetPrice(assets: DDO[]): Promise<any> {
|
|||||||
const poolDTadressDID: any = {}
|
const poolDTadressDID: any = {}
|
||||||
const frePriceAssets: string[] = []
|
const frePriceAssets: string[] = []
|
||||||
const freDTadressDID: any = {}
|
const freDTadressDID: any = {}
|
||||||
for (const ddo: DDO of assets) {
|
for (const ddo of assets) {
|
||||||
if (ddo.price?.type === 'pool') {
|
if (ddo.price?.type === 'pool') {
|
||||||
poolDTadressDID[ddo?.dataToken.toLowerCase()] = ddo.id
|
poolDTadressDID[ddo?.dataToken.toLowerCase()] = ddo.id
|
||||||
poolPriceAssets.push(ddo?.dataToken.toLowerCase())
|
poolPriceAssets.push(ddo?.dataToken.toLowerCase())
|
||||||
} else if (ddo.price?.type === 'exchange') {
|
} else if (ddo.price?.type === 'exchange') {
|
||||||
freDTadressDID[ddo?.dataToken.toLowerCase()] = ddo.id
|
freDTadressDID[ddo?.dataToken.toLowerCase()] = ddo.id
|
||||||
frePriceAssets.push(ddo?.dataToken.toLowerCase())
|
frePriceAssets.push(ddo?.dataToken.toLowerCase())
|
||||||
|
} else {
|
||||||
|
priceList[ddo.id] = 'none'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const freVariables = {
|
const freVariables = {
|
||||||
@ -65,12 +109,10 @@ export async function getAssetPrice(assets: DDO[]): Promise<any> {
|
|||||||
datatokenAddress_in: poolPriceAssets
|
datatokenAddress_in: poolPriceAssets
|
||||||
}
|
}
|
||||||
const poolPriceResponse: any = await fetchData(poolQuery, poolVariables)
|
const poolPriceResponse: any = await fetchData(poolQuery, poolVariables)
|
||||||
console.log('poolPriceResponse', poolPriceResponse)
|
|
||||||
for (const poolPirce of poolPriceResponse.data?.pools) {
|
for (const poolPirce of poolPriceResponse.data?.pools) {
|
||||||
priceList[poolDTadressDID[poolPirce.datatokenAddress]] = poolPirce.spotPrice
|
priceList[poolDTadressDID[poolPirce.datatokenAddress]] = poolPirce.spotPrice
|
||||||
}
|
}
|
||||||
const frePriceResponse: any = await fetchData(freQuery, freVariables)
|
const frePriceResponse: any = await fetchData(freQuery, freVariables)
|
||||||
console.log('frePriceResponse', frePriceResponse)
|
|
||||||
for (const frePrice of frePriceResponse.data?.fixedRateExchanges) {
|
for (const frePrice of frePriceResponse.data?.fixedRateExchanges) {
|
||||||
priceList[freDTadressDID[frePrice.datatoken?.address]] = frePrice.rate
|
priceList[freDTadressDID[frePrice.datatoken?.address]] = frePrice.rate
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user