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)
try {
// Return early when valid order is found, and no provider fees
// are to be paid
if ( if (
initializeData.providerFee && initializeData?.validOrder &&
initializeData.providerFee.providerFeeAmount !== '0' (!initializeData.providerFee ||
initializeData?.providerFee?.providerFeeAmount === '0')
) { ) {
const baseToken = LoggerInstance.log(
asset?.accessDetails?.type === 'free' '[compute] Has valid order: ',
? getOceanConfig(asset.chainId).oceanTokenAddress initializeData.validOrder
: asset?.accessDetails?.baseToken?.address
const txApproveWei = await approveWei(
web3,
accountId,
baseToken,
asset?.accessDetails?.datatoken?.address,
initializeData?.providerFee?.providerFeeAmount
) )
if (!txApproveWei) {
toast.error('Failed to approve provider fees!')
return
}
}
if (initializeData.validOrder && !initializeData.providerFee) {
LoggerInstance.log('[compute] Has valid order: ', initializeData.validOrder)
return initializeData.validOrder return initializeData.validOrder
} else if (initializeData.validOrder) { }
// Approve potential Provider fee amount first
if (initializeData?.providerFee?.providerFeeAmount !== '0') {
const txApproveProvider = await approveProviderFee(
asset,
accountId,
web3,
initializeData.providerFee.providerFeeAmount
)
if (!txApproveProvider)
throw new Error('Failed to approve provider fees!')
LoggerInstance.log('[compute] Approved provider fees:', txApproveProvider)
}
if (initializeData?.validOrder) {
LoggerInstance.log('[compute] Calling reuseOrder ...', initializeData) LoggerInstance.log('[compute] Calling reuseOrder ...', initializeData)
const tx = await reuseOrder( const txReuseOrder = await reuseOrder(
web3, web3,
asset, asset,
accountId, accountId,
initializeData.validOrder, initializeData.validOrder,
initializeData.providerFee initializeData.providerFee
) )
LoggerInstance.log('[compute] Reused order:', tx.transactionHash) if (!txReuseOrder) throw new Error('Failed to reuse order!')
return tx.transactionHash LoggerInstance.log('[compute] Reused order:', txReuseOrder)
} else { 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}`)
} }
} }