1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-01-21 02:09:57 +01:00
This commit is contained in:
alexcos20 2020-10-14 09:19:22 -07:00
parent 8896e776b3
commit c5cce9e7cf
2 changed files with 25 additions and 20 deletions

View File

@ -1,13 +1,6 @@
import { DID, DDO, Logger, Metadata } from '@oceanprotocol/lib' import { Logger } from '@oceanprotocol/lib'
import {
Service,
ServiceComputePrivacy,
ServiceType
} from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
import { useState } from 'react' import { useState } from 'react'
import { useOcean } from 'providers' import { useOcean } from 'providers'
import ProviderStatus from 'providers/OceanProvider/ProviderStatus'
import { publishFeedback } from 'utils'
import { PriceOptions } from './PriceOptions' import { PriceOptions } from './PriceOptions'
import { TransactionReceipt } from 'web3-core' import { TransactionReceipt } from 'web3-core'
import { getBestDataTokenPrice, getFirstPool } from 'utils/dtUtils' import { getBestDataTokenPrice, getFirstPool } from 'utils/dtUtils'
@ -17,7 +10,7 @@ interface UsePricing {
createPricing: ( createPricing: (
dataTokenAddress: string, dataTokenAddress: string,
priceOptions: PriceOptions priceOptions: PriceOptions
) => Promise<TransactionReceipt | null> ) => Promise<TransactionReceipt | string | null>
buyDT: ( buyDT: (
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string dtAmount: number | string
@ -73,7 +66,8 @@ function usePricing(): UsePricing {
async function createPricing( async function createPricing(
dataTokenAddress: string, dataTokenAddress: string,
priceOptions: PriceOptions priceOptions: PriceOptions
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | string | null> {
if (!ocean || !account || !accountId) return null
setStepCreatePricing(0) setStepCreatePricing(0)
let response = null let response = null
try { try {
@ -105,7 +99,7 @@ function usePricing(): UsePricing {
await ocean.datatokens.approve( await ocean.datatokens.approve(
dataTokenAddress, dataTokenAddress,
config.fixedRateExchangeAddress, config.fixedRateExchangeAddress,
priceOptions.dtAmount, String(priceOptions.dtAmount),
accountId accountId
) )
setStepCreatePricing(3) setStepCreatePricing(3)
@ -120,13 +114,14 @@ function usePricing(): UsePricing {
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setIsLoading(false)
} }
return null
} }
async function buyDT( async function buyDT(
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string dtAmount: number | string
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | null> {
if (!ocean || !account || !accountId) return if (!ocean || !account || !accountId) return null
setIsLoading(true) setIsLoading(true)
setPricingError(undefined) setPricingError(undefined)
@ -159,11 +154,11 @@ function usePricing(): UsePricing {
case 'exchange': { case 'exchange': {
if (!config.oceanTokenAddress) { if (!config.oceanTokenAddress) {
Logger.error(`'oceanTokenAddress' not set in config`) Logger.error(`'oceanTokenAddress' not set in config`)
return return null
} }
if (!config.fixedRateExchangeAddress) { if (!config.fixedRateExchangeAddress) {
Logger.error(`'fixedRateExchangeAddress' not set in config`) Logger.error(`'fixedRateExchangeAddress' not set in config`)
return return null
} }
Logger.log('Buying token from exchange', bestPrice, account.getId()) Logger.log('Buying token from exchange', bestPrice, account.getId())
await ocean.datatokens.approve( await ocean.datatokens.approve(
@ -191,21 +186,26 @@ function usePricing(): UsePricing {
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setIsLoading(false)
} }
return null
} }
async function sellDT( async function sellDT(
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string dtAmount: number | string
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | null> {
if (!ocean || !account || !accountId) return if (!ocean || !account || !accountId) return null
if (!config.oceanTokenAddress) {
Logger.error(`'oceanTokenAddress' not set in config`)
return null
}
setIsLoading(true) setIsLoading(true)
setPricingError(undefined) setPricingError(undefined)
setStepSellDT(0) setStepSellDT(0)
try { try {
const pool = await getFirstPool(ocean, dataTokenAddress) const pool = await getFirstPool(ocean, dataTokenAddress)
const price = new Decimal(pool.value).times(0.95).toString() if (!pool || pool.price === 0) return null
const price = new Decimal(pool.price).times(0.95).toString()
setStepSellDT(1) setStepSellDT(1)
Logger.log('Selling token to pool', pool, account.getId(), price) Logger.log('Selling token to pool', pool, account.getId(), price)
const sellResponse = await ocean.pool.sellDT( const sellResponse = await ocean.pool.sellDT(
@ -225,6 +225,7 @@ function usePricing(): UsePricing {
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setIsLoading(false)
} }
return null
} }
return { return {

View File

@ -65,7 +65,7 @@ function usePublish(): UsePublish {
const publishedDate = const publishedDate =
new Date(Date.now()).toISOString().split('.')[0] + 'Z' new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const timeout = 0 let timeout = 0
const services: Service[] = [] const services: Service[] = []
const price = '1' const price = '1'
@ -75,13 +75,15 @@ function usePublish(): UsePublish {
account, account,
price, price,
publishedDate, publishedDate,
timeout timeout,
providerUri
) )
Logger.log('access service created', accessService) Logger.log('access service created', accessService)
services.push(accessService) services.push(accessService)
break break
} }
case 'compute': { case 'compute': {
timeout = 3600
const cluster = ocean.compute.createClusterAttributes( const cluster = ocean.compute.createClusterAttributes(
'Kubernetes', 'Kubernetes',
'http://10.0.0.17/xxx' 'http://10.0.0.17/xxx'
@ -122,7 +124,9 @@ function usePublish(): UsePublish {
price, price,
publishedDate, publishedDate,
provider, provider,
origComputePrivacy as ServiceComputePrivacy origComputePrivacy as ServiceComputePrivacy,
timeout,
providerUri
) )
services.push(computeService) services.push(computeService)
break break