From 4216774d70332e6031f2b73a0a95f00d01e1524f Mon Sep 17 00:00:00 2001 From: Andrew Skubarenko Date: Sun, 11 Sep 2022 21:47:43 +0300 Subject: [PATCH] Take fees of locked swaps --- src/atomex/atomexSwapPreviewManager.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/atomex/atomexSwapPreviewManager.ts b/src/atomex/atomexSwapPreviewManager.ts index 48f6197..fff939e 100644 --- a/src/atomex/atomexSwapPreviewManager.ts +++ b/src/atomex/atomexSwapPreviewManager.ts @@ -194,7 +194,15 @@ export class AtomexSwapPreviewManager implements Disposable { if (!fromCurrencyBalance || !fromNativeCurrencyBalance) throw new Error('Can not get from currency balances'); - const maxFromNativeCurrencyFee = AtomexSwapPreviewManager.calculateMaxTotalFee(swapPreviewFees, fromNativeCurrencyInfo.currency.id); + const isFromCurrencyNative = fromCurrencyInfo.currency.id === fromNativeCurrencyInfo.currency.id; + const maxFromNativeCurrencyFeePerSwap = AtomexSwapPreviewManager.calculateMaxTotalFee(swapPreviewFees, fromNativeCurrencyInfo.currency.id); + const userInvolvedSwapsInfo = isFromCurrencyNative + ? await this.getUserInvolvedSwapsInfo(fromAddress, fromCurrencyInfo.currency.id) + : null; + const maxFromNativeCurrencyFee = userInvolvedSwapsInfo + ? maxFromNativeCurrencyFeePerSwap.multipliedBy(userInvolvedSwapsInfo.swapIds.length + 1) + : maxFromNativeCurrencyFeePerSwap; + if (fromNativeCurrencyBalance.isLessThan(maxFromNativeCurrencyFee)) { errors.push({ id: 'not-enough-funds', @@ -206,10 +214,7 @@ export class AtomexSwapPreviewManager implements Disposable { }); } - const balanceIncludingFees = fromCurrencyInfo.currency.id === fromNativeCurrencyInfo.currency.id - ? fromCurrencyBalance.minus(maxFromNativeCurrencyFee) - : fromCurrencyBalance; - + const balanceIncludingFees = isFromCurrencyNative ? fromCurrencyBalance.minus(maxFromNativeCurrencyFee) : fromCurrencyBalance; maxOrderPreview = await this.getMaxOrderPreview( actualOrderPreview, availableLiquidity,