mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
add isOrderable and other helpers
This commit is contained in:
parent
5f9bc14095
commit
0077ac728e
@ -9,11 +9,20 @@
|
|||||||
// Account
|
// Account
|
||||||
// } from '@oceanprotocol/lib'
|
// } from '@oceanprotocol/lib'
|
||||||
// import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/Compute'
|
// import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/Compute'
|
||||||
import { Asset } from '@oceanprotocol/lib'
|
import {
|
||||||
|
Asset,
|
||||||
|
DDO,
|
||||||
|
ComputeAlgorithm,
|
||||||
|
Service,
|
||||||
|
LoggerInstance,
|
||||||
|
ProviderInstance
|
||||||
|
} from '@oceanprotocol/lib'
|
||||||
import { CancelToken } from 'axios'
|
import { CancelToken } from 'axios'
|
||||||
import { gql } from 'urql'
|
import { gql } from 'urql'
|
||||||
import { queryMetadata, getFilterTerm, generateBaseQuery } from './aquarius'
|
import { queryMetadata, getFilterTerm, generateBaseQuery } from './aquarius'
|
||||||
import { fetchDataForMultipleChains } from './subgraph'
|
import { fetchDataForMultipleChains } from './subgraph'
|
||||||
|
import { getServiceById } from './ddo'
|
||||||
|
import { getOceanConfig } from './ocean'
|
||||||
|
|
||||||
const getComputeOrders = gql`
|
const getComputeOrders = gql`
|
||||||
query ComputeOrders($user: String!) {
|
query ComputeOrders($user: String!) {
|
||||||
@ -76,6 +85,38 @@ async function getAssetMetadata(
|
|||||||
return result.results
|
return result.results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function isOrderable(
|
||||||
|
asset: Asset | DDO,
|
||||||
|
serviceId: string,
|
||||||
|
algorithm: ComputeAlgorithm,
|
||||||
|
algorithmDDO: Asset | DDO
|
||||||
|
): Promise<boolean> {
|
||||||
|
const datasetService: Service = getServiceById(asset, serviceId)
|
||||||
|
if (!datasetService) return false
|
||||||
|
if (datasetService.type === 'compute') {
|
||||||
|
if (algorithm.meta) {
|
||||||
|
// check if raw algo is allowed
|
||||||
|
if (datasetService.compute.allowRawAlgorithm) return true
|
||||||
|
LoggerInstance.error('ERROR: This service does not allow raw algorithm')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (algorithm.documentId) {
|
||||||
|
const algoService: Service = getServiceById(
|
||||||
|
algorithmDDO,
|
||||||
|
algorithm.serviceId
|
||||||
|
)
|
||||||
|
if (algoService && algoService.type === 'compute') {
|
||||||
|
if (algoService.serviceEndpoint !== datasetService.serviceEndpoint) {
|
||||||
|
this.logger.error(
|
||||||
|
'ERROR: Both assets with compute service are not served by the same provider'
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getServiceEndpoints(data: TokenOrder[], assets: Asset[]): string[] {
|
function getServiceEndpoints(data: TokenOrder[], assets: Asset[]): string[] {
|
||||||
// const serviceEndpoints: string[] = []
|
// const serviceEndpoints: string[] = []
|
||||||
|
|
||||||
|
@ -15,6 +15,13 @@ export function getServiceByName(
|
|||||||
return service
|
return service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getServiceById(ddo: Asset | DDO, serviceId: string): Service {
|
||||||
|
if (!ddo) return
|
||||||
|
|
||||||
|
const service = ddo.services.find((s) => s.id === serviceId)
|
||||||
|
return service
|
||||||
|
}
|
||||||
|
|
||||||
export function mapTimeoutStringToSeconds(timeout: string): number {
|
export function mapTimeoutStringToSeconds(timeout: string): number {
|
||||||
switch (timeout) {
|
switch (timeout) {
|
||||||
case 'Forever':
|
case 'Forever':
|
||||||
|
@ -29,6 +29,7 @@ import FormStartComputeDataset from './FormComputeDataset'
|
|||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import SuccessConfetti from '@shared/SuccessConfetti'
|
import SuccessConfetti from '@shared/SuccessConfetti'
|
||||||
import { getServiceByName, secondsToString } from '@utils/ddo'
|
import { getServiceByName, secondsToString } from '@utils/ddo'
|
||||||
|
import { isOrderable } from '@utils/compute'
|
||||||
import { AssetSelectionAsset } from '@shared/FormFields/AssetSelection'
|
import { AssetSelectionAsset } from '@shared/FormFields/AssetSelection'
|
||||||
import AlgorithmDatasetsListForCompute from './AlgorithmDatasetsListForCompute'
|
import AlgorithmDatasetsListForCompute from './AlgorithmDatasetsListForCompute'
|
||||||
import { getPreviousOrders, getPrice } from '@utils/subgraph'
|
import { getPreviousOrders, getPrice } from '@utils/subgraph'
|
||||||
@ -243,61 +244,64 @@ export default function Compute({
|
|||||||
toast.error(newError)
|
toast.error(newError)
|
||||||
}, [error, pricingError])
|
}, [error, pricingError])
|
||||||
|
|
||||||
// async function startJob(algorithmId: string) {
|
async function startJob(algorithmId: string) {
|
||||||
// try {
|
try {
|
||||||
// if (!ocean) return
|
setIsJobStarting(true)
|
||||||
|
setIsPublished(false) // would be nice to rename this
|
||||||
|
setError(undefined)
|
||||||
|
|
||||||
// setIsJobStarting(true)
|
const computeService = getServiceByName(ddo, 'compute')
|
||||||
// setIsPublished(false)
|
const serviceAlgo = getServiceByName(selectedAlgorithmAsset, 'access')
|
||||||
// setError(undefined)
|
? getServiceByName(selectedAlgorithmAsset, 'access')
|
||||||
|
: getServiceByName(selectedAlgorithmAsset, 'compute')
|
||||||
|
|
||||||
// const computeService = getServiceByName(ddo, 'compute')
|
const computeAlgorithm: ComputeAlgorithm = {
|
||||||
// const serviceAlgo = getServiceByName(selectedAlgorithmAsset, 'access')
|
documentId: selectedAlgorithmAsset.id,
|
||||||
// ? getServiceByName(selectedAlgorithmAsset, 'access')
|
serviceId: serviceAlgo.id
|
||||||
// : getServiceByName(selectedAlgorithmAsset, 'compute')
|
|
||||||
|
|
||||||
// const computeAlgorithm: ComputeAlgorithm = {
|
|
||||||
// did: selectedAlgorithmAsset.id,
|
|
||||||
// serviceIndex: serviceAlgo.index,
|
|
||||||
// dataToken: selectedAlgorithmAsset.services[0].datatokenAddress
|
// dataToken: selectedAlgorithmAsset.services[0].datatokenAddress
|
||||||
// }
|
}
|
||||||
// const allowed = await ocean.compute.isOrderable(
|
|
||||||
// ddo.id,
|
|
||||||
// computeService.index,
|
|
||||||
// computeAlgorithm
|
|
||||||
// )
|
|
||||||
// LoggerInstance.log('[compute] Is data set orderable?', allowed)
|
|
||||||
|
|
||||||
// if (!allowed) {
|
const allowed = await isOrderable(
|
||||||
// setError(
|
ddo,
|
||||||
// 'Data set is not orderable in combination with selected algorithm.'
|
computeService.id,
|
||||||
// )
|
computeAlgorithm,
|
||||||
// LoggerInstance.error(
|
selectedAlgorithmAsset
|
||||||
// '[compute] Error starting compute job. Dataset is not orderable in combination with selected algorithm.'
|
)
|
||||||
// )
|
LoggerInstance.log('[compute] Is data set orderable?', allowed)
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (!hasPreviousDatasetOrder && !hasDatatoken) {
|
if (!allowed) {
|
||||||
// const tx = await buyDT('1', price, ddo)
|
setError(
|
||||||
// if (!tx) {
|
'Data set is not orderable in combination with selected algorithm.'
|
||||||
// setError('Error buying datatoken.')
|
)
|
||||||
// LoggerInstance.error('[compute] Error buying datatoken for data set ', ddo.id)
|
LoggerInstance.error(
|
||||||
// return
|
'[compute] Error starting compute job. Dataset is not orderable in combination with selected algorithm.'
|
||||||
// }
|
)
|
||||||
// }
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// if (!hasPreviousAlgorithmOrder && !hasAlgoAssetDatatoken) {
|
if (!hasPreviousDatasetOrder && !hasDatatoken) {
|
||||||
// const tx = await buyDT('1', algorithmPrice, selectedAlgorithmAsset)
|
const tx = await buyDT('1', price, ddo)
|
||||||
// if (!tx) {
|
if (!tx) {
|
||||||
// setError('Error buying datatoken.')
|
setError('Error buying datatoken.')
|
||||||
// LoggerInstance.error(
|
LoggerInstance.error(
|
||||||
// '[compute] Error buying datatoken for algorithm ',
|
'[compute] Error buying datatoken for data set ',
|
||||||
// selectedAlgorithmAsset.id
|
ddo.id
|
||||||
// )
|
)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
if (!hasPreviousAlgorithmOrder && !hasAlgoAssetDatatoken) {
|
||||||
|
const tx = await buyDT('1', algorithmPrice, selectedAlgorithmAsset)
|
||||||
|
if (!tx) {
|
||||||
|
setError('Error buying datatoken.')
|
||||||
|
LoggerInstance.error(
|
||||||
|
'[compute] Error buying datatoken for algorithm ',
|
||||||
|
selectedAlgorithmAsset.id
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// // TODO: pricingError is always undefined even upon errors during buyDT for whatever reason.
|
// // TODO: pricingError is always undefined even upon errors during buyDT for whatever reason.
|
||||||
// // So manually drop out above, but ideally could be replaced with this alone.
|
// // So manually drop out above, but ideally could be replaced with this alone.
|
||||||
@ -381,15 +385,15 @@ export default function Compute({
|
|||||||
// await checkPreviousOrders(selectedAlgorithmAsset)
|
// await checkPreviousOrders(selectedAlgorithmAsset)
|
||||||
// await checkPreviousOrders(ddo)
|
// await checkPreviousOrders(ddo)
|
||||||
// setIsPublished(true)
|
// setIsPublished(true)
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// await checkPreviousOrders(selectedAlgorithmAsset)
|
await checkPreviousOrders(selectedAlgorithmAsset)
|
||||||
// await checkPreviousOrders(ddo)
|
await checkPreviousOrders(ddo)
|
||||||
// setError('Failed to start job!')
|
setError('Failed to start job!')
|
||||||
// LoggerInstance.error('[compute] Failed to start job: ', error.message)
|
LoggerInstance.error('[compute] Failed to start job: ', error.message)
|
||||||
// } finally {
|
} finally {
|
||||||
// setIsJobStarting(false)
|
setIsJobStarting(false)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user