mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
optimize c2d order logic and fix compute output price tooltip (#1896)
* set price updates and tooltip fix * update order * cleanup
This commit is contained in:
parent
8fd3eaf8de
commit
fe7dd8e312
@ -22,7 +22,6 @@ import {
|
|||||||
consumeMarketFixedSwapFee
|
consumeMarketFixedSwapFee
|
||||||
} from '../../app.config'
|
} from '../../app.config'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { getEncryptedFiles, getFileInfo } from './provider'
|
|
||||||
|
|
||||||
async function initializeProvider(
|
async function initializeProvider(
|
||||||
asset: AssetExtended,
|
asset: AssetExtended,
|
||||||
@ -64,12 +63,6 @@ export async function order(
|
|||||||
const datatoken = new Datatoken(web3)
|
const datatoken = new Datatoken(web3)
|
||||||
const config = getOceanConfig(asset.chainId)
|
const config = getOceanConfig(asset.chainId)
|
||||||
|
|
||||||
const filesEncrypted = await getEncryptedFiles(
|
|
||||||
asset.services[0].files,
|
|
||||||
asset.chainId,
|
|
||||||
asset.services[0].serviceEndpoint
|
|
||||||
)
|
|
||||||
|
|
||||||
const initializeData = await initializeProvider(
|
const initializeData = await initializeProvider(
|
||||||
asset,
|
asset,
|
||||||
accountId,
|
accountId,
|
||||||
@ -251,27 +244,6 @@ async function approveProviderFee(
|
|||||||
return txApproveWei
|
return txApproveWei
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startOrder(
|
|
||||||
web3: Web3,
|
|
||||||
asset: AssetExtended,
|
|
||||||
orderPriceAndFees: OrderPriceAndFees,
|
|
||||||
accountId: string,
|
|
||||||
hasDatatoken: boolean,
|
|
||||||
initializeData: ProviderComputeInitialize,
|
|
||||||
computeConsumerAddress?: string
|
|
||||||
): Promise<TransactionReceipt> {
|
|
||||||
const tx = await order(
|
|
||||||
web3,
|
|
||||||
asset,
|
|
||||||
orderPriceAndFees,
|
|
||||||
accountId,
|
|
||||||
initializeData.providerFee,
|
|
||||||
computeConsumerAddress
|
|
||||||
)
|
|
||||||
LoggerInstance.log('[compute] Asset ordered:', tx)
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles order for compute assets for the following scenarios:
|
* Handles order for compute assets for the following scenarios:
|
||||||
* - have validOrder and no providerFees -> then order is valid, providerFees are valid, it returns the valid order value
|
* - have validOrder and no providerFees -> then order is valid, providerFees are valid, it returns the valid order value
|
||||||
@ -291,7 +263,6 @@ export async function handleComputeOrder(
|
|||||||
asset: AssetExtended,
|
asset: AssetExtended,
|
||||||
orderPriceAndFees: OrderPriceAndFees,
|
orderPriceAndFees: OrderPriceAndFees,
|
||||||
accountId: string,
|
accountId: string,
|
||||||
hasDatatoken: boolean,
|
|
||||||
initializeData: ProviderComputeInitialize,
|
initializeData: ProviderComputeInitialize,
|
||||||
computeConsumerAddress?: string
|
computeConsumerAddress?: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
@ -342,15 +313,16 @@ export async function handleComputeOrder(
|
|||||||
}
|
}
|
||||||
|
|
||||||
LoggerInstance.log('[compute] Calling order ...', initializeData)
|
LoggerInstance.log('[compute] Calling order ...', initializeData)
|
||||||
const txStartOrder = await startOrder(
|
|
||||||
|
const txStartOrder = await order(
|
||||||
web3,
|
web3,
|
||||||
asset,
|
asset,
|
||||||
orderPriceAndFees,
|
orderPriceAndFees,
|
||||||
accountId,
|
accountId,
|
||||||
hasDatatoken,
|
initializeData.providerFee,
|
||||||
initializeData,
|
|
||||||
computeConsumerAddress
|
computeConsumerAddress
|
||||||
)
|
)
|
||||||
|
|
||||||
LoggerInstance.log('[compute] Order succeeded', txStartOrder)
|
LoggerInstance.log('[compute] Order succeeded', txStartOrder)
|
||||||
return txStartOrder?.transactionHash
|
return txStartOrder?.transactionHash
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -13,7 +13,6 @@ import {
|
|||||||
ProviderInstance,
|
ProviderInstance,
|
||||||
UrlFile
|
UrlFile
|
||||||
} from '@oceanprotocol/lib'
|
} from '@oceanprotocol/lib'
|
||||||
import { chainIds } from 'app.config'
|
|
||||||
import { QueryHeader } from '@shared/FormInput/InputElement/Headers'
|
import { QueryHeader } from '@shared/FormInput/InputElement/Headers'
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import { AbiItem } from 'web3-utils/types'
|
import { AbiItem } from 'web3-utils/types'
|
||||||
|
@ -42,21 +42,13 @@ function Row({
|
|||||||
sign?: string
|
sign?: string
|
||||||
type?: string
|
type?: string
|
||||||
}) {
|
}) {
|
||||||
const { isSupportedOceanNetwork } = useWeb3()
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.priceRow}>
|
<div className={styles.priceRow}>
|
||||||
<div className={styles.sign}>{sign}</div>
|
<div className={styles.sign}>{sign}</div>
|
||||||
<div className={styles.type}>{type}</div>
|
<div className={styles.type}>{type}</div>
|
||||||
<div>
|
<div>
|
||||||
<PriceUnit
|
<PriceUnit
|
||||||
price={
|
price={hasPreviousOrder || hasDatatoken ? 0 : Number(price)}
|
||||||
!isSupportedOceanNetwork
|
|
||||||
? hasPreviousOrder || hasDatatoken
|
|
||||||
? 0
|
|
||||||
: Number(price)
|
|
||||||
: Number(price)
|
|
||||||
}
|
|
||||||
symbol={symbol}
|
symbol={symbol}
|
||||||
size="small"
|
size="small"
|
||||||
className={styles.price}
|
className={styles.price}
|
||||||
|
@ -13,7 +13,8 @@ import {
|
|||||||
ComputeOutput,
|
ComputeOutput,
|
||||||
ProviderComputeInitializeResults,
|
ProviderComputeInitializeResults,
|
||||||
unitsToAmount,
|
unitsToAmount,
|
||||||
minAbi
|
minAbi,
|
||||||
|
ProviderFees
|
||||||
} from '@oceanprotocol/lib'
|
} from '@oceanprotocol/lib'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import Price from '@shared/Price'
|
import Price from '@shared/Price'
|
||||||
@ -129,6 +130,82 @@ export default function Compute({
|
|||||||
return hasAlgoDt
|
return hasAlgoDt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setComputeFees(
|
||||||
|
providerData: ProviderComputeInitializeResults
|
||||||
|
): Promise<ProviderComputeInitializeResults> {
|
||||||
|
if (asset.accessDetails.validProviderFees) {
|
||||||
|
providerData.datasets[0].providerFee.providerFeeAmount = '0'
|
||||||
|
}
|
||||||
|
|
||||||
|
const providerFeeToken =
|
||||||
|
providerData?.datasets?.[0]?.providerFee?.providerFeeToken
|
||||||
|
const providerFeeAmount = asset.accessDetails.validProviderFees
|
||||||
|
? '0'
|
||||||
|
: providerData?.datasets?.[0]?.providerFee?.providerFeeAmount
|
||||||
|
const feeValidity = providerData?.datasets?.[0]?.providerFee?.validUntil
|
||||||
|
|
||||||
|
const feeAmount = await unitsToAmount(
|
||||||
|
!isSupportedOceanNetwork || !isAssetNetwork
|
||||||
|
? await getDummyWeb3(asset?.chainId)
|
||||||
|
: web3,
|
||||||
|
providerFeeToken,
|
||||||
|
providerFeeAmount
|
||||||
|
)
|
||||||
|
setProviderFeeAmount(feeAmount)
|
||||||
|
|
||||||
|
const datatoken = new Datatoken(
|
||||||
|
await getDummyWeb3(asset?.chainId),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
minAbi
|
||||||
|
)
|
||||||
|
|
||||||
|
setProviderFeesSymbol(await datatoken.getSymbol(providerFeeToken))
|
||||||
|
|
||||||
|
const computeDuration = asset.accessDetails.validProviderFees
|
||||||
|
? asset.accessDetails.validProviderFees.validUntil
|
||||||
|
: (parseInt(feeValidity) - Math.floor(Date.now() / 1000)).toString()
|
||||||
|
setComputeValidUntil(computeDuration)
|
||||||
|
|
||||||
|
return providerData
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setAlgoPrice(algoProviderFees: ProviderFees) {
|
||||||
|
if (
|
||||||
|
selectedAlgorithmAsset?.accessDetails?.addressOrId !== ZERO_ADDRESS &&
|
||||||
|
selectedAlgorithmAsset?.accessDetails?.type !== 'free' &&
|
||||||
|
algoProviderFees
|
||||||
|
) {
|
||||||
|
const algorithmOrderPriceAndFees = await getOrderPriceAndFees(
|
||||||
|
selectedAlgorithmAsset,
|
||||||
|
ZERO_ADDRESS,
|
||||||
|
algoProviderFees
|
||||||
|
)
|
||||||
|
if (!algorithmOrderPriceAndFees)
|
||||||
|
throw new Error('Error setting algorithm price and fees!')
|
||||||
|
|
||||||
|
setAlgoOrderPriceAndFees(algorithmOrderPriceAndFees)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setDatasetPrice(datasetProviderFees: ProviderFees) {
|
||||||
|
if (
|
||||||
|
asset?.accessDetails?.addressOrId !== ZERO_ADDRESS &&
|
||||||
|
asset?.accessDetails?.type !== 'free' &&
|
||||||
|
datasetProviderFees
|
||||||
|
) {
|
||||||
|
const datasetPriceAndFees = await getOrderPriceAndFees(
|
||||||
|
asset,
|
||||||
|
ZERO_ADDRESS,
|
||||||
|
datasetProviderFees
|
||||||
|
)
|
||||||
|
if (!datasetPriceAndFees)
|
||||||
|
throw new Error('Error setting dataset price and fees!')
|
||||||
|
|
||||||
|
setDatasetOrderPriceAndFees(datasetPriceAndFees)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function initPriceAndFees() {
|
async function initPriceAndFees() {
|
||||||
try {
|
try {
|
||||||
const computeEnv = await getComputeEnviroment(asset)
|
const computeEnv = await getComputeEnviroment(asset)
|
||||||
@ -150,101 +227,25 @@ export default function Compute({
|
|||||||
)
|
)
|
||||||
throw new Error(`Error initializing provider for the compute job!`)
|
throw new Error(`Error initializing provider for the compute job!`)
|
||||||
|
|
||||||
setInitializedProviderResponse(initializedProvider)
|
setComputeStatusText(
|
||||||
|
getComputeFeedback(
|
||||||
if (
|
asset.accessDetails?.baseToken?.symbol,
|
||||||
asset.accessDetails?.validProviderFees &&
|
asset.accessDetails?.datatoken?.symbol,
|
||||||
initializedProvider?.datasets?.[0]?.providerFee?.providerFeeAmount
|
asset.metadata.type
|
||||||
) {
|
)[0]
|
||||||
initializedProvider.datasets[0].providerFee = {
|
)
|
||||||
providerFeeAmount: '0',
|
await setDatasetPrice(initializedProvider?.datasets?.[0]?.providerFee)
|
||||||
...asset.accessDetails?.validProviderFees
|
setComputeStatusText(
|
||||||
}
|
getComputeFeedback(
|
||||||
}
|
selectedAlgorithmAsset?.accessDetails?.baseToken?.symbol,
|
||||||
|
selectedAlgorithmAsset?.accessDetails?.datatoken?.symbol,
|
||||||
if (
|
selectedAlgorithmAsset?.metadata?.type
|
||||||
selectedAlgorithmAsset?.accessDetails?.validProviderFees &&
|
)[0]
|
||||||
initializedProvider?.algorithm?.providerFee?.providerFeeAmount
|
|
||||||
) {
|
|
||||||
initializedProvider.algorithm.providerFee = {
|
|
||||||
providerFeeAmount: '0',
|
|
||||||
...selectedAlgorithmAsset.accessDetails.validProviderFees
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const feeAmount = await unitsToAmount(
|
|
||||||
!isSupportedOceanNetwork || !isAssetNetwork
|
|
||||||
? await getDummyWeb3(asset?.chainId)
|
|
||||||
: web3,
|
|
||||||
initializedProvider?.datasets?.[0]?.providerFee?.providerFeeToken,
|
|
||||||
initializedProvider?.datasets?.[0]?.providerFee?.providerFeeAmount
|
|
||||||
)
|
)
|
||||||
|
|
||||||
setProviderFeeAmount(feeAmount)
|
await setAlgoPrice(initializedProvider?.algorithm?.providerFee)
|
||||||
|
const sanitizedResponse = await setComputeFees(initializedProvider)
|
||||||
const datatoken = new Datatoken(
|
setInitializedProviderResponse(sanitizedResponse)
|
||||||
await getDummyWeb3(asset?.chainId),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
minAbi
|
|
||||||
)
|
|
||||||
setProviderFeesSymbol(
|
|
||||||
await datatoken.getSymbol(
|
|
||||||
initializedProvider?.datasets?.[0]?.providerFee?.providerFeeToken
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
const computeDuration = (
|
|
||||||
parseInt(initializedProvider?.datasets?.[0]?.providerFee?.validUntil) -
|
|
||||||
Math.floor(Date.now() / 1000)
|
|
||||||
).toString()
|
|
||||||
setComputeValidUntil(computeDuration)
|
|
||||||
|
|
||||||
if (
|
|
||||||
asset?.accessDetails?.addressOrId !== ZERO_ADDRESS &&
|
|
||||||
asset?.accessDetails?.type !== 'free' &&
|
|
||||||
initializedProvider?.datasets?.[0]?.providerFee
|
|
||||||
) {
|
|
||||||
setComputeStatusText(
|
|
||||||
getComputeFeedback(
|
|
||||||
asset.accessDetails?.baseToken?.symbol,
|
|
||||||
asset.accessDetails?.datatoken?.symbol,
|
|
||||||
asset.metadata.type
|
|
||||||
)[0]
|
|
||||||
)
|
|
||||||
const datasetPriceAndFees = await getOrderPriceAndFees(
|
|
||||||
asset,
|
|
||||||
ZERO_ADDRESS,
|
|
||||||
initializedProvider?.datasets?.[0]?.providerFee
|
|
||||||
)
|
|
||||||
if (!datasetPriceAndFees)
|
|
||||||
throw new Error('Error setting dataset price and fees!')
|
|
||||||
|
|
||||||
setDatasetOrderPriceAndFees(datasetPriceAndFees)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
selectedAlgorithmAsset?.accessDetails?.addressOrId !== ZERO_ADDRESS &&
|
|
||||||
selectedAlgorithmAsset?.accessDetails?.type !== 'free' &&
|
|
||||||
initializedProvider?.algorithm?.providerFee
|
|
||||||
) {
|
|
||||||
setComputeStatusText(
|
|
||||||
getComputeFeedback(
|
|
||||||
selectedAlgorithmAsset?.accessDetails?.baseToken?.symbol,
|
|
||||||
selectedAlgorithmAsset?.accessDetails?.datatoken?.symbol,
|
|
||||||
selectedAlgorithmAsset?.metadata?.type
|
|
||||||
)[0]
|
|
||||||
)
|
|
||||||
const algorithmOrderPriceAndFees = await getOrderPriceAndFees(
|
|
||||||
selectedAlgorithmAsset,
|
|
||||||
ZERO_ADDRESS,
|
|
||||||
initializedProvider.algorithm.providerFee
|
|
||||||
)
|
|
||||||
if (!algorithmOrderPriceAndFees)
|
|
||||||
throw new Error('Error setting algorithm price and fees!')
|
|
||||||
|
|
||||||
setAlgoOrderPriceAndFees(algorithmOrderPriceAndFees)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(error.message)
|
setError(error.message)
|
||||||
LoggerInstance.error(`[compute] ${error.message} `)
|
LoggerInstance.error(`[compute] ${error.message} `)
|
||||||
@ -376,7 +377,6 @@ export default function Compute({
|
|||||||
selectedAlgorithmAsset,
|
selectedAlgorithmAsset,
|
||||||
algoOrderPriceAndFees,
|
algoOrderPriceAndFees,
|
||||||
accountId,
|
accountId,
|
||||||
hasAlgoAssetDatatoken,
|
|
||||||
initializedProviderResponse.algorithm,
|
initializedProviderResponse.algorithm,
|
||||||
computeEnv.consumerAddress
|
computeEnv.consumerAddress
|
||||||
)
|
)
|
||||||
@ -395,7 +395,6 @@ export default function Compute({
|
|||||||
asset,
|
asset,
|
||||||
datasetOrderPriceAndFees,
|
datasetOrderPriceAndFees,
|
||||||
accountId,
|
accountId,
|
||||||
hasDatatoken,
|
|
||||||
initializedProviderResponse.datasets[0],
|
initializedProviderResponse.datasets[0],
|
||||||
computeEnv.consumerAddress
|
computeEnv.consumerAddress
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user