Skip to content

Commit

Permalink
Extend the swap preview. Specify errors;
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Aug 31, 2022
1 parent b8714dd commit 20ffdd9
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 114 deletions.
250 changes: 157 additions & 93 deletions src/atomex/atomexSwapPreviewManager.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/atomex/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export type { SwapPreviewParameters, NormalizedSwapPreviewParameters } from './s
export type {
SwapPreview, SwapPreviewDirectionData, SwapPreviewCurrencyData,
SwapPreviewFee, SwapPreviewError, SwapPreviewWarning
} from './swapPreview';
} from './swapPreview/index';

export { SwapOperationCompleteStage } from './swapOperationCompleteStage';
3 changes: 3 additions & 0 deletions src/atomex/models/swapPreview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type { SwapPreview, SwapPreviewCurrencyData, SwapPreviewDirectionData, SwapPreviewFee } from './swapPreview';
export type { SwapPreviewError } from './swapPreviewError';
export type { SwapPreviewWarning } from './swapPreviewWarning';
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type BigNumber from 'bignumber.js';

import type { Currency, Side } from '../../common/index';
import type { OrderType } from '../../exchange/index';
import type { Currency, Side } from '../../../common/index';
import type { OrderType } from '../../../exchange/index';
import type { SwapPreviewError } from './swapPreviewError';
import type { SwapPreviewWarning } from './swapPreviewWarning';

