@@ -123,6 +123,14 @@ export class Mento {
123123 )
124124 }
125125
126+ /**
127+ * Get tradable pairs for backwards compatibility
128+ * @returns an array of Asset pairs
129+ */
130+ async getTradablePairs ( cached = true ) : Promise < [ Asset , Asset ] [ ] > {
131+ return ( await this . getTradablePairsWithPath ( cached ) ) . map ( ( pair ) => pair . assets )
132+ }
133+
126134 /**
127135 * Returns a list of all tradable pairs on Mento via direct exchanges.
128136 * Each pair is represented using the TradablePair interface, with its id
@@ -181,7 +189,7 @@ export class Mento {
181189 * the two Asset objects, and an array of exchange details for each hop.
182190 * @returns An array of TradablePair objects representing available trade routes.
183191 */
184- async getTradablePairs (
192+ async getTradablePairsWithPath (
185193 cached = true
186194 ) : Promise < readonly TradablePair [ ] > {
187195 // Get tradable pairs from cache if available.
@@ -286,8 +294,11 @@ export class Mento {
286294 tokenIn : Address ,
287295 tokenOut : Address ,
288296 amountOut : BigNumberish ,
289- tradablePair : TradablePair
297+ tradablePair ? : TradablePair
290298 ) : Promise < BigNumber > {
299+ if ( ! tradablePair ) {
300+ tradablePair = await this . findPairForTokens ( tokenIn , tokenOut )
301+ }
291302 if ( tradablePair . path . length === 1 ) {
292303 return this . getAmountInDirect ( tokenIn , tokenOut , amountOut , tradablePair )
293304 } else {
@@ -309,8 +320,11 @@ export class Mento {
309320 tokenIn : Address ,
310321 tokenOut : Address ,
311322 amountIn : BigNumberish ,
312- tradablePair : TradablePair
323+ tradablePair ? : TradablePair
313324 ) : Promise < BigNumber > {
325+ if ( ! tradablePair ) {
326+ tradablePair = await this . findPairForTokens ( tokenIn , tokenOut )
327+ }
314328 if ( tradablePair . path . length === 1 ) {
315329 return this . getAmountOutDirect ( tokenIn , tokenOut , amountIn , tradablePair )
316330 } else {
@@ -393,10 +407,10 @@ export class Mento {
393407 async increaseTradingAllowance (
394408 tokenIn : Address ,
395409 amount : BigNumberish ,
396- tradablePair : TradablePair
410+ tradablePair ? : TradablePair
397411 ) : Promise < providers . TransactionRequest > {
398412 const spender =
399- tradablePair . path . length == 1 ? this . broker . address : this . router . address
413+ ! tradablePair || tradablePair ? .path . length == 1 ? this . broker . address : this . router . address
400414 const tx = await increaseAllowance (
401415 tokenIn ,
402416 spender ,
@@ -428,8 +442,11 @@ export class Mento {
428442 tokenOut : Address ,
429443 amountIn : BigNumberish ,
430444 amountOutMin : BigNumberish ,
431- tradablePair : TradablePair
445+ tradablePair ? : TradablePair
432446 ) : Promise < providers . TransactionRequest > {
447+ if ( ! tradablePair ) {
448+ tradablePair = await this . findPairForTokens ( tokenIn , tokenOut )
449+ }
433450 if ( tradablePair . path . length === 1 ) {
434451 return this . swapInDirect ( tokenIn , tokenOut , amountIn , amountOutMin )
435452 } else {
@@ -489,16 +506,18 @@ export class Mento {
489506 * @param tokenOut the token to be bought
490507 * @param amountOut the amount of tokenOut to be bought
491508 * @param amountInMax the maximum amount of tokenIn to be sold
492- * @param tradablePair the tradable pair details to determine routing
493509 * @returns the populated TransactionRequest object
494510 */
495511 async swapOut (
496512 tokenIn : Address ,
497513 tokenOut : Address ,
498514 amountOut : BigNumberish ,
499515 amountInMax : BigNumberish ,
500- tradablePair : TradablePair
516+ tradablePair ? : TradablePair
501517 ) : Promise < providers . TransactionRequest > {
518+ if ( ! tradablePair ) {
519+ tradablePair = await this . findPairForTokens ( tokenIn , tokenOut )
520+ }
502521 if ( tradablePair . path . length === 1 ) {
503522 return this . swapOutDirect ( tokenIn , tokenOut , amountOut , amountInMax )
504523 } else {
@@ -602,6 +621,33 @@ export class Mento {
602621 return this . broker
603622 }
604623
624+ /**
625+ * Finds a tradable pair for the given input and output tokens
626+ * @param tokenIn the input token address
627+ * @param tokenOut the output token address
628+ * @returns the tradable pair containing the path between the tokens
629+ * @throws if no path is found between the tokens
630+ */
631+ async findPairForTokens ( tokenIn : Address , tokenOut : Address ) : Promise < TradablePair > {
632+ const pair = ( await this . getTradablePairsWithPath ( ) ) . find (
633+ ( p ) =>
634+ // Direct path
635+ ( p . path . length === 1 && p . path [ 0 ] . assets . includes ( tokenIn ) && p . path [ 0 ] . assets . includes ( tokenOut ) ) ||
636+ // Routed path
637+ ( p . path . length === 2 &&
638+ ( ( p . path [ 0 ] . assets . includes ( tokenIn ) && p . path [ 1 ] . assets . includes ( tokenOut ) ) ||
639+ ( p . path [ 0 ] . assets . includes ( tokenOut ) && p . path [ 1 ] . assets . includes ( tokenIn ) ) )
640+ )
641+ )
642+
643+ if ( ! pair ) {
644+ throw new Error ( `No tradable pair found for tokens ${ tokenIn } and ${ tokenOut } ` )
645+ }
646+
647+ return pair
648+ }
649+
650+
605651 /**
606652 * Returns the list of exchanges available in Mento (cached)
607653 * @returns the list of exchanges
0 commit comments