react/src/hooks/usePublish/usePublish.ts

212 lines
6.2 KiB
TypeScript
Raw Normal View History

2020-09-07 10:54:56 +02:00
import { useState } from 'react'
import { DDO, Metadata, Logger } from '@oceanprotocol/lib'
2020-07-14 13:34:08 +02:00
import { useOcean } from '../../providers'
2020-07-09 14:33:22 +02:00
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
2020-07-17 15:09:01 +02:00
import {
Service,
2020-08-04 13:37:12 +02:00
ServiceComputePrivacy,
ServiceType
2020-07-17 15:09:01 +02:00
} from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
2020-09-07 10:54:56 +02:00
import { PriceOptions } from './PriceOptions'
2020-07-17 12:24:46 +02:00
import { publishFeedback } from '../../utils'
2020-09-07 16:25:31 +02:00
import { DataTokenOptions } from '.'
2020-07-09 14:33:22 +02:00
interface UsePublish {
2020-07-14 13:34:08 +02:00
publish: (
asset: Metadata,
2020-09-07 10:54:56 +02:00
priceOptions: PriceOptions,
2020-08-04 13:37:12 +02:00
serviceConfigs: ServiceType,
2020-09-07 16:25:31 +02:00
dataTokenOptions?: DataTokenOptions
) => Promise<DDO | undefined | null>
2020-07-16 11:25:01 +02:00
mint: (tokenAddress: string, tokensToMint: string) => void
2020-07-17 12:24:46 +02:00
publishStep?: number
publishStepText?: string
publishError?: string
isLoading: boolean
2020-07-09 14:33:22 +02:00
}
function usePublish(): UsePublish {
2020-09-10 16:59:39 +02:00
const { ocean, status, account, accountId, config } = useOcean()
2020-07-17 12:24:46 +02:00
const [isLoading, setIsLoading] = useState(false)
const [publishStep, setPublishStep] = useState<number | undefined>()
const [publishStepText, setPublishStepText] = useState<string | undefined>()
const [publishError, setPublishError] = useState<string | undefined>()
2020-07-14 13:34:08 +02:00
function setStep(index?: number) {
2020-07-17 12:24:46 +02:00
setPublishStep(index)
index && setPublishStepText(publishFeedback[index])
2020-07-16 11:25:01 +02:00
}
2020-09-15 21:57:44 +02:00
async function mint(tokenAddress: string, tokensToMint: string) {
Logger.log('mint function', tokenAddress, accountId)
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
}
async function createPricing(
priceOptions: PriceOptions,
dataTokenAddress: string,
mintedTokens: string
): Promise<void | null> {
switch (priceOptions.type) {
case 'dynamic': {
const pool = await ocean.pool.createDTPool(
accountId,
dataTokenAddress,
priceOptions.tokensToMint.toString(),
priceOptions.weightOnDataToken,
priceOptions.liquidityProviderFee
)
break
}
case 'fixed': {
if (!config.fixedRateExchangeAddress) {
Logger.error(`'fixedRateExchangeAddress' not set in ccnfig.`)
return null
}
const fixedPriceExchange = await ocean.fixedRateExchange.create(
dataTokenAddress,
priceOptions.price.toString(),
accountId
)
await ocean.datatokens.approve(
dataTokenAddress,
config.fixedRateExchangeAddress,
mintedTokens,
accountId
)
break
}
}
}
2020-07-17 12:24:46 +02:00
/**
* 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
2020-09-07 10:54:56 +02:00
* @param {ServiceType} serviceType Desired service type of the asset access or compute
2020-09-07 16:25:31 +02:00
* @param {DataTokenOptions} dataTokenOptions custom name, symbol and cap for datatoken
2020-07-17 12:24:46 +02:00
* @return {Promise<DDO>} Returns the newly published ddo
*/
2020-07-14 13:34:08 +02:00
async function publish(
asset: Metadata,
2020-09-07 10:54:56 +02:00
priceOptions: PriceOptions,
2020-09-07 16:25:31 +02:00
serviceType: ServiceType,
dataTokenOptions?: DataTokenOptions
): Promise<DDO | undefined | null> {
if (status !== ProviderStatus.CONNECTED || !ocean || !account) return null
2020-07-17 12:24:46 +02:00
setIsLoading(true)
setPublishError(undefined)
2020-07-17 12:24:46 +02:00
try {
2020-09-07 10:54:56 +02:00
const tokensToMint = priceOptions.tokensToMint.toString()
2020-07-14 13:34:08 +02:00
2020-07-17 12:24:46 +02:00
const publishedDate =
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const timeout = 0
const services: Service[] = []
2020-07-17 15:09:01 +02:00
2020-08-04 13:37:12 +02:00
const price = ocean.datatokens.toWei('1')
switch (serviceType) {
case 'access': {
const accessService = await ocean.assets.createAccessServiceAttributes(
account,
price,
publishedDate,
timeout
)
Logger.log('access service created', accessService)
services.push(accessService)
break
}
case 'compute': {
const cluster = ocean.compute.createClusterAttributes(
'Kubernetes',
'http://10.0.0.17/xxx'
)
const servers = [
ocean.compute.createServerAttributes(
'1',
'xlsize',
'50',
'16',
'0',
'128gb',
'160gb',
2020-07-17 12:24:46 +02:00
timeout
)
2020-08-04 13:37:12 +02:00
]
const containers = [
ocean.compute.createContainerAttributes(
'tensorflow/tensorflow',
'latest',
'sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc'
2020-07-17 12:24:46 +02:00
)
2020-08-04 13:37:12 +02:00
]
const provider = ocean.compute.createProviderAttributes(
'Azure',
'Compute service with 16gb ram for each node.',
cluster,
containers,
servers
)
const origComputePrivacy = {
allowRawAlgorithm: true,
allowNetworkAccess: false,
trustedAlgorithms: [] as any
2020-07-17 12:24:46 +02:00
}
2020-08-04 13:37:12 +02:00
const computeService = ocean.compute.createComputeService(
account,
price,
publishedDate,
provider,
origComputePrivacy as ServiceComputePrivacy
)
services.push(computeService)
break
2020-07-16 16:03:26 +02:00
}
2020-08-04 13:37:12 +02:00
}
2020-07-17 12:24:46 +02:00
Logger.log('services created', services)
2020-09-07 10:54:56 +02:00
const ddo = await ocean.assets
2020-09-07 16:25:31 +02:00
.create(
asset,
account,
services,
dataTokenOptions?.cap,
dataTokenOptions?.name,
dataTokenOptions?.symbol
)
2020-09-07 10:54:56 +02:00
.next(setStep)
2020-07-17 15:09:01 +02:00
Logger.log('ddo created', ddo)
2020-09-07 10:54:56 +02:00
setStep(7)
await mint(ddo.dataToken, tokensToMint)
Logger.log(`minted ${tokensToMint} tokens`)
2020-07-14 13:34:08 +02:00
2020-09-10 16:59:39 +02:00
await createPricing(priceOptions, ddo.dataToken, tokensToMint)
2020-09-07 10:54:56 +02:00
setStep(8)
2020-07-17 12:24:46 +02:00
return ddo
} catch (error) {
setPublishError(error.message)
Logger.error(error)
setStep()
2020-07-17 12:24:46 +02:00
} finally {
setIsLoading(false)
}
2020-07-14 13:34:08 +02:00
}
return {
publish,
2020-07-17 12:24:46 +02:00
mint,
publishStep,
publishStepText,
isLoading,
publishError
2020-07-14 13:34:08 +02:00
}
2020-07-09 14:33:22 +02:00
}
export { usePublish, UsePublish }
export default usePublish