From f8527d7a65e5af3f14fbaa5a4977ef72377e6569 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 15 Dec 2023 13:17:04 +0200 Subject: [PATCH 1/3] fix provider fee approval in order helper --- src/utils/OrderUtils.ts | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/utils/OrderUtils.ts b/src/utils/OrderUtils.ts index 53384ad0c..f8de0e808 100644 --- a/src/utils/OrderUtils.ts +++ b/src/utils/OrderUtils.ts @@ -1,4 +1,4 @@ -import { Signer } from 'ethers' +import { Signer, ethers } from 'ethers' import { ProviderInstance, Datatoken, @@ -10,7 +10,9 @@ import { approve, FixedRateExchange, ConsumeMarketFee, - ProviderFees + ProviderFees, + ZERO_ADDRESS, + approveWei } from '../index' import Decimal from 'decimal.js' @@ -88,13 +90,32 @@ export async function orderAsset( ) ).providerFee + if ( + fees && + fees.providerFeeAddress != ZERO_ADDRESS && + fees.providerFeeAmount && + parseInt(fees.providerFeeAmount) > 0 + ) { + try { + await approveWei( + consumerAccount, + config, + await consumerAccount.getAddress(), + fees.providerFeeToken, + asset.services[0].datatokenAddress, + fees.providerFeeAmount + ) + } catch (error) { + throw new Error(`Failed to approve provider fee token ${fees.providerFeeToken}`) + } + } + const orderParams = { consumer: consumerAddress || (await consumerAccount.getAddress()), serviceIndex, _providerFee: fees, _consumeMarketFee: consumeMarketOrderFee } as OrderParams - switch (pricingType) { case 'free': { if (templateIndex === 1) { @@ -105,8 +126,9 @@ export async function orderAsset( await consumerAccount.getAddress() ) if (!dispenserTx) { - return + throw new Error(`Failed to dispense !`) } + await dispenserTx.wait() return await datatoken.startOrder( asset.datatokens[datatokenIndex].address, orderParams.consumer, @@ -133,6 +155,7 @@ export async function orderAsset( ) const fees = await fre.getFeesInfo(fixedRates[fixedRateIndex].id) const exchange = await fre.getExchange(fixedRates[fixedRateIndex].id) + const { baseTokenAmount } = await fre.calcBaseInGivenDatatokensOut( fees.exchangeId, '1', @@ -161,12 +184,12 @@ export async function orderAsset( await consumerAccount.getAddress(), exchange.baseToken, config.fixedRateExchangeAddress, - price, + '1', false ) const txApprove = typeof tx !== 'number' ? await tx.wait() : tx if (!txApprove) { - return + throw new Error(`Failed to appove ${exchange.baseToken} !`) } const freTx = await fre.buyDatatokens( exchange.exchangeId, @@ -177,7 +200,7 @@ export async function orderAsset( ) const buyDtTx = await freTx.wait() if (!buyDtTx) { - return + throw new Error(`Failed to buy datatoken from fixed rate!`) } return await datatoken.startOrder( asset.datatokens[datatokenIndex].address, @@ -202,11 +225,12 @@ export async function orderAsset( if (!txApprove) { return } - return await datatoken.buyFromFreAndOrder( + const txBuy = await datatoken.buyFromFreAndOrder( asset.datatokens[datatokenIndex].address, orderParams, freParams ) + return txBuy } break } From 41f2ba763d26c72b38f12f2c80b698e047f8922b Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 15 Dec 2023 13:23:39 +0200 Subject: [PATCH 2/3] fix lint --- src/utils/OrderUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/OrderUtils.ts b/src/utils/OrderUtils.ts index f8de0e808..88c2dad4b 100644 --- a/src/utils/OrderUtils.ts +++ b/src/utils/OrderUtils.ts @@ -1,4 +1,4 @@ -import { Signer, ethers } from 'ethers' +import { Signer } from 'ethers' import { ProviderInstance, Datatoken, @@ -92,7 +92,7 @@ export async function orderAsset( if ( fees && - fees.providerFeeAddress != ZERO_ADDRESS && + fees.providerFeeAddress !== ZERO_ADDRESS && fees.providerFeeAmount && parseInt(fees.providerFeeAmount) > 0 ) { From bbcb867d2035cebe2c8e8017dbaf2f18db2151ee Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 15 Dec 2023 13:37:31 +0200 Subject: [PATCH 3/3] undo hardcode --- src/utils/OrderUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/OrderUtils.ts b/src/utils/OrderUtils.ts index 88c2dad4b..132d81e13 100644 --- a/src/utils/OrderUtils.ts +++ b/src/utils/OrderUtils.ts @@ -184,7 +184,7 @@ export async function orderAsset( await consumerAccount.getAddress(), exchange.baseToken, config.fixedRateExchangeAddress, - '1', + price, false ) const txApprove = typeof tx !== 'number' ? await tx.wait() : tx