Skip to content

Commit

Permalink
Merge pull request #69 from atomex-protocol/fix/from-to-calculation
Browse files Browse the repository at this point in the history
Use different rounding algorithms depending on the order side
  • Loading branch information
maxima-net authored Aug 17, 2022
2 parents d0a6edc + ae8fad7 commit ae20abf
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "atomex-sdk",
"version": "0.5.3",
"version": "0.5.5",
"description": "Atomex SDK",
"engines": {
"node": ">=16.14.0",
Expand Down
7 changes: 4 additions & 3 deletions src/exchange/helpers/symbolsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const convertSymbolToFromToCurrenciesPair = (
): readonly [from: SymbolCurrency, to: SymbolCurrency] => {
const preparedQuoteCurrencyPrice = converters.toFixedBigNumber(quoteCurrencyPrice, symbol.decimals.price, BigNumber.ROUND_FLOOR);
const [quoteCurrencyId, baseCurrencyId] = getQuoteBaseCurrenciesBySymbol(symbol.name);
const isBuySide = side === 'Buy';

let preparedQuoteCurrencyAmount: BigNumber;
let preparedBaseCurrencyAmount: BigNumber;
Expand All @@ -28,15 +29,15 @@ export const convertSymbolToFromToCurrenciesPair = (
preparedBaseCurrencyAmount = converters.toFixedBigNumber(
preparedQuoteCurrencyPrice.multipliedBy(preparedQuoteCurrencyAmount),
symbol.decimals.baseCurrency,
BigNumber.ROUND_FLOOR
isBuySide ? BigNumber.ROUND_CEIL : BigNumber.ROUND_FLOOR
);
}
else {
preparedBaseCurrencyAmount = converters.toFixedBigNumber(currencyAmount, symbol.decimals.baseCurrency, BigNumber.ROUND_FLOOR);
preparedQuoteCurrencyAmount = converters.toFixedBigNumber(
preparedBaseCurrencyAmount.div(preparedQuoteCurrencyPrice),
symbol.decimals.quoteCurrency,
BigNumber.ROUND_CEIL
isBuySide ? BigNumber.ROUND_FLOOR : BigNumber.ROUND_CEIL
);
}

Expand All @@ -58,7 +59,7 @@ export const convertSymbolToFromToCurrenciesPair = (
price: preparedBaseCurrencyPrice,
};

return side === 'Buy'
return isBuySide
? [baseCurrency, quoteCurrency]
: [quoteCurrency, baseCurrency];
};
Expand Down
2 changes: 1 addition & 1 deletion tests/clients/testCases/validOrderTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const validOrderTestCases: ReadonlyArray<readonly [
timeStamp: new Date('2022-07-20T18:28:14.521308Z'),
from: {
currencyId: 'ETH',
amount: new BigNumber(0.009999999),
amount: new BigNumber(0.01),
price: new BigNumber(920.085936026)
},
to: {
Expand Down
2 changes: 1 addition & 1 deletion tests/clients/testCases/validSwapTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const validSwapTestCases: ReadonlyArray<readonly [
},
from: {
currencyId: 'ETH',
amount: new BigNumber(0.009999999),
amount: new BigNumber(0.01),
price: new BigNumber(920.085936026)
},
to: {
Expand Down
2 changes: 1 addition & 1 deletion tests/clients/testCases/validWsOrderUpdatedTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const validWsOrderUpdatedTestCases: ReadonlyArray<readonly [
timeStamp: new Date('2022-07-20T18:28:14.521308Z'),
from: {
currencyId: 'ETH',
amount: new BigNumber(0.009999999),
amount: new BigNumber(0.01),
price: new BigNumber(920.085936026)
},
to: {
Expand Down
2 changes: 1 addition & 1 deletion tests/clients/testCases/validWsSwapUpdatedTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const validWsSwapUpdatedTestCases: ReadonlyArray<readonly [
},
from: {
currencyId: 'ETH',
amount: new BigNumber(0.009999999),
amount: new BigNumber(0.01),
price: new BigNumber(920.085936026)
},
to: {
Expand Down
67 changes: 63 additions & 4 deletions tests/exchange/testCases/validGetOrderPreviewTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ const symbols: ExchangeSymbol[] =
quoteCurrency: 6,
price: 9
}
}
},
{
name: 'XTZ/USDT_XTZ',
baseCurrency: 'USDT_XTZ',
quoteCurrency: 'XTZ',
minimumQty: new BigNumber(0.0001),
decimals: {
baseCurrency: 6,
quoteCurrency: 6,
price: 9
}
},
];

const orderBooks = [
Expand All @@ -50,6 +61,28 @@ const orderBooks = [
]
}
]
} as OrderBook),
({
updateId: 200,
symbol: 'XTZ/USDT_XTZ',
baseCurrency: 'USDT_XTZ',
quoteCurrency: 'XTZ',
entries: [
{
side: 'Buy',
price: new BigNumber(1.799568),
qtyProfile: [
100.0
]
},
{
side: 'Sell',
price: new BigNumber(1.841839),
qtyProfile: [
100.0
]
}
]
} as OrderBook)
] as const;

Expand All @@ -61,7 +94,7 @@ const validGetOrderPreviewTestCases: ReadonlyArray<[
orderBook: OrderBook
]> = [
[
'simple (from, to)',
'simple, from XTZ to ETH',
{
type: 'SolidFillOrKill',
amount: new BigNumber('35.483843'),
Expand All @@ -87,7 +120,7 @@ const validGetOrderPreviewTestCases: ReadonlyArray<[
orderBooks[0],
],
[
'simple (symbol, side)',
'simple, symbol=XTZ/ETH, side=Sell',
{
type: 'SolidFillOrKill',
amount: new BigNumber('35.483843'),
Expand All @@ -113,7 +146,7 @@ const validGetOrderPreviewTestCases: ReadonlyArray<[
orderBooks[0]
],
[
'floor amount',
'floor amount, from XTZ to ETH',
{
type: 'SolidFillOrKill',
amount: new BigNumber('57.123456789123456789'),
Expand All @@ -137,6 +170,32 @@ const validGetOrderPreviewTestCases: ReadonlyArray<[
},
symbols,
orderBooks[0]
],
[
'simple, from USDT_XTZ to XTZ',
{
type: 'SolidFillOrKill',
amount: new BigNumber('100'),
from: 'USDT_XTZ',
to: 'XTZ'
},
{
type: 'SolidFillOrKill',
side: 'Buy',
symbol: 'XTZ/USDT_XTZ',
from: {
currencyId: 'USDT_XTZ',
amount: new BigNumber('100'),
price: new BigNumber('0.54293562')
},
to: {
currencyId: 'XTZ',
amount: new BigNumber('54.293562'),
price: new BigNumber('1.841839')
}
},
symbols,
orderBooks[1]
]
];

Expand Down

0 comments on commit ae20abf

Please sign in to comment.