mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
Refactor Asset context provider (#1055)
* refactor Asset context provider * use useIsMounted hook * typings and effect tweaks * use chainId * effect tweak
This commit is contained in:
parent
57be62a6b1
commit
8074a6143c
@ -7,7 +7,7 @@ import React, {
|
|||||||
useCallback,
|
useCallback,
|
||||||
ReactNode
|
ReactNode
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { Asset, Config, LoggerInstance, Purgatory } from '@oceanprotocol/lib'
|
import { Config, LoggerInstance, Purgatory } from '@oceanprotocol/lib'
|
||||||
import { CancelToken } from 'axios'
|
import { CancelToken } from 'axios'
|
||||||
import { retrieveAsset } from '@utils/aquarius'
|
import { retrieveAsset } from '@utils/aquarius'
|
||||||
import { useWeb3 } from './Web3'
|
import { useWeb3 } from './Web3'
|
||||||
@ -16,20 +16,20 @@ import { useCancelToken } from '@hooks/useCancelToken'
|
|||||||
import { getOceanConfig, getDevelopmentConfig } from '@utils/ocean'
|
import { getOceanConfig, getDevelopmentConfig } from '@utils/ocean'
|
||||||
import { AssetExtended } from 'src/@types/AssetExtended'
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
||||||
import { getAccessDetails } from '@utils/accessDetailsAndPricing'
|
import { getAccessDetails } from '@utils/accessDetailsAndPricing'
|
||||||
|
import { useIsMounted } from '@hooks/useIsMounted'
|
||||||
|
|
||||||
interface AssetProviderValue {
|
interface AssetProviderValue {
|
||||||
isInPurgatory: boolean
|
isInPurgatory: boolean
|
||||||
purgatoryData: Purgatory
|
purgatoryData: Purgatory
|
||||||
assetExtended: AssetExtended
|
asset: AssetExtended
|
||||||
title: string
|
title: string
|
||||||
owner: string
|
owner: string
|
||||||
accessDetails: AccessDetails
|
|
||||||
error?: string
|
error?: string
|
||||||
refreshInterval: number
|
refreshInterval: number
|
||||||
isAssetNetwork: boolean
|
isAssetNetwork: boolean
|
||||||
oceanConfig: Config
|
oceanConfig: Config
|
||||||
loading: boolean
|
loading: boolean
|
||||||
refreshAsset: (token?: CancelToken) => Promise<void>
|
fetchAsset: (token?: CancelToken) => Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
const AssetContext = createContext({} as AssetProviderValue)
|
const AssetContext = createContext({} as AssetProviderValue)
|
||||||
@ -45,12 +45,11 @@ function AssetProvider({
|
|||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { appConfig } = useSiteMetadata()
|
const { appConfig } = useSiteMetadata()
|
||||||
|
|
||||||
const { networkId, accountId } = useWeb3()
|
const { chainId, accountId } = useWeb3()
|
||||||
const [isInPurgatory, setIsInPurgatory] = useState(false)
|
const [isInPurgatory, setIsInPurgatory] = useState(false)
|
||||||
const [purgatoryData, setPurgatoryData] = useState<Purgatory>()
|
const [purgatoryData, setPurgatoryData] = useState<Purgatory>()
|
||||||
const [assetExtended, setAssetExtended] = useState<Asset>()
|
const [asset, setAsset] = useState<AssetExtended>()
|
||||||
const [title, setTitle] = useState<string>()
|
const [title, setTitle] = useState<string>()
|
||||||
const [accessDetails, setConsumeDetails] = useState<AccessDetails>()
|
|
||||||
const [owner, setOwner] = useState<string>()
|
const [owner, setOwner] = useState<string>()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
@ -58,126 +57,121 @@ function AssetProvider({
|
|||||||
const [oceanConfig, setOceanConfig] = useState<Config>()
|
const [oceanConfig, setOceanConfig] = useState<Config>()
|
||||||
|
|
||||||
const newCancelToken = useCancelToken()
|
const newCancelToken = useCancelToken()
|
||||||
|
const isMounted = useIsMounted()
|
||||||
|
|
||||||
|
// -----------------------------------
|
||||||
|
// Helper: Get and set asset based on passed DID
|
||||||
|
// -----------------------------------
|
||||||
const fetchAsset = useCallback(
|
const fetchAsset = useCallback(
|
||||||
async (token?: CancelToken) => {
|
async (token?: CancelToken) => {
|
||||||
LoggerInstance.log('[asset] Init asset, get assetExtended')
|
if (!did) return
|
||||||
setLoading(true)
|
|
||||||
const assetExtended = await retrieveAsset(did, token)
|
|
||||||
|
|
||||||
if (!assetExtended) {
|
LoggerInstance.log('[asset] Fetching asset...')
|
||||||
|
setLoading(true)
|
||||||
|
const asset = await retrieveAsset(did, token)
|
||||||
|
|
||||||
|
if (!asset) {
|
||||||
setError(
|
setError(
|
||||||
`[asset] The assetExtended for ${did} was not found in MetadataCache. If you just published a new data set, wait some seconds and refresh this page.`
|
`[asset] The asset for ${did} was not found in MetadataCache. If you just published a new data set, wait some seconds and refresh this page.`
|
||||||
)
|
)
|
||||||
|
LoggerInstance.error(`[asset] Failed getting asset for ${did}`, asset)
|
||||||
} else {
|
} else {
|
||||||
setError(undefined)
|
setError(undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setAsset((prevState) => ({
|
||||||
|
...prevState,
|
||||||
|
...asset
|
||||||
|
}))
|
||||||
|
setTitle(asset.metadata.name)
|
||||||
|
setOwner(asset.nft.owner)
|
||||||
|
setIsInPurgatory((asset.purgatory?.state as unknown as string) === 'true')
|
||||||
|
setPurgatoryData(asset.purgatory)
|
||||||
|
LoggerInstance.log('[asset] Got asset', asset)
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
return assetExtended
|
|
||||||
},
|
},
|
||||||
[did]
|
[did]
|
||||||
)
|
)
|
||||||
|
|
||||||
const refreshAsset = async (token?: CancelToken) => {
|
// -----------------------------------
|
||||||
setLoading(true)
|
// Helper: Get and set asset access details
|
||||||
const assetExtended = await fetchAsset(token)
|
// -----------------------------------
|
||||||
LoggerInstance.debug('[asset] Got assetExtended', assetExtended)
|
const fetchAccessDetails = useCallback(async (): Promise<void> => {
|
||||||
setAssetExtended(assetExtended)
|
if (!asset?.chainId || !asset?.services) return
|
||||||
setLoading(false)
|
|
||||||
}
|
const accessDetails = await getAccessDetails(
|
||||||
|
asset.chainId,
|
||||||
|
asset.services[0].datatokenAddress,
|
||||||
|
asset.services[0].timeout,
|
||||||
|
accountId
|
||||||
|
)
|
||||||
|
setAsset((prevState) => ({
|
||||||
|
...prevState,
|
||||||
|
accessDetails
|
||||||
|
}))
|
||||||
|
LoggerInstance.log(`[asset] Got access details for ${did}`, accessDetails)
|
||||||
|
}, [asset?.chainId, asset?.services, accountId, did])
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// Get and set assetExtended based on passed DID
|
// 1. Get and set asset based on passed DID
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!did || !appConfig.metadataCacheUri) return
|
if (!isMounted || !appConfig?.metadataCacheUri) return
|
||||||
|
|
||||||
let isMounted = true
|
fetchAsset(newCancelToken())
|
||||||
|
}, [appConfig?.metadataCacheUri, fetchAsset, newCancelToken, isMounted])
|
||||||
async function init() {
|
|
||||||
const assetExtended = await fetchAsset(newCancelToken())
|
|
||||||
if (!isMounted || !assetExtended) return
|
|
||||||
LoggerInstance.debug('[asset] Got assetExtended', assetExtended)
|
|
||||||
setAssetExtended(assetExtended)
|
|
||||||
setTitle(assetExtended.metadata.name)
|
|
||||||
setOwner(assetExtended.nft.owner)
|
|
||||||
setIsInPurgatory(
|
|
||||||
(assetExtended.purgatory?.state as unknown as string) === 'true'
|
|
||||||
)
|
|
||||||
setPurgatoryData(assetExtended.purgatory)
|
|
||||||
}
|
|
||||||
init()
|
|
||||||
return () => {
|
|
||||||
isMounted = false
|
|
||||||
}
|
|
||||||
}, [did, appConfig.metadataCacheUri, fetchAsset, newCancelToken])
|
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// Attach price to asset
|
// 2. Attach access details to asset
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
const initPrice = useCallback(
|
|
||||||
async (assetExtended: AssetExtended, accountId: string): Promise<void> => {
|
|
||||||
if (!assetExtended) return
|
|
||||||
const accessDetails = await getAccessDetails(
|
|
||||||
assetExtended.chainId,
|
|
||||||
assetExtended.services[0].datatokenAddress,
|
|
||||||
assetExtended.services[0].timeout,
|
|
||||||
accountId
|
|
||||||
)
|
|
||||||
assetExtended.accessDetails = accessDetails
|
|
||||||
setConsumeDetails({ ...accessDetails })
|
|
||||||
setAssetExtended(assetExtended)
|
|
||||||
},
|
|
||||||
[]
|
|
||||||
)
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!assetExtended) return
|
if (!isMounted) return
|
||||||
initPrice(assetExtended, accountId)
|
|
||||||
}, [accountId, assetExtended, initPrice])
|
fetchAccessDetails()
|
||||||
|
}, [accountId, fetchAccessDetails, isMounted])
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// Check user network against asset network
|
// Check user network against asset network
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!networkId || !assetExtended) return
|
if (!chainId || !asset?.chainId) return
|
||||||
|
|
||||||
const isAssetNetwork = networkId === assetExtended?.chainId
|
const isAssetNetwork = chainId === asset?.chainId
|
||||||
setIsAssetNetwork(isAssetNetwork)
|
setIsAssetNetwork(isAssetNetwork)
|
||||||
}, [networkId, assetExtended])
|
}, [chainId, asset?.chainId])
|
||||||
|
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
// Load ocean config based on asset network
|
// Load ocean config based on asset network
|
||||||
// -----------------------------------
|
// -----------------------------------
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!assetExtended?.chainId) return
|
if (!asset?.chainId) return
|
||||||
|
|
||||||
const oceanConfig = {
|
const oceanConfig = {
|
||||||
...getOceanConfig(assetExtended?.chainId),
|
...getOceanConfig(asset?.chainId),
|
||||||
|
|
||||||
// add local dev values
|
// add local dev values
|
||||||
...(assetExtended?.chainId === 8996 && {
|
...(asset?.chainId === 8996 && {
|
||||||
...getDevelopmentConfig()
|
...getDevelopmentConfig()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
setOceanConfig(oceanConfig)
|
setOceanConfig(oceanConfig)
|
||||||
}, [assetExtended])
|
}, [asset?.chainId])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AssetContext.Provider
|
<AssetContext.Provider
|
||||||
value={
|
value={
|
||||||
{
|
{
|
||||||
assetExtended,
|
asset,
|
||||||
did,
|
did,
|
||||||
title,
|
title,
|
||||||
owner,
|
owner,
|
||||||
accessDetails,
|
|
||||||
error,
|
error,
|
||||||
isInPurgatory,
|
isInPurgatory,
|
||||||
purgatoryData,
|
purgatoryData,
|
||||||
refreshInterval,
|
refreshInterval,
|
||||||
loading,
|
loading,
|
||||||
refreshAsset,
|
fetchAsset,
|
||||||
isAssetNetwork,
|
isAssetNetwork,
|
||||||
oceanConfig
|
oceanConfig
|
||||||
} as AssetProviderValue
|
} as AssetProviderValue
|
||||||
|
@ -209,9 +209,9 @@ function getAccessDetailsFromTokenPrice(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* returns various consume details for the desired datatoken
|
* returns various consume details for the desired datatoken
|
||||||
* @param chain chain on witch the dt is preset
|
* @param chain chain on which the datatoken is preset
|
||||||
* @param datatokenAddress address of the datatoken
|
* @param datatokenAddress address of the datatoken
|
||||||
* @param timeout timeout of the service , only needed if you want order details like owned and validOrderId
|
* @param timeout timeout of the service, only needed if you want order details like owned and validOrderId
|
||||||
* @param account account that wants to consume, only needed if you want order details like owned and validOrderId
|
* @param account account that wants to consume, only needed if you want order details like owned and validOrderId
|
||||||
* @returns AccessDetails
|
* @returns AccessDetails
|
||||||
*/
|
*/
|
||||||
@ -221,20 +221,21 @@ export async function getAccessDetails(
|
|||||||
timeout?: number,
|
timeout?: number,
|
||||||
account = ''
|
account = ''
|
||||||
): Promise<AccessDetails> {
|
): Promise<AccessDetails> {
|
||||||
let accessDetails = {} as AccessDetails
|
|
||||||
const queryContext = getQueryContext(Number(chain))
|
const queryContext = getQueryContext(Number(chain))
|
||||||
const tokenQueryResult: OperationResult<TokenPriceQuery, any> =
|
const tokenQueryResult: OperationResult<
|
||||||
await fetchData(
|
TokenPriceQuery,
|
||||||
TokenPriceQuery,
|
{ datatokenId: string; account: string }
|
||||||
{
|
> = await fetchData(
|
||||||
datatokenId: datatokenAddress.toLowerCase(),
|
TokenPriceQuery,
|
||||||
account: account.toLowerCase()
|
{
|
||||||
},
|
datatokenId: datatokenAddress.toLowerCase(),
|
||||||
queryContext
|
account: account.toLowerCase()
|
||||||
)
|
},
|
||||||
|
queryContext
|
||||||
|
)
|
||||||
|
|
||||||
const tokenPrice: TokenPrice = tokenQueryResult.data.token
|
const tokenPrice: TokenPrice = tokenQueryResult.data.token
|
||||||
accessDetails = getAccessDetailsFromTokenPrice(tokenPrice, timeout)
|
const accessDetails = getAccessDetailsFromTokenPrice(tokenPrice, timeout)
|
||||||
return accessDetails
|
return accessDetails
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +244,7 @@ export async function getAccessDetailsForAssets(
|
|||||||
account = ''
|
account = ''
|
||||||
): Promise<AssetExtended[]> {
|
): Promise<AssetExtended[]> {
|
||||||
const assetsExtended: AssetExtended[] = assets
|
const assetsExtended: AssetExtended[] = assets
|
||||||
const chainAssetLists: any = {}
|
const chainAssetLists: { [key: number]: string[] } = {}
|
||||||
|
|
||||||
for (const asset of assets) {
|
for (const asset of assets) {
|
||||||
// harcoded until we have chainId on assets
|
// harcoded until we have chainId on assets
|
||||||
@ -261,15 +262,17 @@ export async function getAccessDetailsForAssets(
|
|||||||
|
|
||||||
for (const chainKey in chainAssetLists) {
|
for (const chainKey in chainAssetLists) {
|
||||||
const queryContext = getQueryContext(Number(chainKey))
|
const queryContext = getQueryContext(Number(chainKey))
|
||||||
const tokenQueryResult: OperationResult<TokensPriceQuery, any> =
|
const tokenQueryResult: OperationResult<
|
||||||
await fetchData(
|
TokensPriceQuery,
|
||||||
TokensPriceQuery,
|
{ datatokenId: string; account: string }
|
||||||
{
|
> = await fetchData(
|
||||||
datatokenIds: chainAssetLists[chainKey],
|
TokensPriceQuery,
|
||||||
account: account.toLowerCase()
|
{
|
||||||
},
|
datatokenIds: chainAssetLists[chainKey],
|
||||||
queryContext
|
account: account.toLowerCase()
|
||||||
)
|
},
|
||||||
|
queryContext
|
||||||
|
)
|
||||||
tokenQueryResult.data?.tokens.forEach((token) => {
|
tokenQueryResult.data?.tokens.forEach((token) => {
|
||||||
const accessDetails = getAccessDetailsFromTokenPrice(token)
|
const accessDetails = getAccessDetailsFromTokenPrice(token)
|
||||||
const currentAsset = assetsExtended.find(
|
const currentAsset = assetsExtended.find(
|
||||||
|
@ -80,7 +80,7 @@ export default function AssetList({
|
|||||||
{assetsWithPrices.length > 0 ? (
|
{assetsWithPrices.length > 0 ? (
|
||||||
assetsWithPrices.map((assetWithPrice) => (
|
assetsWithPrices.map((assetWithPrice) => (
|
||||||
<AssetTeaser
|
<AssetTeaser
|
||||||
assetExtended={assetWithPrice}
|
asset={assetWithPrice}
|
||||||
key={assetWithPrice.id}
|
key={assetWithPrice.id}
|
||||||
noPublisher={noPublisher}
|
noPublisher={noPublisher}
|
||||||
/>
|
/>
|
||||||
|
@ -11,22 +11,22 @@ import { getServiceByName } from '@utils/ddo'
|
|||||||
import { AssetExtended } from 'src/@types/AssetExtended'
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
||||||
|
|
||||||
declare type AssetTeaserProps = {
|
declare type AssetTeaserProps = {
|
||||||
assetExtended: AssetExtended
|
asset: AssetExtended
|
||||||
noPublisher?: boolean
|
noPublisher?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function AssetTeaser({
|
export default function AssetTeaser({
|
||||||
assetExtended,
|
asset,
|
||||||
noPublisher
|
noPublisher
|
||||||
}: AssetTeaserProps): ReactElement {
|
}: AssetTeaserProps): ReactElement {
|
||||||
const { name, type, description } = assetExtended.metadata
|
const { name, type, description } = asset.metadata
|
||||||
const { datatokens } = assetExtended
|
const { datatokens } = asset
|
||||||
const isCompute = Boolean(getServiceByName(assetExtended, 'compute'))
|
const isCompute = Boolean(getServiceByName(asset, 'compute'))
|
||||||
const accessType = isCompute ? 'compute' : 'access'
|
const accessType = isCompute ? 'compute' : 'access'
|
||||||
const { owner } = assetExtended.nft
|
const { owner } = asset.nft
|
||||||
return (
|
return (
|
||||||
<article className={`${styles.teaser} ${styles[type]}`}>
|
<article className={`${styles.teaser} ${styles[type]}`}>
|
||||||
<Link href={`/asset/${assetExtended.id}`}>
|
<Link href={`/asset/${asset.id}`}>
|
||||||
<a className={styles.link}>
|
<a className={styles.link}>
|
||||||
<header className={styles.header}>
|
<header className={styles.header}>
|
||||||
<div className={styles.symbol}>{datatokens[0]?.symbol}</div>
|
<div className={styles.symbol}>{datatokens[0]?.symbol}</div>
|
||||||
@ -51,11 +51,8 @@ export default function AssetTeaser({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<footer className={styles.foot}>
|
<footer className={styles.foot}>
|
||||||
<Price accessDetails={assetExtended.accessDetails} small />
|
<Price accessDetails={asset.accessDetails} small />
|
||||||
<NetworkName
|
<NetworkName networkId={asset.chainId} className={styles.network} />
|
||||||
networkId={assetExtended.chainId}
|
|
||||||
className={styles.network}
|
|
||||||
/>
|
|
||||||
</footer>
|
</footer>
|
||||||
</a>
|
</a>
|
||||||
</Link>
|
</Link>
|
||||||
|
@ -18,12 +18,12 @@ export default function TokenApproval({
|
|||||||
tokenAddress: string
|
tokenAddress: string
|
||||||
tokenSymbol: string
|
tokenSymbol: string
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { accessDetails, isAssetNetwork } = useAsset()
|
const { asset, isAssetNetwork } = useAsset()
|
||||||
const [tokenApproved, setTokenApproved] = useState(false)
|
const [tokenApproved, setTokenApproved] = useState(false)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const { web3, accountId } = useWeb3()
|
const { web3, accountId } = useWeb3()
|
||||||
|
|
||||||
const spender = accessDetails?.addressOrId
|
const spender = asset?.accessDetails?.addressOrId
|
||||||
|
|
||||||
const checkTokenApproval = useCallback(async () => {
|
const checkTokenApproval = useCallback(async () => {
|
||||||
if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
|
if (!web3 || !tokenAddress || !spender || !isAssetNetwork || !amount) return
|
||||||
|
@ -11,15 +11,13 @@ import { useAsset } from '@context/Asset'
|
|||||||
|
|
||||||
export default function WalletNetworkSwitcher(): ReactElement {
|
export default function WalletNetworkSwitcher(): ReactElement {
|
||||||
const { networkId, web3Provider } = useWeb3()
|
const { networkId, web3Provider } = useWeb3()
|
||||||
const { assetExtended } = useAsset()
|
const { asset } = useAsset()
|
||||||
const { networksList } = useNetworkMetadata()
|
const { networksList } = useNetworkMetadata()
|
||||||
const ddoNetworkData = getNetworkDataById(networksList, assetExtended.chainId)
|
const ddoNetworkData = getNetworkDataById(networksList, asset.chainId)
|
||||||
const walletNetworkData = getNetworkDataById(networksList, networkId)
|
const walletNetworkData = getNetworkDataById(networksList, networkId)
|
||||||
|
|
||||||
const ddoNetworkName = (
|
const ddoNetworkName = (
|
||||||
<strong>
|
<strong>{getNetworkDisplayName(ddoNetworkData, asset.chainId)}</strong>
|
||||||
{getNetworkDisplayName(ddoNetworkData, assetExtended.chainId)}
|
|
||||||
</strong>
|
|
||||||
)
|
)
|
||||||
const walletNetworkName = (
|
const walletNetworkName = (
|
||||||
<strong>{getNetworkDisplayName(walletNetworkData, networkId)}</strong>
|
<strong>{getNetworkDisplayName(walletNetworkData, networkId)}</strong>
|
||||||
@ -27,7 +25,7 @@ export default function WalletNetworkSwitcher(): ReactElement {
|
|||||||
|
|
||||||
async function switchWalletNetwork() {
|
async function switchWalletNetwork() {
|
||||||
const networkNode = await networksList.find(
|
const networkNode = await networksList.find(
|
||||||
(data) => data.chainId === assetExtended.chainId
|
(data) => data.chainId === asset.chainId
|
||||||
)
|
)
|
||||||
addCustomNetwork(web3Provider, networkNode)
|
addCustomNetwork(web3Provider, networkNode)
|
||||||
}
|
}
|
||||||
|
@ -62,8 +62,8 @@ export default function FormStartCompute({
|
|||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { isValid, values }: FormikContextType<{ algorithm: string }> =
|
const { isValid, values }: FormikContextType<{ algorithm: string }> =
|
||||||
useFormikContext()
|
useFormikContext()
|
||||||
const { accessDetails, assetExtended, isAssetNetwork } = useAsset()
|
const { asset, isAssetNetwork } = useAsset()
|
||||||
const [totalPrice, setTotalPrice] = useState(accessDetails?.price)
|
const [totalPrice, setTotalPrice] = useState(asset?.accessDetails?.price)
|
||||||
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>(false)
|
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>(false)
|
||||||
const { accountId, balance } = useWeb3()
|
const { accountId, balance } = useWeb3()
|
||||||
const [algorithmConsumableStatus, setAlgorithmConsumableStatus] =
|
const [algorithmConsumableStatus, setAlgorithmConsumableStatus] =
|
||||||
@ -97,10 +97,10 @@ export default function FormStartCompute({
|
|||||||
// Set price for calculation output
|
// Set price for calculation output
|
||||||
//
|
//
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!accessDetails || !algorithmConsumeDetails) return
|
if (!asset?.accessDetails || !algorithmConsumeDetails) return
|
||||||
|
|
||||||
const priceDataset =
|
const priceDataset =
|
||||||
hasPreviousOrder || hasDatatoken ? 0 : Number(accessDetails.price)
|
hasPreviousOrder || hasDatatoken ? 0 : Number(asset.accessDetails.price)
|
||||||
const priceAlgo =
|
const priceAlgo =
|
||||||
hasPreviousOrderSelectedComputeAsset || hasDatatokenSelectedComputeAsset
|
hasPreviousOrderSelectedComputeAsset || hasDatatokenSelectedComputeAsset
|
||||||
? 0
|
? 0
|
||||||
@ -108,7 +108,7 @@ export default function FormStartCompute({
|
|||||||
|
|
||||||
setTotalPrice(priceDataset + priceAlgo)
|
setTotalPrice(priceDataset + priceAlgo)
|
||||||
}, [
|
}, [
|
||||||
accessDetails,
|
asset?.accessDetails,
|
||||||
algorithmConsumeDetails,
|
algorithmConsumeDetails,
|
||||||
hasPreviousOrder,
|
hasPreviousOrder,
|
||||||
hasDatatoken,
|
hasDatatoken,
|
||||||
@ -159,7 +159,7 @@ export default function FormStartCompute({
|
|||||||
}
|
}
|
||||||
hasPreviousOrder={hasPreviousOrder}
|
hasPreviousOrder={hasPreviousOrder}
|
||||||
hasDatatoken={hasDatatoken}
|
hasDatatoken={hasDatatoken}
|
||||||
dtSymbol={assetExtended?.datatokens[0]?.symbol}
|
dtSymbol={asset?.datatokens[0]?.symbol}
|
||||||
dtBalance={dtBalance}
|
dtBalance={dtBalance}
|
||||||
datasetLowPoolLiquidity={datasetLowPoolLiquidity}
|
datasetLowPoolLiquidity={datasetLowPoolLiquidity}
|
||||||
assetTimeout={assetTimeout}
|
assetTimeout={assetTimeout}
|
||||||
@ -177,7 +177,7 @@ export default function FormStartCompute({
|
|||||||
stepText={stepText}
|
stepText={stepText}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
type="submit"
|
type="submit"
|
||||||
priceType={accessDetails?.type}
|
priceType={asset?.accessDetails?.type}
|
||||||
algorithmPriceType={algorithmConsumeDetails?.type}
|
algorithmPriceType={algorithmConsumeDetails?.type}
|
||||||
isBalanceSufficient={isBalanceSufficient}
|
isBalanceSufficient={isBalanceSufficient}
|
||||||
isConsumable={isConsumable}
|
isConsumable={isConsumable}
|
||||||
|
@ -63,7 +63,7 @@ export default function PriceOutput({
|
|||||||
algorithmConsumeDetails,
|
algorithmConsumeDetails,
|
||||||
selectedComputeAssetTimeout
|
selectedComputeAssetTimeout
|
||||||
}: PriceOutputProps): ReactElement {
|
}: PriceOutputProps): ReactElement {
|
||||||
const { accessDetails } = useAsset()
|
const { asset } = useAsset()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.priceComponent}>
|
<div className={styles.priceComponent}>
|
||||||
@ -74,7 +74,7 @@ export default function PriceOutput({
|
|||||||
<Row
|
<Row
|
||||||
hasPreviousOrder={hasPreviousOrder}
|
hasPreviousOrder={hasPreviousOrder}
|
||||||
hasDatatoken={hasDatatoken}
|
hasDatatoken={hasDatatoken}
|
||||||
price={accessDetails?.price}
|
price={asset?.accessDetails?.price}
|
||||||
timeout={assetTimeout}
|
timeout={assetTimeout}
|
||||||
symbol={symbol}
|
symbol={symbol}
|
||||||
/>
|
/>
|
||||||
|
@ -60,14 +60,8 @@ const initialPoolInfoCreator: Partial<PoolInfoUser> = initialPoolInfoUser
|
|||||||
|
|
||||||
export default function Pool(): ReactElement {
|
export default function Pool(): ReactElement {
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const {
|
const { isInPurgatory, asset, owner, refreshInterval, isAssetNetwork } =
|
||||||
isInPurgatory,
|
useAsset()
|
||||||
assetExtended,
|
|
||||||
owner,
|
|
||||||
accessDetails,
|
|
||||||
refreshInterval,
|
|
||||||
isAssetNetwork
|
|
||||||
} = useAsset()
|
|
||||||
|
|
||||||
const [poolData, setPoolData] = useState<PoolDataPoolData>()
|
const [poolData, setPoolData] = useState<PoolDataPoolData>()
|
||||||
const [poolInfo, setPoolInfo] = useState<PoolInfo>(
|
const [poolInfo, setPoolInfo] = useState<PoolInfo>(
|
||||||
@ -88,11 +82,11 @@ export default function Pool(): ReactElement {
|
|||||||
const [fetchInterval, setFetchInterval] = useState<NodeJS.Timeout>()
|
const [fetchInterval, setFetchInterval] = useState<NodeJS.Timeout>()
|
||||||
|
|
||||||
const fetchAllData = useCallback(async () => {
|
const fetchAllData = useCallback(async () => {
|
||||||
if (!assetExtended?.chainId || !accessDetails?.addressOrId || !owner) return
|
if (!asset?.chainId || !asset?.accessDetails?.addressOrId || !owner) return
|
||||||
|
|
||||||
const response = await getPoolData(
|
const response = await getPoolData(
|
||||||
assetExtended.chainId,
|
asset.chainId,
|
||||||
accessDetails.addressOrId,
|
asset.accessDetails.addressOrId,
|
||||||
owner,
|
owner,
|
||||||
accountId || ''
|
accountId || ''
|
||||||
)
|
)
|
||||||
@ -107,7 +101,7 @@ export default function Pool(): ReactElement {
|
|||||||
LoggerInstance.log('[pool] Fetched pool data:', response.poolData)
|
LoggerInstance.log('[pool] Fetched pool data:', response.poolData)
|
||||||
LoggerInstance.log('[pool] Fetched user data:', response.poolDataUser)
|
LoggerInstance.log('[pool] Fetched user data:', response.poolDataUser)
|
||||||
LoggerInstance.log('[pool] Fetched pool snapshots:', response.poolSnapshots)
|
LoggerInstance.log('[pool] Fetched pool snapshots:', response.poolSnapshots)
|
||||||
}, [assetExtended?.chainId, accessDetails?.addressOrId, owner, accountId])
|
}, [asset?.chainId, asset?.accessDetails?.addressOrId, owner, accountId])
|
||||||
|
|
||||||
// Helper: start interval fetching
|
// Helper: start interval fetching
|
||||||
const initFetchInterval = useCallback(() => {
|
const initFetchInterval = useCallback(() => {
|
||||||
@ -242,7 +236,7 @@ export default function Pool(): ReactElement {
|
|||||||
if (
|
if (
|
||||||
!poolData ||
|
!poolData ||
|
||||||
!poolInfo?.totalPoolTokens ||
|
!poolInfo?.totalPoolTokens ||
|
||||||
!assetExtended?.chainId ||
|
!asset?.chainId ||
|
||||||
!accountId
|
!accountId
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
@ -310,7 +304,7 @@ export default function Pool(): ReactElement {
|
|||||||
poolData,
|
poolData,
|
||||||
poolInfoUser?.poolShares,
|
poolInfoUser?.poolShares,
|
||||||
accountId,
|
accountId,
|
||||||
assetExtended?.chainId,
|
asset?.chainId,
|
||||||
owner,
|
owner,
|
||||||
poolInfo?.totalPoolTokens
|
poolInfo?.totalPoolTokens
|
||||||
])
|
])
|
||||||
@ -328,7 +322,7 @@ export default function Pool(): ReactElement {
|
|||||||
{showAdd ? (
|
{showAdd ? (
|
||||||
<Add
|
<Add
|
||||||
setShowAdd={setShowAdd}
|
setShowAdd={setShowAdd}
|
||||||
poolAddress={accessDetails?.addressOrId}
|
poolAddress={asset?.accessDetails?.addressOrId}
|
||||||
totalPoolTokens={poolInfo?.totalPoolTokens}
|
totalPoolTokens={poolInfo?.totalPoolTokens}
|
||||||
totalBalance={{
|
totalBalance={{
|
||||||
baseToken: new Decimal(poolData?.baseTokenLiquidity).toString(),
|
baseToken: new Decimal(poolData?.baseTokenLiquidity).toString(),
|
||||||
@ -343,7 +337,7 @@ export default function Pool(): ReactElement {
|
|||||||
) : showRemove ? (
|
) : showRemove ? (
|
||||||
<Remove
|
<Remove
|
||||||
setShowRemove={setShowRemove}
|
setShowRemove={setShowRemove}
|
||||||
poolAddress={accessDetails?.addressOrId}
|
poolAddress={asset?.accessDetails?.addressOrId}
|
||||||
poolTokens={poolInfoUser?.poolShares}
|
poolTokens={poolInfoUser?.poolShares}
|
||||||
totalPoolTokens={poolInfo?.totalPoolTokens}
|
totalPoolTokens={poolInfo?.totalPoolTokens}
|
||||||
tokenOutAddress={poolInfo?.baseTokenAddress}
|
tokenOutAddress={poolInfo?.baseTokenAddress}
|
||||||
@ -361,18 +355,17 @@ export default function Pool(): ReactElement {
|
|||||||
<Tooltip content={content.pool.tooltips.price} />
|
<Tooltip content={content.pool.tooltips.price} />
|
||||||
<div className={styles.dataTokenLinks}>
|
<div className={styles.dataTokenLinks}>
|
||||||
<ExplorerLink
|
<ExplorerLink
|
||||||
networkId={assetExtended?.chainId}
|
networkId={asset?.chainId}
|
||||||
path={`address/${accessDetails?.addressOrId}`}
|
path={`address/${asset?.accessDetails?.addressOrId}`}
|
||||||
>
|
>
|
||||||
Pool
|
Pool
|
||||||
</ExplorerLink>
|
</ExplorerLink>
|
||||||
<ExplorerLink
|
<ExplorerLink
|
||||||
networkId={assetExtended?.chainId}
|
networkId={asset?.chainId}
|
||||||
path={
|
path={
|
||||||
assetExtended?.chainId === 2021000 ||
|
asset?.chainId === 2021000 || asset?.chainId === 1287
|
||||||
assetExtended?.chainId === 1287
|
? `tokens/${asset.services[0].datatokenAddress}`
|
||||||
? `tokens/${assetExtended.services[0].datatokenAddress}`
|
: `token/${asset.services[0].datatokenAddress}`
|
||||||
: `token/${assetExtended.services[0].datatokenAddress}`
|
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
Datatoken
|
Datatoken
|
||||||
@ -478,8 +471,8 @@ export default function Pool(): ReactElement {
|
|||||||
<AssetActionHistoryTable title="Your Pool Transactions">
|
<AssetActionHistoryTable title="Your Pool Transactions">
|
||||||
<PoolTransactions
|
<PoolTransactions
|
||||||
accountId={accountId}
|
accountId={accountId}
|
||||||
poolAddress={accessDetails?.addressOrId}
|
poolAddress={asset?.accessDetails?.addressOrId}
|
||||||
poolChainId={[assetExtended?.chainId]}
|
poolChainId={[asset?.chainId]}
|
||||||
minimal
|
minimal
|
||||||
/>
|
/>
|
||||||
</AssetActionHistoryTable>
|
</AssetActionHistoryTable>
|
||||||
|
@ -17,12 +17,12 @@ import content from '../../../../../content/price.json'
|
|||||||
import { AssetExtended } from 'src/@types/AssetExtended'
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
||||||
|
|
||||||
export default function FormTrade({
|
export default function FormTrade({
|
||||||
assetExtended,
|
asset,
|
||||||
balance,
|
balance,
|
||||||
maxDt,
|
maxDt,
|
||||||
maxOcean
|
maxOcean
|
||||||
}: {
|
}: {
|
||||||
assetExtended: AssetExtended
|
asset: AssetExtended
|
||||||
balance: PoolBalance
|
balance: PoolBalance
|
||||||
maxDt: string
|
maxDt: string
|
||||||
maxOcean: string
|
maxOcean: string
|
||||||
@ -110,7 +110,7 @@ export default function FormTrade({
|
|||||||
<>
|
<>
|
||||||
{isWarningAccepted ? (
|
{isWarningAccepted ? (
|
||||||
<Swap
|
<Swap
|
||||||
assetExtended={assetExtended}
|
asset={asset}
|
||||||
balance={balance}
|
balance={balance}
|
||||||
maxDt={maxDt}
|
maxDt={maxDt}
|
||||||
maxOcean={maxOcean}
|
maxOcean={maxOcean}
|
||||||
|
@ -17,7 +17,7 @@ import { AssetExtended } from 'src/@types/AssetExtended'
|
|||||||
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
|
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
|
||||||
|
|
||||||
export default function Swap({
|
export default function Swap({
|
||||||
assetExtended,
|
asset,
|
||||||
maxDt,
|
maxDt,
|
||||||
maxOcean,
|
maxOcean,
|
||||||
balance,
|
balance,
|
||||||
@ -25,7 +25,7 @@ export default function Swap({
|
|||||||
setMaximumOcean,
|
setMaximumOcean,
|
||||||
setCoin
|
setCoin
|
||||||
}: {
|
}: {
|
||||||
assetExtended: AssetExtended
|
asset: AssetExtended
|
||||||
maxDt: string
|
maxDt: string
|
||||||
maxOcean: string
|
maxOcean: string
|
||||||
balance: PoolBalance
|
balance: PoolBalance
|
||||||
@ -36,12 +36,12 @@ export default function Swap({
|
|||||||
const { isAssetNetwork } = useAsset()
|
const { isAssetNetwork } = useAsset()
|
||||||
const [oceanItem, setOceanItem] = useState<TradeItem>({
|
const [oceanItem, setOceanItem] = useState<TradeItem>({
|
||||||
amount: '0',
|
amount: '0',
|
||||||
token: assetExtended.accessDetails.baseToken?.symbol,
|
token: asset.accessDetails.baseToken?.symbol,
|
||||||
maxAmount: '0'
|
maxAmount: '0'
|
||||||
})
|
})
|
||||||
const [dtItem, setDtItem] = useState<TradeItem>({
|
const [dtItem, setDtItem] = useState<TradeItem>({
|
||||||
amount: '0',
|
amount: '0',
|
||||||
token: assetExtended.accessDetails.datatoken.symbol,
|
token: asset.accessDetails.datatoken.symbol,
|
||||||
maxAmount: '0'
|
maxAmount: '0'
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ export default function Swap({
|
|||||||
const [tokenAmount, setTokenAmount] = useState<string>()
|
const [tokenAmount, setTokenAmount] = useState<string>()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!assetExtended || !balance || !values?.type) return
|
if (!asset || !balance || !values?.type) return
|
||||||
|
|
||||||
async function calculateMaximum() {
|
async function calculateMaximum() {
|
||||||
const amountDataToken =
|
const amountDataToken =
|
||||||
@ -114,7 +114,7 @@ export default function Swap({
|
|||||||
}
|
}
|
||||||
calculateMaximum()
|
calculateMaximum()
|
||||||
}, [
|
}, [
|
||||||
assetExtended,
|
asset,
|
||||||
maxOcean,
|
maxOcean,
|
||||||
maxDt,
|
maxDt,
|
||||||
balance,
|
balance,
|
||||||
@ -125,9 +125,7 @@ export default function Swap({
|
|||||||
|
|
||||||
const switchTokens = () => {
|
const switchTokens = () => {
|
||||||
setFieldValue('type', values.type === 'buy' ? 'sell' : 'buy')
|
setFieldValue('type', values.type === 'buy' ? 'sell' : 'buy')
|
||||||
setCoin(
|
setCoin(values.type === 'sell' ? 'OCEAN' : asset.datatokens[0].symbol)
|
||||||
values.type === 'sell' ? 'OCEAN' : assetExtended.datatokens[0].symbol
|
|
||||||
)
|
|
||||||
// don't reset form because we don't want to reset type
|
// don't reset form because we don't want to reset type
|
||||||
setFieldValue('datatoken', 0)
|
setFieldValue('datatoken', 0)
|
||||||
setFieldValue('ocean', 0)
|
setFieldValue('ocean', 0)
|
||||||
@ -223,7 +221,7 @@ export default function Swap({
|
|||||||
<Output
|
<Output
|
||||||
dtSymbol={dtItem.token}
|
dtSymbol={dtItem.token}
|
||||||
oceanSymbol={oceanItem.token}
|
oceanSymbol={oceanItem.token}
|
||||||
poolAddress={assetExtended.accessDetails?.addressOrId}
|
poolAddress={asset.accessDetails?.addressOrId}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<PriceImpact
|
<PriceImpact
|
||||||
|
@ -11,7 +11,7 @@ export default function Trade(): ReactElement {
|
|||||||
const { accountId, balance, web3 } = useWeb3()
|
const { accountId, balance, web3 } = useWeb3()
|
||||||
const { isAssetNetwork } = useAsset()
|
const { isAssetNetwork } = useAsset()
|
||||||
const [tokenBalance, setTokenBalance] = useState<PoolBalance>()
|
const [tokenBalance, setTokenBalance] = useState<PoolBalance>()
|
||||||
const { assetExtended } = useAsset()
|
const { asset } = useAsset()
|
||||||
const [maxDt, setMaxDt] = useState('0')
|
const [maxDt, setMaxDt] = useState('0')
|
||||||
const [maxOcean, setMaxOcean] = useState('0')
|
const [maxOcean, setMaxOcean] = useState('0')
|
||||||
|
|
||||||
@ -23,14 +23,14 @@ export default function Trade(): ReactElement {
|
|||||||
!isAssetNetwork ||
|
!isAssetNetwork ||
|
||||||
!balance?.ocean ||
|
!balance?.ocean ||
|
||||||
!accountId ||
|
!accountId ||
|
||||||
!assetExtended?.services[0].datatokenAddress
|
!asset?.services[0].datatokenAddress
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
async function getTokenBalance() {
|
async function getTokenBalance() {
|
||||||
const datatokenInstance = new Datatoken(web3)
|
const datatokenInstance = new Datatoken(web3)
|
||||||
const dtBalance = await datatokenInstance.balance(
|
const dtBalance = await datatokenInstance.balance(
|
||||||
assetExtended.services[0].datatokenAddress,
|
asset.services[0].datatokenAddress,
|
||||||
accountId
|
accountId
|
||||||
)
|
)
|
||||||
setTokenBalance({
|
setTokenBalance({
|
||||||
@ -39,14 +39,14 @@ export default function Trade(): ReactElement {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
getTokenBalance()
|
getTokenBalance()
|
||||||
}, [web3, balance.ocean, accountId, assetExtended, isAssetNetwork])
|
}, [web3, balance.ocean, accountId, asset, isAssetNetwork])
|
||||||
|
|
||||||
// Get maximum amount for either OCEAN or datatoken
|
// Get maximum amount for either OCEAN or datatoken
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
!isAssetNetwork ||
|
!isAssetNetwork ||
|
||||||
!assetExtended.accessDetails ||
|
!asset.accessDetails ||
|
||||||
assetExtended.accessDetails.price === 0
|
asset.accessDetails.price === 0
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -69,11 +69,11 @@ export default function Trade(): ReactElement {
|
|||||||
// )
|
// )
|
||||||
}
|
}
|
||||||
getMaximum()
|
getMaximum()
|
||||||
}, [isAssetNetwork, balance.ocean, assetExtended])
|
}, [isAssetNetwork, balance.ocean, asset])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormTrade
|
<FormTrade
|
||||||
assetExtended={assetExtended}
|
asset={asset}
|
||||||
balance={tokenBalance}
|
balance={tokenBalance}
|
||||||
maxDt={maxDt}
|
maxDt={maxDt}
|
||||||
maxOcean={maxOcean}
|
maxOcean={maxOcean}
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
import React, { ReactElement, useState, useEffect } from 'react'
|
import React, { ReactElement, useState, useEffect } from 'react'
|
||||||
import Compute from './Compute'
|
import Compute from './Compute'
|
||||||
import Consume from './Consume'
|
import Consume from './Consume'
|
||||||
import {
|
import { FileMetadata, LoggerInstance, Datatoken } from '@oceanprotocol/lib'
|
||||||
Asset,
|
|
||||||
FileMetadata,
|
|
||||||
LoggerInstance,
|
|
||||||
Datatoken
|
|
||||||
} from '@oceanprotocol/lib'
|
|
||||||
import Tabs, { TabsItem } from '@shared/atoms/Tabs'
|
import Tabs, { TabsItem } from '@shared/atoms/Tabs'
|
||||||
import { compareAsBN } from '@utils/numbers'
|
import { compareAsBN } from '@utils/numbers'
|
||||||
import Pool from './Pool'
|
import Pool from './Pool'
|
||||||
@ -21,13 +16,12 @@ import { useIsMounted } from '@hooks/useIsMounted'
|
|||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import { useFormikContext } from 'formik'
|
import { useFormikContext } from 'formik'
|
||||||
import { FormPublishData } from 'src/components/Publish/_types'
|
import { FormPublishData } from 'src/components/Publish/_types'
|
||||||
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
||||||
|
|
||||||
export default function AssetActions({
|
export default function AssetActions({
|
||||||
ddo,
|
asset
|
||||||
accessDetails
|
|
||||||
}: {
|
}: {
|
||||||
ddo: Asset
|
asset: AssetExtended
|
||||||
accessDetails: AccessDetails
|
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { accountId, balance, web3 } = useWeb3()
|
const { accountId, balance, web3 } = useWeb3()
|
||||||
const { isAssetNetwork } = useAsset()
|
const { isAssetNetwork } = useAsset()
|
||||||
@ -43,7 +37,7 @@ export default function AssetActions({
|
|||||||
const [fileMetadata, setFileMetadata] = useState<FileMetadata>()
|
const [fileMetadata, setFileMetadata] = useState<FileMetadata>()
|
||||||
const [fileIsLoading, setFileIsLoading] = useState<boolean>(false)
|
const [fileIsLoading, setFileIsLoading] = useState<boolean>(false)
|
||||||
const isCompute = Boolean(
|
const isCompute = Boolean(
|
||||||
ddo?.services.filter((service) => service.type === 'compute')[0]
|
asset?.services.filter((service) => service.type === 'compute')[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
const [isConsumable, setIsConsumable] = useState<boolean>(true)
|
const [isConsumable, setIsConsumable] = useState<boolean>(true)
|
||||||
@ -68,14 +62,14 @@ export default function AssetActions({
|
|||||||
// }, [accountId, isAssetNetwork, ddo, ocean])
|
// }, [accountId, isAssetNetwork, ddo, ocean])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const oceanConfig = getOceanConfig(ddo?.chainId)
|
const oceanConfig = getOceanConfig(asset?.chainId)
|
||||||
if (!oceanConfig) return
|
if (!oceanConfig) return
|
||||||
|
|
||||||
async function initFileInfo() {
|
async function initFileInfo() {
|
||||||
setFileIsLoading(true)
|
setFileIsLoading(true)
|
||||||
const fileUrl =
|
const fileUrl =
|
||||||
formikState?.values?.services?.[0].files?.[0].url ||
|
formikState?.values?.services?.[0].files?.[0].url ||
|
||||||
(ddo.metadata?.links ? ddo.metadata?.links[0] : ' ')
|
(asset.metadata?.links ? asset.metadata?.links[0] : ' ')
|
||||||
const providerUrl =
|
const providerUrl =
|
||||||
formikState?.values?.services[0].providerUrl.url ||
|
formikState?.values?.services[0].providerUrl.url ||
|
||||||
oceanConfig.providerUri
|
oceanConfig.providerUri
|
||||||
@ -89,7 +83,7 @@ export default function AssetActions({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
initFileInfo()
|
initFileInfo()
|
||||||
}, [ddo, isMounted, newCancelToken, formikState?.values?.services])
|
}, [asset, isMounted, newCancelToken, formikState?.values?.services])
|
||||||
|
|
||||||
// Get and set user DT balance
|
// Get and set user DT balance
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -99,7 +93,7 @@ export default function AssetActions({
|
|||||||
try {
|
try {
|
||||||
const datatokenInstance = new Datatoken(web3)
|
const datatokenInstance = new Datatoken(web3)
|
||||||
const dtBalance = await datatokenInstance.balance(
|
const dtBalance = await datatokenInstance.balance(
|
||||||
ddo.services[0].datatokenAddress,
|
asset.services[0].datatokenAddress,
|
||||||
accountId
|
accountId
|
||||||
)
|
)
|
||||||
setDtBalance(dtBalance)
|
setDtBalance(dtBalance)
|
||||||
@ -108,28 +102,33 @@ export default function AssetActions({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [web3, accountId, ddo, isAssetNetwork])
|
}, [web3, accountId, asset, isAssetNetwork])
|
||||||
|
|
||||||
// Check user balance against price
|
// Check user balance against price
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (accessDetails?.type === 'free') setIsBalanceSufficient(true)
|
if (asset?.accessDetails?.type === 'free') setIsBalanceSufficient(true)
|
||||||
if (!accessDetails?.price || !accountId || !balance?.ocean || !dtBalance)
|
if (
|
||||||
|
!asset?.accessDetails?.price ||
|
||||||
|
!accountId ||
|
||||||
|
!balance?.ocean ||
|
||||||
|
!dtBalance
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
setIsBalanceSufficient(
|
setIsBalanceSufficient(
|
||||||
compareAsBN(balance.ocean, `${accessDetails.price}`) ||
|
compareAsBN(balance.ocean, `${asset?.accessDetails.price}`) ||
|
||||||
Number(dtBalance) >= 1
|
Number(dtBalance) >= 1
|
||||||
)
|
)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
setIsBalanceSufficient(false)
|
setIsBalanceSufficient(false)
|
||||||
}
|
}
|
||||||
}, [balance, accountId, accessDetails, dtBalance])
|
}, [balance, accountId, asset?.accessDetails, dtBalance])
|
||||||
|
|
||||||
const UseContent = isCompute ? (
|
const UseContent = isCompute ? (
|
||||||
<Compute
|
<Compute
|
||||||
ddo={ddo}
|
ddo={asset}
|
||||||
accessDetails={accessDetails}
|
accessDetails={asset?.accessDetails}
|
||||||
dtBalance={dtBalance}
|
dtBalance={dtBalance}
|
||||||
file={fileMetadata}
|
file={fileMetadata}
|
||||||
fileIsLoading={fileIsLoading}
|
fileIsLoading={fileIsLoading}
|
||||||
@ -138,8 +137,8 @@ export default function AssetActions({
|
|||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Consume
|
<Consume
|
||||||
ddo={ddo}
|
ddo={asset}
|
||||||
accessDetails={accessDetails}
|
accessDetails={asset?.accessDetails}
|
||||||
dtBalance={dtBalance}
|
dtBalance={dtBalance}
|
||||||
isBalanceSufficient={isBalanceSufficient}
|
isBalanceSufficient={isBalanceSufficient}
|
||||||
file={fileMetadata}
|
file={fileMetadata}
|
||||||
@ -156,24 +155,27 @@ export default function AssetActions({
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
accessDetails?.type === 'dynamic' &&
|
asset?.accessDetails?.type === 'dynamic' &&
|
||||||
tabs.push(
|
tabs.push(
|
||||||
{
|
{
|
||||||
title: 'Pool',
|
title: 'Pool',
|
||||||
content: <Pool />,
|
content: <Pool />,
|
||||||
disabled: !accessDetails.datatoken
|
disabled: !asset?.accessDetails.datatoken
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Trade',
|
title: 'Trade',
|
||||||
content: <Trade />,
|
content: <Trade />,
|
||||||
disabled: !accessDetails.datatoken
|
disabled: !asset?.accessDetails.datatoken
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Tabs items={tabs} className={styles.actions} />
|
<Tabs items={tabs} className={styles.actions} />
|
||||||
<Web3Feedback networkId={ddo?.chainId} isAssetNetwork={isAssetNetwork} />
|
<Web3Feedback
|
||||||
|
networkId={asset?.chainId}
|
||||||
|
isAssetNetwork={isAssetNetwork}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ const getReceipts = gql`
|
|||||||
`
|
`
|
||||||
|
|
||||||
export default function EditHistory(): ReactElement {
|
export default function EditHistory(): ReactElement {
|
||||||
const { assetExtended } = useAsset()
|
const { asset } = useAsset()
|
||||||
|
|
||||||
function getUpdateType(type: string): string {
|
function getUpdateType(type: string): string {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -49,17 +49,17 @@ export default function EditHistory(): ReactElement {
|
|||||||
const [queryContext, setQueryContext] = useState<OperationContext>()
|
const [queryContext, setQueryContext] = useState<OperationContext>()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!assetExtended) return
|
if (!asset) return
|
||||||
|
|
||||||
const queryContext = getQueryContext(assetExtended.chainId)
|
const queryContext = getQueryContext(asset.chainId)
|
||||||
setQueryContext(queryContext)
|
setQueryContext(queryContext)
|
||||||
}, [assetExtended])
|
}, [asset])
|
||||||
|
|
||||||
const [result] = useQuery({
|
const [result] = useQuery({
|
||||||
query: getReceipts,
|
query: getReceipts,
|
||||||
variables: { address: assetExtended?.nft.address.toLowerCase() },
|
variables: { address: asset?.nft.address.toLowerCase() },
|
||||||
context: queryContext,
|
context: queryContext,
|
||||||
pause: !assetExtended || !queryContext
|
pause: !asset || !queryContext
|
||||||
})
|
})
|
||||||
const { data } = result
|
const { data } = result
|
||||||
|
|
||||||
@ -80,10 +80,7 @@ export default function EditHistory(): ReactElement {
|
|||||||
<ul className={styles.history}>
|
<ul className={styles.history}>
|
||||||
{receipts?.map((receipt) => (
|
{receipts?.map((receipt) => (
|
||||||
<li key={receipt.id} className={styles.item}>
|
<li key={receipt.id} className={styles.item}>
|
||||||
<ExplorerLink
|
<ExplorerLink networkId={asset?.chainId} path={`/tx/${receipt.tx}`}>
|
||||||
networkId={assetExtended?.chainId}
|
|
||||||
path={`/tx/${receipt.tx}`}
|
|
||||||
>
|
|
||||||
{getUpdateType(receipt.type)}{' '}
|
{getUpdateType(receipt.type)}{' '}
|
||||||
<Time date={`${receipt.timestamp}`} relative isUnix />
|
<Time date={`${receipt.timestamp}`} relative isUnix />
|
||||||
</ExplorerLink>
|
</ExplorerLink>
|
||||||
|
@ -13,14 +13,12 @@ import EditHistory from './EditHistory'
|
|||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import NetworkName from '@shared/NetworkName'
|
import NetworkName from '@shared/NetworkName'
|
||||||
import content from '../../../../content/purgatory.json'
|
import content from '../../../../content/purgatory.json'
|
||||||
import { Asset } from '@oceanprotocol/lib'
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
||||||
|
|
||||||
export default function AssetContent({
|
export default function AssetContent({
|
||||||
asset,
|
asset
|
||||||
accessDetails
|
|
||||||
}: {
|
}: {
|
||||||
asset: Asset
|
asset: AssetExtended
|
||||||
accessDetails: AccessDetails
|
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { debug } = useUserPreferences()
|
const { debug } = useUserPreferences()
|
||||||
const { isInPurgatory, purgatoryData } = useAsset()
|
const { isInPurgatory, purgatoryData } = useAsset()
|
||||||
@ -35,7 +33,9 @@ export default function AssetContent({
|
|||||||
<div>
|
<div>
|
||||||
<div className={styles.content}>
|
<div className={styles.content}>
|
||||||
<MetaMain ddo={asset} />
|
<MetaMain ddo={asset} />
|
||||||
{accessDetails?.datatoken !== null && <Bookmark did={asset?.id} />}
|
{asset?.accessDetails?.datatoken !== null && (
|
||||||
|
<Bookmark did={asset?.id} />
|
||||||
|
)}
|
||||||
|
|
||||||
{isInPurgatory === true ? (
|
{isInPurgatory === true ? (
|
||||||
<Alert
|
<Alert
|
||||||
@ -61,7 +61,7 @@ export default function AssetContent({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={styles.actions}>
|
<div className={styles.actions}>
|
||||||
<AssetActions ddo={asset} accessDetails={accessDetails} />
|
<AssetActions asset={asset} />
|
||||||
|
|
||||||
{/*
|
{/*
|
||||||
TODO: restore edit actions, ideally put edit screens on new page
|
TODO: restore edit actions, ideally put edit screens on new page
|
||||||
|
@ -19,7 +19,7 @@ export default function EditComputeDataset({
|
|||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { debug } = useUserPreferences()
|
const { debug } = useUserPreferences()
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const { assetExtended, isAssetNetwork, refreshAsset } = useAsset()
|
const { asset, isAssetNetwork, fetchAsset } = useAsset()
|
||||||
const [success, setSuccess] = useState<string>()
|
const [success, setSuccess] = useState<string>()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ export default function EditComputeDataset({
|
|||||||
/>
|
/>
|
||||||
</article>
|
</article>
|
||||||
<Web3Feedback
|
<Web3Feedback
|
||||||
networkId={assetExtended?.chainId}
|
networkId={asset?.chainId}
|
||||||
isAssetNetwork={isAssetNetwork}
|
isAssetNetwork={isAssetNetwork}
|
||||||
/>
|
/>
|
||||||
{debug === true && (
|
{debug === true && (
|
||||||
|
@ -28,12 +28,12 @@ export default function FormEditComputeDataset({
|
|||||||
setShowEdit: (show: boolean) => void
|
setShowEdit: (show: boolean) => void
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { appConfig } = useSiteMetadata()
|
const { appConfig } = useSiteMetadata()
|
||||||
const { assetExtended } = useAsset()
|
const { asset } = useAsset()
|
||||||
const { values }: FormikContextType<ComputePrivacyForm> = useFormikContext()
|
const { values }: FormikContextType<ComputePrivacyForm> = useFormikContext()
|
||||||
const [allAlgorithms, setAllAlgorithms] = useState<AssetSelectionAsset[]>()
|
const [allAlgorithms, setAllAlgorithms] = useState<AssetSelectionAsset[]>()
|
||||||
const newCancelToken = useCancelToken()
|
const newCancelToken = useCancelToken()
|
||||||
const { publisherTrustedAlgorithms } = getServiceByName(
|
const { publisherTrustedAlgorithms } = getServiceByName(
|
||||||
assetExtended,
|
asset,
|
||||||
'compute'
|
'compute'
|
||||||
).compute
|
).compute
|
||||||
|
|
||||||
@ -41,14 +41,14 @@ export default function FormEditComputeDataset({
|
|||||||
publisherTrustedAlgorithms: PublisherTrustedAlgorithm[]
|
publisherTrustedAlgorithms: PublisherTrustedAlgorithm[]
|
||||||
): Promise<AssetSelectionAsset[]> {
|
): Promise<AssetSelectionAsset[]> {
|
||||||
const baseParams = {
|
const baseParams = {
|
||||||
chainIds: [assetExtended.chainId],
|
chainIds: [asset.chainId],
|
||||||
sort: { sortBy: SortTermOptions.Created },
|
sort: { sortBy: SortTermOptions.Created },
|
||||||
filters: [getFilterTerm('service.attributes.main.type', 'algorithm')]
|
filters: [getFilterTerm('service.attributes.main.type', 'algorithm')]
|
||||||
} as BaseQueryParams
|
} as BaseQueryParams
|
||||||
|
|
||||||
const query = generateBaseQuery(baseParams)
|
const query = generateBaseQuery(baseParams)
|
||||||
const querryResult = await queryMetadata(query, newCancelToken())
|
const querryResult = await queryMetadata(query, newCancelToken())
|
||||||
const datasetComputeService = getServiceByName(assetExtended, 'compute')
|
const datasetComputeService = getServiceByName(asset, 'compute')
|
||||||
const algorithmSelectionList = await transformDDOToAssetSelection(
|
const algorithmSelectionList = await transformDDOToAssetSelection(
|
||||||
datasetComputeService?.serviceEndpoint,
|
datasetComputeService?.serviceEndpoint,
|
||||||
querryResult.results,
|
querryResult.results,
|
||||||
|
@ -22,11 +22,11 @@ export default function Edit({
|
|||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { debug } = useUserPreferences()
|
const { debug } = useUserPreferences()
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const { assetExtended, refreshAsset, accessDetails } = useAsset()
|
const { asset, fetchAsset } = useAsset()
|
||||||
const [success, setSuccess] = useState<string>()
|
const [success, setSuccess] = useState<string>()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const [timeoutStringValue, setTimeoutStringValue] = useState<string>()
|
const [timeoutStringValue, setTimeoutStringValue] = useState<string>()
|
||||||
const { timeout } = assetExtended.services[0]
|
const { timeout } = asset.services[0]
|
||||||
|
|
||||||
const hasFeedback = error || success
|
const hasFeedback = error || success
|
||||||
|
|
||||||
@ -119,9 +119,9 @@ export default function Edit({
|
|||||||
return (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={getInitialValues(
|
initialValues={getInitialValues(
|
||||||
assetExtended.metadata,
|
asset.metadata,
|
||||||
timeout,
|
timeout,
|
||||||
accessDetails.price
|
asset.accessDetails.price
|
||||||
)}
|
)}
|
||||||
validationSchema={validationSchema}
|
validationSchema={validationSchema}
|
||||||
onSubmit={async (values, { resetForm }) => {
|
onSubmit={async (values, { resetForm }) => {
|
||||||
@ -148,7 +148,7 @@ export default function Edit({
|
|||||||
/> */}
|
/> */}
|
||||||
|
|
||||||
<aside>
|
<aside>
|
||||||
<Web3Feedback networkId={assetExtended?.chainId} />
|
<Web3Feedback networkId={asset?.chainId} />
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
{/* {debug === true && <Debug values={values} ddo={ddo} />} */}
|
{/* {debug === true && <Debug values={values} ddo={ddo} />} */}
|
||||||
|
@ -6,21 +6,20 @@ import { useAsset } from '@context/Asset'
|
|||||||
import AssetContent from './AssetContent'
|
import AssetContent from './AssetContent'
|
||||||
|
|
||||||
export default function AssetDetails({ uri }: { uri: string }): ReactElement {
|
export default function AssetDetails({ uri }: { uri: string }): ReactElement {
|
||||||
const { assetExtended, title, error, isInPurgatory, loading, accessDetails } =
|
const { asset, title, error, isInPurgatory, loading } = useAsset()
|
||||||
useAsset()
|
|
||||||
const [pageTitle, setPageTitle] = useState<string>()
|
const [pageTitle, setPageTitle] = useState<string>()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!assetExtended || error) {
|
if (!asset || error) {
|
||||||
setPageTitle('Could not retrieve asset')
|
setPageTitle('Could not retrieve asset')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setPageTitle(isInPurgatory ? '' : title)
|
setPageTitle(isInPurgatory ? '' : title)
|
||||||
}, [assetExtended, error, isInPurgatory, title])
|
}, [asset, error, isInPurgatory, title])
|
||||||
|
|
||||||
return assetExtended && pageTitle !== undefined && !loading ? (
|
return asset && pageTitle !== undefined && !loading ? (
|
||||||
<Page title={pageTitle} uri={uri}>
|
<Page title={pageTitle} uri={uri}>
|
||||||
<AssetContent asset={assetExtended} accessDetails={accessDetails} />
|
<AssetContent asset={asset} />
|
||||||
</Page>
|
</Page>
|
||||||
) : error ? (
|
) : error ? (
|
||||||
<Page title={pageTitle} noPageHeader uri={uri}>
|
<Page title={pageTitle} noPageHeader uri={uri}>
|
||||||
|
@ -74,7 +74,7 @@ export default function ComputeJobs({
|
|||||||
minimal?: boolean
|
minimal?: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { accountId, networkId } = useWeb3()
|
const { accountId, networkId } = useWeb3()
|
||||||
const { assetExtended } = useAsset()
|
const { asset } = useAsset()
|
||||||
const { chainIds } = useUserPreferences()
|
const { chainIds } = useUserPreferences()
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
const [jobs, setJobs] = useState<ComputeJobMetaData[]>([])
|
const [jobs, setJobs] = useState<ComputeJobMetaData[]>([])
|
||||||
@ -96,7 +96,7 @@ export default function ComputeJobs({
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
LoggerInstance.error(error.message)
|
LoggerInstance.error(error.message)
|
||||||
}
|
}
|
||||||
}, [chainIds, accountId, assetExtended, isMounted])
|
}, [chainIds, accountId, asset, isMounted])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchJobs()
|
fetchJobs()
|
||||||
|
@ -44,7 +44,7 @@ export default function Preview(): ReactElement {
|
|||||||
<h2 className={styles.previewTitle}>Preview</h2>
|
<h2 className={styles.previewTitle}>Preview</h2>
|
||||||
|
|
||||||
<h3 className={styles.assetTitle}>{values.metadata.name}</h3>
|
<h3 className={styles.assetTitle}>{values.metadata.name}</h3>
|
||||||
<AssetContent asset={asset} accessDetails={accessDetails} />
|
<AssetContent asset={asset} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user