1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

add balance check and check is consumable

This commit is contained in:
Bogdan Fazakas 2022-01-19 09:12:59 +02:00
parent 6689f55808
commit afd1c2d9db
3 changed files with 38 additions and 16 deletions

View File

@ -1,8 +1,13 @@
import { Asset, DDO, Service } from '@oceanprotocol/lib'
interface Consumable {
status: number
message: string
result: boolean
}
export function getServiceByName(
ddo: Asset | DDO,
name: 'access' | 'compute'
name: 'access' | 'compute' | 'metadata'
): Service {
if (!ddo) return
@ -58,3 +63,19 @@ export function secondsToString(numberOfSeconds: number): string {
? `${seconds} second${numberEnding(seconds)}`
: 'less than a second'
}
export async function checkIfConsumable(ddo: DDO): Promise<Consumable> {
if (!ddo) throw new Error('ERROR: DDO does not exist')
const allowedConsume = { status: 0, message: 'All good', result: true }
const orderDisabled = {
status: 1,
message:
'Ordering this asset has been temporarily disabled by the publisher.',
result: false
}
const metadata = this.getServiceByName(ddo, 'metadata')
if (metadata.attributes.status?.isOrderDisabled) return orderDisabled
return allowedConsume
}

View File

@ -8,6 +8,7 @@ import ButtonBuy from '@shared/ButtonBuy'
import PriceOutput from './PriceOutput'
import { useAsset } from '@context/Asset'
import { useWeb3 } from '@context/Web3'
import { checkIfConsumable } from '@utils/ddo'
import content from '../../../../../content/pages/startComputeDataset.json'
import { Asset } from '@oceanprotocol/lib'
@ -84,11 +85,8 @@ export default function FormStartCompute({
if (!accountId || !isConsumable) return
async function checkIsConsumable() {
// const consumable = await ocean.assets.isConsumable(
// algorithmDDO as any,
// accountId.toLowerCase()
// )
// if (consumable) setAlgorithmConsumableStatus(consumable.status)
const consumable = await checkIfConsumable(algorithmDDO as any)
if (consumable) setAlgorithmConsumableStatus(consumable.status)
}
checkIsConsumable()
}, [values.algorithm, accountId, isConsumable])

View File

@ -6,7 +6,8 @@ import {
Asset,
DDO,
PublisherTrustedAlgorithm,
FileMetadata
FileMetadata,
Datatoken
} from '@oceanprotocol/lib'
import { toast } from 'react-toastify'
import Price from '@shared/Price'
@ -36,6 +37,7 @@ import ComputeJobs from '../../../Profile/History/ComputeJobs'
import { useCancelToken } from '@hooks/useCancelToken'
import { useIsMounted } from '@hooks/useIsMounted'
import { SortTermOptions } from '../../../../@types/aquarius/SearchQuery'
import { Decimal } from 'decimal.js'
export default function Compute({
ddo,
@ -55,7 +57,7 @@ export default function Compute({
consumableFeedback?: string
}): ReactElement {
const { appConfig } = useSiteMetadata()
const { accountId } = useWeb3()
const { accountId, web3 } = useWeb3()
const { buyDT, pricingError, pricingStepText } = usePricing()
const [isJobStarting, setIsJobStarting] = useState(false)
const [error, setError] = useState<string>()
@ -69,7 +71,7 @@ export default function Compute({
const [previousDatasetOrderId, setPreviousDatasetOrderId] = useState<string>()
const [hasPreviousAlgorithmOrder, setHasPreviousAlgorithmOrder] =
useState(false)
const [algorithmDTBalance, setalgorithmDTBalance] = useState<string>()
const [algorithmDTBalance, setAlgorithmDTBalance] = useState<string>()
const [algorithmPrice, setAlgorithmPrice] = useState<BestPrice>()
const [previousAlgorithmOrderId, setPreviousAlgorithmOrderId] =
useState<string>()
@ -110,12 +112,13 @@ export default function Compute({
}
async function checkAssetDTBalance(asset: DDO) {
// const AssetDtBalance = await ocean.datatokens.balance(
// asset.services[0].datatokenAddress,
// accountId
// )
// setalgorithmDTBalance(AssetDtBalance)
// setHasAlgoAssetDatatoken(Number(AssetDtBalance) >= 1)
const datatokenInstance = new Datatoken(web3)
const AssetDtBalance = await datatokenInstance.balance(
asset.services[0].datatokenAddress,
accountId
)
setAlgorithmDTBalance(new Decimal(AssetDtBalance).toString())
setHasAlgoAssetDatatoken(Number(AssetDtBalance) >= 1)
}
function getQuerryString(
@ -230,7 +233,7 @@ export default function Compute({
checkPreviousOrders(selectedAlgorithmAsset)
}
}
// ocean && checkAssetDTBalance(selectedAlgorithmAsset)
checkAssetDTBalance(selectedAlgorithmAsset)
}, [ddo, selectedAlgorithmAsset, accountId, hasPreviousAlgorithmOrder])
// Output errors in toast UI