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

checking previous orders in consume and compute

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2020-10-23 12:57:12 +03:00
parent 21257bba13
commit f085ecd2ef
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29
2 changed files with 42 additions and 25 deletions

View File

@ -1,4 +1,4 @@
import React, { useState, ReactElement, ChangeEvent } from 'react'
import React, { useState, ReactElement, ChangeEvent, useEffect } from 'react'
import { DDO, Logger } from '@oceanprotocol/lib'
import Loader from '../../atoms/Loader'
import Web3Feedback from '../../molecules/Wallet/Feedback'
@ -29,7 +29,7 @@ export default function Compute({
}): ReactElement {
const { marketFeeAddress } = useSiteMetadata()
const { ocean, account } = useOcean()
const { ocean, accountId } = useOcean()
const { compute, isLoading, computeStepText, computeError } = useCompute()
const { buyDT, dtSymbol } = usePricing(ddo)
@ -45,6 +45,7 @@ export default function Compute({
const [algorithmRawCode, setAlgorithmRawCode] = useState('')
const [isPublished, setIsPublished] = useState(false)
const [file, setFile] = useState(null)
const [hasPreviousOrder, setHasPreviousOrder] = useState(false)
const isComputeButtonDisabled =
isJobStarting === true ||
@ -54,14 +55,22 @@ export default function Compute({
!isBalanceSufficient
const hasDatatoken = Number(dtBalance) >= 1
const previousOrder = ocean.datatokens.getPreviousValidOrders(
ddo.dataToken,
computeService.attributes.main.cost,
computeService.index,
computeService.attributes.main.timeout,
account.getId()
)
const hasPreviousOrder = !!previousOrder
useEffect(() => {
async function checkPreviousOrders() {
const service = ddo.findServiceByType('access')
const previousOrder = await ocean.datatokens.getPreviousValidOrders(
ddo.dataToken,
service.attributes.main.cost,
service.index,
service.attributes.main.timeout,
accountId
)
console.log('prev ord', previousOrder, !!previousOrder)
setHasPreviousOrder(!!previousOrder)
}
checkPreviousOrders()
}, [ddo, accountId])
const onDrop = async (files: File[]) => {
setFile(files[0])
const fileText = await readFileContent(files[0])
@ -85,7 +94,7 @@ export default function Compute({
setIsPublished(false)
setError('')
!hasDatatoken && (await buyDT('1'))
!hasPreviousOrder && !hasDatatoken && (await buyDT('1'))
await compute(
ddo.id,
@ -96,6 +105,7 @@ export default function Compute({
marketFeeAddress
)
setHasPreviousOrder(true)
setIsPublished(true)
setFile(null)
} catch (error) {
@ -114,7 +124,7 @@ export default function Compute({
</div>
<div className={styles.pricewrapper}>
<Price ddo={ddo} conversion />
{hasDatatoken && (
{hasPreviousOrder && hasDatatoken && (
<div className={styles.hasTokens}>
You own {dtBalance} {dtSymbol} allowing you to use this data set
without paying again.

View File

@ -1,4 +1,4 @@
import React, { ReactElement, useEffect } from 'react'
import React, { ReactElement, useEffect, useState } from 'react'
import { toast } from 'react-toastify'
import { File as FileMetadata, DDO } from '@oceanprotocol/lib'
import Button from '../../atoms/Button'
@ -21,8 +21,9 @@ export default function Consume({
isBalanceSufficient: boolean
dtBalance: string
}): ReactElement {
const { ocean, account } = useOcean()
const { ocean, accountId } = useOcean()
const { marketFeeAddress } = useSiteMetadata()
const [hasPreviousOrder, setHasPreviousOrder] = useState(false)
const {
dtSymbol,
buyDT,
@ -31,7 +32,6 @@ export default function Consume({
pricingIsLoading
} = usePricing(ddo)
const { consumeStepText, consume, consumeError } = useConsume()
const isDisabled =
!ocean ||
!isBalanceSufficient ||
@ -39,19 +39,26 @@ export default function Consume({
pricingIsLoading
const hasDatatoken = Number(dtBalance) >= 1
const service = ddo.findServiceByType('access')
const previousOrder = ocean.datatokens.getPreviousValidOrders(
ddo.dataToken,
service.attributes.main.cost,
service.index,
service.attributes.main.timeout,
account.getId()
)
const hasPreviousOrder = !!previousOrder
useEffect(() => {
async function checkPreviousOrders() {
const service = ddo.findServiceByType('access')
const previousOrder = await ocean.datatokens.getPreviousValidOrders(
ddo.dataToken,
service.attributes.main.cost,
service.index,
service.attributes.main.timeout,
accountId
)
console.log('prev ord', previousOrder, !!previousOrder)
setHasPreviousOrder(!!previousOrder)
}
checkPreviousOrders()
}, [ddo, accountId])
async function handleConsume() {
!hasDatatoken && (await buyDT('1'))
!hasPreviousOrder && !hasDatatoken && (await buyDT('1'))
await consume(ddo.id, ddo.dataToken, 'access', marketFeeAddress)
setHasPreviousOrder(true)
}
// Output errors in UI