LEND
Deposit Fraxswap LP tokens into a BAMM contract to enable borrowing and liquidity management on the Fraxtal blockchain.
Instructions
Lend Fraxswap LP tokens to a BAMM contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | The amount of LP tokens to lend | |
| bammAddress | Yes | The address of the BAMM contract |
Implementation Reference
- src/tools/lend.ts:19-60 (handler)The main handler for the 'LEND' tool, defining the tool name, description, parameters schema, and the execute function that orchestrates wallet setup and calls the LendService.export const lendTool: Tool<undefined, typeof lendToolParams> = { name: "LEND", description: "Lend Fraxswap LP tokens to a BAMM contract", parameters: lendToolParams, execute: async (params) => { 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/tools/lend.ts:9-17 (schema)Zod schema definition for the LEND tool parameters (bammAddress and amount), including TypeScript type export.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"), }); export type LendToolParams = z.infer<typeof lendToolParams>;
- src/index.ts:22-22 (registration)Registration of the LEND tool on the FastMCP server instance.server.addTool(lendTool);
- src/services/lend.ts:11-72 (helper)The LendService class containing the core logic for lending: reading LP token from BAMM.pair(), balance checks, approvals, and minting BAMM tokens.export class LendService { constructor(private walletService: WalletService) {} /** * Lend Fraxswap LP tokens to the BAMM contract. * Reads the LP token address from bamm.pair(), approves the BAMM to spend them if needed, * and calls bamm.mint() to deposit. */ 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; } } }