Skip to content

Commit

Permalink
fix: calculation of enforced limits of LTC/BTC pair
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Aug 27, 2019
1 parent b5f1676 commit e874595
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
11 changes: 7 additions & 4 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ class Service {

const rate = getRate(pairRate, side, false);

this.verifyAmount(pairId, rate, invoiceAmount, side);
this.verifyAmount(pairId, rate, invoiceAmount, side, false);

const { baseFee, percentageFee } = await this.feeProvider.getFees(pairId, rate, side, invoiceAmount, false);
const expectedAmount = Math.ceil(invoiceAmount * rate) + baseFee + percentageFee;
Expand Down Expand Up @@ -412,7 +412,7 @@ class Service {

const rate = getRate(pairRate, side, true);

this.verifyAmount(pairId, rate, invoiceAmount, side);
this.verifyAmount(pairId, rate, invoiceAmount, side, true);

const { baseFee, percentageFee } = await this.feeProvider.getFees(pairId, rate, side, invoiceAmount, true);

Expand Down Expand Up @@ -507,8 +507,11 @@ class Service {
/**
* Verfies that the requested amount is neither above the maximal nor beneath the minimal
*/
private verifyAmount = (pairId: string, rate: number, amount: number, orderSide: OrderSide) => {
if (orderSide === OrderSide.SELL) {
private verifyAmount = (pairId: string, rate: number, amount: number, orderSide: OrderSide, isReverse: boolean) => {
if (
(!isReverse && orderSide === OrderSide.BUY) ||
(isReverse && orderSide === OrderSide.SELL)
) {
// tslint:disable-next-line:no-parameter-reassignment
amount = Math.floor(amount * rate);
}
Expand Down
26 changes: 19 additions & 7 deletions test/unit/service/Service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,24 +684,36 @@ describe('Service', () => {
})).rejects.toEqual(Errors.SCRIPT_TYPE_NOT_FOUND(noOutputType));
});

test('should verify amount', () => {
test('should verify amounts', () => {
const rate = 2;
const verifyAmount = service['verifyAmount'];

verifyAmount('test', rate, 5, OrderSide.BUY);
verifyAmount('test', rate, 5, OrderSide.SELL);
// Normal swaps
verifyAmount('test', rate, 5, OrderSide.BUY, false);
verifyAmount('test', rate, 10, OrderSide.SELL, false);

expect(() => verifyAmount('test', rate, 1, OrderSide.BUY)).toThrow(
Errors.BENEATH_MINIMAL_AMOUNT(1, 5).message,
expect(() => verifyAmount('test', rate, 1.5, OrderSide.BUY, false)).toThrow(
Errors.BENEATH_MINIMAL_AMOUNT(3, 5).message,
);
expect(() => verifyAmount('test', rate, 6, OrderSide.SELL)).toThrow(
expect(() => verifyAmount('test', rate, 12, OrderSide.SELL, false)).toThrow(
Errors.EXCEED_MAXIMAL_AMOUNT(12, 10).message,
);

// Reverse swaps
verifyAmount('test', rate, 10, OrderSide.BUY, true);
verifyAmount('test', rate, 5, OrderSide.SELL, true);

expect(() => verifyAmount('test', rate, 1.5, OrderSide.BUY, true)).toThrow(
Errors.BENEATH_MINIMAL_AMOUNT(1.5, 5).message,
);
expect(() => verifyAmount('test', rate, 12, OrderSide.SELL, true)).toThrow(
Errors.EXCEED_MAXIMAL_AMOUNT(24, 10).message,
);

// Throw if limits of pair cannot be found
const notFound = 'notFound';

expect(() => verifyAmount(notFound, 0, 0, OrderSide.BUY)).toThrow(
expect(() => verifyAmount(notFound, 0, 0, OrderSide.BUY, false)).toThrow(
Errors.PAIR_NOT_FOUND(notFound).message,
);
});
Expand Down

0 comments on commit e874595

Please sign in to comment.