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

fix conditions and cleanups

This commit is contained in:
Bogdan Fazakas 2022-05-16 07:57:48 +03:00
parent 24aa157f92
commit feb1f41d5c
4 changed files with 45 additions and 204 deletions

View File

@ -18,9 +18,8 @@ export function getComputeFeedback(
): { [key in number]: string } {
return {
0: `Setting price and fees for ${assetType}`,
1: `Approving and buying one ${datatokenSymbol} from pool`,
2: `Ordering ${assetType} and transfering datatoken ...`,
3: `Approving ${baseTokenSymbol} and ordering ${assetType}`,
4: 'Generating signature. Starting compute job ...'
1: `Approving ${datatokenSymbol} and ordering ${assetType} `,
2: `Approving ${baseTokenSymbol} and ordering ${assetType}`,
3: 'Generating signature. Starting compute job ...'
}
}

View File

@ -5,7 +5,6 @@ import {
LoggerInstance,
OrderParams,
ProviderComputeInitialize,
ProviderComputeInitializeResults,
ProviderFees,
ProviderInstance
} from '@oceanprotocol/lib'
@ -135,8 +134,6 @@ export async function reuseOrder(
validOrderTx: string,
providerFees?: ProviderFees
): Promise<TransactionReceipt> {
LoggerInstance.log('[compute] reuseOrder', providerFees)
const datatoken = new Datatoken(web3)
const initializeData =
!providerFees &&
@ -147,23 +144,21 @@ export async function reuseOrder(
accountId,
asset.services[0].serviceEndpoint
))
LoggerInstance.log('[compute] initializeData', initializeData)
if (
providerFees?.providerFeeAmount ||
initializeData.providerFee.providerFeeToken
initializeData?.providerFee?.providerFeeAmount
) {
const txApprove = await approve(
web3,
accountId,
providerFees.providerFeeToken ||
initializeData.providerFee.providerFeeToken,
initializeData.providerFee.providerFeeAmount,
asset.accessDetails.datatoken.address,
providerFees.providerFeeAmount,
providerFees.providerFeeAmount ||
initializeData.providerFee.providerFeeToken,
false
)
LoggerInstance.log('[compute] txApprove', txApprove)
if (!txApprove) {
return
}
@ -201,16 +196,15 @@ export async function handleComputeOrder(
initializeData: ProviderComputeInitialize,
computeConsumerAddress?: string
): Promise<string> {
LoggerInstance.log('[compute] handleComputeOrder asset: ', asset)
LoggerInstance.log(
'[compute] handleComputeOrder orderPriceAndFees: ',
orderPriceAndFees
'[compute] Handle compute order for asset type: ',
asset.metadata.type
)
if (initializeData.validOrder && !initializeData.providerFee) {
LoggerInstance.log('[compute] Has valid order: ', initializeData.validOrder)
return initializeData.validOrder
} else if (initializeData.validOrder) {
LoggerInstance.log('[compute] Call reuse order', initializeData)
LoggerInstance.log('[compute] Calling reuseOrder ...', initializeData)
const tx = await reuseOrder(
web3,
asset,
@ -218,12 +212,13 @@ export async function handleComputeOrder(
initializeData.validOrder,
initializeData.providerFee
)
LoggerInstance.log('[compute] Reused order:', tx.transactionHash)
return tx.transactionHash
} else {
LoggerInstance.log('[compute] Call order', initializeData)
LoggerInstance.log('[compute] Calling order ...', initializeData)
if (!hasDatatoken && asset?.accessDetails.type === 'dynamic') {
const poolTx = await buyDtFromPool(asset?.accessDetails, accountId, web3)
LoggerInstance.log('[compute] Buy dt from pool: ', poolTx)
LoggerInstance.log('[compute] Buoght dt from pool: ', poolTx)
if (!poolTx) {
toast.error('Failed to buy datatoken from pool!')
return
@ -237,6 +232,7 @@ export async function handleComputeOrder(
initializeData.providerFee,
computeConsumerAddress
)
LoggerInstance.log('[compute] Asset ordered:', tx.transactionHash)
return tx.transactionHash
}
}

View File

@ -20,13 +20,13 @@ export async function initializeProviderForCompute(
): Promise<ProviderComputeInitializeResults> {
const computeAsset: ComputeAsset = {
documentId: dataset.id,
serviceId: dataset.services[0].id
// transferTxId: dataset.accessDetails.validOrderTx
serviceId: dataset.services[0].id,
transferTxId: dataset.accessDetails.validOrderTx
}
const computeAlgo: ComputeAlgorithm = {
documentId: algorithm.id,
serviceId: algorithm.services[0].id
// transferTxId: algorithm.accessDetails.validOrderTx
serviceId: algorithm.services[0].id,
transferTxId: algorithm.accessDetails.validOrderTx
}
const validUntil = getValidUntilTime(

View File

@ -35,13 +35,12 @@ import AlgorithmDatasetsListForCompute from './AlgorithmDatasetsListForCompute'
import AssetActionHistoryTable from '../AssetActionHistoryTable'
import ComputeJobs from '../../../Profile/History/ComputeJobs'
import { useCancelToken } from '@hooks/useCancelToken'
import { useIsMounted } from '@hooks/useIsMounted'
// import { useIsMounted } from '@hooks/useIsMounted'
import { Decimal } from 'decimal.js'
import { useAbortController } from '@hooks/useAbortController'
import { getOrderPriceAndFees } from '@utils/accessDetailsAndPricing'
import { OrderPriceAndFees } from 'src/@types/Price'
import { buyDtFromPool } from '@utils/pool'
import { handleComputeOrder, order, reuseOrder } from '@utils/order'
import { handleComputeOrder } from '@utils/order'
import { AssetExtended } from 'src/@types/AssetExtended'
import { getComputeFeedback } from '@utils/feedback'
import { usePool } from '@context/Pool'
@ -78,9 +77,9 @@ export default function Compute({
const [isPublished, setIsPublished] = useState(false)
const [algorithmDTBalance, setAlgorithmDTBalance] = useState<string>()
const [isOwned, setIsOwned] = useState(false)
// const [isOwned, setIsOwned] = useState(false)
const [validOrderTx, setValidOrderTx] = useState('')
const [isAlgorithmOwned, setIsAlgorithmOwned] = useState(false)
// const [isAlgorithmOwned, setIsAlgorithmOwned] = useState(false)
const [validAlgorithmOrderTx, setValidAlgorithmOrderTx] = useState('')
const hasDatatoken = Number(dtBalance) >= 1
@ -135,33 +134,10 @@ export default function Compute({
)
console.log('initializedProvider == ', initializedProvider)
setInitializedProviderResponse(initializedProvider)
// setIsProviderFeeValid(
// await checkComputeResourcesValidity(
// asset,
// accountId,
// computeEnv?.maxJobDuration,
// asset.services[0].timeout,
// selectedAlgorithmAsset.services[0].timeout
// )
// )
// let datasetOrderTx = await checkComputeResourcesValidity(
// dataset,
// accountId,
// computeEnv?.maxJobDuration,
// asset.services[0].timeout,
// selectedAlgorithmAsset.services[0].timeout
// )
// const validUntil = getValidUntilTime(
// computeEnv?.maxJobDuration,
// asset.services[0].timeout,
// selectedAlgorithmAsset.services[0].timeout
// )
// setComputeValidUntil(validUntil)
if (
asset?.accessDetails?.addressOrId !== ZERO_ADDRESS ||
asset?.accessDetails?.type !== 'free' ||
(!initializedProvider.datasets[0].validOrder &&
initializedProvider.datasets[0].providerFee)
asset?.accessDetails?.addressOrId !== ZERO_ADDRESS &&
asset?.accessDetails?.type !== 'free' &&
initializedProvider.datasets[0].providerFee
) {
setIsRequestingDataseOrderPrice(true)
setComputeStatusText(
@ -204,10 +180,9 @@ export default function Compute({
}
if (
selectedAlgorithmAsset?.accessDetails?.addressOrId !== ZERO_ADDRESS ||
selectedAlgorithmAsset?.accessDetails?.type !== 'free' ||
(!initializedProvider.algorithm.validOrder &&
initializedProvider.algorithm.providerFee)
selectedAlgorithmAsset?.accessDetails?.addressOrId !== ZERO_ADDRESS &&
selectedAlgorithmAsset?.accessDetails?.type !== 'free' &&
initializedProvider.algorithm.providerFee
) {
setIsRequestingAlgoOrderPrice(true)
setComputeStatusText(
@ -260,7 +235,7 @@ export default function Compute({
if (!asset?.accessDetails || !accountId) return
setIsConsumablePrice(asset?.accessDetails?.isPurchasable)
setIsOwned(asset?.accessDetails?.isOwned)
// setIsOwned(asset?.accessDetails?.isOwned)
setValidOrderTx(asset?.accessDetails?.validOrderTx)
}, [asset?.accessDetails])
@ -268,7 +243,7 @@ export default function Compute({
if (!selectedAlgorithmAsset?.accessDetails || !accountId) return
setIsConsumablePrice(selectedAlgorithmAsset?.accessDetails?.isPurchasable)
setIsAlgorithmOwned(selectedAlgorithmAsset?.accessDetails?.isOwned)
// setIsAlgorithmOwned(selectedAlgorithmAsset?.accessDetails?.isOwned)
setValidAlgorithmOrderTx(
selectedAlgorithmAsset?.accessDetails?.validOrderTx
)
@ -329,6 +304,13 @@ export default function Compute({
return
}
setComputeStatusText(
getComputeFeedback(
asset.accessDetails.baseToken?.symbol,
asset.accessDetails.datatoken?.symbol,
asset.metadata.type
)[asset.accessDetails?.type === 'fixed' ? 2 : 1]
)
const datasetOrderTx = await handleComputeOrder(
web3,
asset,
@ -338,65 +320,14 @@ export default function Compute({
initializedProviderResponse.datasets[0],
computeEnv.consumerAddress
)
console.log('datasetOrderTx', datasetOrderTx)
// if (isOwned) {
// datasetOrderTx = validOrderTx
// LoggerInstance.log('[compute] Dataset owned txId:', validOrderTx)
// } else {
// try {
// if (!hasDatatoken && asset?.accessDetails.type === 'dynamic') {
// setComputeStatusText(
// getComputeFeedback(
// asset.accessDetails.baseToken?.symbol,
// asset.accessDetails.datatoken?.symbol,
// asset.metadata.type
// )[1]
// )
// const tx = await buyDtFromPool(
// asset?.accessDetails,
// accountId,
// web3
// )
// LoggerInstance.log('[compute] Buy dataset dt from pool: ', tx)
// if (!tx) {
// toast.error('Failed to buy datatoken from pool!')
// return
// }
// }
// LoggerInstance.log(
// 'dataset orderPriceAndFees: ',
// datasetOrderPriceAndFees
// )
// setComputeStatusText(
// getComputeFeedback(
// asset.accessDetails.baseToken?.symbol,
// asset.accessDetails.datatoken?.symbol,
// asset.metadata.type
// )[asset.accessDetails?.type === 'fixed' ? 3 : 2]
// )
// const orderTx = await order(
// web3,
// asset,
// datasetOrderPriceAndFees,
// accountId, )
// if (!orderTx) {
// toast.error('Failed to order dataset asset!')
// return
// }
// LoggerInstance.log(
// '[compute] Order dataset: ',
// orderTx.transactionHash
// )
// setIsOwned(true)
// setValidOrderTx(orderTx.transactionHash)
// datasetOrderTx = orderTx.transactionHash
// } catch (e) {
// LoggerInstance.log(e.message)
// toast.error('Failed to order dataset asset!')
// return
// }
// }
setComputeStatusText(
getComputeFeedback(
asset.accessDetails.baseToken?.symbol,
asset.accessDetails.datatoken?.symbol,
asset.metadata.type
)[asset.accessDetails?.type === 'fixed' ? 2 : 1]
)
const algorithmOrderTx = await handleComputeOrder(
web3,
selectedAlgorithmAsset,
@ -406,91 +337,6 @@ export default function Compute({
initializedProviderResponse.algorithm,
computeEnv.consumerAddress
)
console.log('algorithmOrderTx', algorithmOrderTx)
// if (isAlgorithmOwned) {
// algorithmOrderTx = validAlgorithmOrderTx
// LoggerInstance.log(
// '[compute] Algorithm owned txId:',
// validAlgorithmOrderTx
// )
// } else {
// try {
// if (
// !hasAlgoAssetDatatoken &&
// selectedAlgorithmAsset?.accessDetails?.type === 'dynamic'
// ) {
// setComputeStatusText(
// getComputeFeedback(
// selectedAlgorithmAsset.accessDetails.baseToken?.symbol,
// selectedAlgorithmAsset.accessDetails.datatoken?.symbol,
// selectedAlgorithmAsset.metadata.type
// )[1]
// )
// const tx = await buyDtFromPool(
// selectedAlgorithmAsset?.accessDetails,
// accountId,
// web3
// )
// LoggerInstance.log('[compute] Buy algorithm dt from pool: ', tx)
// if (!tx) {
// toast.error('Failed to buy datatoken from pool!')
// return
// }
// }
// setComputeStatusText(
// getComputeFeedback(
// selectedAlgorithmAsset.accessDetails.baseToken?.symbol,
// selectedAlgorithmAsset.accessDetails.datatoken?.symbol,
// selectedAlgorithmAsset.metadata.type
// )[selectedAlgorithmAsset.accessDetails?.type === 'fixed' ? 3 : 2]
// )
// const orderTx = await order(
// web3,
// selectedAlgorithmAsset,
// algoOrderPriceAndFees,
// accountId,
// computeEnv?.id,
// computeValidUntil,
// computeEnv?.consumerAddress
// )
// if (!orderTx) {
// toast.error('Failed to order algorithm asset!')
// return
// }
// LoggerInstance.log(
// '[compute] Order algorithm: ',
// orderTx.transactionHash
// )
// setIsAlgorithmOwned(true)
// setValidAlgorithmOrderTx(orderTx.transactionHash)
// algorithmOrderTx = orderTx.transactionHash
// } catch (e) {
// LoggerInstance.log(e.message)
// toast.error('Failed to order algorithm asset!')
// return
// }
// }
// if (isOwned && !isProviderFeeValid) {
// const reuseOrderTx = await reuseOrder(
// web3,
// asset,
// accountId,
// validOrderTx,
// computeEnv?.id,
// computeValidUntil
// )
// if (!reuseOrderTx) {
// toast.error('Failed to pay provider fees!')
// return
// }
// LoggerInstance.log(
// '[compute] Reused order: ',
// reuseOrderTx.transactionHash
// )
// datasetOrderTx = reuseOrderTx.transactionHash
// }
LoggerInstance.log('[compute] Starting compute job.')
const computeAsset: ComputeAsset = {
documentId: asset.id,
@ -502,7 +348,7 @@ export default function Compute({
publishAlgorithmLog: true,
publishOutput: true
}
setComputeStatusText(getComputeFeedback()[4])
setComputeStatusText(getComputeFeedback()[3])
const response = await ProviderInstance.computeStart(
asset.services[0].serviceEndpoint,
web3,