1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-28 00:27:49 +02:00
market/src/@context/Asset.tsx

189 lines
5.4 KiB
TypeScript
Raw Normal View History

import React, {
useContext,
useState,
useEffect,
createContext,
ReactElement,
useCallback,
ReactNode
} from 'react'
import { Config, LoggerInstance, Purgatory } from '@oceanprotocol/lib'
2021-10-13 18:48:59 +02:00
import { CancelToken } from 'axios'
import { checkV3Asset, retrieveAsset } from '@utils/aquarius'
import { useWeb3 } from './Web3'
2021-10-13 18:48:59 +02:00
import { useCancelToken } from '@hooks/useCancelToken'
2022-01-13 14:15:15 +01:00
import { getOceanConfig, getDevelopmentConfig } from '@utils/ocean'
import { AssetExtended } from 'src/@types/AssetExtended'
import { getAccessDetails } from '@utils/accessDetailsAndPricing'
import { useIsMounted } from '@hooks/useIsMounted'
Various fixes in the pool component (#1327) * remove legacy check to prevent rug pull Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * various fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove old prop Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove old prop Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * add expected output to remove Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove console.logs Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix max calculations Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * refactors Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * temp fixes for build Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * local calc for pice and liquidity Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove var Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix profile liquidity Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * global context, opc fee Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * comment Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * various fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * refactor global context Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove nesting from market context Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix build Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix undefined appConfig Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * direct import of appConfig & siteContent * this never changes on run time, so we should never have to wait for it and have it in js bundle at all times * in utility methods, import directly * for components, import directly in MarketMetadata context and pass through * remove screen CSS fixes * put back auto-fetching indicator, move manual refresh action behind debug Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2022-04-22 02:38:35 +02:00
import { useMarketMetadata } from './MarketMetadata'
export interface AssetProviderValue {
isInPurgatory: boolean
purgatoryData: Purgatory
asset: AssetExtended
2021-06-14 20:04:44 +02:00
title: string
owner: string
error?: string
isAssetNetwork: boolean
isV3Asset: boolean
2022-01-13 14:15:15 +01:00
oceanConfig: Config
loading: boolean
fetchAsset: (token?: CancelToken) => Promise<void>
}
const AssetContext = createContext({} as AssetProviderValue)
function AssetProvider({
2022-01-13 14:15:15 +01:00
did,
children
}: {
2022-01-13 14:15:15 +01:00
did: string
children: ReactNode
}): ReactElement {
Various fixes in the pool component (#1327) * remove legacy check to prevent rug pull Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * various fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove old prop Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove old prop Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * add expected output to remove Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove console.logs Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix max calculations Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * refactors Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * temp fixes for build Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * local calc for pice and liquidity Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove var Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix profile liquidity Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * global context, opc fee Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * comment Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * various fixes Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * refactor global context Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove nesting from market context Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix build Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix undefined appConfig Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * direct import of appConfig & siteContent * this never changes on run time, so we should never have to wait for it and have it in js bundle at all times * in utility methods, import directly * for components, import directly in MarketMetadata context and pass through * remove screen CSS fixes * put back auto-fetching indicator, move manual refresh action behind debug Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2022-04-22 02:38:35 +02:00
const { appConfig } = useMarketMetadata()
2021-05-31 14:27:04 +02:00
const { chainId, accountId } = useWeb3()
const [isInPurgatory, setIsInPurgatory] = useState(false)
const [purgatoryData, setPurgatoryData] = useState<Purgatory>()
const [asset, setAsset] = useState<AssetExtended>()
const [title, setTitle] = useState<string>()
const [owner, setOwner] = useState<string>()
const [error, setError] = useState<string>()
2021-06-14 20:04:44 +02:00
const [loading, setLoading] = useState(false)
const [isAssetNetwork, setIsAssetNetwork] = useState<boolean>()
const [isV3Asset, setIsV3Asset] = useState<boolean>()
2022-01-13 14:15:15 +01:00
const [oceanConfig, setOceanConfig] = useState<Config>()
const newCancelToken = useCancelToken()
const isMounted = useIsMounted()
// -----------------------------------
// Helper: Get and set asset based on passed DID
// -----------------------------------
const fetchAsset = useCallback(
async (token?: CancelToken) => {
if (!did) return
LoggerInstance.log('[asset] Fetching asset...')
setLoading(true)
const asset = await retrieveAsset(did, token)
if (!asset) {
setIsV3Asset(await checkV3Asset(did, token))
setError(
`\`${did}\`` +
'\n\nWe could not find an asset for this DID in the cache. If you just published a new asset, wait some seconds and refresh this page.'
)
LoggerInstance.error(`[asset] Failed getting asset for ${did}`, asset)
} else {
setError(undefined)
setAsset((prevState) => ({
...prevState,
...asset
}))
setTitle(asset.metadata?.name)
setOwner(asset.nft?.owner)
setIsInPurgatory(asset.purgatory?.state)
setPurgatoryData(asset.purgatory)
LoggerInstance.log('[asset] Got asset', asset)
}
setLoading(false)
},
[did]
)
// -----------------------------------
// Helper: Get and set asset access details
// -----------------------------------
const fetchAccessDetails = useCallback(async (): Promise<void> => {
if (!asset?.chainId || !asset?.services) return
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])
2022-01-13 14:15:15 +01:00
// -----------------------------------
// 1. Get and set asset based on passed DID
2022-01-13 14:15:15 +01:00
// -----------------------------------
useEffect(() => {
if (!isMounted || !appConfig?.metadataCacheUri) return
fetchAsset(newCancelToken())
}, [appConfig?.metadataCacheUri, fetchAsset, newCancelToken, isMounted])
2022-01-13 14:15:15 +01:00
// -----------------------------------
// 2. Attach access details to asset
2022-01-13 14:15:15 +01:00
// -----------------------------------
useEffect(() => {
if (!isMounted) return
fetchAccessDetails()
}, [accountId, fetchAccessDetails, isMounted])
2022-01-13 14:15:15 +01:00
// -----------------------------------
// Check user network against asset network
2022-01-13 14:15:15 +01:00
// -----------------------------------
useEffect(() => {
if (!chainId || !asset?.chainId) return
const isAssetNetwork = chainId === asset?.chainId
setIsAssetNetwork(isAssetNetwork)
}, [chainId, asset?.chainId])
2022-01-13 14:15:15 +01:00
// -----------------------------------
// Load ocean config based on asset network
// -----------------------------------
useEffect(() => {
if (!asset?.chainId) return
2022-01-13 14:15:15 +01:00
const oceanConfig = {
...getOceanConfig(asset?.chainId),
2022-01-13 14:15:15 +01:00
// add local dev values
...(asset?.chainId === 8996 && {
2022-01-13 14:15:15 +01:00
...getDevelopmentConfig()
})
}
setOceanConfig(oceanConfig)
}, [asset?.chainId])
2022-01-13 14:15:15 +01:00
return (
<AssetContext.Provider
value={
{
asset,
did,
title,
owner,
error,
isInPurgatory,
purgatoryData,
loading,
fetchAsset,
2022-01-13 14:15:15 +01:00
isAssetNetwork,
isV3Asset,
2022-01-13 14:15:15 +01:00
oceanConfig
} as AssetProviderValue
}
>
{children}
</AssetContext.Provider>
)
}
// Helper hook to access the provider values
const useAsset = (): AssetProviderValue => useContext(AssetContext)
export { AssetProvider, useAsset, AssetContext }
export default AssetProvider