From be9fde8827fe321dee98c22aae509c67ab6547cb Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 19 Oct 2020 11:33:06 +0200 Subject: [PATCH] get datatoken balance for use in consume/compute --- .../organisms/AssetActions/Compute.tsx | 4 ++- .../organisms/AssetActions/Consume.tsx | 22 +++++++------- .../organisms/AssetActions/index.tsx | 30 +++++++++++++++++-- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/components/organisms/AssetActions/Compute.tsx b/src/components/organisms/AssetActions/Compute.tsx index 93b3d0cb0..2768e2418 100644 --- a/src/components/organisms/AssetActions/Compute.tsx +++ b/src/components/organisms/AssetActions/Compute.tsx @@ -19,10 +19,12 @@ import { useSiteMetadata } from '../../../hooks/useSiteMetadata' export default function Compute({ ddo, - isBalanceSufficient + isBalanceSufficient, + dtBalance }: { ddo: DDO isBalanceSufficient: boolean + dtBalance: string }): ReactElement { const { ocean } = useOcean() const { compute, isLoading, computeStepText, computeError } = useCompute() diff --git a/src/components/organisms/AssetActions/Consume.tsx b/src/components/organisms/AssetActions/Consume.tsx index 275632f23..5a6029f4d 100644 --- a/src/components/organisms/AssetActions/Consume.tsx +++ b/src/components/organisms/AssetActions/Consume.tsx @@ -1,4 +1,4 @@ -import React, { ReactElement } from 'react' +import React, { ReactElement, useEffect } from 'react' import { toast } from 'react-toastify' import { File as FileMetadata, DDO } from '@oceanprotocol/lib' import Button from '../../atoms/Button' @@ -13,11 +13,13 @@ import { useSiteMetadata } from '../../../hooks/useSiteMetadata' export default function Consume({ ddo, file, - isBalanceSufficient + isBalanceSufficient, + dtBalance }: { ddo: DDO file: FileMetadata isBalanceSufficient: boolean + dtBalance: string }): ReactElement { const { ocean } = useOcean() const { marketFeeAddress } = useSiteMetadata() @@ -25,22 +27,20 @@ export default function Consume({ const isDisabled = !ocean || !isBalanceSufficient - if (consumeError) { - toast.error(consumeError) + async function handleConsume() { + await consume(ddo.id, ddo.dataToken, 'access', marketFeeAddress) } + useEffect(() => { + consumeError && toast.error(consumeError) + }, [consumeError]) + const PurchaseButton = () => (
{consumeStepText ? ( ) : ( - )} diff --git a/src/components/organisms/AssetActions/index.tsx b/src/components/organisms/AssetActions/index.tsx index a561aca69..4527acf71 100644 --- a/src/components/organisms/AssetActions/index.tsx +++ b/src/components/organisms/AssetActions/index.tsx @@ -2,7 +2,7 @@ import React, { ReactElement, useState, useEffect } from 'react' import styles from './index.module.css' import Compute from './Compute' import Consume from './Consume' -import { DDO } from '@oceanprotocol/lib' +import { DDO, Logger } from '@oceanprotocol/lib' import Tabs from '../../atoms/Tabs' import { useOcean, useMetadata } from '@oceanprotocol/react' import compareAsBN from '../../../utils/compareAsBN' @@ -10,13 +10,32 @@ import Pool from './Pool' import { AdditionalInformationMarket } from '../../../@types/MetaData' export default function AssetActions({ ddo }: { ddo: DDO }): ReactElement { - const { balance } = useOcean() + const { ocean, balance, accountId } = useOcean() const { price } = useMetadata(ddo) const [isBalanceSufficient, setIsBalanceSufficient] = useState() + const [dtBalance, setDtBalance] = useState() const isCompute = Boolean(ddo.findServiceByType('compute')) const { attributes } = ddo.findServiceByType('metadata') + // Get and set user DT balance + useEffect(() => { + if (!ocean || !accountId) return + + async function init() { + try { + const dtBalance = await ocean.datatokens.balance( + ddo.dataToken, + accountId + ) + setDtBalance(dtBalance) + } catch (e) { + Logger.error(e.message) + } + } + init() + }, [ocean, accountId, ddo.dataToken]) + // Check user balance against price useEffect(() => { if (!price || !price.value || !balance || !balance.ocean) return @@ -29,10 +48,15 @@ export default function AssetActions({ ddo }: { ddo: DDO }): ReactElement { }, [balance, price]) const UseContent = isCompute ? ( - + ) : (