@@ -95,7 +95,7 @@ const sortedQuotes = (
9595 * - Iterate the orders from best rate to worst rate:
9696 * - Let `m` denote the maximum tradable amount not larger than `n`
9797 * - Add the id of the order along with `m` to the output matching
98- * - If `m < n` then subtract `m` from `n` and continue, otherwise break
98+ * - Subtract `m` from ` n` and repeat the process until `n` is zero
9999 */
100100const matchFast = (
101101 amount : BigNumber ,
@@ -107,37 +107,14 @@ const matchFast = (
107107 const actions : MatchAction [ ] = [ ] ;
108108
109109 for ( const quote of quotes ) {
110- if ( amount . gt ( quote . rate . input ) ) {
111- if ( filter ( quote . rate ) ) {
112- actions . push ( {
113- id : quote . id ,
114- input : quote . rate . input ,
115- output : quote . rate . output ,
116- } ) ;
117- amount = amount . sub ( quote . rate . input ) ;
118- }
119- } else if ( amount . eq ( quote . rate . input ) ) {
120- if ( filter ( quote . rate ) ) {
121- actions . push ( {
122- id : quote . id ,
123- input : quote . rate . input ,
124- output : quote . rate . output ,
125- } ) ;
126- }
127- break ;
128- } /* if (amount.lt(rate.input)) */ else {
129- const adjustedRate : Rate = {
130- input : amount ,
131- output : trade ( amount , ordersMap [ quote . id . toString ( ) ] ) . output ,
132- } ;
133- if ( filter ( adjustedRate ) ) {
134- actions . push ( {
135- id : quote . id ,
136- input : adjustedRate . input ,
137- output : adjustedRate . output ,
138- } ) ;
110+ const input : BigNumber = BigNumberMin ( quote . rate . input , amount ) ;
111+ const output : BigNumber = trade ( input , ordersMap [ quote . id . toString ( ) ] ) . output ;
112+ if ( filter ( { input, output} ) ) {
113+ actions . push ( { id : quote . id , input, output} ) ;
114+ amount = amount . sub ( input ) ;
115+ if ( amount . eq ( 0 ) ) {
116+ break ;
139117 }
140- break ;
141118 }
142119 }
143120
0 commit comments