1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

refactor handleComputeOrder for less complexity and more useful error reporting

This commit is contained in:
Matthias Kretschmann 2022-06-23 11:05:16 +01:00
parent e1168a451d
commit 8edd3a4f60
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -155,6 +155,55 @@ export async function reuseOrder(
return tx return tx
} }
async function approveProviderFee(
asset: AssetExtended,
accountId: string,
web3: Web3,
providerFeeAmount: string
): Promise<string> {
const baseToken =
asset?.accessDetails?.type === 'free'
? getOceanConfig(asset.chainId).oceanTokenAddress
: asset?.accessDetails?.baseToken?.address
const txApproveWei = await approveWei(
web3,
accountId,
baseToken,
asset?.accessDetails?.datatoken?.address,
providerFeeAmount
)
return txApproveWei as string // thanks ocean.js
}
async function startOrder(
web3: Web3,
asset: AssetExtended,
orderPriceAndFees: OrderPriceAndFees,
accountId: string,
hasDatatoken: boolean,
initializeData: ProviderComputeInitialize,
computeConsumerAddress?: string
): Promise<TransactionReceipt> {
if (!hasDatatoken && asset?.accessDetails.type === 'dynamic') {
const poolTx = await buyDtFromPool(asset?.accessDetails, accountId, web3)
LoggerInstance.log('[compute] Bought datatoken from pool: ', poolTx)
if (!poolTx) {
toast.error('Failed to buy datatoken from pool!')
return
}
}
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
@ -182,60 +231,66 @@ export async function handleComputeOrder(
'[compute] Handle compute order for asset type: ', '[compute] Handle compute order for asset type: ',
asset.metadata.type asset.metadata.type
) )
LoggerInstance.log('[compute] Using initializeData: ', initializeData)
if ( try {
initializeData.providerFee && // Return early when valid order is found, and no provider fees
initializeData.providerFee.providerFeeAmount !== '0' // are to be paid
) { if (
const baseToken = initializeData?.validOrder &&
asset?.accessDetails?.type === 'free' (!initializeData.providerFee ||
? getOceanConfig(asset.chainId).oceanTokenAddress initializeData?.providerFee?.providerFeeAmount === '0')
: asset?.accessDetails?.baseToken?.address ) {
const txApproveWei = await approveWei( LoggerInstance.log(
web3, '[compute] Has valid order: ',
accountId, initializeData.validOrder
baseToken, )
asset?.accessDetails?.datatoken?.address, return initializeData.validOrder
initializeData?.providerFee?.providerFeeAmount
)
if (!txApproveWei) {
toast.error('Failed to approve provider fees!')
return
} }
}
if (initializeData.validOrder && !initializeData.providerFee) { // Approve potential Provider fee amount first
LoggerInstance.log('[compute] Has valid order: ', initializeData.validOrder) if (initializeData?.providerFee?.providerFeeAmount !== '0') {
return initializeData.validOrder const txApproveProvider = await approveProviderFee(
} else if (initializeData.validOrder) { asset,
LoggerInstance.log('[compute] Calling reuseOrder ...', initializeData) accountId,
const tx = await reuseOrder( web3,
web3, initializeData.providerFee.providerFeeAmount
asset, )
accountId,
initializeData.validOrder, if (!txApproveProvider)
initializeData.providerFee throw new Error('Failed to approve provider fees!')
)
LoggerInstance.log('[compute] Reused order:', tx.transactionHash) LoggerInstance.log('[compute] Approved provider fees:', txApproveProvider)
return tx.transactionHash }
} else {
if (initializeData?.validOrder) {
LoggerInstance.log('[compute] Calling reuseOrder ...', initializeData)
const txReuseOrder = await reuseOrder(
web3,
asset,
accountId,
initializeData.validOrder,
initializeData.providerFee
)
if (!txReuseOrder) throw new Error('Failed to reuse order!')
LoggerInstance.log('[compute] Reused order:', txReuseOrder)
return txReuseOrder?.transactionHash
}
LoggerInstance.log('[compute] Calling order ...', initializeData) LoggerInstance.log('[compute] Calling order ...', initializeData)
if (!hasDatatoken && asset?.accessDetails.type === 'dynamic') { const txStartOrder = await startOrder(
const poolTx = await buyDtFromPool(asset?.accessDetails, accountId, web3)
LoggerInstance.log('[compute] Buoght dt from pool: ', poolTx)
if (!poolTx) {
toast.error('Failed to buy datatoken from pool!')
return
}
}
const tx = await order(
web3, web3,
asset, asset,
orderPriceAndFees, orderPriceAndFees,
accountId, accountId,
initializeData.providerFee, hasDatatoken,
initializeData,
computeConsumerAddress computeConsumerAddress
) )
LoggerInstance.log('[compute] Asset ordered:', tx.transactionHash) LoggerInstance.log('[compute] Order succeeded', txStartOrder)
return tx.transactionHash return txStartOrder?.transactionHash
} catch (error) {
toast.error(error.message)
LoggerInstance.error(`[compute] ${error.message}`)
} }
} }