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

check order from the graph

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2021-03-04 15:53:34 +02:00
parent 5c4c13452f
commit a261c35ec7
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29
2 changed files with 45 additions and 13 deletions

View File

@ -40,7 +40,6 @@ export default function AssetListTitle({
!ddo && did && getAssetName() !ddo && did && getAssetName()
return () => { return () => {
console.log('canceled?')
source.cancel() source.cancel()
} }
}, [assetTitle, config?.metadataCacheUri, ddo, did, title]) }, [assetTitle, config?.metadataCacheUri, ddo, did, title])

View File

@ -12,6 +12,23 @@ import { useSiteMetadata } from '../../../hooks/useSiteMetadata'
import checkPreviousOrder from '../../../utils/checkPreviousOrder' import checkPreviousOrder from '../../../utils/checkPreviousOrder'
import { useAsset } from '../../../providers/Asset' import { useAsset } from '../../../providers/Asset'
import { secondsToString } from '../../../utils/metadata' import { secondsToString } from '../../../utils/metadata'
import { gql, useQuery } from '@apollo/client'
import { OrdersData } from '../../../@types/apollo/OrdersData'
import BigNumber from 'bignumber.js'
const previousOrderQuery = gql`
query PreviousOrder($id: String!, $account: String!) {
tokenOrders(
first: 1
where: { datatokenId: $id, payer: $account }
orderBy: timestamp
orderDirection: desc
) {
timestamp
tx
}
}
`
function getHelpText( function getHelpText(
token: { token: {
@ -58,6 +75,34 @@ export default function Consume({
const [isConsumable, setIsConsumable] = useState(true) const [isConsumable, setIsConsumable] = useState(true)
const [assetTimeout, setAssetTimeout] = useState('') const [assetTimeout, setAssetTimeout] = useState('')
const { data } = useQuery<OrdersData>(previousOrderQuery, {
variables: {
id: ddo.dataToken?.toLowerCase(),
account: accountId?.toLowerCase()
},
pollInterval: 5000
})
useEffect(() => {
if (!data || !assetTimeout || data.tokenOrders.length === 0) return
const lastOrder = data.tokenOrders[0]
if (assetTimeout === 'Forever') {
setPreviousOrderId(lastOrder.tx)
setHasPreviousOrder(true)
} else {
const expiry = new BigNumber(lastOrder.timestamp).plus(assetTimeout)
const unixTime = new BigNumber(Math.floor(Date.now() / 1000))
if (unixTime.isLessThan(expiry)) {
setPreviousOrderId(lastOrder.tx)
setHasPreviousOrder(true)
} else {
setHasPreviousOrder(false)
}
}
}, [data, assetTimeout])
useEffect(() => { useEffect(() => {
const { timeout } = ddo.findServiceByType('access').attributes.main const { timeout } = ddo.findServiceByType('access').attributes.main
setAssetTimeout(secondsToString(timeout)) setAssetTimeout(secondsToString(timeout))
@ -95,18 +140,6 @@ export default function Consume({
hasDatatoken hasDatatoken
]) ])
useEffect(() => {
if (!ocean || !accountId) return
async function checkOrders() {
// HEADS UP! checkPreviousOrder() also checks for expiration of possible set timeout.
const orderId = await checkPreviousOrder(ocean, accountId, ddo, 'access')
setPreviousOrderId(orderId)
setHasPreviousOrder(!!orderId)
}
checkOrders()
}, [ocean, ddo, accountId])
async function handleConsume() { async function handleConsume() {
!hasPreviousOrder && !hasDatatoken && (await buyDT('1')) !hasPreviousOrder && !hasDatatoken && (await buyDT('1'))
await consume( await consume(