1
0
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:
Bogdan Fazakas 2022-01-27 10:52:53 +02:00
parent 5f9bc14095
commit 0077ac728e
3 changed files with 183 additions and 131 deletions

View File

@ -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[] = []

View File

@ -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':

View File

@ -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,153 +244,156 @@ 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') // dataToken: selectedAlgorithmAsset.services[0].datatokenAddress
}
// const computeAlgorithm: ComputeAlgorithm = { const allowed = await isOrderable(
// did: selectedAlgorithmAsset.id, ddo,
// serviceIndex: serviceAlgo.index, computeService.id,
// dataToken: selectedAlgorithmAsset.services[0].datatokenAddress computeAlgorithm,
// } selectedAlgorithmAsset
// const allowed = await ocean.compute.isOrderable( )
// ddo.id, LoggerInstance.log('[compute] Is data set orderable?', allowed)
// computeService.index,
// computeAlgorithm
// )
// LoggerInstance.log('[compute] Is data set orderable?', allowed)
// if (!allowed) { if (!allowed) {
// setError( setError(
// 'Data set is not orderable in combination with selected algorithm.' 'Data set is not orderable in combination with selected algorithm.'
// ) )
// LoggerInstance.error( LoggerInstance.error(
// '[compute] Error starting compute job. Dataset is not orderable in combination with selected algorithm.' '[compute] Error starting compute job. Dataset is not orderable in combination with selected algorithm.'
// ) )
// return return
// } }
// if (!hasPreviousDatasetOrder && !hasDatatoken) { if (!hasPreviousDatasetOrder && !hasDatatoken) {
// const tx = await buyDT('1', price, ddo) const tx = await buyDT('1', price, ddo)
// if (!tx) { if (!tx) {
// setError('Error buying datatoken.') setError('Error buying datatoken.')
// LoggerInstance.error('[compute] Error buying datatoken for data set ', ddo.id) LoggerInstance.error(
// return '[compute] Error buying datatoken for data set ',
// } ddo.id
// } )
return
}
}
// if (!hasPreviousAlgorithmOrder && !hasAlgoAssetDatatoken) { if (!hasPreviousAlgorithmOrder && !hasAlgoAssetDatatoken) {
// const tx = await buyDT('1', algorithmPrice, selectedAlgorithmAsset) const tx = await buyDT('1', algorithmPrice, selectedAlgorithmAsset)
// 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 algorithm ',
// selectedAlgorithmAsset.id selectedAlgorithmAsset.id
// ) )
// return 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.
// if (pricingError) { // if (pricingError) {
// setError(pricingError) // setError(pricingError)
// return // return
// } // }
// const assetOrderId = hasPreviousDatasetOrder // const assetOrderId = hasPreviousDatasetOrder
// ? previousDatasetOrderId // ? previousDatasetOrderId
// : await ocean.compute.orderAsset( // : await ocean.compute.orderAsset(
// accountId, // accountId,
// ddo.id, // ddo.id,
// computeService.index, // computeService.index,
// computeAlgorithm, // computeAlgorithm,
// appConfig.marketFeeAddress, // appConfig.marketFeeAddress,
// undefined, // undefined,
// null, // null,
// false // false
// ) // )
// assetOrderId && // assetOrderId &&
// LoggerInstance.log( // LoggerInstance.log(
// `[compute] Got ${ // `[compute] Got ${
// hasPreviousDatasetOrder ? 'existing' : 'new' // hasPreviousDatasetOrder ? 'existing' : 'new'
// } order ID for dataset: `, // } order ID for dataset: `,
// assetOrderId // assetOrderId
// ) // )
// const algorithmAssetOrderId = hasPreviousAlgorithmOrder // const algorithmAssetOrderId = hasPreviousAlgorithmOrder
// ? previousAlgorithmOrderId // ? previousAlgorithmOrderId
// : await ocean.compute.orderAlgorithm( // : await ocean.compute.orderAlgorithm(
// algorithmId, // algorithmId,
// serviceAlgo.type, // serviceAlgo.type,
// accountId, // accountId,
// serviceAlgo.index, // serviceAlgo.index,
// appConfig.marketFeeAddress, // appConfig.marketFeeAddress,
// undefined, // undefined,
// null, // null,
// false // false
// ) // )
// algorithmAssetOrderId && // algorithmAssetOrderId &&
// LoggerInstance.log( // LoggerInstance.log(
// `[compute] Got ${ // `[compute] Got ${
// hasPreviousAlgorithmOrder ? 'existing' : 'new' // hasPreviousAlgorithmOrder ? 'existing' : 'new'
// } order ID for algorithm: `, // } order ID for algorithm: `,
// algorithmAssetOrderId // algorithmAssetOrderId
// ) // )
// if (!assetOrderId || !algorithmAssetOrderId) { // if (!assetOrderId || !algorithmAssetOrderId) {
// setError('Error ordering assets.') // setError('Error ordering assets.')
// return // return
// } // }
// computeAlgorithm.transferTxId = algorithmAssetOrderId // computeAlgorithm.transferTxId = algorithmAssetOrderId
// LoggerInstance.log('[compute] Starting compute job.') // LoggerInstance.log('[compute] Starting compute job.')
// const output: ComputeOutput = { // const output: ComputeOutput = {
// publishAlgorithmLog: true, // publishAlgorithmLog: true,
// publishOutput: true // publishOutput: true
// } // }
// const response = await ocean.compute.start( // const response = await ocean.compute.start(
// ddo.id, // ddo.id,
// assetOrderId, // assetOrderId,
// ddo.services[0].datatokenAddress, // ddo.services[0].datatokenAddress,
// account, // account,
// computeAlgorithm, // computeAlgorithm,
// output, // output,
// `${computeService.index}`, // `${computeService.index}`,
// computeService.type // computeService.type
// ) // )
// if (!response) { // if (!response) {
// setError('Error starting compute job.') // setError('Error starting compute job.')
// return // return
// } // }
// LoggerInstance.log('[compute] Starting compute job response: ', response) // LoggerInstance.log('[compute] Starting compute job response: ', response)
// 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 (
<> <>