@@ -623,13 +623,17 @@ class Service {
623623 let calculatedQuantity : number ;
624624
625625 if ( max ) {
626+ if ( ! price ) {
627+ throw errors . INVALID_ARGUMENT ( "max flag can't be used for market orders" ) ;
628+ }
629+
626630 const baseCurrency = pairId . split ( '/' ) [ 0 ] ;
627631 const baseSwapClient = this . swapClientManager . get ( baseCurrency ) ?. type ;
628632
629633 const quoteCurrency = pairId . split ( '/' ) [ 1 ] ;
630634 const quoteSwapClient = this . swapClientManager . get ( quoteCurrency ) ?. type ;
631635
632- calculatedQuantity = await this . calculateOrderMaxQuantity ( baseCurrency , quoteCurrency , side , price , baseSwapClient , quoteSwapClient ) ;
636+ calculatedQuantity = await this . calculateLimitOrderMaxQuantity ( baseCurrency , quoteCurrency , side , price , baseSwapClient , quoteSwapClient ) ;
633637 } else {
634638 calculatedQuantity = quantity || 0 ;
635639 }
@@ -663,61 +667,43 @@ class Service {
663667 await this . orderBook . placeMarketOrder ( placeOrderRequest ) ;
664668 }
665669
666- private async calculateOrderMaxQuantity ( baseCurrency : string , quoteCurrency : string , side : number ,
667- price ? : number , baseSwapClient ?: SwapClientType , quoteSwapClient ?: SwapClientType ) {
670+ private async calculateLimitOrderMaxQuantity ( baseCurrency : string , quoteCurrency : string , side : number ,
671+ price : number , baseSwapClient ?: SwapClientType , quoteSwapClient ?: SwapClientType ) {
668672 let calculatedQuantity ;
669673
670674 const baseTradingLimits = ( await this . tradingLimits ( { currency : baseCurrency } ) ) . get ( baseCurrency ) ;
671675 const quoteTradingLimits = ( await this . tradingLimits ( { currency : quoteCurrency } ) ) . get ( quoteCurrency ) ;
672- const pairId = `${ baseCurrency } /${ quoteCurrency } ` ;
673676
674677 if ( baseSwapClient === SwapClientType . Lnd && quoteSwapClient === SwapClientType . Lnd ) {
675- let maxGettableFromQuote ;
676- let maxGettableFromBase ;
677-
678- if ( side === OrderSide . Sell ) {
679- const quoteMaxBuy = quoteTradingLimits ?. maxBuy || 0 ;
680- maxGettableFromQuote = price ? ( quoteMaxBuy / price ) : this . calculateMaxGettableFromOrderBook ( OrderSide . Sell , pairId , quoteMaxBuy ) ;
681- maxGettableFromBase = baseTradingLimits ?. maxSell || 0 ;
682- } else {
683- const quoteMaxSell = quoteTradingLimits ?. maxSell || 0 ;
684- maxGettableFromQuote = price ? ( quoteMaxSell / price ) : this . calculateMaxGettableFromOrderBook ( OrderSide . Buy , pairId , quoteMaxSell ) ;
685- maxGettableFromBase = baseTradingLimits ?. maxBuy || 0 ;
686- }
678+ const maxGettableFromQuote = ( ( side === OrderSide . Sell ? quoteTradingLimits ?. maxBuy : quoteTradingLimits ?. maxSell ) || 0 ) / price ;
679+ const maxGettableFromBase = ( side === OrderSide . Sell ? baseTradingLimits ?. maxSell : baseTradingLimits ?. maxBuy ) || 0 ;
687680
688681 calculatedQuantity = Math . min ( maxGettableFromBase , maxGettableFromQuote ) ;
689682 } else if ( baseSwapClient === SwapClientType . Lnd && quoteSwapClient === SwapClientType . Connext ) {
690-
691683 if ( side === OrderSide . Sell ) {
692684 calculatedQuantity = baseTradingLimits ?. maxSell || 0 ;
693685 } else {
694- const quoteMaxSell = quoteTradingLimits ?. maxSell || 0 ;
695- const maxGettableFromQuote = price ? ( quoteMaxSell / price ) : this . calculateMaxGettableFromOrderBook ( OrderSide . Buy , pairId , quoteMaxSell ) ;
696- const maxGettableFromBase = baseTradingLimits ?. maxBuy || 0 ;
686+ const maxSellableFromQuote = ( quoteTradingLimits ?. maxSell || 0 ) / price ;
687+ const maxBuyableFromBase = baseTradingLimits ?. maxBuy || 0 ;
697688
698- calculatedQuantity = Math . min ( maxGettableFromQuote , maxGettableFromBase ) ;
689+ calculatedQuantity = Math . min ( maxSellableFromQuote , maxBuyableFromBase ) ;
699690 }
700691
701692 } else if ( baseSwapClient === SwapClientType . Connext && quoteSwapClient === SwapClientType . Lnd ) {
702-
703693 if ( side === OrderSide . Sell ) {
704- const quoteMaxBuy = quoteTradingLimits ?. maxBuy || 0 ;
705- const maxGettableFromQuote = price ? ( quoteMaxBuy / price ) : this . calculateMaxGettableFromOrderBook ( OrderSide . Sell , pairId , quoteMaxBuy ) ;
706- const maxGettableFromBase = baseTradingLimits ?. maxSell || 0 ;
694+ const maxBuyableFromQuote = ( quoteTradingLimits ?. maxBuy || 0 ) / price ;
695+ const maxSellableFromBase = baseTradingLimits ?. maxSell || 0 ;
707696
708- calculatedQuantity = Math . min ( maxGettableFromQuote , maxGettableFromBase ) ;
697+ calculatedQuantity = Math . min ( maxBuyableFromQuote , maxSellableFromBase ) ;
709698 } else {
710- const quoteMaxSell = quoteTradingLimits ?. maxSell || 0 ;
711- calculatedQuantity = price ? ( quoteMaxSell / price ) : this . calculateMaxGettableFromOrderBook ( OrderSide . Buy , pairId , quoteMaxSell ) ;
699+ calculatedQuantity = ( quoteTradingLimits ?. maxSell || 0 ) / price ;
712700 }
713701
714702 } else if ( baseSwapClient === SwapClientType . Connext && quoteSwapClient === SwapClientType . Connext ) {
715-
716703 if ( side === OrderSide . Sell ) {
717704 calculatedQuantity = baseTradingLimits ?. maxSell || 0 ;
718705 } else {
719- const quoteMaxSell = quoteTradingLimits ?. maxSell || 0 ;
720- calculatedQuantity = price ? ( quoteMaxSell / price ) : this . calculateMaxGettableFromOrderBook ( OrderSide . Buy , pairId , quoteMaxSell ) ;
706+ calculatedQuantity = ( quoteTradingLimits ?. maxSell || 0 ) / price ;
721707 }
722708
723709 } else {
@@ -727,31 +713,6 @@ class Service {
727713 return calculatedQuantity ;
728714 }
729715
730- private calculateMaxGettableFromOrderBook ( side : OrderSide , pairId : string , balance : number ) {
731- let result = 0 ;
732- let currentBalance = balance ;
733-
734- this . listOrders ( { pairId, owner : Owner . Both , limit : 0 , includeAliases : false } ) . forEach ( ( orderArrays , _ ) => {
735- const array = side === OrderSide . Sell ? orderArrays . buyArray : orderArrays . sellArray ;
736- for ( const order of array ) {
737- if ( order . quantity && order . price ) {
738- // market buy max calculation
739- const maxBuyableFromThisPrice = currentBalance / order . price ;
740- const calculatedQuantity = ( maxBuyableFromThisPrice > order . quantity ) ? order . quantity : maxBuyableFromThisPrice ;
741- result += calculatedQuantity ;
742- currentBalance -= order . price * calculatedQuantity ;
743-
744- if ( currentBalance === 0 ) {
745- // we filled our buy quantity with this order
746- break ;
747- }
748- }
749- }
750- } ) ;
751-
752- return result ;
753- }
754-
755716 /** Removes a currency. */
756717 public removeCurrency = async ( args : { currency : string } ) => {
757718 argChecks . VALID_CURRENCY ( args ) ;
0 commit comments