1
0
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:
Bogdan Fazakas 2023-04-05 17:11:47 +03:00 committed by GitHub
parent 8fd3eaf8de
commit fe7dd8e312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 138 deletions

View File

@ -22,7 +22,6 @@ import {
consumeMarketFixedSwapFee
} from '../../app.config'
import { toast } from 'react-toastify'
import { getEncryptedFiles, getFileInfo } from './provider'
async function initializeProvider(
asset: AssetExtended,
@ -64,12 +63,6 @@ export async function order(
const datatoken = new Datatoken(web3)
const config = getOceanConfig(asset.chainId)
const filesEncrypted = await getEncryptedFiles(
asset.services[0].files,
asset.chainId,
asset.services[0].serviceEndpoint
)
const initializeData = await initializeProvider(
asset,
accountId,
@ -251,27 +244,6 @@ async function approveProviderFee(
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:
* - 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,
orderPriceAndFees: OrderPriceAndFees,
accountId: string,
hasDatatoken: boolean,
initializeData: ProviderComputeInitialize,
computeConsumerAddress?: string
): Promise<string> {
@ -342,15 +313,16 @@ export async function handleComputeOrder(
}
LoggerInstance.log('[compute] Calling order ...', initializeData)
const txStartOrder = await startOrder(
const txStartOrder = await order(
web3,
asset,
orderPriceAndFees,
accountId,
hasDatatoken,
initializeData,
initializeData.providerFee,
computeConsumerAddress
)
LoggerInstance.log('[compute] Order succeeded', txStartOrder)
return txStartOrder?.transactionHash
} catch (error) {

View File

@ -13,7 +13,6 @@ import {
ProviderInstance,
UrlFile
} from '@oceanprotocol/lib'
import { chainIds } from 'app.config'
import { QueryHeader } from '@shared/FormInput/InputElement/Headers'
import Web3 from 'web3'
import { AbiItem } from 'web3-utils/types'

View File

@ -42,21 +42,13 @@ function Row({
sign?: string
type?: string
}) {
const { isSupportedOceanNetwork } = useWeb3()
return (
<div className={styles.priceRow}>
<div className={styles.sign}>{sign}</div>
<div className={styles.type}>{type}</div>
<div>
<PriceUnit
price={
!isSupportedOceanNetwork
? hasPreviousOrder || hasDatatoken
? 0
: Number(price)
: Number(price)
}
price={hasPreviousOrder || hasDatatoken ? 0 : Number(price)}
symbol={symbol}
size="small"
className={styles.price}

View File

@ -13,7 +13,8 @@ import {
ComputeOutput,
ProviderComputeInitializeResults,
unitsToAmount,
minAbi
minAbi,
ProviderFees
} from '@oceanprotocol/lib'
import { toast } from 'react-toastify'
import Price from '@shared/Price'
@ -129,6 +130,82 @@ export default function Compute({
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() {
try {
const computeEnv = await getComputeEnviroment(asset)
@ -150,61 +227,6 @@ export default function Compute({
)
throw new Error(`Error initializing provider for the compute job!`)
setInitializedProviderResponse(initializedProvider)
if (
asset.accessDetails?.validProviderFees &&
initializedProvider?.datasets?.[0]?.providerFee?.providerFeeAmount
) {
initializedProvider.datasets[0].providerFee = {
providerFeeAmount: '0',
...asset.accessDetails?.validProviderFees
}
}
if (
selectedAlgorithmAsset?.accessDetails?.validProviderFees &&
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)
const datatoken = new Datatoken(
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,
@ -212,22 +234,7 @@ export default function Compute({
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
) {
await setDatasetPrice(initializedProvider?.datasets?.[0]?.providerFee)
setComputeStatusText(
getComputeFeedback(
selectedAlgorithmAsset?.accessDetails?.baseToken?.symbol,
@ -235,16 +242,10 @@ export default function Compute({
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)
}
await setAlgoPrice(initializedProvider?.algorithm?.providerFee)
const sanitizedResponse = await setComputeFees(initializedProvider)
setInitializedProviderResponse(sanitizedResponse)
} catch (error) {
setError(error.message)
LoggerInstance.error(`[compute] ${error.message} `)
@ -376,7 +377,6 @@ export default function Compute({
selectedAlgorithmAsset,
algoOrderPriceAndFees,
accountId,
hasAlgoAssetDatatoken,
initializedProviderResponse.algorithm,
computeEnv.consumerAddress
)
@ -395,7 +395,6 @@ export default function Compute({
asset,
datasetOrderPriceAndFees,
accountId,
hasDatatoken,
initializedProviderResponse.datasets[0],
computeEnv.consumerAddress
)