Skip to content

Commit

Permalink
Add the error: not enough liquidity
Browse files Browse the repository at this point in the history
Fix the currency id in redeem fee
  • Loading branch information
skubarenko committed Aug 24, 2022
1 parent 55cb1b3 commit 516fa09
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 17 deletions.
39 changes: 25 additions & 14 deletions src/atomex/atomex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AtomexContext } from './atomexContext';
import { isNormalizedSwapPreviewParameters, normalizeSwapPreviewParameters } from './helpers';
import {
NewSwapRequest, SwapOperationCompleteStage, AtomexOptions,
AtomexBlockchainNetworkOptions, SwapPreview, SwapPreviewParameters, NormalizedSwapPreviewParameters, SwapPreviewFee
AtomexBlockchainNetworkOptions, SwapPreview, SwapPreviewParameters, NormalizedSwapPreviewParameters, SwapPreviewFee, SwapPreviewError
} from './models/index';

export class Atomex implements AtomexService {
Expand Down Expand Up @@ -97,9 +97,6 @@ export class Atomex implements AtomexService {
throw new Error(`No available liquidity for the "${normalizedSwapPreviewParameters.exchangeSymbol.name}" symbol`);

const actualOrderPreview = await this.exchangeManager.getOrderPreview(normalizedSwapPreviewParameters);
if (!actualOrderPreview)
throw new Error(`It's no possible to calculate the order preview for the "${normalizedSwapPreviewParameters.exchangeSymbol.name}" symbol`);

const fees = await this.calculateSwapPreviewFees(
fromCurrencyInfo,
fromNativeCurrencyInfo,
Expand Down Expand Up @@ -148,15 +145,24 @@ export class Atomex implements AtomexService {
});
}

const errors: Array<SwapPreviewError<unknown>> = [];
if (!actualOrderPreview)
errors.push({ id: 'not-enough-liquidity' });

return {
type: normalizedSwapPreviewParameters.type,
from: {
currencyId: normalizedSwapPreviewParameters.from,
address: fromAddress,
actual: {
amount: actualOrderPreview.from.amount,
price: actualOrderPreview.from.price,
},
actual: actualOrderPreview
? {
amount: actualOrderPreview.from.amount,
price: actualOrderPreview.from.price,
}
: {
amount: normalizedSwapPreviewParameters.isFromAmount ? normalizedSwapPreviewParameters.amount : new BigNumber(0),
price: new BigNumber(0)
},
available: {
amount: availableLiquidity.from.amount,
price: availableLiquidity.from.price
Expand All @@ -169,10 +175,15 @@ export class Atomex implements AtomexService {
to: {
currencyId: normalizedSwapPreviewParameters.to,
address: toAddress,
actual: {
amount: actualOrderPreview.to.amount,
price: actualOrderPreview.to.price,
},
actual: actualOrderPreview
? {
amount: actualOrderPreview.to.amount,
price: actualOrderPreview.to.price,
}
: {
amount: !normalizedSwapPreviewParameters.isFromAmount ? normalizedSwapPreviewParameters.amount : new BigNumber(0),
price: new BigNumber(0)
},
available: {
amount: availableLiquidity.to.amount,
price: availableLiquidity.to.price
Expand All @@ -185,7 +196,7 @@ export class Atomex implements AtomexService {
symbol: normalizedSwapPreviewParameters.exchangeSymbol.name,
side: normalizedSwapPreviewParameters.side,
fees,
errors: [],
errors,
warnings: []
};
}
Expand Down Expand Up @@ -307,7 +318,7 @@ export class Atomex implements AtomexService {
makerFee,
{
name: useWatchTower ? 'redeem-reward' : 'redeem-fee',
currencyId: toCurrencyInfo.currency.id,
currencyId: useWatchTower ? toCurrencyInfo.currency.id : toNativeCurrencyInfo.currency.id,
estimated: toRedeemOrRewardForRedeem.estimated,
max: toRedeemOrRewardForRedeem.max
}
Expand Down
2 changes: 1 addition & 1 deletion src/atomex/models/swapPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface SwapPreviewCurrencyData {

export interface SwapPreviewError<TErrorData = unknown> {
readonly id: string
readonly data: TErrorData;
readonly data?: TErrorData;
}

export interface SwapPreviewWarning<TWarningData = unknown> {
Expand Down
2 changes: 1 addition & 1 deletion tests/atomex/swapPreview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Atomex | Swap Preview', () => {
});

test.each(swapPreviewWithoutAccountTestCases)(
'getting swap preview without account: %s',
'getting swap preview without account: %s\n\tSwap Preview Parameters: %j',
async (_, swapPreviewParameters, expectedSwapPreview, environment) => {
mockedAtomexContext.services.exchangeService.getSymbols.mockResolvedValue(environment.symbols);
mockedAtomexContext.services.exchangeService.getOrderBook.mockImplementation(symbol => {
Expand Down
Loading

0 comments on commit 516fa09

Please sign in to comment.