2020-11-20 14:31:28 +01:00
|
|
|
import React, {
|
|
|
|
useContext,
|
|
|
|
useState,
|
|
|
|
useEffect,
|
|
|
|
createContext,
|
|
|
|
ReactElement,
|
|
|
|
useCallback,
|
|
|
|
ReactNode
|
|
|
|
} from 'react'
|
2022-01-13 14:15:15 +01:00
|
|
|
import { Asset, Config, LoggerInstance, Purgatory } from '@oceanprotocol/lib'
|
2021-10-13 18:48:59 +02:00
|
|
|
import getAssetPurgatoryData from '@utils/purgatory'
|
|
|
|
import { CancelToken } from 'axios'
|
|
|
|
import { retrieveDDO } from '@utils/aquarius'
|
|
|
|
import { getPrice } from '@utils/subgraph'
|
2021-05-27 13:58:52 +02:00
|
|
|
import { useWeb3 } from './Web3'
|
2021-10-13 18:48:59 +02:00
|
|
|
import { useSiteMetadata } from '@hooks/useSiteMetadata'
|
|
|
|
import { useCancelToken } from '@hooks/useCancelToken'
|
2022-01-13 14:15:15 +01:00
|
|
|
import { getOceanConfig, getDevelopmentConfig } from '@utils/ocean'
|
2020-11-20 14:31:28 +01:00
|
|
|
|
|
|
|
interface AssetProviderValue {
|
|
|
|
isInPurgatory: boolean
|
2022-01-11 20:05:07 +01:00
|
|
|
purgatoryData: Purgatory
|
2021-11-11 08:51:13 +01:00
|
|
|
ddo: Asset
|
2021-06-14 20:04:44 +02:00
|
|
|
title: string
|
|
|
|
owner: string
|
|
|
|
price: BestPrice
|
2020-11-20 14:31:28 +01:00
|
|
|
error?: string
|
|
|
|
refreshInterval: number
|
2021-05-27 13:58:52 +02:00
|
|
|
isAssetNetwork: boolean
|
2022-01-13 14:15:15 +01:00
|
|
|
oceanConfig: Config
|
2021-06-14 14:37:03 +02:00
|
|
|
loading: boolean
|
2020-12-10 14:30:40 +01:00
|
|
|
refreshDdo: (token?: CancelToken) => Promise<void>
|
2020-11-20 14:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const AssetContext = createContext({} as AssetProviderValue)
|
|
|
|
|
|
|
|
const refreshInterval = 10000 // 10 sec.
|
|
|
|
|
|
|
|
function AssetProvider({
|
2022-01-13 14:15:15 +01:00
|
|
|
did,
|
2020-11-20 14:31:28 +01:00
|
|
|
children
|
|
|
|
}: {
|
2022-01-13 14:15:15 +01:00
|
|
|
did: string
|
2020-11-20 14:31:28 +01:00
|
|
|
children: ReactNode
|
|
|
|
}): ReactElement {
|
2021-05-31 14:27:04 +02:00
|
|
|
const { appConfig } = useSiteMetadata()
|
|
|
|
|
2021-05-27 13:58:52 +02:00
|
|
|
const { networkId } = useWeb3()
|
2020-11-20 14:31:28 +01:00
|
|
|
const [isInPurgatory, setIsInPurgatory] = useState(false)
|
2022-01-11 20:05:07 +01:00
|
|
|
const [purgatoryData, setPurgatoryData] = useState<Purgatory>()
|
2021-11-11 08:51:13 +01:00
|
|
|
const [ddo, setDDO] = useState<Asset>()
|
2020-11-20 14:31:28 +01:00
|
|
|
const [title, setTitle] = useState<string>()
|
|
|
|
const [price, setPrice] = useState<BestPrice>()
|
|
|
|
const [owner, setOwner] = useState<string>()
|
|
|
|
const [error, setError] = useState<string>()
|
2021-06-14 20:04:44 +02:00
|
|
|
const [loading, setLoading] = useState(false)
|
2021-05-27 13:58:52 +02:00
|
|
|
const [isAssetNetwork, setIsAssetNetwork] = useState<boolean>()
|
2022-01-13 14:15:15 +01:00
|
|
|
const [oceanConfig, setOceanConfig] = useState<Config>()
|
2021-11-11 08:51:13 +01:00
|
|
|
|
2021-09-24 13:56:42 +02:00
|
|
|
const newCancelToken = useCancelToken()
|
2021-11-11 08:51:13 +01:00
|
|
|
|
2020-12-10 14:30:40 +01:00
|
|
|
const fetchDdo = async (token?: CancelToken) => {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.log('[asset] Init asset, get DDO')
|
2021-06-14 14:37:03 +02:00
|
|
|
setLoading(true)
|
2022-01-13 14:15:15 +01:00
|
|
|
const ddo = await retrieveDDO(did, token)
|
2020-12-10 14:30:40 +01:00
|
|
|
|
|
|
|
if (!ddo) {
|
|
|
|
setError(
|
2022-01-13 14:15:15 +01:00
|
|
|
`[asset] The DDO for ${did} was not found in MetadataCache. If you just published a new data set, wait some seconds and refresh this page.`
|
2020-12-10 14:30:40 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
setError(undefined)
|
|
|
|
}
|
2021-06-14 14:37:03 +02:00
|
|
|
setLoading(false)
|
2020-12-10 14:30:40 +01:00
|
|
|
return ddo
|
|
|
|
}
|
|
|
|
|
|
|
|
const refreshDdo = async (token?: CancelToken) => {
|
2021-06-14 14:37:03 +02:00
|
|
|
setLoading(true)
|
2020-12-10 14:30:40 +01:00
|
|
|
const ddo = await fetchDdo(token)
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.debug('[asset] Got DDO', ddo)
|
2020-12-10 14:30:40 +01:00
|
|
|
setDDO(ddo)
|
2021-06-14 14:37:03 +02:00
|
|
|
setLoading(false)
|
2020-12-10 14:30:40 +01:00
|
|
|
}
|
2021-03-17 11:44:26 +01:00
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
const setPurgatory = useCallback(async (did: string): Promise<void> => {
|
|
|
|
if (!did) return
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await getAssetPurgatoryData(did)
|
2022-01-11 20:05:07 +01:00
|
|
|
const isInPurgatory = result?.state === true
|
2021-11-11 08:51:13 +01:00
|
|
|
setIsInPurgatory(isInPurgatory)
|
|
|
|
isInPurgatory && setPurgatoryData(result)
|
|
|
|
} catch (error) {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.error(error)
|
2021-11-11 08:51:13 +01:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2022-01-13 14:15:15 +01:00
|
|
|
// -----------------------------------
|
|
|
|
// Get and set DDO based on passed DID
|
|
|
|
// -----------------------------------
|
2020-11-20 14:31:28 +01:00
|
|
|
useEffect(() => {
|
2022-01-13 14:15:15 +01:00
|
|
|
if (!did || !appConfig.metadataCacheUri) return
|
2020-11-20 14:31:28 +01:00
|
|
|
|
|
|
|
let isMounted = true
|
|
|
|
|
2020-12-10 14:30:40 +01:00
|
|
|
async function init() {
|
2021-09-24 13:56:42 +02:00
|
|
|
const ddo = await fetchDdo(newCancelToken())
|
2021-11-11 08:51:13 +01:00
|
|
|
if (!isMounted || !ddo) return
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.debug('[asset] Got DDO', ddo)
|
2020-11-20 14:31:28 +01:00
|
|
|
setDDO(ddo)
|
2021-11-11 08:51:13 +01:00
|
|
|
setTitle(ddo.metadata.name)
|
|
|
|
setOwner(ddo.nft.owner)
|
2022-01-13 19:17:09 +01:00
|
|
|
// TODO: restore asset purgatory once Aquarius supports it, ref #953
|
|
|
|
// setIsInPurgatory(ddo.purgatory.state === true)
|
2021-11-11 08:51:13 +01:00
|
|
|
await setPurgatory(ddo.id)
|
2020-11-20 14:31:28 +01:00
|
|
|
}
|
|
|
|
init()
|
|
|
|
return () => {
|
|
|
|
isMounted = false
|
|
|
|
}
|
2022-01-13 14:15:15 +01:00
|
|
|
}, [did, appConfig.metadataCacheUri])
|
2020-11-20 14:31:28 +01:00
|
|
|
|
2022-01-13 14:15:15 +01:00
|
|
|
// -----------------------------------
|
|
|
|
// Attach price to asset
|
|
|
|
// -----------------------------------
|
2021-11-11 08:51:13 +01:00
|
|
|
const initPrice = useCallback(async (ddo: Asset): Promise<void> => {
|
2021-03-17 11:44:26 +01:00
|
|
|
if (!ddo) return
|
2021-04-20 08:44:18 +02:00
|
|
|
const returnedPrice = await getPrice(ddo)
|
2021-04-22 13:03:58 +02:00
|
|
|
setPrice({ ...returnedPrice })
|
2021-03-17 11:44:26 +01:00
|
|
|
}, [])
|
2020-11-20 14:31:28 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ddo) return
|
2021-11-11 08:51:13 +01:00
|
|
|
initPrice(ddo)
|
|
|
|
}, [ddo, initPrice])
|
2020-11-20 14:31:28 +01:00
|
|
|
|
2022-01-13 14:15:15 +01:00
|
|
|
// -----------------------------------
|
2021-05-27 13:58:52 +02:00
|
|
|
// Check user network against asset network
|
2022-01-13 14:15:15 +01:00
|
|
|
// -----------------------------------
|
2021-05-27 13:58:52 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!networkId || !ddo) return
|
|
|
|
|
2021-07-07 08:45:20 +02:00
|
|
|
const isAssetNetwork = networkId === ddo?.chainId
|
2021-05-27 13:58:52 +02:00
|
|
|
setIsAssetNetwork(isAssetNetwork)
|
|
|
|
}, [networkId, ddo])
|
|
|
|
|
2022-01-13 14:15:15 +01:00
|
|
|
// -----------------------------------
|
|
|
|
// Load ocean config based on asset network
|
|
|
|
// -----------------------------------
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ddo?.chainId) return
|
|
|
|
|
|
|
|
const oceanConfig = {
|
|
|
|
...getOceanConfig(ddo?.chainId),
|
|
|
|
|
|
|
|
// add local dev values
|
|
|
|
...(ddo?.chainId === 8996 && {
|
|
|
|
...getDevelopmentConfig()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setOceanConfig(oceanConfig)
|
|
|
|
}, [ddo])
|
|
|
|
|
2020-11-20 14:31:28 +01:00
|
|
|
return (
|
|
|
|
<AssetContext.Provider
|
|
|
|
value={
|
|
|
|
{
|
|
|
|
ddo,
|
|
|
|
did,
|
|
|
|
title,
|
|
|
|
owner,
|
|
|
|
price,
|
|
|
|
error,
|
|
|
|
isInPurgatory,
|
|
|
|
purgatoryData,
|
|
|
|
refreshInterval,
|
2021-06-14 14:37:03 +02:00
|
|
|
loading,
|
2021-05-27 13:58:52 +02:00
|
|
|
refreshDdo,
|
2022-01-13 14:15:15 +01:00
|
|
|
isAssetNetwork,
|
|
|
|
oceanConfig
|
2020-11-20 14:31:28 +01:00
|
|
|
} as AssetProviderValue
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</AssetContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper hook to access the provider values
|
|
|
|
const useAsset = (): AssetProviderValue => useContext(AssetContext)
|
|
|
|
|
2021-10-27 12:27:14 +02:00
|
|
|
export { AssetProvider, useAsset, AssetContext }
|
2020-11-20 14:31:28 +01:00
|
|
|
export default AssetProvider
|