1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
This commit is contained in:
Matthias Kretschmann 2021-11-24 14:59:42 +00:00
parent 8e9cb3acad
commit 7b3b0f5ebe
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 2 additions and 215 deletions

View File

@ -1,159 +0,0 @@
import { Logger } from '@oceanprotocol/lib'
import {
Service,
ServiceComputePrivacy,
ServiceType
} from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
import { useEffect, useState } from 'react'
import { publishFeedback } from '@utils/feedback'
import { useOcean } from '@context/Ocean'
import { useWeb3 } from '@context/Web3'
import { getOceanConfig } from '@utils/ocean'
import { DataTokenOptions } from '@utils/datatokens'
interface UsePublish {
publish: (
asset: DDO,
serviceType: ServiceType,
dataTokenOptions?: DataTokenOptions,
timeout?: number,
providerUri?: string
) => Promise<Asset>
publishStep?: number
publishStepText?: string
publishError?: string
isLoading: boolean
}
function usePublish(): UsePublish {
const { networkId, web3Loading } = useWeb3()
const { connect, ocean, account } = useOcean()
const [isLoading, setIsLoading] = useState(false)
const [publishStep, setPublishStep] = useState<number | undefined>()
const [publishStepText, setPublishStepText] = useState<string | undefined>()
const [publishError, setPublishError] = useState<string | undefined>()
function setStep(index?: number) {
setPublishStep(index)
index && setPublishStepText(publishFeedback[index])
}
//
// Initiate OceanProvider based on user wallet
//
useEffect(() => {
if (web3Loading || !connect) return
async function initOcean() {
const config = getOceanConfig(networkId)
await connect(config)
}
initOcean()
}, [web3Loading, networkId, connect])
/**
* Publish an asset. It also creates the datatoken, mints tokens and gives the market allowance
* @param {Metadata} asset The metadata of the asset.
* @param {PriceOptions} priceOptions : number of tokens to mint, datatoken weight , liquidity fee, type : fixed, dynamic
* @param {ServiceType} serviceType Desired service type of the asset access or compute
* @param {DataTokenOptions} dataTokenOptions custom name, symbol and cap for datatoken
* @return {Promise<Asset>} Returns the newly published ddo
*/
async function publish(
asset: Asset,
serviceType: ServiceType,
dataTokenOptions?: DataTokenOptions,
timeout?: number,
providerUri?: string
): Promise<Asset> {
if (!ocean || !account) return null
setIsLoading(true)
setPublishError(undefined)
setStep(0)
try {
const publishedDate =
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const services: Service[] = []
const price = '1'
asset.metadata.created = publishedDate
switch (serviceType) {
case 'access': {
if (!timeout) timeout = 0
const accessService =
await ocean.assets.createAccessServiceAttributes(
account,
price,
publishedDate,
timeout,
providerUri,
null
)
Logger.log('access service created', accessService)
services.push(accessService)
break
}
case 'compute': {
if (!timeout) timeout = 3600
const provider = {}
const origComputePrivacy: ServiceComputePrivacy = {
allowRawAlgorithm: false,
allowNetworkAccess: false,
allowAllPublishedAlgorithms: false,
publisherTrustedAlgorithms: []
}
const computeService = ocean.compute.createComputeService(
account,
price,
publishedDate,
provider,
origComputePrivacy,
timeout,
providerUri
)
services.push(computeService)
break
}
}
Logger.log('services created', services)
// const ddo = await ocean.assets
// .create(
// asset,
// account,
// services,
// undefined,
// dataTokenOptions?.cap,
// dataTokenOptions?.name,
// dataTokenOptions?.symbol,
// providerUri
// )
// .next(setStep)
// Logger.log('ddo created', ddo)
// await ocean.assets.publishDdo(ddo, account.getId())
// Logger.log('ddo published')
// await sleep(20000)
// setStep(7)
// return ddo
} catch (error) {
setPublishError(error.message)
Logger.error(error)
setStep()
} finally {
setIsLoading(false)
}
}
return {
publish,
publishStep,
publishStepText,
isLoading,
publishError
}
}
export { usePublish }
export default usePublish

View File

@ -4,7 +4,7 @@ import { FormPublishData } from '../_types'
import { useFormikContext } from 'formik'
export default function Submission(): ReactElement {
const { values } = useFormikContext<FormPublishData>()
const { values, handleSubmit } = useFormikContext<FormPublishData>()
return (
<div className={styles.submission}>

View File

@ -1,9 +1,7 @@
import React, { ReactElement, useState, useRef } from 'react'
import { Form, Formik, FormikState } from 'formik'
import { Form, Formik } from 'formik'
import { initialValues } from './_constants'
import { validationSchema } from './_validation'
import { validateDockerImage } from '@utils/docker'
import { Logger } from '@oceanprotocol/lib'
import { useAccountPurgatory } from '@hooks/useAccountPurgatory'
import { useWeb3 } from '@context/Web3'
import { transformPublishFormToDdo } from './_utils'
@ -30,7 +28,6 @@ export default function PublishPage({
const { debug } = useUserPreferences()
const { accountId, chainId } = useWeb3()
const { isInPurgatory, purgatoryData } = useAccountPurgatory(accountId)
// const { publish, publishError, isLoading, publishStepText } = usePublish()
const [success, setSuccess] = useState<string>()
const [error, setError] = useState<string>()
const scrollToRef = useRef()
@ -76,57 +73,6 @@ export default function PublishPage({
}
}
// async function handleSubmit(
// values: Partial<FormPublishData>,
// resetForm: (
// nextState?: Partial<FormikState<Partial<FormPublishData>>>
// ) => void
// ): Promise<void> {
// try {
// const metadata = transformPublishFormToMetadata(values)
// const timeout = mapTimeoutStringToSeconds(values.timeout)
// const serviceType = values.access === 'Download' ? 'access' : 'compute'
// Logger.log(
// 'Publish with ',
// metadata,
// serviceType,
// values.dataTokenOptions,
// values.providerUri
// )
// const ddo = await publish(
// metadata as unknown as Metadata,
// serviceType,
// values.dataTokenOptions,
// timeout,
// values.providerUri
// )
// // Publish failed
// if (!ddo || publishError) {
// setError(publishError || 'Publishing DDO failed.')
// Logger.error(publishError || 'Publishing DDO failed.')
// return
// }
// // Publish succeeded
// // setDid(ddo.id)
// setSuccess(
// '🎉 Successfully published. 🎉 Now create a price on your data set.'
// )
// resetForm({
// values: initialValues as FormPublishData,
// status: 'empty'
// })
// // move user's focus to top of screen
// window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
// } catch (error) {
// setError(error.message)
// Logger.error(error.message)
// }
// }
return (
<>
<PageHeader title={<Title />} description={content.description} />