REMOVE_COLLATERAL
Remove collateral from a BAMM position by specifying the BAMM contract address, collateral token details, and the amount to withdraw on the Fraxtal blockchain.
Instructions
Remove collateral from your BAMM position
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | The amount of collateral to remove | |
| bammAddress | Yes | The address of the BAMM contract | |
| collateralToken | No | The address of the collateral token | |
| collateralTokenSymbol | No | The symbol of the collateral token (e.g., 'IQT') |
Implementation Reference
- src/tools/remove-collateral.ts:37-82 (handler)MCP tool handler execute function for REMOVE_COLLATERAL, orchestrating wallet setup, service execution, and formatted response.
execute: async (params) => { 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/tools/remove-collateral.ts:9-24 (schema)Zod input schema defining parameters for the REMOVE_COLLATERAL tool.
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')"), }); - src/index.ts:24-24 (registration)Registration of the REMOVE_COLLATERAL tool in the FastMCP server.
server.addTool(removeCollateralTool); - Core execution logic in RemoveCollateralService for performing the blockchain transaction to remove collateral.
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"); } } - Type definition for parameters used in the RemoveCollateralService.
export interface RemoveCollateralParams { bammAddress: Address; collateralToken?: Address; collateralTokenSymbol?: string; amount: string; }