dex-find-routes
Discover available swap routes between two tokens within MantraChain DEX pools by specifying network name and token denominations.
Instructions
Find available swap routes between two tokens - must first check two tokens are available in the DEX pools by using dex-get-pools
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| networkName | Yes | Name of the network to use | |
| tokenInDenom | Yes | Denomination of the token to swap from | |
| tokenOutDenom | Yes | Denomination of the token to swap to |
Implementation Reference
- src/tools/dex.ts:37-43 (handler)MCP tool handler function that initializes the MantraClient for the given network and retrieves swap routes between two tokens.async ({ networkName, tokenInDenom, tokenOutDenom }) => { await mantraClient.initialize(networkName); const routes = await mantraClient.findSwapRoutes(tokenInDenom, tokenOutDenom); return { content: [{type: "text", text: JSON.stringify(routes)}], }; }
- src/tools/dex.ts:30-36 (schema)Zod schema defining the input parameters for the dex-find-routes tool: networkName, tokenInDenom, tokenOutDenom.{ networkName: z.string().refine(val => Object.keys(networks).includes(val), { message: "Must be a valid network name" }).describe("Name of the network to use"), tokenInDenom: z.string().describe("Denomination of the token to swap from"), tokenOutDenom: z.string().describe("Denomination of the token to swap to"), },
- src/tools/dex.ts:27-44 (registration)Direct registration of the dex-find-routes tool using McpServer.tool() within the registerDexTools function.server.tool( "dex-find-routes", "Find available swap routes between two tokens - must first check two tokens are available in the DEX pools by using `dex-get-pools`", { networkName: z.string().refine(val => Object.keys(networks).includes(val), { message: "Must be a valid network name" }).describe("Name of the network to use"), tokenInDenom: z.string().describe("Denomination of the token to swap from"), tokenOutDenom: z.string().describe("Denomination of the token to swap to"), }, async ({ networkName, tokenInDenom, tokenOutDenom }) => { await mantraClient.initialize(networkName); const routes = await mantraClient.findSwapRoutes(tokenInDenom, tokenOutDenom); return { content: [{type: "text", text: JSON.stringify(routes)}], }; } );
- src/tools/index.ts:25-25 (registration)Invocation of registerDexTools in the main tools registration function, which registers dex-find-routes among other DEX tools.registerDexTools(server, mantraClient);
- src/services/dex-service.ts:74-142 (helper)Core helper function in DexService that implements the route finding logic: fetches pools, finds direct routes, and constructs 2-hop multi-hop routes between tokens.async findRoutes(tokenInDenom: string, tokenOutDenom: string): Promise<SwapOperation[][]> { try { const pools = await this.getPools(); // Check for direct routes (single pool swaps) const directPools = pools.filter(pool => { const denoms = pool.pool_info.asset_denoms; return denoms.includes(tokenInDenom) && denoms.includes(tokenOutDenom); }); const directRoutes = directPools.map(pool => [{ mantra_swap: { pool_identifier: pool.pool_info.pool_identifier, token_in_denom: tokenInDenom, token_out_denom: tokenOutDenom } }]); if (directRoutes.length > 0) { return directRoutes; } // Find multi-hop routes (2-hop routes only for simplicity) const multiHopRoutes = []; const poolsWithTokenIn = pools.filter(pool => pool.pool_info.asset_denoms.some(denom => denom === tokenInDenom) ); const poolsWithTokenOut = pools.filter(pool => pool.pool_info.asset_denoms.some(denom => denom === tokenOutDenom) ); for (const inPool of poolsWithTokenIn) { const inPoolDenoms = inPool.pool_info.asset_denoms; for (const intermediateToken of inPoolDenoms) { if (intermediateToken === tokenInDenom) continue; const connectedOutPools = poolsWithTokenOut.filter(outPool => outPool.pool_info.asset_denoms.some(denom => denom === intermediateToken) ); for (const outPool of connectedOutPools) { multiHopRoutes.push([ { mantra_swap: { pool_identifier: inPool.pool_info.pool_identifier, token_in_denom: tokenInDenom, token_out_denom: intermediateToken } }, { mantra_swap: { pool_identifier: outPool.pool_info.pool_identifier, token_in_denom: intermediateToken, token_out_denom: tokenOutDenom } } ]); } } } return [...directRoutes, ...multiHopRoutes]; } catch (error) { throw new Error(`Failed to find swap routes: ${error instanceof Error ? error.message : String(error)}`); } }