mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
useMetadata refactor for strict type checking
This commit is contained in:
parent
d8107d6686
commit
f5724c553d
@ -13,7 +13,7 @@ interface UseCompute {
|
||||
dataTokenAddress: string,
|
||||
algorithmRawCode: string,
|
||||
computeContainer: ComputeValue
|
||||
) => Promise<ComputeJob>
|
||||
) => Promise<ComputeJob | void>
|
||||
computeStep?: number
|
||||
computeStepText?: string
|
||||
computeError?: string
|
||||
@ -44,7 +44,13 @@ function useCompute(): UseCompute {
|
||||
const [computeError, setComputeError] = useState<string | undefined>()
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
function setStep(index: number) {
|
||||
function setStep(index?: number) {
|
||||
if (!index) {
|
||||
setComputeStep(undefined)
|
||||
setComputeStepText(undefined)
|
||||
return
|
||||
}
|
||||
|
||||
setComputeStep(index)
|
||||
setComputeStepText(computeFeedback[index])
|
||||
}
|
||||
@ -55,7 +61,7 @@ function useCompute(): UseCompute {
|
||||
dataTokenAddress: string,
|
||||
algorithmRawCode: string,
|
||||
computeContainer: ComputeValue
|
||||
): Promise<ComputeJob> {
|
||||
): Promise<ComputeJob | void> {
|
||||
if (!ocean || !account) return
|
||||
|
||||
setComputeError(undefined)
|
||||
|
@ -7,89 +7,100 @@ import { isDDO } from '../../utils'
|
||||
import BestPrice from './BestPrice'
|
||||
|
||||
interface UseMetadata {
|
||||
ddo: DDO
|
||||
did: DID | string
|
||||
metadata: Metadata
|
||||
title: string
|
||||
price: BestPrice
|
||||
ddo: DDO | undefined
|
||||
did: DID | string | undefined
|
||||
metadata: Metadata | undefined
|
||||
title: string | undefined
|
||||
price: BestPrice | undefined
|
||||
isLoaded: boolean
|
||||
getPrice: (dataTokenAddress?: string) => Promise<BestPrice>
|
||||
getPrice: (dataTokenAddress: string) => Promise<BestPrice | void>
|
||||
}
|
||||
|
||||
function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
const { ocean, status, accountId } = useOcean()
|
||||
const [internalDdo, setDDO] = useState<DDO | undefined>()
|
||||
const [internalDid, setDID] = useState<DID | string | undefined>()
|
||||
const [metadata, setMetadata] = useState<Metadata | undefined>()
|
||||
const [title, setTitle] = useState<string | undefined>()
|
||||
const { ocean, status, accountId, chainId } = useOcean()
|
||||
const [internalDdo, setDDO] = useState<DDO>()
|
||||
const [internalDid, setDID] = useState<DID | string>()
|
||||
const [metadata, setMetadata] = useState<Metadata>()
|
||||
const [title, setTitle] = useState<string>()
|
||||
const [isLoaded, setIsLoaded] = useState(false)
|
||||
const [price, setPrice] = useState<BestPrice | undefined>()
|
||||
const [price, setPrice] = useState<BestPrice>()
|
||||
|
||||
const getDDO = useCallback(
|
||||
async (did: DID | string): Promise<DDO | null> => {
|
||||
if (status !== ProviderStatus.CONNECTED) return null
|
||||
|
||||
async (did: DID | string): Promise<DDO> => {
|
||||
const ddo = await ocean.metadatastore.retrieveDDO(did)
|
||||
return ddo
|
||||
},
|
||||
[ocean?.metadatastore, status]
|
||||
[ocean?.metadatastore]
|
||||
)
|
||||
|
||||
const getPrice = useCallback(
|
||||
async (dataTokenAddress?: string): Promise<BestPrice> => {
|
||||
if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken
|
||||
return await getBestDataTokenPrice(ocean, dataTokenAddress, accountId)
|
||||
async (dataTokenAddress: string): Promise<BestPrice> => {
|
||||
const price = await getBestDataTokenPrice(
|
||||
ocean,
|
||||
dataTokenAddress,
|
||||
accountId
|
||||
)
|
||||
return price
|
||||
},
|
||||
[ocean, accountId, internalDdo?.dataToken]
|
||||
[ocean, accountId]
|
||||
)
|
||||
|
||||
const getMetadata = useCallback(async (): Promise<Metadata | null> => {
|
||||
if (!internalDdo) return null
|
||||
const metadata = internalDdo.findServiceByType('metadata')
|
||||
const getMetadata = useCallback(async (ddo: DDO): Promise<Metadata> => {
|
||||
const metadata = ddo.findServiceByType('metadata')
|
||||
return metadata.attributes
|
||||
}, [internalDdo])
|
||||
}, [])
|
||||
|
||||
//
|
||||
// Get and set DDO based on passed DDO or DID
|
||||
//
|
||||
useEffect(() => {
|
||||
async function init(): Promise<void> {
|
||||
if (ocean && status === ProviderStatus.CONNECTED) {
|
||||
if (!asset) return
|
||||
if (!asset || !ocean || status !== ProviderStatus.CONNECTED) return
|
||||
|
||||
if (isDDO(asset)) {
|
||||
setDDO(asset)
|
||||
setDID(asset.id)
|
||||
} else {
|
||||
const ddo = await getDDO(asset)
|
||||
Logger.debug('DDO', ddo)
|
||||
setDDO(ddo)
|
||||
setDID(asset)
|
||||
}
|
||||
async function init(): Promise<void> {
|
||||
if (!asset) return
|
||||
|
||||
if (isDDO(asset as string | DDO | DID)) {
|
||||
setDDO(asset as DDO)
|
||||
setDID((asset as DDO).id)
|
||||
} else {
|
||||
// asset is a DID
|
||||
const ddo = await getDDO(asset as DID)
|
||||
Logger.debug('DDO', ddo)
|
||||
setDDO(ddo)
|
||||
setDID(asset as DID)
|
||||
}
|
||||
}
|
||||
init()
|
||||
}, [ocean, status, asset, getDDO])
|
||||
|
||||
//
|
||||
// Get metadata for stored DDO
|
||||
//
|
||||
useEffect(() => {
|
||||
if (!accountId) return
|
||||
|
||||
async function init(): Promise<void> {
|
||||
if (internalDdo) {
|
||||
const metadata = await getMetadata()
|
||||
setMetadata(metadata)
|
||||
setTitle(metadata.main.name)
|
||||
const price = await getPrice()
|
||||
if (!internalDdo) return
|
||||
|
||||
setPrice(price)
|
||||
setIsLoaded(true)
|
||||
}
|
||||
const metadata = await getMetadata(internalDdo)
|
||||
setMetadata(metadata)
|
||||
setTitle(metadata.main.name)
|
||||
const price = await getPrice(internalDdo.dataToken)
|
||||
price && setPrice(price)
|
||||
setIsLoaded(true)
|
||||
}
|
||||
init()
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
const price = await getPrice()
|
||||
setPrice(price)
|
||||
if (!internalDdo) return
|
||||
const price = await getPrice(internalDdo.dataToken)
|
||||
price && setPrice(price)
|
||||
}, 10000)
|
||||
return () => clearInterval(interval)
|
||||
}, [accountId, internalDdo, getMetadata, getPrice])
|
||||
|
||||
return () => {
|
||||
clearInterval(interval)
|
||||
}
|
||||
}, [accountId, chainId, internalDdo, getMetadata, getPrice])
|
||||
|
||||
return {
|
||||
ddo: internalDdo,
|
||||
|
@ -50,8 +50,7 @@ export async function getCheapestPool(
|
||||
export async function getCheapestExchange(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string
|
||||
): Promise<{ address?: string; price: string } | null> {
|
||||
if (!ocean || !dataTokenAddress) return
|
||||
): Promise<{ address?: string; price: string }> {
|
||||
try {
|
||||
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
||||
dataTokenAddress,
|
||||
@ -94,7 +93,7 @@ export async function getBestDataTokenPrice(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string,
|
||||
accountId: string
|
||||
): Promise<BestPrice | undefined> {
|
||||
): Promise<BestPrice> {
|
||||
const cheapestPool = await getCheapestPool(ocean, accountId, dataTokenAddress)
|
||||
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
||||
Decimal.set({ precision: 5 })
|
||||
|
@ -11,11 +11,7 @@
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"importHelpers": true,
|
||||
|
||||
/* Strict Type-Checking */
|
||||
// TODO: remove and adapt for `strictNullChecks`
|
||||
"strict": true,
|
||||
"strictNullChecks": false
|
||||
"strict": true
|
||||
},
|
||||
"include": ["./src/@types", "./src/index.ts"]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user