LEND
Deposit Fraxswap LP tokens into a BAMM contract to earn interest and unlock borrowing power.
Instructions
Lend Fraxswap LP tokens to a BAMM contract
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bammAddress | Yes | The address of the BAMM contract | |
| amount | Yes | The amount of LP tokens to lend |
Implementation Reference
- src/tools/lend.ts:19-60 (handler)The 'LEND' tool definition with the execute function that creates a WalletService and LendService, calls lendService.execute(), and returns a formatted success/error message. This is the primary handler invoked when the LEND tool is called.
export const lendTool: Tool<FastMCPSessionAuth, typeof lendToolParams> = { name: "LEND", description: "Lend Fraxswap LP tokens to a BAMM contract", parameters: lendToolParams, execute: async (params, _context) => { try { 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 lendService = new LendService(walletService); const result = await lendService.execute({ bammAddress: params.bammAddress as Address, amount: params.amount, }); return dedent` ✅ Lending Successful 🌐 BAMM Address: ${params.bammAddress} 💰 Amount: ${formatNumber(Number(params.amount))} LP tokens 🔗 Transaction: ${result.txHash} LP tokens have been deposited to the BAMM contract. `; } catch (error) { if (error instanceof Error) { return dedent` ❌ Lending Failed Error: ${error.message} Please verify your inputs and try again. `; } return "An unknown error occurred while lending LP tokens"; } }, }; - src/services/lend.ts:19-72 (handler)The LendService.execute() method that performs the actual on-chain logic: reads LP token address from BAMM via pair(), checks balance, ensures token approval, then calls bamm.mint() to deposit LP tokens into the BAMM contract.
async execute(params: LendParams): Promise<{ txHash: string }> { const { bammAddress, amount } = params; 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 lpAmountWei = BigInt(Math.floor(Number(amount) * 1e18)); try { // 1. Read the Fraxswap LP token address from the BAMM contract const lpTokenAddress: Address = await publicClient.readContract({ address: bammAddress, abi: BAMM_ABI, functionName: "pair", args: [], }); // 2. Check user's LP token balance await checkTokenBalance( lpTokenAddress, userAddress, lpAmountWei, publicClient, ); // 3. Approve the BAMM contract to spend LP tokens await ensureTokenApproval( lpTokenAddress, bammAddress, lpAmountWei, publicClient, walletClient, ); // 4. Call bamm.mint(to, lpIn) to deposit LP and receive BAMM tokens const { request: mintRequest } = await publicClient.simulateContract({ address: bammAddress, abi: BAMM_ABI, functionName: "mint", args: [userAddress, lpAmountWei], account: walletClient.account, }); const txHash = await walletClient.writeContract(mintRequest); await publicClient.waitForTransactionReceipt({ hash: txHash }); return { txHash }; } catch (error) { console.error("Error in lend service", error); throw error; } } } - src/tools/lend.ts:9-15 (schema)Zod schema for the LEND tool params: bammAddress (Ethereum address regex) and amount (string). Defines input validation for the tool.
const lendToolParams = 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 of LP tokens to lend"), }); - src/index.ts:22-22 (registration)Registration of the lendTool on the FastMCP server via server.addTool(lendTool).
server.addTool(lendTool); - src/lib/templates.ts:74-77 (helper)The LEND_TEMPLATE, a prompt template derived from SIMPLE_AMOUNT_TEMPLATE, used for AI to extract lending parameters (bammAddress, amount) from natural language.
export const LEND_TEMPLATE = SIMPLE_AMOUNT_TEMPLATE.replace( /{{operation}}/g, "lending", );