Skip to content

Commit ed8cb78

Browse files
committed
feat: change interface to make it backwards compatible
1 parent 104ff6a commit ed8cb78

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

scripts/swap.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function main() {
4545
const mento = await Mento.create(wallet)
4646

4747
// Fetch tradable pairs
48-
const pairs = await mento.getTradablePairs(true)
48+
const pairs = await mento.getTradablePairsWithPath(true)
4949

5050
// Find the specified tradable pair
5151
const tradablePair = pairs.find((p) => p.id === pairId)
@@ -84,7 +84,6 @@ async function main() {
8484
tokenIn,
8585
tokenOut,
8686
amountIn,
87-
tradablePair
8887
)
8988

9089
// Calculate minAmountOut with 5% slippage
@@ -109,7 +108,6 @@ async function main() {
109108
tokenOut,
110109
amountIn,
111110
minAmountOut,
112-
tradablePair
113111
)
114112
const swapTx = await wallet.sendTransaction(swapTxReq)
115113
console.log(`Swap transaction sent: ${swapTx.hash}`)

src/mento.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('Mento', () => {
136136
return {
137137
getExchanges: () =>
138138
fakeExchangesByProviders[
139-
exchangeProvider as keyof typeof fakeExchangesByProviders
139+
exchangeProvider as keyof typeof fakeExchangesByProviders
140140
],
141141
}
142142
})
@@ -164,7 +164,7 @@ describe('Mento', () => {
164164
symbol: jest.fn(
165165
() =>
166166
fakeSymbolsByTokenAddr[
167-
contractAddr as keyof typeof fakeSymbolsByTokenAddr
167+
contractAddr as keyof typeof fakeSymbolsByTokenAddr
168168
]
169169
),
170170
populateTransaction: {
@@ -263,11 +263,11 @@ describe('Mento', () => {
263263
})
264264
})
265265

266-
describe('getTradeablePairs', () => {
266+
describe('getTradeablePairsWithPath', () => {
267267
it('should return an array of pairs including direct and routed (one-hop) pairs', async () => {
268268
const testee = await Mento.create(provider)
269269

270-
const pairs = await testee.getTradablePairs(false)
270+
const pairs = await testee.getTradablePairsWithPath(false)
271271
// Check direct pairs (length 2)
272272
const directPairs = pairs.filter((p: TradablePair) => p.path.length === 1)
273273
expect(directPairs.length).toBe(nOfFakeDirectExchanges)

src/mento.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)