Skip to content

Commit

Permalink
Use a balance instead min amount in the max order preview
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Aug 31, 2022
1 parent 0b6e472 commit f5674b8
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/atomex/atomexSwapPreviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,15 @@ export class AtomexSwapPreviewManager implements Disposable {
});
}

const fromMinAvailableAmount = BigNumber.min(
fromCurrencyInfo.currency.id === fromNativeCurrencyInfo.currency.id
? fromCurrencyBalance.minus(maxFromNativeCurrencyFee)
: fromCurrencyBalance,
availableLiquidity.from.amount
);
const balanceIncludingFees = fromCurrencyInfo.currency.id === fromNativeCurrencyInfo.currency.id
? fromCurrencyBalance.minus(maxFromNativeCurrencyFee)
: fromCurrencyBalance;

maxOrderPreview = await this.getMaxOrderPreview(
actualOrderPreview,
availableLiquidity,
fromAddress,
fromMinAvailableAmount,
balanceIncludingFees,
fromCurrencyInfo,
fromNativeCurrencyInfo,
errors,
Expand Down Expand Up @@ -249,13 +246,13 @@ export class AtomexSwapPreviewManager implements Disposable {
actualOrderPreview: OrderPreview | undefined,
availableLiquidity: SymbolLiquidity,
authorizedFromAddress: string,
fromMinAvailableAmount: BigNumber,
balanceIncludingFees: BigNumber,
fromCurrencyInfo: CurrencyInfo,
fromNativeCurrencyInfo: CurrencyInfo,
errors: Mutable<SwapPreview['errors']>,
_warnings: Mutable<SwapPreview['warnings']>
): Promise<OrderPreview | undefined> {
if (fromMinAvailableAmount.isLessThanOrEqualTo(0)) {
if (balanceIncludingFees.isLessThanOrEqualTo(0)) {
if (fromCurrencyInfo.currency.id !== fromNativeCurrencyInfo.currency.id) {
errors.push({
id: 'not-enough-funds',
Expand All @@ -271,7 +268,11 @@ export class AtomexSwapPreviewManager implements Disposable {
}

const userInvolvedSwapsInfo = await this.getUserInvolvedSwapsInfo(authorizedFromAddress, fromCurrencyInfo.currency.id);
const maxAmount = fromMinAvailableAmount.minus(userInvolvedSwapsInfo.fromTotalAmount);
const maxAmount = BigNumber.min(
balanceIncludingFees.minus(userInvolvedSwapsInfo.fromTotalAmount),
availableLiquidity.from.amount
);

if (maxAmount.isLessThanOrEqualTo(0)) {
errors.push({
id: 'not-enough-funds',
Expand All @@ -293,7 +294,7 @@ export class AtomexSwapPreviewManager implements Disposable {
isFromAmount: true,
});
if (maxOrderPreview && actualOrderPreview && actualOrderPreview.from.amount.isGreaterThan(maxOrderPreview.from.amount)) {
if (actualOrderPreview.from.amount.isGreaterThan(fromMinAvailableAmount))
if (actualOrderPreview.from.amount.isGreaterThan(balanceIncludingFees))
errors.push({
id: 'not-enough-funds',
data: {
Expand Down Expand Up @@ -341,7 +342,7 @@ export class AtomexSwapPreviewManager implements Disposable {

if (!(
(swapTimeStamp + swap.user.requisites.lockTime * 1000 > now)
&& (swapTimeStamp + swap.counterParty.requisites.lockTime * 1000 > now)
&& (swap.counterParty.requisites.lockTime === 0 || (swapTimeStamp + swap.counterParty.requisites.lockTime * 1000 > now))
)) {
return false;
}
Expand Down

0 comments on commit f5674b8

Please sign in to comment.