mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
108 lines
2.6 KiB
TypeScript
108 lines
2.6 KiB
TypeScript
import {
|
|
ComputeAlgorithm,
|
|
ComputeAsset,
|
|
ComputeEnvironment,
|
|
downloadFileBrowser,
|
|
FileMetadata,
|
|
LoggerInstance,
|
|
ProviderComputeInitializeResults,
|
|
ProviderInstance
|
|
} from '@oceanprotocol/lib'
|
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
|
import Web3 from 'web3'
|
|
import { getValidUntilTime } from './compute'
|
|
|
|
export async function initializeProviderForCompute(
|
|
dataset: AssetExtended,
|
|
algorithm: AssetExtended,
|
|
accountId: string,
|
|
computeEnv: ComputeEnvironment = null
|
|
): Promise<ProviderComputeInitializeResults> {
|
|
const computeAsset: ComputeAsset = {
|
|
documentId: dataset.id,
|
|
serviceId: dataset.services[0].id
|
|
// transferTxId: dataset.accessDetails.validOrderTx
|
|
}
|
|
const computeAlgo: ComputeAlgorithm = {
|
|
documentId: algorithm.id,
|
|
serviceId: algorithm.services[0].id
|
|
// transferTxId: algorithm.accessDetails.validOrderTx
|
|
}
|
|
|
|
const validUntil = getValidUntilTime(
|
|
computeEnv?.maxJobDuration,
|
|
dataset.services[0].timeout,
|
|
algorithm.services[0].timeout
|
|
)
|
|
|
|
return await ProviderInstance.initializeCompute(
|
|
[computeAsset],
|
|
computeAlgo,
|
|
computeEnv?.id,
|
|
validUntil,
|
|
dataset.services[0].serviceEndpoint,
|
|
accountId
|
|
)
|
|
}
|
|
|
|
// TODO: Why do we have these one line functions ?!?!?!
|
|
export async function getEncryptedFiles(
|
|
files: FileMetadata[],
|
|
providerUrl: string
|
|
): Promise<string> {
|
|
try {
|
|
// https://github.com/oceanprotocol/provider/blob/v4main/API.md#encrypt-endpoint
|
|
const response = await ProviderInstance.encrypt(files, providerUrl)
|
|
return response
|
|
} catch (error) {
|
|
console.error('Error parsing json: ' + error.message)
|
|
}
|
|
}
|
|
|
|
export async function getFileDidInfo(
|
|
did: string,
|
|
serviceId: string,
|
|
providerUrl: string
|
|
): Promise<FileMetadata[]> {
|
|
try {
|
|
const response = await ProviderInstance.checkDidFiles(
|
|
did,
|
|
serviceId as any, // TODO: why does ocean.js want a number here?
|
|
providerUrl
|
|
)
|
|
return response
|
|
} catch (error) {
|
|
LoggerInstance.error(error.message)
|
|
}
|
|
}
|
|
|
|
export async function getFileUrlInfo(
|
|
url: string,
|
|
providerUrl: string
|
|
): Promise<FileMetadata[]> {
|
|
try {
|
|
const response = await ProviderInstance.checkFileUrl(url, providerUrl)
|
|
return response
|
|
} catch (error) {
|
|
LoggerInstance.error(error.message)
|
|
}
|
|
}
|
|
|
|
export async function downloadFile(
|
|
web3: Web3,
|
|
asset: AssetExtended,
|
|
accountId: string,
|
|
validOrderTx?: string
|
|
) {
|
|
const downloadUrl = await ProviderInstance.getDownloadUrl(
|
|
asset.id,
|
|
accountId,
|
|
asset.services[0].id,
|
|
0,
|
|
validOrderTx || asset.accessDetails.validOrderTx,
|
|
asset.services[0].serviceEndpoint,
|
|
web3
|
|
)
|
|
await downloadFileBrowser(downloadUrl)
|
|
}
|