Skip to content

Commit

Permalink
Fix consume of FRE assets & improve error handling (#1940)
Browse files Browse the repository at this point in the history
* wait for approve tx to finish

* handle provider errors better

* more error handling updates

* cleanups

* bump ocean lib

* fix publish try catch const assign
  • Loading branch information
bogdanfazakas authored Jul 17, 2023
1 parent 4d9002d commit 2e69739
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 141 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@coingecko/cryptoformat": "^0.5.4",
"@loadable/component": "^5.15.2",
"@oceanprotocol/art": "^3.2.0",
"@oceanprotocol/lib": "^3.0.1",
"@oceanprotocol/lib": "^3.0.2",
"@oceanprotocol/typographies": "^0.1.0",
"@oceanprotocol/use-dark-mode": "^2.4.3",
"@orbisclub/orbis-sdk": "^0.4.40",
Expand Down
28 changes: 18 additions & 10 deletions 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 {
getErrorMessage,
LoggerInstance,
ProviderFees,
ProviderInstance
Expand All @@ -16,6 +17,7 @@ import {
publisherMarketOrderFee
} from '../../app.config'
import { Signer } from 'ethers'
import { toast } from 'react-toastify'

const tokenPriceQuery = gql`
query TokenPriceQuery($datatokenId: ID!, $account: String) {
Expand Down Expand Up @@ -82,7 +84,6 @@ function getAccessDetailsFromTokenPrice(
timeout?: number
): AccessDetails {
const accessDetails = {} as AccessDetails

// Return early when no supported pricing schema found.
if (
tokenPrice?.dispensers?.length === 0 &&
Expand Down Expand Up @@ -176,15 +177,22 @@ export async function getOrderPriceAndFees(
} as OrderPriceAndFees

// fetch provider fee
const initializeData =
!providerFees &&
(await ProviderInstance.initialize(
asset?.id,
asset?.services[0].id,
0,
accountId,
asset?.services[0].serviceEndpoint
))
let initializeData
try {
initializeData =
!providerFees &&
(await ProviderInstance.initialize(
asset?.id,
asset?.services[0].id,
0,
accountId,
asset?.services[0].serviceEndpoint
))
} catch (error) {
const message = getErrorMessage(JSON.parse(error.message))
LoggerInstance.error('[Initialize Provider] Error:', message)
toast.error(message)
}
orderPriceAndFee.providerFee = providerFees || initializeData.providerFee

// fetch price and swap fees
Expand Down
18 changes: 12 additions & 6 deletions src/@utils/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
Service,
ProviderInstance,
ComputeEnvironment,
ComputeJob
ComputeJob,
getErrorMessage
} from '@oceanprotocol/lib'
import { CancelToken } from 'axios'
import { gql } from 'urql'
Expand All @@ -26,6 +27,7 @@ import { AssetSelectionAsset } from '@shared/FormInput/InputElement/AssetSelecti
import { transformAssetToAssetSelection } from './assetConvertor'
import { ComputeEditForm } from '../components/Asset/Edit/_types'
import { getFileDidInfo } from './provider'
import { toast } from 'react-toastify'

const getComputeOrders = gql`
query ComputeOrders($user: String!) {
Expand Down Expand Up @@ -146,7 +148,12 @@ export async function getComputeEnviroment(
if (!computeEnvs[asset.chainId][0]) return null
return computeEnvs[asset.chainId][0]
} catch (e) {
LoggerInstance.error('[compute] Fetch compute enviroment: ', e.message)
const message = getErrorMessage(JSON.parse(e.message))
LoggerInstance.error(
'[Compute to Data] Fetch compute environment:',
message
)
toast.error(message)
}
}

Expand Down Expand Up @@ -222,8 +229,6 @@ async function getJobs(
assets: Asset[]
): Promise<ComputeJobMetaData[]> {
const computeJobs: ComputeJobMetaData[] = []
// commented loop since we decide how to filter jobs
// for await (const providerUrl of providerUrls) {
try {
const providerComputeJobs = (await ProviderInstance.computeStatus(
providerUrls[0],
Expand Down Expand Up @@ -256,9 +261,10 @@ async function getJobs(
})
}
} catch (err) {
LoggerInstance.error(err.message)
const message = getErrorMessage(JSON.parse(err.message))
LoggerInstance.error('[Compute to Data] Error:', message)
toast.error(message)
}
// }
return computeJobs
}

Expand Down
42 changes: 29 additions & 13 deletions src/@utils/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
ProviderInstance,
DDO,
MetadataAndTokenURI,
NftCreateData
NftCreateData,
getErrorMessage
} from '@oceanprotocol/lib'
import { SvgWaves } from './SvgWaves'
import { Signer, ethers } from 'ethers'
import { toast } from 'react-toastify'

// https://docs.opensea.io/docs/metadata-standards
export interface NftMetadata {
Expand Down Expand Up @@ -101,12 +103,19 @@ export async function setNftMetadata(
signer: Signer,
signal: AbortSignal
): Promise<ethers.providers.TransactionResponse> {
const encryptedDdo = await ProviderInstance.encrypt(
asset,
asset.chainId,
asset.services[0].serviceEndpoint,
signal
)
let encryptedDdo
try {
encryptedDdo = await ProviderInstance.encrypt(
asset,
asset.chainId,
asset.services[0].serviceEndpoint,
signal
)
} catch (err) {
const message = getErrorMessage(JSON.parse(err.message))
LoggerInstance.error('[Encrypt Data] Error:', message)
toast.error(message)
}
LoggerInstance.log('[setNftMetadata] Got encrypted DDO', encryptedDdo)

const metadataHash = getHash(JSON.stringify(asset))
Expand Down Expand Up @@ -136,12 +145,19 @@ export async function setNFTMetadataAndTokenURI(
nftMetadata: NftMetadata,
signal: AbortSignal
): Promise<ethers.providers.TransactionResponse> {
const encryptedDdo = await ProviderInstance.encrypt(
asset,
asset.chainId,
asset.services[0].serviceEndpoint,
signal
)
let encryptedDdo
try {
encryptedDdo = await ProviderInstance.encrypt(
asset,
asset.chainId,
asset.services[0].serviceEndpoint,
signal
)
} catch (err) {
const message = getErrorMessage(JSON.parse(err.message))
LoggerInstance.error('[Encrypt Data] Error:', message)
toast.error(message)
}
LoggerInstance.log(
'[setNFTMetadataAndTokenURI] Got encrypted DDO',
encryptedDdo
Expand Down
29 changes: 14 additions & 15 deletions src/@utils/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
ProviderComputeInitialize,
ProviderFees,
ProviderInstance,
ProviderInitialize
ProviderInitialize,
getErrorMessage
} from '@oceanprotocol/lib'
import { Signer, ethers } from 'ethers'
import { getOceanConfig } from './ocean'
Expand All @@ -38,7 +39,9 @@ async function initializeProvider(
)
return provider
} catch (error) {
LoggerInstance.log('[Initialize Provider] Error:', error)
const message = getErrorMessage(JSON.parse(error.message))
LoggerInstance.log('[Initialize Provider] Error:', message)
toast.error(message)
}
}

Expand Down Expand Up @@ -99,19 +102,16 @@ export async function order(
if (asset.accessDetails.templateId === 1) {
if (!hasDatatoken) {
// buy datatoken
const txApprove = await approve(
const tx: any = await approve(
signer,
config,
accountId,
await signer.getAddress(),
asset.accessDetails.baseToken.address,
config.fixedRateExchangeAddress,
await amountToUnits(
signer,
asset?.accessDetails?.baseToken?.address,
orderPriceAndFees.price
),
orderPriceAndFees.price,
false
)
const txApprove = typeof tx !== 'number' ? await tx.wait() : tx
if (!txApprove) {
return
}
Expand All @@ -126,6 +126,7 @@ export async function order(
marketFeeAddress,
consumeMarketFixedSwapFee
)
const buyDtTx = await freTx.wait()
}
return await datatoken.startOrder(
asset.accessDetails.datatoken.address,
Expand All @@ -136,19 +137,17 @@ export async function order(
)
}
if (asset.accessDetails.templateId === 2) {
const txApprove = await approve(
const tx: any = await approve(
signer,
config,
accountId,
asset.accessDetails.baseToken.address,
asset.accessDetails.datatoken.address,
await amountToUnits(
signer,
asset?.accessDetails?.baseToken?.address,
orderPriceAndFees.price
),
orderPriceAndFees.price,
false
)

const txApprove = typeof tx !== 'number' ? await tx.wait() : tx
if (!txApprove) {
return
}
Expand Down
Loading

1 comment on commit 2e69739

@vercel
Copy link

@vercel vercel bot commented on 2e69739 Jul 17, 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.