export interface SwapPreview {
readonly type: OrderType;
Expand All @@ -20,6 +22,7 @@ export interface SwapPreview {
export interface SwapPreviewDirectionData {
readonly address?: string;
readonly currencyId: Currency['id'];
readonly price: BigNumber;
readonly actual: SwapPreviewCurrencyData;
readonly available: SwapPreviewCurrencyData;
readonly max?: SwapPreviewCurrencyData;
Expand All @@ -30,16 +33,6 @@ export interface SwapPreviewCurrencyData {
readonly price: BigNumber;
}

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

export interface SwapPreviewWarning<TWarningData = unknown> {
readonly id: string
readonly data: TWarningData;
}

export interface SwapPreviewFee {
readonly name: string;
readonly currencyId: Currency['id'];
Expand Down
48 changes: 48 additions & 0 deletions src/atomex/models/swapPreview/swapPreviewError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type BigNumber from 'bignumber.js';

import type { Currency } from '../../../common/index';
import type { Swap } from '../../../swaps';

interface SwapPreviewErrorBase<TErrorId extends string = string, TErrorData = unknown> {
readonly id: TErrorId;
readonly data?: TErrorData;
}

type CheckErrorType<TErrorId extends string, TError extends SwapPreviewErrorBase<TErrorId>> = TError;

type NotEnoughFundsSwapPreviewError = CheckErrorType<
'not-enough-funds',
| {
readonly id: 'not-enough-funds';
readonly data: {
readonly type: 'balance';
readonly currencyId: Currency['id'];
readonly requiredAmount: BigNumber;
};
}
| {
readonly id: 'not-enough-funds';
readonly data: {
readonly type: 'fees';
readonly currencyId: Currency['id'];
readonly requiredAmount: BigNumber;
};
}
| {
readonly id: 'not-enough-funds';
readonly data: {
readonly type: 'swaps';
readonly swapIds: ReadonlyArray<Swap['id']>;
readonly currencyId: Currency['id'];
readonly lockedAmount: BigNumber;
};
}
>;

export type SwapPreviewError = CheckErrorType<
string,
| NotEnoughFundsSwapPreviewError
| {
readonly id: 'not-enough-liquidity'
}
>;
11 changes: 11 additions & 0 deletions src/atomex/models/swapPreview/swapPreviewWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface SwapPreviewWarningBase<TWarningId extends string = string, TWarningData = unknown> {
readonly id: TWarningId;
readonly data?: TWarningData;
}

type CheckWarningType<TWarningId extends string, TWarning extends SwapPreviewWarningBase<TWarningId>> = TWarning;

export type SwapPreviewWarning = CheckWarningType<
string,
never
>;
17 changes: 16 additions & 1 deletion tests/atomex/swapPreview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Atomex | Swap Preview', () => {
});
mockedAtomexContext.services.swapService.getSwaps.mockResolvedValue([]);
mockPriceManagerByOrderBook(environment.orderBooks);
mockAccounts({ ethereum: {}, tezos: {} });
mockAtomexProtocolMultiChainFees(environment.atomexProtocolFees);
await atomex.start();

Expand Down Expand Up @@ -149,6 +150,21 @@ describe('Atomex | Swap Preview', () => {
return Promise.resolve(wallet as any);
}
);
mockedAtomexContext.managers.authorizationManager.getAuthToken.mockImplementation(
address => ({
address,
expired: new Date(Date.now() + 60 * 60 * 1000),
userId: address,
value: address,
request: {
algorithm: 'test-algorithm',
message: 'test-message',
publicKey: address,
signature: `test-signature-${address}`,
timeStamp: Date.now(),
}
})
);

mockedAtomexContext.managers.balanceManager.getBalance
.mockImplementation((address, currency) => {
Expand Down Expand Up @@ -176,7 +192,6 @@ describe('Atomex | Swap Preview', () => {
return buyPrice && sellPrice
? buyPrice.plus(sellPrice).div(2)
: undefined;

};

mockedAtomexContext.managers.priceManager.getPrice.mockImplementation(async params => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'XTZ/ETH',
from: {
currencyId: 'XTZ',
price: new BigNumber('0.000948468'),
address: 'tz101',
actual: {
amount: new BigNumber('10'),
Expand All @@ -156,6 +157,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1054.331827747'),
address: '0x01',
actual: {
amount: new BigNumber('0.00948468'),
Expand Down Expand Up @@ -248,6 +250,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'XTZ/ETH',
from: {
currencyId: 'XTZ',
price: new BigNumber('0.000948468'),
address: 'tz101',
actual: {
amount: new BigNumber('35'),
Expand All @@ -264,6 +267,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1054.331827747'),
address: '0x01',
actual: {
amount: new BigNumber('0.03319638'),
Expand Down Expand Up @@ -321,7 +325,15 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
]
},
errors: [
{ id: 'not-enough-funds' }
{
id: 'not-enough-funds',
data: {
type: 'swaps',
currencyId: 'XTZ',
lockedAmount: new BigNumber(68),
swapIds: [100, 89]
}
}
],
warnings: []
},
Expand Down Expand Up @@ -358,6 +370,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'XTZ/ETH',
from: {
currencyId: 'XTZ',
price: new BigNumber('0.000948468'),
address: 'tz101',
actual: {
amount: new BigNumber('35'),
Expand All @@ -374,6 +387,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1054.331827747'),
address: '0x01',
actual: {
amount: new BigNumber('0.03319638'),
Expand Down Expand Up @@ -431,7 +445,15 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
]
},
errors: [
{ id: 'not-enough-funds' }
{
id: 'not-enough-funds',
data: {
type: 'swaps',
currencyId: 'XTZ',
lockedAmount: new BigNumber(68),
swapIds: [100, 89]
}
}
],
warnings: []
},
Expand Down
35 changes: 30 additions & 5 deletions tests/atomex/testCases/swapPreviewWithAccountTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'XTZ/ETH',
from: {
currencyId: 'XTZ',
price: new BigNumber('0.000948468'),
address: 'tz101',
actual: {
amount: new BigNumber('35'),
Expand All @@ -48,6 +49,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1054.331827747'),
address: '0x01',
actual: {
amount: new BigNumber('0.03319638'),
Expand Down Expand Up @@ -140,6 +142,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'XTZ/ETH',
from: {
currencyId: 'XTZ',
price: new BigNumber('0.000948468'),
address: 'tz101',
actual: {
amount: new BigNumber('35'),
Expand All @@ -156,6 +159,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1054.331827747'),
address: '0x01',
actual: {
amount: new BigNumber('0.03319638'),
Expand Down Expand Up @@ -214,8 +218,10 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
errors: [
{
id: 'not-enough-funds-network-fee',
id: 'not-enough-funds',
data: {
type: 'fees',
currencyId: 'ETH',
requiredAmount: new BigNumber('0.003')
}
}
Expand Down Expand Up @@ -254,6 +260,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'XTZ/ETH',
from: {
currencyId: 'XTZ',
price: new BigNumber('0.000948468'),
address: 'tz101',
actual: {
amount: new BigNumber('3'),
Expand All @@ -267,6 +274,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1054.331827747'),
address: '0x01',
actual: {
amount: new BigNumber('0.002845404'),
Expand Down Expand Up @@ -322,8 +330,12 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
errors: [
{
id: 'not-enough-funds-network-fee',
data: { requiredAmount: new BigNumber('9.554791') }
id: 'not-enough-funds',
data: {
type: 'fees',
currencyId: 'XTZ',
requiredAmount: new BigNumber('9.554791')
}
}
],
warnings: []
Expand Down Expand Up @@ -360,6 +372,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'ETH/USDT_XTZ',
from: {
currencyId: 'USDT_XTZ',
price: new BigNumber('0.000613034'),
address: 'tz101',
actual: {
amount: new BigNumber('1217.234'),
Expand All @@ -376,6 +389,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1631.22858'),
address: '0x01',
actual: {
amount: new BigNumber('0.746206886'),
Expand Down Expand Up @@ -434,8 +448,12 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
errors: [
{
id: 'not-enough-funds-network-fee',
data: { requiredAmount: new BigNumber(0.2) }
id: 'not-enough-funds',
data: {
type: 'fees',
currencyId: 'XTZ',
requiredAmount: new BigNumber(0.2)
}
}
],
warnings: []
Expand Down Expand Up @@ -476,6 +494,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
symbol: 'ETH/USDT_XTZ',
from: {
currencyId: 'USDT_XTZ',
price: new BigNumber('0.000613034'),
address: 'tz101',
actual: {
amount: new BigNumber('1217.234'),
Expand All @@ -492,6 +511,7 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
},
to: {
currencyId: 'ETH',
price: new BigNumber('1631.22858'),
address: '0x01',
actual: {
amount: new BigNumber('0.746206886'),
Expand Down Expand Up @@ -551,6 +571,11 @@ const swapPreviewWithoutAccountTestCases: ReadonlyArray<[
errors: [
{
id: 'not-enough-funds',
data: {
type: 'balance',
currencyId: 'USDT_XTZ',
requiredAmount: new BigNumber(217.234)
}
}
],
warnings: []
Expand Down
Loading

0 comments on commit 20ffdd9

Please sign in to comment.