REMOVE_COLLATERAL
Withdraw collateral from a BAMM position by providing the BAMM contract address and the amount to remove.
Instructions
Remove collateral from your BAMM position
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bammAddress | Yes | The address of the BAMM contract | |
| amount | Yes | The amount of collateral to remove | |
| collateralToken | No | The address of the collateral token | |
| collateralTokenSymbol | No | The symbol of the collateral token (e.g., 'IQT') |
Implementation Reference
- src/services/remove-collateral.ts:15-92 (handler)The RemoveCollateralService class with the execute() method that handles the core logic: resolves collateral token address, validates token against BAMM, checks balance, constructs the action struct with negative token amounts (removal), simulates and writes the executeActions contract call on the BAMM contract.
export class RemoveCollateralService { constructor(private walletService: WalletService) {} async execute(params: RemoveCollateralParams): Promise<{ txHash: string }> { let { bammAddress, collateralToken, collateralTokenSymbol, amount } = params; if (!collateralToken && !collateralTokenSymbol) { throw new Error( "Either collateralToken or collateralTokenSymbol 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 removeAmountWei = -BigInt(Math.floor(Number(amount) * 1e18)); try { if (collateralTokenSymbol) { collateralToken = await getTokenAddressFromSymbol( collateralTokenSymbol, ); } if (!collateralToken) { throw new Error("Could not resolve collateral token address"); } const tokenValidation = await validateTokenAgainstBAMM( bammAddress, collateralToken, publicClient, ); await checkTokenBalance( collateralToken, userAddress, removeAmountWei, publicClient, ); const currentTime = Math.floor(Date.now() / 1000); const deadline = BigInt(currentTime + 300); const action = { token0Amount: tokenValidation.isToken0 ? removeAmountWei : 0n, token1Amount: tokenValidation.isToken1 ? removeAmountWei : 0n, rent: 0n, 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 executing remove-collateral", error); throw Error("Error executing remove-collateral"); } } } - src/tools/remove-collateral.ts:9-28 (schema)Zod schema (removeCollateralToolParams) defining the input parameters for REMOVE_COLLATERAL: bammAddress (hex address), amount (string), collateralToken (optional hex address), collateralTokenSymbol (optional string).
const removeCollateralToolParams = 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 collateral to remove"), collateralToken: z .string() .regex(/^0x[a-fA-F0-9]{40}$/) .optional() .describe("The address of the collateral token"), collateralTokenSymbol: z .string() .optional() .describe("The symbol of the collateral token (e.g., 'IQT')"), }); export type RemoveCollateralToolParams = z.infer< typeof removeCollateralToolParams >; - src/tools/remove-collateral.ts:30-83 (registration)The Tool object named 'REMOVE_COLLATERAL' with description 'Remove collateral from your BAMM position', parameters, and the execute handler that instantiates WalletService and RemoveCollateralService.
export const removeCollateralTool: Tool< FastMCPSessionAuth, typeof removeCollateralToolParams > = { name: "REMOVE_COLLATERAL", description: "Remove collateral from your BAMM position", parameters: removeCollateralToolParams, execute: async (params, _context) => { try { if (!params.collateralToken && !params.collateralTokenSymbol) { return "Error: Either collateralToken address or collateralTokenSymbol 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 removeCollateralService = new RemoveCollateralService( walletService, ); const result = await removeCollateralService.execute({ bammAddress: params.bammAddress as Address, collateralToken: params.collateralToken as Address | undefined, collateralTokenSymbol: params.collateralTokenSymbol, amount: params.amount, }); return dedent` ✅ Collateral Removal Successful 🌐 BAMM Address: ${params.bammAddress} 🔓 Amount: ${formatNumber(Number(params.amount))} 💰 Token: ${params.collateralTokenSymbol ?? params.collateralToken} 🔗 Transaction: ${result.txHash} Collateral has been removed from your BAMM position. `; } catch (error) { if (error instanceof Error) { return dedent` ❌ Collateral Removal Failed Error: ${error.message} Please verify your inputs and try again. `; } return "An unknown error occurred while removing collateral"; } }, }; - src/index.ts:8-24 (registration)Import and registration of the removeCollateralTool via server.addTool(removeCollateralTool) in the main server entry point.
import { removeCollateralTool } from "./tools/remove-collateral.js"; import { repayTool } from "./tools/repay.js"; async function main() { console.log("🚀 Initializing BAMM MCP Server..."); const server = new FastMCP({ name: "BAMM MCP Server", version: "0.0.1", }); server.addTool(addCollateralTool); server.addTool(borrowTool); server.addTool(getPositionsTool); server.addTool(lendTool); server.addTool(poolStatsTool); server.addTool(removeCollateralTool); - src/lib/templates.ts:98-101 (helper)The REMOVE_COLLATERAL_TEMPLATE string, a prompt template for LLM extraction of 'collateral withdrawal' parameters using the TOKEN_OPERATION_TEMPLATE.
export const REMOVE_COLLATERAL_TEMPLATE = TOKEN_OPERATION_TEMPLATE.replace( /{{operation}}/g, "collateral withdrawal", ).replace(/{{tokenType}}/g, "collateralToken");