Skip to content

Commit

Permalink
Fix/Fallback to subgraph price (#1959)
Browse files Browse the repository at this point in the history
* wip use subgraph

* add more fallbacks

* fix checks

* fix tests

* fix teaser

* fix build

* add return type
  • Loading branch information
bogdanfazakas authored Oct 18, 2023
1 parent 1514e78 commit ba17184
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
13 changes: 12 additions & 1 deletion src/@utils/accessDetailsAndPricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TokenPriceQuery_token as TokenPrice
} from '../@types/subgraph/TokenPriceQuery'
import {
AssetPrice,
getErrorMessage,
LoggerInstance,
ProviderFees,
Expand Down Expand Up @@ -179,7 +180,6 @@ export async function getOrderPriceAndFees(
},
opcFee: '0'
} as OrderPriceAndFees

// fetch provider fee
let initializeData
try {
Expand Down Expand Up @@ -255,3 +255,14 @@ export async function getAccessDetails(
LoggerInstance.error('Error getting access details: ', error.message)
}
}

export function getAvailablePrice(asset: AssetExtended): AssetPrice {
const price: AssetPrice = asset?.stats?.price?.value
? asset?.stats?.price
: {
value: Number(asset?.accessDetails?.price),
tokenSymbol: asset?.accessDetails?.baseToken?.symbol,
tokenAddress: asset?.accessDetails?.baseToken?.address
}
return price
}
2 changes: 1 addition & 1 deletion src/components/@shared/AssetTeaser/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('@shared/AssetTeaser', () => {
testRender(<AssetTeaser asset={asset} />)

it('renders no pricing schema available', () => {
asset.stats.price = null
asset.accessDetails.type = 'NOT_SUPPORTED'
render(<AssetTeaser asset={asset} />)
expect(screen.getByText('No pricing schema available')).toBeInTheDocument()
expect(screen.getByText('This is a test.')).toBeInTheDocument()
Expand Down
3 changes: 2 additions & 1 deletion src/components/@shared/AssetTeaser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import styles from './index.module.css'
import { getServiceByName } from '@utils/ddo'
import { useUserPreferences } from '@context/UserPreferences'
import { formatNumber } from '@utils/numbers'
import { AssetPrice } from '@oceanprotocol/lib'

export declare type AssetTeaserProps = {
asset: AssetExtended
Expand All @@ -31,7 +32,7 @@ export default function AssetTeaser({
const { orders, allocated, price } = asset.stats
const isUnsupportedPricing =
!asset.services.length ||
asset?.stats?.price?.value === undefined ||
price.value === undefined ||
asset?.accessDetails?.type === 'NOT_SUPPORTED'
const { locale } = useUserPreferences()

Expand Down
1 change: 0 additions & 1 deletion src/components/@shared/Price/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default function Price({
size?: 'small' | 'mini' | 'large'
}): ReactElement {
if (!price && !orderPriceAndFees) return

return (
<PriceUnit
price={Number(orderPriceAndFees?.price) || price?.value}
Expand Down
10 changes: 8 additions & 2 deletions src/components/Asset/AssetActions/Compute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ProviderComputeInitializeResults,
unitsToAmount,
ProviderFees,
AssetPrice,
UserCustomParameters,
getErrorMessage
} from '@oceanprotocol/lib'
Expand Down Expand Up @@ -41,7 +42,10 @@ import ComputeJobs from '../../../Profile/History/ComputeJobs'
import { useCancelToken } from '@hooks/useCancelToken'
import { Decimal } from 'decimal.js'
import { useAbortController } from '@hooks/useAbortController'
import { getOrderPriceAndFees } from '@utils/accessDetailsAndPricing'
import {
getAvailablePrice,
getOrderPriceAndFees
} from '@utils/accessDetailsAndPricing'
import { handleComputeOrder } from '@utils/order'
import { getComputeFeedback } from '@utils/feedback'
import { initializeProviderForCompute } from '@utils/provider'
Expand Down Expand Up @@ -111,6 +115,8 @@ export default function Compute({
const { isSupportedOceanNetwork } = useNetworkMetadata()
const { isAssetNetwork } = useAsset()

const price: AssetPrice = getAvailablePrice(asset)

const hasDatatoken = Number(dtBalance) >= 1
const isComputeButtonDisabled =
isOrdering === true ||
Expand Down Expand Up @@ -489,7 +495,7 @@ export default function Compute({
/>
) : (
<Price
price={asset.stats?.price}
price={price}
orderPriceAndFees={datasetOrderPriceAndFees}
conversion
size="large"
Expand Down
12 changes: 8 additions & 4 deletions src/components/Asset/AssetActions/Download/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import Price from '@shared/Price'
import { useAsset } from '@context/Asset'
import ButtonBuy from '../ButtonBuy'
import { secondsToString } from '@utils/ddo'
import AlgorithmDatasetsListForCompute from '../Compute/AlgorithmDatasetsListForCompute'
import styles from './index.module.css'
import AlgorithmDatasetsListForCompute from '../Compute/AlgorithmDatasetsListForCompute'
import {
AssetPrice,
FileInfo,
LoggerInstance,
UserCustomParameters,
Expand All @@ -15,7 +16,10 @@ import {
import { order } from '@utils/order'
import { downloadFile } from '@utils/provider'
import { getOrderFeedback } from '@utils/feedback'
import { getOrderPriceAndFees } from '@utils/accessDetailsAndPricing'
import {
getAvailablePrice,
getOrderPriceAndFees
} from '@utils/accessDetailsAndPricing'
import { toast } from 'react-toastify'
import { useIsMounted } from '@hooks/useIsMounted'
import { useMarketMetadata } from '@context/MarketMetadata'
Expand Down Expand Up @@ -64,10 +68,10 @@ export default function Download({
useState<OrderPriceAndFees>()
const [retry, setRetry] = useState<boolean>(false)

const price: AssetPrice = getAvailablePrice(asset)
const isUnsupportedPricing =
!asset?.accessDetails ||
!asset.services.length ||
asset?.stats?.price?.value === undefined ||
asset?.accessDetails?.type === 'NOT_SUPPORTED' ||
(asset?.accessDetails?.type === 'fixed' &&
!asset?.accessDetails?.baseToken?.symbol)
Expand Down Expand Up @@ -287,7 +291,7 @@ export default function Download({
) : (
<Price
className={styles.price}
price={asset.stats?.price}
price={price}
orderPriceAndFees={orderPriceAndFees}
conversion
size="large"
Expand Down

1 comment on commit ba17184

@vercel
Copy link

@vercel vercel bot commented on ba17184 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.