UNPKG

@iqai/mcp-atp

Version:

Mcp server for ATP (IQAI's Agent Tokenization Platform) access

73 lines 3.24 kB
import { erc20Abi } from "viem"; import { fraxtal } from "viem/chains"; import { env } from "../config.js"; import { ROUTER_MINIMAL_ABI } from "../lib/router.abi.minimal.js"; export class SwapService { walletService; routerAddress; baseTokenAddress; routerAbi; constructor(walletService) { this.walletService = walletService; this.routerAddress = env.ATP_AGENT_ROUTER_ADDRESS; this.baseTokenAddress = env.ATP_BASE_TOKEN_ADDRESS; this.routerAbi = ROUTER_MINIMAL_ABI; } async approveTokenAllowance(amount, tokenContract) { const walletClient = this.walletService.getWalletClient(); const publicClient = this.walletService.getPublicClient(); if (!walletClient || !walletClient.account) { throw new Error("Wallet client or account is not available. Ensure the wallet is properly initialized with a private key."); } // Approve base token const approveTx = await walletClient.writeContract({ address: tokenContract, abi: erc20Abi, functionName: "approve", args: [this.routerAddress, amount], chain: fraxtal, account: walletClient.account, }); await publicClient.waitForTransactionReceipt({ hash: approveTx }); } async buy({ tokenContract, amount, }) { const walletClient = this.walletService.getWalletClient(); const publicClient = this.walletService.getPublicClient(); if (!walletClient || !walletClient.account) { throw new Error("Wallet client or account is not available. Ensure the wallet is properly initialized with a private key."); } const amountInWei = BigInt(amount); await this.approveTokenAllowance(amountInWei, this.baseTokenAddress); // Execute buy (using 3-arg overload, minAmountOut = 0n) const buyTx = await walletClient.writeContract({ address: this.routerAddress, abi: this.routerAbi, functionName: "buy", args: [tokenContract, amountInWei, 0n], chain: fraxtal, account: walletClient.account, }); await publicClient.waitForTransactionReceipt({ hash: buyTx }); return { txHash: buyTx }; } async sell({ tokenContract, amount, }) { const walletClient = this.walletService.getWalletClient(); const publicClient = this.walletService.getPublicClient(); if (!walletClient || !walletClient.account) { throw new Error("Wallet client or account is not available. Ensure the wallet is properly initialized with a private key."); } const amountInWei = BigInt(amount); await this.approveTokenAllowance(amountInWei, tokenContract); const sellTx = await walletClient.writeContract({ address: this.routerAddress, abi: this.routerAbi, functionName: "sell", args: [tokenContract, amountInWei, 0n], chain: fraxtal, account: walletClient.account, }); await publicClient.waitForTransactionReceipt({ hash: sellTx }); return { txHash: sellTx }; } } //# sourceMappingURL=swap.js.map