2022-01-27 09:52:53 +01:00
|
|
|
import {
|
|
|
|
Asset,
|
2022-02-25 11:33:07 +01:00
|
|
|
ServiceComputeOptions,
|
|
|
|
PublisherTrustedAlgorithm,
|
2022-02-25 13:26:39 +01:00
|
|
|
getHash,
|
2022-02-28 13:56:13 +01:00
|
|
|
LoggerInstance,
|
|
|
|
ComputeAlgorithm,
|
|
|
|
DDO,
|
2022-03-01 10:59:26 +01:00
|
|
|
Service,
|
|
|
|
ProviderInstance,
|
|
|
|
ComputeEnvironment
|
2022-01-27 09:52:53 +01:00
|
|
|
} from '@oceanprotocol/lib'
|
2021-10-21 09:24:00 +02:00
|
|
|
import { CancelToken } from 'axios'
|
2021-09-01 16:03:52 +02:00
|
|
|
import { gql } from 'urql'
|
2022-02-10 15:33:16 +01:00
|
|
|
import {
|
|
|
|
queryMetadata,
|
|
|
|
getFilterTerm,
|
|
|
|
generateBaseQuery,
|
2022-02-25 11:33:07 +01:00
|
|
|
retrieveDDOListByDIDs
|
2022-02-10 15:33:16 +01:00
|
|
|
} from './aquarius'
|
2021-09-01 16:03:52 +02:00
|
|
|
import { fetchDataForMultipleChains } from './subgraph'
|
2022-02-10 15:33:16 +01:00
|
|
|
import { getServiceById, getServiceByName } from './ddo'
|
2022-01-27 09:52:53 +01:00
|
|
|
import { getOceanConfig } from './ocean'
|
2022-02-10 15:33:16 +01:00
|
|
|
import { SortTermOptions } from 'src/@types/aquarius/SearchQuery'
|
|
|
|
import { AssetSelectionAsset } from '@shared/FormFields/AssetSelection'
|
2022-02-28 13:56:13 +01:00
|
|
|
import { transformAssetToAssetSelection } from './assetConvertor'
|
2021-09-01 16:03:52 +02:00
|
|
|
|
|
|
|
const getComputeOrders = gql`
|
|
|
|
query ComputeOrders($user: String!) {
|
2022-01-13 22:24:06 +01:00
|
|
|
orders(
|
|
|
|
orderBy: createdTimestamp
|
2021-09-01 16:03:52 +02:00
|
|
|
orderDirection: desc
|
|
|
|
where: { payer: $user }
|
|
|
|
) {
|
|
|
|
id
|
2022-01-31 13:41:58 +01:00
|
|
|
serviceIndex
|
|
|
|
datatoken {
|
2021-09-01 16:03:52 +02:00
|
|
|
address
|
|
|
|
}
|
|
|
|
tx
|
2022-01-13 22:24:06 +01:00
|
|
|
createdTimestamp
|
2021-09-01 16:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const getComputeOrdersByDatatokenAddress = gql`
|
|
|
|
query ComputeOrdersByDatatokenAddress(
|
|
|
|
$user: String!
|
|
|
|
$datatokenAddress: String!
|
|
|
|
) {
|
2022-01-13 22:24:06 +01:00
|
|
|
orders(
|
|
|
|
orderBy: createdTimestamp
|
2021-09-01 16:03:52 +02:00
|
|
|
orderDirection: desc
|
2022-01-31 13:41:58 +01:00
|
|
|
where: { payer: $user, datatoken: $datatokenAddress }
|
2021-09-01 16:03:52 +02:00
|
|
|
) {
|
|
|
|
id
|
2022-01-31 13:41:58 +01:00
|
|
|
serviceIndex
|
|
|
|
datatoken {
|
2021-09-01 16:03:52 +02:00
|
|
|
address
|
|
|
|
}
|
|
|
|
tx
|
2022-01-13 22:24:06 +01:00
|
|
|
createdTimestamp
|
2021-09-01 16:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
async function getAssetMetadata(
|
2021-10-21 09:24:00 +02:00
|
|
|
queryDtList: string[],
|
2021-09-01 16:03:52 +02:00
|
|
|
cancelToken: CancelToken,
|
|
|
|
chainIds: number[]
|
2021-11-11 08:51:13 +01:00
|
|
|
): Promise<Asset[]> {
|
2021-10-21 09:24:00 +02:00
|
|
|
const baseQueryparams = {
|
|
|
|
chainIds,
|
|
|
|
filters: [
|
|
|
|
getFilterTerm('dataToken', queryDtList),
|
|
|
|
getFilterTerm('service.type', 'compute'),
|
|
|
|
getFilterTerm('service.attributes.main.type', 'dataset')
|
|
|
|
],
|
|
|
|
ignorePurgatory: true
|
|
|
|
} as BaseQueryParams
|
|
|
|
const query = generateBaseQuery(baseQueryparams)
|
|
|
|
const result = await queryMetadata(query, cancelToken)
|
2021-09-01 16:03:52 +02:00
|
|
|
|
|
|
|
return result.results
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:52:53 +01:00
|
|
|
export async function isOrderable(
|
|
|
|
asset: Asset | DDO,
|
|
|
|
serviceId: string,
|
|
|
|
algorithm: ComputeAlgorithm,
|
|
|
|
algorithmDDO: Asset | DDO
|
|
|
|
): Promise<boolean> {
|
|
|
|
const datasetService: Service = getServiceById(asset, serviceId)
|
2022-03-01 00:43:57 +01:00
|
|
|
console.log('datasetService', datasetService)
|
2022-01-27 09:52:53 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 00:43:57 +01:00
|
|
|
return true
|
2022-01-27 09:52:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 10:59:26 +01:00
|
|
|
export function getValidUntilTime() {
|
|
|
|
const mytime = new Date()
|
|
|
|
mytime.setMinutes(mytime.getMinutes() + 19)
|
|
|
|
return Math.floor(mytime.getTime() / 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getComputeEnviroment(
|
|
|
|
asset: Asset
|
|
|
|
): Promise<ComputeEnvironment> {
|
|
|
|
if (asset?.services[0]?.type !== 'compute') return null
|
|
|
|
try {
|
|
|
|
const computeEnvs = await ProviderInstance.getComputeEnvironments(
|
|
|
|
asset.services[0].serviceEndpoint
|
|
|
|
)
|
|
|
|
if (!computeEnvs[0]) return null
|
|
|
|
return computeEnvs[0]
|
|
|
|
} catch (e) {
|
|
|
|
LoggerInstance.error('[compute] Fetch compute enviroment: ', e.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:56:13 +01:00
|
|
|
export function getQuerryString(
|
2022-02-10 15:33:16 +01:00
|
|
|
trustedAlgorithmList: PublisherTrustedAlgorithm[],
|
|
|
|
chainId?: number
|
|
|
|
): SearchQuery {
|
|
|
|
const algorithmDidList = trustedAlgorithmList.map((x) => x.did)
|
|
|
|
|
|
|
|
const baseParams = {
|
|
|
|
chainIds: [chainId],
|
|
|
|
sort: { sortBy: SortTermOptions.Created },
|
|
|
|
filters: [
|
|
|
|
getFilterTerm('metadata.type', 'algorithm'),
|
2022-02-28 13:56:13 +01:00
|
|
|
getFilterTerm('_id', algorithmDidList)
|
2022-02-10 15:33:16 +01:00
|
|
|
]
|
|
|
|
} as BaseQueryParams
|
|
|
|
|
|
|
|
const query = generateBaseQuery(baseParams)
|
2022-02-28 13:56:13 +01:00
|
|
|
console.log('querry', query)
|
2022-02-10 15:33:16 +01:00
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAlgorithmsForAsset(
|
|
|
|
asset: Asset,
|
|
|
|
token: CancelToken
|
|
|
|
): Promise<Asset[]> {
|
|
|
|
const computeService: Service = getServiceByName(asset, 'compute')
|
2022-02-28 13:56:13 +01:00
|
|
|
console.log('computeService: ', computeService)
|
2022-02-10 15:33:16 +01:00
|
|
|
let algorithms: Asset[]
|
|
|
|
if (
|
|
|
|
!computeService.compute ||
|
|
|
|
!computeService.compute.publisherTrustedAlgorithms ||
|
|
|
|
computeService.compute.publisherTrustedAlgorithms.length === 0
|
|
|
|
) {
|
2022-02-28 13:56:13 +01:00
|
|
|
console.log('computeService: []')
|
2022-02-10 15:33:16 +01:00
|
|
|
algorithms = []
|
|
|
|
} else {
|
|
|
|
const gueryResults = await queryMetadata(
|
|
|
|
getQuerryString(
|
|
|
|
computeService.compute.publisherTrustedAlgorithms,
|
|
|
|
asset.chainId
|
|
|
|
),
|
|
|
|
token
|
|
|
|
)
|
2022-02-28 13:56:13 +01:00
|
|
|
console.log('gueryResults: ', gueryResults)
|
2022-02-10 15:33:16 +01:00
|
|
|
algorithms = gueryResults?.results
|
|
|
|
}
|
|
|
|
return algorithms
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAlgorithmAssetSelectionList(
|
|
|
|
asset: Asset,
|
2022-02-28 13:56:13 +01:00
|
|
|
algorithms: Asset[]
|
2022-02-10 15:33:16 +01:00
|
|
|
): Promise<AssetSelectionAsset[]> {
|
|
|
|
const computeService: Service = getServiceByName(asset, 'compute')
|
|
|
|
let algorithmSelectionList: AssetSelectionAsset[]
|
|
|
|
if (
|
|
|
|
!computeService.compute ||
|
|
|
|
!computeService.compute.publisherTrustedAlgorithms ||
|
|
|
|
computeService.compute.publisherTrustedAlgorithms.length === 0
|
|
|
|
) {
|
|
|
|
algorithmSelectionList = []
|
|
|
|
} else {
|
2022-02-28 13:56:13 +01:00
|
|
|
algorithmSelectionList = await transformAssetToAssetSelection(
|
2022-02-10 15:33:16 +01:00
|
|
|
computeService?.serviceEndpoint,
|
|
|
|
algorithms,
|
2022-02-28 13:56:13 +01:00
|
|
|
[]
|
2022-02-10 15:33:16 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return algorithmSelectionList
|
|
|
|
}
|
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
function getServiceEndpoints(data: TokenOrder[], assets: Asset[]): string[] {
|
|
|
|
// const serviceEndpoints: string[] = []
|
2021-09-01 16:03:52 +02:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
// for (let i = 0; i < data.length; i++) {
|
|
|
|
// try {
|
|
|
|
// const did = web3.utils
|
|
|
|
// .toChecksumAddress(data[i].datatokenId.address)
|
|
|
|
// .replace('0x', 'did:op:')
|
|
|
|
// const ddo = assets.filter((x) => x.id === did)[0]
|
|
|
|
// if (ddo === undefined) continue
|
2021-09-01 16:03:52 +02:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
// const service = ddo.services.filter(
|
|
|
|
// (x: Service) => x.index === data[i].serviceId
|
|
|
|
// )[0]
|
2021-09-01 16:03:52 +02:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
// if (!service || service.type !== 'compute') continue
|
|
|
|
// const { providerEndpoint } = service
|
2021-09-01 16:03:52 +02:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
// const wasProviderQueried =
|
|
|
|
// serviceEndpoints?.filter((x) => x === providerEndpoint).length > 0
|
2021-09-01 16:03:52 +02:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
// if (wasProviderQueried) continue
|
|
|
|
// serviceEndpoints.push(providerEndpoint)
|
|
|
|
// } catch (err) {
|
2021-12-10 12:33:47 +01:00
|
|
|
// LoggerInstance.error(err.message)
|
2021-11-11 08:51:13 +01:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return serviceEndpoints
|
2021-09-01 16:03:52 +02:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
return ['dummy']
|
2021-09-01 16:03:52 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:33:47 +01:00
|
|
|
// async function getProviders(
|
|
|
|
// serviceEndpoints: string[],
|
|
|
|
// config: Config,
|
|
|
|
// ocean: Ocean
|
|
|
|
// ): Promise<Provider[]> {
|
|
|
|
// const providers: Provider[] = []
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// for (let i = 0; i < serviceEndpoints?.length; i++) {
|
|
|
|
// const instanceConfig = {
|
|
|
|
// config,
|
|
|
|
// web3: config.web3Provider,
|
|
|
|
// logger: LoggerInstance,
|
|
|
|
// ocean
|
|
|
|
// }
|
|
|
|
// const provider = await Provider.getInstance(instanceConfig)
|
|
|
|
// await provider.setBaseUrl(serviceEndpoints[i])
|
|
|
|
// const hasSameCompute =
|
|
|
|
// providers.filter((x) => x.computeAddress === provider.computeAddress)
|
|
|
|
// .length > 0
|
|
|
|
// if (!hasSameCompute) providers.push(provider)
|
|
|
|
// }
|
|
|
|
// } catch (err) {
|
|
|
|
// LoggerInstance.error(err.message)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return providers
|
|
|
|
// }
|
|
|
|
|
|
|
|
// async function getJobs(
|
|
|
|
// providers: Provider[],
|
|
|
|
// account: Account,
|
|
|
|
// assets: Asset[]
|
|
|
|
// ): Promise<ComputeJobMetaData[]> {
|
|
|
|
// const computeJobs: ComputeJobMetaData[] = []
|
|
|
|
|
|
|
|
// for (let i = 0; i < providers.length; i++) {
|
|
|
|
// try {
|
|
|
|
// const providerComputeJobs = (await providers[i].computeStatus(
|
|
|
|
// '',
|
|
|
|
// account,
|
|
|
|
// undefined,
|
|
|
|
// undefined,
|
|
|
|
// false
|
|
|
|
// )) as ComputeJob[]
|
|
|
|
|
|
|
|
// // means the provider uri is not good, so we ignore it and move on
|
|
|
|
// if (!providerComputeJobs) continue
|
|
|
|
// providerComputeJobs.sort((a, b) => {
|
|
|
|
// if (a.dateCreated > b.dateCreated) {
|
|
|
|
// return -1
|
|
|
|
// }
|
|
|
|
// if (a.dateCreated < b.dateCreated) {
|
|
|
|
// return 1
|
|
|
|
// }
|
|
|
|
// return 0
|
|
|
|
// })
|
|
|
|
|
|
|
|
// for (let j = 0; j < providerComputeJobs?.length; j++) {
|
|
|
|
// const job = providerComputeJobs[j]
|
|
|
|
// const did = job.inputDID[0]
|
|
|
|
// const ddo = assets.filter((x) => x.id === did)[0]
|
|
|
|
|
|
|
|
// if (!ddo) continue
|
|
|
|
|
|
|
|
// const compJob: ComputeJobMetaData = {
|
|
|
|
// ...job,
|
|
|
|
// assetName: ddo.metadata.name,
|
|
|
|
// assetDtSymbol: ddo.dataTokenInfo.symbol,
|
|
|
|
// networkId: ddo.chainId
|
|
|
|
// }
|
|
|
|
// computeJobs.push(compJob)
|
|
|
|
// }
|
|
|
|
// } catch (err) {
|
|
|
|
// LoggerInstance.error(err.message)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return computeJobs
|
|
|
|
// }
|
|
|
|
|
|
|
|
// function getDtList(data: TokenOrder[]): string[] {
|
|
|
|
// const dtList = []
|
|
|
|
|
|
|
|
// for (let i = 0; i < data.length; i++) {
|
|
|
|
// dtList.push(data[i].datatokenId.address)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return dtList
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export async function getComputeJobs(
|
|
|
|
// chainIds: number[],
|
|
|
|
// account: Account,
|
|
|
|
// ddo?: Asset,
|
|
|
|
// token?: CancelToken
|
|
|
|
// ): Promise<ComputeResults> {
|
|
|
|
// const assetDTAddress = ddo?.dataTokenInfo?.address
|
|
|
|
// let computeResult: ComputeResults = {
|
|
|
|
// computeJobs: [],
|
|
|
|
// isLoaded: false
|
|
|
|
// }
|
|
|
|
// let isLoading = true
|
|
|
|
// const variables = assetDTAddress
|
|
|
|
// ? {
|
|
|
|
// user: account?.getId().toLowerCase(),
|
|
|
|
// datatokenAddress: assetDTAddress.toLowerCase()
|
|
|
|
// }
|
|
|
|
// : {
|
|
|
|
// user: account?.getId().toLowerCase()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const result = await fetchDataForMultipleChains(
|
|
|
|
// assetDTAddress ? getComputeOrdersByDatatokenAddress : getComputeOrders,
|
|
|
|
// variables,
|
|
|
|
// assetDTAddress ? [ddo?.chainId] : chainIds
|
|
|
|
// )
|
|
|
|
// let data: TokenOrder[] = []
|
|
|
|
// for (let i = 0; i < result.length; i++) {
|
|
|
|
// if (!result[i]?.tokenOrders || result[i].tokenOrders.length === 0) continue
|
|
|
|
// result[i]?.tokenOrders.forEach((tokenOrder: TokenOrder) => {
|
|
|
|
// data.push(tokenOrder)
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// if (!ocean || !account || !data) return
|
|
|
|
|
|
|
|
// if (data.length === 0) {
|
|
|
|
// return computeResult
|
|
|
|
// }
|
|
|
|
|
|
|
|
// data = data.sort((a, b) => b.timestamp - a.timestamp)
|
|
|
|
// const queryDtList = getDtList(data)
|
|
|
|
// if (!queryDtList) return
|
|
|
|
|
|
|
|
// const assets = await getAssetMetadata(queryDtList, token, chainIds)
|
|
|
|
// const serviceEndpoints = getServiceEndpoints(data, assets)
|
|
|
|
// const providers: Provider[] = await getProviders(
|
|
|
|
// serviceEndpoints,
|
|
|
|
// config,
|
|
|
|
// ocean
|
|
|
|
// )
|
|
|
|
// const computeJobs = await getJobs(providers, account, assets)
|
|
|
|
// isLoading = false
|
|
|
|
// computeResult = {
|
|
|
|
// computeJobs: computeJobs,
|
|
|
|
// isLoaded: isLoading
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return computeResult
|
|
|
|
// }
|
|
|
|
|
2022-02-25 11:33:07 +01:00
|
|
|
export async function createTrustedAlgorithmList(
|
|
|
|
selectedAlgorithms: string[], // list of DIDs,
|
|
|
|
assetChainId: number,
|
|
|
|
cancelToken: CancelToken
|
|
|
|
): Promise<PublisherTrustedAlgorithm[]> {
|
|
|
|
const trustedAlgorithms: PublisherTrustedAlgorithm[] = []
|
|
|
|
|
|
|
|
const selectedAssets = await retrieveDDOListByDIDs(
|
|
|
|
selectedAlgorithms,
|
|
|
|
[assetChainId],
|
|
|
|
cancelToken
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const selectedAlgorithm of selectedAssets) {
|
|
|
|
const trustedAlgorithm = {
|
|
|
|
did: selectedAlgorithm.id,
|
|
|
|
containerSectionChecksum: getHash(
|
|
|
|
JSON.stringify(selectedAlgorithm.metadata.algorithm.container)
|
|
|
|
),
|
|
|
|
filesChecksum: getHash(selectedAlgorithm.services[0].files)
|
|
|
|
}
|
|
|
|
trustedAlgorithms.push(trustedAlgorithm)
|
|
|
|
}
|
|
|
|
return trustedAlgorithms
|
|
|
|
}
|
2021-12-10 12:33:47 +01:00
|
|
|
|
2022-02-25 11:33:07 +01:00
|
|
|
export async function transformComputeFormToServiceComputeOptions(
|
|
|
|
values: ComputePrivacyForm,
|
|
|
|
currentOptions: ServiceComputeOptions,
|
|
|
|
assetChainId: number,
|
|
|
|
cancelToken: CancelToken
|
|
|
|
): Promise<ServiceComputeOptions> {
|
|
|
|
const publisherTrustedAlgorithms = values.allowAllPublishedAlgorithms
|
|
|
|
? []
|
|
|
|
: await createTrustedAlgorithmList(
|
|
|
|
values.publisherTrustedAlgorithms,
|
|
|
|
assetChainId,
|
|
|
|
cancelToken
|
|
|
|
)
|
2021-12-10 12:33:47 +01:00
|
|
|
|
2022-02-25 11:33:07 +01:00
|
|
|
const privacy: ServiceComputeOptions = {
|
|
|
|
...currentOptions,
|
|
|
|
publisherTrustedAlgorithms
|
|
|
|
}
|
2021-12-10 12:33:47 +01:00
|
|
|
|
2022-02-25 11:33:07 +01:00
|
|
|
return privacy
|
|
|
|
}
|