From 69f502f12b9fab12a558cc304eb5b58e19836561 Mon Sep 17 00:00:00 2001 From: rsercano Date: Tue, 20 Oct 2020 13:57:04 +0300 Subject: [PATCH] fix: addpair should prevent adding same base and quote assets (#1559) --- lib/grpc/getGrpcError.ts | 1 + lib/orderbook/OrderBook.ts | 3 +++ lib/orderbook/errors.ts | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/lib/grpc/getGrpcError.ts b/lib/grpc/getGrpcError.ts index 06358e0c8..1d50636c3 100644 --- a/lib/grpc/getGrpcError.ts +++ b/lib/grpc/getGrpcError.ts @@ -22,6 +22,7 @@ const getGrpcError = (err: any) => { case orderErrorCodes.MIN_QUANTITY_VIOLATED: case orderErrorCodes.QUANTITY_DOES_NOT_MATCH: case swapErrorCodes.REMOTE_IDENTIFIER_MISSING: + case orderErrorCodes.DUPLICATE_PAIR_CURRENCIES: code = status.INVALID_ARGUMENT; break; case orderErrorCodes.PAIR_DOES_NOT_EXIST: diff --git a/lib/orderbook/OrderBook.ts b/lib/orderbook/OrderBook.ts index ed8b42de7..008456973 100644 --- a/lib/orderbook/OrderBook.ts +++ b/lib/orderbook/OrderBook.ts @@ -290,6 +290,9 @@ class OrderBook extends EventEmitter { public addPair = async (pair: Pair) => { const pairId = derivePairId(pair); + if (pair.baseCurrency.toLowerCase() === pair.quoteCurrency.toLowerCase()) { + throw errors.DUPLICATE_PAIR_CURRENCIES(pair.baseCurrency, pair.quoteCurrency); + } if (this.pairInstances.has(pairId)) { throw errors.PAIR_ALREADY_EXISTS(pairId); } diff --git a/lib/orderbook/errors.ts b/lib/orderbook/errors.ts index 67eb64757..e9c674bb3 100644 --- a/lib/orderbook/errors.ts +++ b/lib/orderbook/errors.ts @@ -16,6 +16,7 @@ const errorCodes = { INSUFFICIENT_OUTBOUND_BALANCE: codesPrefix.concat('.12'), MIN_QUANTITY_VIOLATED: codesPrefix.concat('.13'), QUANTITY_ON_HOLD: codesPrefix.concat('.15'), + DUPLICATE_PAIR_CURRENCIES: codesPrefix.concat('.16'), }; const errors = { @@ -75,6 +76,10 @@ const errors = { message: `order with local id ${localId} has a quantity of ${holdQuantity} satoshis on hold, try again later`, code: errorCodes.QUANTITY_DOES_NOT_MATCH, }), + DUPLICATE_PAIR_CURRENCIES: (baseCurrency: string, quoteCurrency: string) => ({ + message: `base asset (${baseCurrency}) and quote asset (${quoteCurrency}) have to be different`, + code: errorCodes.DUPLICATE_PAIR_CURRENCIES, + }), }; export { errorCodes };