From 888aeeb541cf8a42cc943137e67fa3fdabb2337c Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 17 Jun 2022 07:14:41 +0300 Subject: [PATCH] used approveWei for approving provider fees --- src/@utils/order.ts | 47 ++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/@utils/order.ts b/src/@utils/order.ts index b41c195a2..464521f65 100644 --- a/src/@utils/order.ts +++ b/src/@utils/order.ts @@ -1,13 +1,13 @@ import { approve, + approveWei, Datatoken, FreOrderParams, LoggerInstance, OrderParams, ProviderComputeInitialize, ProviderFees, - ProviderInstance, - unitsToAmount + ProviderInstance } from '@oceanprotocol/lib' import { AssetExtended } from 'src/@types/AssetExtended' import Web3 from 'web3' @@ -64,7 +64,6 @@ export async function order( } } as OrderParams - // TODO: we need to approve provider fee separately using aproveWei switch (asset.accessDetails?.type) { case 'fixed': { // this assumes all fees are in ocean @@ -146,31 +145,6 @@ export async function reuseOrder( asset.services[0].serviceEndpoint )) - if ( - providerFees?.providerFeeAmount || - initializeData?.providerFee?.providerFeeAmount - ) { - // need to use approveWei here - const txApprove = await approve( - web3, - accountId, - providerFees.providerFeeToken || - initializeData.providerFee.providerFeeToken, - asset.accessDetails.datatoken.address, - await unitsToAmount( - web3, - providerFees.providerFeeToken || - initializeData.providerFee.providerFeeToken, - providerFees.providerFeeAmount || - initializeData.providerFee.providerFeeAmount - ), - false - ) - if (!txApprove) { - return - } - } - const tx = await datatoken.reuseOrder( asset.accessDetails.datatoken.address, accountId, @@ -207,6 +181,23 @@ export async function handleComputeOrder( '[compute] Handle compute order for asset type: ', asset.metadata.type ) + + if ( + initializeData.providerFee && + initializeData.providerFee.providerFeeAmount !== '0' + ) { + const txApproveWei = await approveWei( + web3, + accountId, + asset.accessDetails.baseToken.address, + 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