2020-08-11 08:20:40 +02:00
|
|
|
import React, { ReactElement, useState, useEffect } from 'react'
|
2020-07-08 17:57:53 +02:00
|
|
|
import styles from './index.module.css'
|
|
|
|
import Compute from './Compute'
|
|
|
|
import Consume from './Consume'
|
2020-12-10 14:30:40 +01:00
|
|
|
import { Logger } from '@oceanprotocol/lib'
|
2020-07-23 12:26:08 +02:00
|
|
|
import Tabs from '../../atoms/Tabs'
|
2020-08-11 09:30:17 +02:00
|
|
|
import compareAsBN from '../../../utils/compareAsBN'
|
2020-08-18 17:14:54 +02:00
|
|
|
import Pool from './Pool'
|
2020-11-16 16:21:15 +01:00
|
|
|
import Trade from './Trade'
|
2020-11-20 14:31:28 +01:00
|
|
|
import { useAsset } from '../../../providers/Asset'
|
2021-03-17 11:44:26 +01:00
|
|
|
import { useOcean } from '../../../providers/Ocean'
|
|
|
|
import { useWeb3 } from '../../../providers/Web3'
|
2020-07-08 17:57:53 +02:00
|
|
|
|
2020-12-10 14:30:40 +01:00
|
|
|
export default function AssetActions(): ReactElement {
|
2021-03-17 11:44:26 +01:00
|
|
|
const { accountId } = useWeb3()
|
|
|
|
const { ocean, balance, account } = useOcean()
|
2020-12-10 14:30:40 +01:00
|
|
|
const { price, ddo, metadata } = useAsset()
|
2021-01-21 16:02:48 +01:00
|
|
|
|
2020-10-29 21:34:53 +01:00
|
|
|
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>()
|
2020-10-19 11:33:06 +02:00
|
|
|
const [dtBalance, setDtBalance] = useState<string>()
|
2020-12-10 14:30:40 +01:00
|
|
|
const isCompute = Boolean(ddo?.findServiceByType('compute'))
|
2020-08-07 18:12:39 +02:00
|
|
|
|
2020-10-19 11:33:06 +02:00
|
|
|
// Get and set user DT balance
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ocean || !accountId) return
|
2021-01-21 16:02:48 +01:00
|
|
|
|
2020-10-19 11:33:06 +02:00
|
|
|
async function init() {
|
|
|
|
try {
|
|
|
|
const dtBalance = await ocean.datatokens.balance(
|
|
|
|
ddo.dataToken,
|
|
|
|
accountId
|
|
|
|
)
|
|
|
|
setDtBalance(dtBalance)
|
|
|
|
} catch (e) {
|
|
|
|
Logger.error(e.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init()
|
|
|
|
}, [ocean, accountId, ddo.dataToken])
|
|
|
|
|
2020-08-11 08:20:40 +02:00
|
|
|
// Check user balance against price
|
2020-10-29 21:34:53 +01:00
|
|
|
useEffect(() => {
|
2021-03-17 11:44:26 +01:00
|
|
|
if (!price?.value || !account || !balance?.ocean || !dtBalance) return
|
2020-08-11 08:20:40 +02:00
|
|
|
|
2020-10-29 21:34:53 +01:00
|
|
|
setIsBalanceSufficient(
|
|
|
|
compareAsBN(balance.ocean, `${price.value}`) || Number(dtBalance) >= 1
|
|
|
|
)
|
2020-08-11 08:20:40 +02:00
|
|
|
|
2020-10-29 21:34:53 +01:00
|
|
|
return () => {
|
|
|
|
setIsBalanceSufficient(false)
|
|
|
|
}
|
2021-03-17 11:44:26 +01:00
|
|
|
}, [balance, account, price, dtBalance])
|
2020-08-11 08:20:40 +02:00
|
|
|
|
2020-07-23 12:26:08 +02:00
|
|
|
const UseContent = isCompute ? (
|
2020-10-19 11:33:06 +02:00
|
|
|
<Compute
|
|
|
|
dtBalance={dtBalance}
|
|
|
|
isBalanceSufficient={isBalanceSufficient}
|
Start compute job (#439)
* Wip start compute job
* Wip select algorithm design
* Asset selection form component, for start compute job (#442)
* prototype AssetSelection
* assetselection styling
* typing "fix"
* put back file info icon
* AssetSelection styling in context
* update start job method, fixed algo select, and fixed option typing
* compute logic update
* add has previous orders for algo asset
* fixed search algorithm assets in start compute form
* fixed lint errors
* updated previous order for algo logic and compute flow
* update use price hook and added buy DT for algo
* display only alg of type exchange and sort by value
* display only trusted algo for asset if field is set
* added logic for allow all published algorithms or no algorithms allowed
* asset selection style & spacing tweaks
* refactor get algorithms for compute and edit compute
* fixed form options and more refactoring
* new ButtonBuy component
* shared component between consume/compute
* dealing with various states: loading, previous orders, help text output
* effect dependencies
* move error output into toast
* formik data flow refactor
* ditch custom field change handler
* fix initialValues
* typed form data & validation
* fixes multiple form validation issues along the way
* isInitialValid → validateOnMount
* metadata display tweaks
* error feedback tweaks
* oler assets checks, confeti on succes job, market fee on order, removed algo compute logic
* more startJob logging
* feedback & messaging changes
* metadata display
* return all algos, fixed & dynamic priced ones
* fix DOM nesting
* messaging updates
* copy tweaks
* check algorithm previous history for both acces and compute sercive types
* handle start compute error
* extra checks on start compute response
* styling tweaks, fix toast UI errors
* AssetSelection: empty screen, tweak min/max height
* fix FRE issues on start compute
* check is ordarable before start compute job
* logging tweaks
* disable eslint no-unused-vars rule for some Apollo code blocks
* fix metadata editing for compute assets
* consider dataset timeout for compute too
Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-04-01 17:21:08 +02:00
|
|
|
file={metadata?.main.files[0]}
|
2020-10-19 11:33:06 +02:00
|
|
|
/>
|
2020-07-23 12:26:08 +02:00
|
|
|
) : (
|
2020-08-11 08:20:40 +02:00
|
|
|
<Consume
|
|
|
|
ddo={ddo}
|
2020-10-19 11:33:06 +02:00
|
|
|
dtBalance={dtBalance}
|
2020-08-11 08:20:40 +02:00
|
|
|
isBalanceSufficient={isBalanceSufficient}
|
2020-12-10 14:30:40 +01:00
|
|
|
file={metadata?.main.files[0]}
|
2020-08-11 08:20:40 +02:00
|
|
|
/>
|
2020-07-08 17:57:53 +02:00
|
|
|
)
|
2020-07-23 12:26:08 +02:00
|
|
|
|
|
|
|
const tabs = [
|
|
|
|
{
|
|
|
|
title: 'Use',
|
|
|
|
content: UseContent
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2021-05-11 10:33:05 +02:00
|
|
|
price?.type === 'pool' &&
|
2020-11-16 16:21:15 +01:00
|
|
|
tabs.push(
|
|
|
|
{
|
|
|
|
title: 'Pool',
|
2020-12-10 14:30:40 +01:00
|
|
|
content: <Pool />
|
2020-11-16 16:21:15 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Trade',
|
2020-12-10 14:30:40 +01:00
|
|
|
content: <Trade />
|
2020-11-16 16:21:15 +01:00
|
|
|
}
|
|
|
|
)
|
2020-09-23 13:55:53 +02:00
|
|
|
|
2020-07-23 12:26:08 +02:00
|
|
|
return <Tabs items={tabs} className={styles.actions} />
|
2020-07-08 17:57:53 +02:00
|
|
|
}
|