REPAY
Repay borrowed tokens to a BAMM position on the Fraxtal blockchain. Manage your BAMM contract loans by specifying the token address, amount, and contract details.
Instructions
Repay borrowed tokens to a BAMM position
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | The amount to repay | |
| bammAddress | Yes | The address of the BAMM contract | |
| borrowToken | No | The address of the token to repay | |
| borrowTokenSymbol | No | The symbol of the token to repay (e.g., 'IQT') |
Implementation Reference
- src/tools/repay.ts:28-76 (handler)REPAY tool handler: validates params, initializes services, executes repayment via RepayService, and formats success/error response.export const repayTool: Tool<undefined, typeof repayToolParams> = { name: "REPAY", description: "Repay borrowed tokens to a BAMM position", parameters: repayToolParams, execute: async (params) => { try { if (!params.borrowToken && !params.borrowTokenSymbol) { return "Error: Either borrowToken address or borrowTokenSymbol is required"; } const privateKey = process.env.WALLET_PRIVATE_KEY; if (!privateKey) { return "Error: WALLET_PRIVATE_KEY environment variable is not set. Please set it with your wallet's private key (without 0x prefix)."; } const walletService = new WalletService(privateKey); const repayService = new RepayService(walletService); const result = await repayService.execute({ bammAddress: params.bammAddress as Address, borrowToken: params.borrowToken as Address | undefined, borrowTokenSymbol: params.borrowTokenSymbol, amount: params.amount, }); return dedent` ✅ Repayment Successful 🌐 BAMM Address: ${params.bammAddress} 💸 Amount: ${formatNumber(Number(params.amount))} 🪙 Token: ${params.borrowTokenSymbol ?? params.borrowToken} 🔗 Transaction: ${result.txHash} Tokens have been repaid to your BAMM position. `; } catch (error) { if (error instanceof Error) { return dedent` ❌ Repayment Failed Error: ${error.message} Please verify your inputs and try again. `; } return "An unknown error occurred while repaying tokens"; } }, };
- src/tools/repay.ts:9-24 (schema)Zod schema defining input parameters for the REPAY tool: bammAddress, amount, borrowToken (optional), borrowTokenSymbol (optional).const repayToolParams = z.object({ bammAddress: z .string() .regex(/^0x[a-fA-F0-9]{40}$/) .describe("The address of the BAMM contract"), amount: z.string().min(1).describe("The amount to repay"), borrowToken: z .string() .regex(/^0x[a-fA-F0-9]{40}$/) .optional() .describe("The address of the token to repay"), borrowTokenSymbol: z .string() .optional() .describe("The symbol of the token to repay (e.g., 'IQT')"), });
- src/index.ts:25-25 (registration)Registers the REPAY tool (repayTool) with the FastMCP server.server.addTool(repayTool);
- src/services/repay.ts:18-104 (helper)RepayService.execute: core logic for repaying borrowed tokens - resolves token, validates balance/approval, calculates effective rent, calls BAMM executeActions.async execute(params: RepayParams): Promise<{ txHash: string }> { let { bammAddress, borrowToken, borrowTokenSymbol, amount } = params; if (!params.borrowToken && !params.borrowTokenSymbol) { throw new Error("Either borrowToken or borrowTokenSymbol is required"); } const publicClient = this.walletService.getPublicClient(); const walletClient = this.walletService.getWalletClient(); if (!walletClient || !walletClient.account) { throw new Error("Wallet client is not initialized"); } const userAddress = walletClient.account.address; const amountInWei = BigInt(Math.floor(Number(amount) * 1e18)); try { if (borrowTokenSymbol) { borrowToken = await getTokenAddressFromSymbol(borrowTokenSymbol); } if (!borrowToken) { throw new Error("Could not resolve borrow token address"); } const tokenValidation = await validateTokenAgainstBAMM( bammAddress, borrowToken, publicClient, ); await checkTokenBalance( borrowToken, userAddress, amountInWei, publicClient, ); await ensureTokenApproval( borrowToken, bammAddress, amountInWei, publicClient, walletClient, ); const rentedMultiplier: bigint = await publicClient.readContract({ address: bammAddress, abi: BAMM_ABI, functionName: "rentedMultiplier", args: [], }); // Calculate effective rent for repayment (negative rent for repayment) const effectiveRent = -(amountInWei * 1_000_000_000_000_000_000n) / rentedMultiplier; const currentTime = Math.floor(Date.now() / 1000); const deadline = BigInt(currentTime + 300); const action = { token0Amount: tokenValidation.isToken0 ? amountInWei : 0n, token1Amount: tokenValidation.isToken1 ? amountInWei : 0n, rent: effectiveRent, to: userAddress, token0AmountMin: 0n, token1AmountMin: 0n, closePosition: false, approveMax: false, v: 0, r: "0x0000000000000000000000000000000000000000000000000000000000000000" as `0x${string}`, s: "0x0000000000000000000000000000000000000000000000000000000000000000" as `0x${string}`, deadline, }; const { request: executeRequest } = await publicClient.simulateContract({ address: bammAddress, abi: BAMM_ABI, functionName: "executeActions", args: [action], account: walletClient.account, }); const txHash = await walletClient.writeContract(executeRequest); await publicClient.waitForTransactionReceipt({ hash: txHash }); return { txHash }; } catch (error) { console.error("Error repaying:", error); throw error; } }
- src/services/repay.ts:8-13 (schema)TypeScript interface for RepayService parameters.export interface RepayParams { bammAddress: Address; borrowToken?: Address; borrowTokenSymbol?: string; amount: string; }