/**
* Tool: Swap Quote
* Gets swap quote with price impact and slippage
*/
async function swapQuoteTool(args, poolService) {
const { poolAddress, fromMint, toMint, amount, slippage = 0.5 } = args;
if (!poolAddress || !fromMint || !toMint || !amount) {
throw new Error("poolAddress, fromMint, toMint, and amount are required");
}
if (amount <= 0) {
throw new Error("Amount must be greater than 0");
}
if (slippage < 0 || slippage > 100) {
throw new Error("Slippage must be between 0 and 100");
}
try {
const quote = await poolService.getSwapQuote(
poolAddress,
fromMint,
toMint,
amount,
slippage
);
const quoteText =
`**Swap Quote**\n\n` +
`**Input:**\n` +
`- Pool: ${poolAddress}\n` +
`- From: ${fromMint.slice(0, 8)}...\n` +
`- To: ${toMint.slice(0, 8)}...\n` +
`- Amount: ${amount}\n\n` +
`**Output:**\n` +
`- Expected Output: ${quote.outputAmount}\n` +
`- Minimum Output (with slippage): ${quote.minimumOutputAmount}\n` +
`- Price Impact: ${quote.priceImpact.toFixed(4)}%\n` +
`- Fee: ${quote.fee}\n` +
`- Slippage Tolerance: ${quote.slippage}%\n\n` +
`**Rate:** 1 token = ${(quote.outputAmount / amount).toFixed(6)} tokens`;
return {
content: [
{
type: "text",
text: quoteText,
},
],
};
} catch (error) {
throw new Error(`Failed to get swap quote: ${error.message}`);
}
}
module.exports = { swapQuoteTool };