mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
adding dummyweb3 and zero_address to calculate price on unsupported network / not connected user
This commit is contained in:
parent
6a048d4c5c
commit
da934f789f
@ -16,9 +16,8 @@ export async function transformAssetToAssetSelection(
|
|||||||
|
|
||||||
for (const asset of extendedAssets) {
|
for (const asset of extendedAssets) {
|
||||||
const algoComputeService = getServiceByName(asset, 'compute')
|
const algoComputeService = getServiceByName(asset, 'compute')
|
||||||
|
|
||||||
if (
|
if (
|
||||||
asset?.accessDetails.price &&
|
asset?.accessDetails?.price &&
|
||||||
algoComputeService?.serviceEndpoint === datasetProviderEndpoint
|
algoComputeService?.serviceEndpoint === datasetProviderEndpoint
|
||||||
) {
|
) {
|
||||||
let selected = false
|
let selected = false
|
||||||
|
@ -2,6 +2,7 @@ import React, { FormEvent, ReactElement } from 'react'
|
|||||||
import Button from '../atoms/Button'
|
import Button from '../atoms/Button'
|
||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import Loader from '../atoms/Loader'
|
import Loader from '../atoms/Loader'
|
||||||
|
import { useWeb3 } from '@context/Web3'
|
||||||
|
|
||||||
interface ButtonBuyProps {
|
interface ButtonBuyProps {
|
||||||
action: 'download' | 'compute'
|
action: 'download' | 'compute'
|
||||||
@ -43,12 +44,13 @@ function getConsumeHelpText(
|
|||||||
isConsumable: boolean,
|
isConsumable: boolean,
|
||||||
isBalanceSufficient: boolean,
|
isBalanceSufficient: boolean,
|
||||||
consumableFeedback: string,
|
consumableFeedback: string,
|
||||||
isSupportedOceanNetwork: boolean
|
isSupportedOceanNetwork: boolean,
|
||||||
|
web3: any
|
||||||
) {
|
) {
|
||||||
const text =
|
const text =
|
||||||
isConsumable === false
|
isConsumable === false
|
||||||
? consumableFeedback
|
? consumableFeedback
|
||||||
: hasPreviousOrder
|
: hasPreviousOrder && web3
|
||||||
? `You bought this ${assetType} already allowing you to use it without paying again.`
|
? `You bought this ${assetType} already allowing you to use it without paying again.`
|
||||||
: hasDatatoken
|
: hasDatatoken
|
||||||
? `You own ${dtBalance} ${dtSymbol} allowing you to use this data set by spending 1 ${dtSymbol}, but without paying OCEAN again.`
|
? `You own ${dtBalance} ${dtSymbol} allowing you to use this data set by spending 1 ${dtSymbol}, but without paying OCEAN again.`
|
||||||
@ -106,7 +108,8 @@ function getComputeAssetHelpText(
|
|||||||
selectedComputeAssettLowPoolLiquidity?: boolean,
|
selectedComputeAssettLowPoolLiquidity?: boolean,
|
||||||
selectedComputeAssetType?: string,
|
selectedComputeAssetType?: string,
|
||||||
isAlgorithmConsumable?: boolean,
|
isAlgorithmConsumable?: boolean,
|
||||||
isSupportedOceanNetwork?: boolean
|
isSupportedOceanNetwork?: boolean,
|
||||||
|
web3?: any
|
||||||
) {
|
) {
|
||||||
const computeAssetHelpText = getConsumeHelpText(
|
const computeAssetHelpText = getConsumeHelpText(
|
||||||
dtBalance,
|
dtBalance,
|
||||||
@ -118,7 +121,8 @@ function getComputeAssetHelpText(
|
|||||||
isConsumable,
|
isConsumable,
|
||||||
isBalanceSufficient,
|
isBalanceSufficient,
|
||||||
consumableFeedback,
|
consumableFeedback,
|
||||||
isSupportedOceanNetwork
|
isSupportedOceanNetwork,
|
||||||
|
web3
|
||||||
)
|
)
|
||||||
|
|
||||||
const computeAlgoHelpText = getAlgoHelpText(
|
const computeAlgoHelpText = getAlgoHelpText(
|
||||||
@ -170,6 +174,7 @@ export default function ButtonBuy({
|
|||||||
isAlgorithmConsumable,
|
isAlgorithmConsumable,
|
||||||
isSupportedOceanNetwork
|
isSupportedOceanNetwork
|
||||||
}: ButtonBuyProps): ReactElement {
|
}: ButtonBuyProps): ReactElement {
|
||||||
|
const { web3 } = useWeb3()
|
||||||
const buttonText =
|
const buttonText =
|
||||||
action === 'download'
|
action === 'download'
|
||||||
? hasPreviousOrder
|
? hasPreviousOrder
|
||||||
@ -210,7 +215,8 @@ export default function ButtonBuy({
|
|||||||
isConsumable,
|
isConsumable,
|
||||||
isBalanceSufficient,
|
isBalanceSufficient,
|
||||||
consumableFeedback,
|
consumableFeedback,
|
||||||
isSupportedOceanNetwork
|
isSupportedOceanNetwork,
|
||||||
|
web3
|
||||||
)
|
)
|
||||||
: getComputeAssetHelpText(
|
: getComputeAssetHelpText(
|
||||||
hasPreviousOrder,
|
hasPreviousOrder,
|
||||||
@ -229,7 +235,8 @@ export default function ButtonBuy({
|
|||||||
selectedComputeAssetLowPoolLiquidity,
|
selectedComputeAssetLowPoolLiquidity,
|
||||||
selectedComputeAssetType,
|
selectedComputeAssetType,
|
||||||
isAlgorithmConsumable,
|
isAlgorithmConsumable,
|
||||||
isSupportedOceanNetwork
|
isSupportedOceanNetwork,
|
||||||
|
web3
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
|
@ -9,7 +9,7 @@ import PriceOutput from './PriceOutput'
|
|||||||
import { useAsset } from '@context/Asset'
|
import { useAsset } from '@context/Asset'
|
||||||
import { useWeb3 } from '@context/Web3'
|
import { useWeb3 } from '@context/Web3'
|
||||||
import content from '../../../../../content/pages/startComputeDataset.json'
|
import content from '../../../../../content/pages/startComputeDataset.json'
|
||||||
import { Asset } from '@oceanprotocol/lib'
|
import { Asset, ZERO_ADDRESS } from '@oceanprotocol/lib'
|
||||||
import { AccessDetails, OrderPriceAndFees } from 'src/@types/Price'
|
import { AccessDetails, OrderPriceAndFees } from 'src/@types/Price'
|
||||||
import {
|
import {
|
||||||
getAccessDetailsForAssets,
|
getAccessDetailsForAssets,
|
||||||
@ -18,6 +18,7 @@ import {
|
|||||||
import { AssetExtended } from 'src/@types/AssetExtended'
|
import { AssetExtended } from 'src/@types/AssetExtended'
|
||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
import { MAX_DECIMALS } from '@utils/constants'
|
import { MAX_DECIMALS } from '@utils/constants'
|
||||||
|
import { getDummyWeb3 } from '@utils/web3'
|
||||||
|
|
||||||
export default function FormStartCompute({
|
export default function FormStartCompute({
|
||||||
algorithms,
|
algorithms,
|
||||||
@ -94,14 +95,14 @@ export default function FormStartCompute({
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!values.algorithm || !accountId || !isConsumable) return
|
if (!values.algorithm || !isConsumable) return
|
||||||
async function fetchAlgorithmAssetExtended() {
|
async function fetchAlgorithmAssetExtended() {
|
||||||
const algorithmAsset = getAlgorithmAsset(values.algorithm)
|
const algorithmAsset = getAlgorithmAsset(values.algorithm)
|
||||||
const accessDetails = await getAccessDetails(
|
const accessDetails = await getAccessDetails(
|
||||||
algorithmAsset.chainId,
|
algorithmAsset.chainId,
|
||||||
algorithmAsset.services[0].datatokenAddress,
|
algorithmAsset.services[0].datatokenAddress,
|
||||||
algorithmAsset.services[0].timeout,
|
algorithmAsset.services[0].timeout,
|
||||||
accountId
|
accountId || ZERO_ADDRESS // if user is not connected, use ZERO_ADDRESS as accountId
|
||||||
)
|
)
|
||||||
const extendedAlgoAsset: AssetExtended = {
|
const extendedAlgoAsset: AssetExtended = {
|
||||||
...algorithmAsset,
|
...algorithmAsset,
|
||||||
@ -121,6 +122,7 @@ export default function FormStartCompute({
|
|||||||
setDatasetOrderPrice(
|
setDatasetOrderPrice(
|
||||||
datasetOrderPriceAndFees?.price || asset.accessDetails.price
|
datasetOrderPriceAndFees?.price || asset.accessDetails.price
|
||||||
)
|
)
|
||||||
|
|
||||||
setAlgoOrderPrice(
|
setAlgoOrderPrice(
|
||||||
algoOrderPriceAndFees?.price ||
|
algoOrderPriceAndFees?.price ||
|
||||||
selectedAlgorithmAsset?.accessDetails.price
|
selectedAlgorithmAsset?.accessDetails.price
|
||||||
@ -142,16 +144,17 @@ export default function FormStartCompute({
|
|||||||
.plus(priceAlgo)
|
.plus(priceAlgo)
|
||||||
.toDecimalPlaces(MAX_DECIMALS)
|
.toDecimalPlaces(MAX_DECIMALS)
|
||||||
.toString()
|
.toString()
|
||||||
|
|
||||||
setTotalPrice(totalPrice)
|
setTotalPrice(totalPrice)
|
||||||
}, [
|
}, [
|
||||||
asset?.accessDetails,
|
asset,
|
||||||
selectedAlgorithmAsset?.accessDetails,
|
|
||||||
hasPreviousOrder,
|
hasPreviousOrder,
|
||||||
hasDatatoken,
|
hasDatatoken,
|
||||||
hasPreviousOrderSelectedComputeAsset,
|
hasPreviousOrderSelectedComputeAsset,
|
||||||
hasDatatokenSelectedComputeAsset,
|
hasDatatokenSelectedComputeAsset,
|
||||||
datasetOrderPriceAndFees,
|
datasetOrderPriceAndFees,
|
||||||
algoOrderPriceAndFees
|
algoOrderPriceAndFees,
|
||||||
|
selectedAlgorithmAsset
|
||||||
])
|
])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -65,6 +65,7 @@ export default function Compute({
|
|||||||
consumableFeedback?: string
|
consumableFeedback?: string
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { accountId, web3 } = useWeb3()
|
const { accountId, web3 } = useWeb3()
|
||||||
|
|
||||||
const [isJobStarting, setIsJobStarting] = useState(false)
|
const [isJobStarting, setIsJobStarting] = useState(false)
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const newAbortController = useAbortController()
|
const newAbortController = useAbortController()
|
||||||
@ -116,7 +117,7 @@ export default function Compute({
|
|||||||
const datatokenInstance = new Datatoken(web3)
|
const datatokenInstance = new Datatoken(web3)
|
||||||
const dtBalance = await datatokenInstance.balance(
|
const dtBalance = await datatokenInstance.balance(
|
||||||
asset?.services[0].datatokenAddress,
|
asset?.services[0].datatokenAddress,
|
||||||
accountId
|
accountId || ZERO_ADDRESS // if the user is not connected, we use ZERO_ADDRESS as accountId
|
||||||
)
|
)
|
||||||
setAlgorithmDTBalance(new Decimal(dtBalance).toString())
|
setAlgorithmDTBalance(new Decimal(dtBalance).toString())
|
||||||
const hasAlgoDt = Number(dtBalance) >= 1
|
const hasAlgoDt = Number(dtBalance) >= 1
|
||||||
@ -134,7 +135,7 @@ export default function Compute({
|
|||||||
const initializedProvider = await initializeProviderForCompute(
|
const initializedProvider = await initializeProviderForCompute(
|
||||||
asset,
|
asset,
|
||||||
selectedAlgorithmAsset,
|
selectedAlgorithmAsset,
|
||||||
accountId,
|
accountId || ZERO_ADDRESS, // if the user is not connected, we use ZERO_ADDRESS as accountId
|
||||||
computeEnv
|
computeEnv
|
||||||
)
|
)
|
||||||
if (
|
if (
|
||||||
@ -176,12 +177,13 @@ export default function Compute({
|
|||||||
}
|
}
|
||||||
: null
|
: null
|
||||||
const datasetPriceAndFees = await getOrderPriceAndFees(
|
const datasetPriceAndFees = await getOrderPriceAndFees(
|
||||||
web3,
|
web3 || (await getDummyWeb3(asset?.chainId)), // if the user is not connected, we need to use a dummy web3
|
||||||
asset,
|
asset,
|
||||||
ZERO_ADDRESS,
|
accountId || ZERO_ADDRESS, // if the user is not connected, we need to use ZERO_ADDRESS as accountId
|
||||||
poolParams,
|
poolParams,
|
||||||
initializedProvider?.datasets?.[0]?.providerFee
|
initializedProvider?.datasets?.[0]?.providerFee
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!datasetPriceAndFees) {
|
if (!datasetPriceAndFees) {
|
||||||
setError('Error setting dataset price and fees!')
|
setError('Error setting dataset price and fees!')
|
||||||
return
|
return
|
||||||
@ -224,12 +226,13 @@ export default function Compute({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const algorithmOrderPriceAndFees = await getOrderPriceAndFees(
|
const algorithmOrderPriceAndFees = await getOrderPriceAndFees(
|
||||||
web3,
|
web3 || (await getDummyWeb3(asset?.chainId)),
|
||||||
selectedAlgorithmAsset,
|
selectedAlgorithmAsset,
|
||||||
ZERO_ADDRESS,
|
accountId || ZERO_ADDRESS,
|
||||||
algoPoolParams,
|
algoPoolParams,
|
||||||
initializedProvider.algorithm.providerFee
|
initializedProvider.algorithm.providerFee
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!algorithmOrderPriceAndFees) {
|
if (!algorithmOrderPriceAndFees) {
|
||||||
setError('Error setting algorithm price and fees!')
|
setError('Error setting algorithm price and fees!')
|
||||||
return
|
return
|
||||||
@ -247,7 +250,7 @@ export default function Compute({
|
|||||||
}, [asset?.accessDetails])
|
}, [asset?.accessDetails])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedAlgorithmAsset?.accessDetails || !accountId) return
|
if (!selectedAlgorithmAsset?.accessDetails) return
|
||||||
|
|
||||||
setIsRequestingAlgoOrderPrice(true)
|
setIsRequestingAlgoOrderPrice(true)
|
||||||
setIsConsumablePrice(selectedAlgorithmAsset?.accessDetails?.isPurchasable)
|
setIsConsumablePrice(selectedAlgorithmAsset?.accessDetails?.isPurchasable)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user