Files
mcp-code/logic/wallet-balance.ts
pablof7z 10fbca0824 feat: Add zap command for sending Bitcoin Lightning tips
- Implemented the `zap` command in the CLI to allow users to send sats to a user, event, or snippet using a NIP-60 wallet.
- Created a new `zap.ts` file to handle the command logic and integrated it into the MCP server.
- Added wallet balance command to check the balance of a user's wallet.
- Enhanced the MCP server to register the new zap command and wallet balance command.
- Introduced caching for wallets to optimize performance and reduce redundant network requests.
- Updated database schema to include snippets table for storing code snippets.
- Improved logging functionality for better debugging and tracking of operations.
- Added functionality to save snippets to the database upon retrieval.
- Updated project overview documentation to reflect new features and structure.
- Refactored existing commands and logic for better modularity and maintainability.
2025-04-08 18:10:16 +01:00

87 lines
2.8 KiB
TypeScript

import { z } from "zod";
import { getUser } from "../config.js";
import { getWallet } from "../lib/cache/wallets.js";
import { getSigner } from "../lib/nostr/utils.js";
import { log } from "../lib/utils/log.js";
import { ndk } from "../ndk.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
/**
* Get the balance for a user's wallet
* @param username Username to check balance for
* @returns Balance information
*/
export async function getWalletBalance(username: string): Promise<{
balance: number;
mint_balances: Record<string, number>;
message: string;
}> {
try {
// Get the appropriate signer based on username
const signer = await getSigner(username);
// Set the signer for this operation
ndk.signer = signer;
const user = await signer.user();
const pubkey = user.pubkey;
// Get wallet for the pubkey
const wallet = await getWallet(pubkey, signer);
if (!wallet) {
throw new Error(`No wallet found for ${username}`);
}
// Get wallet balance
const totalBalance = wallet.balance?.amount || 0;
log(`Wallet balance for ${username}: ${totalBalance} sats`);
// Get individual mint balances
const mintBalances: Record<string, number> = {};
for (const [mintUrl, balance] of Object.entries(wallet.mintBalances)) {
mintBalances[mintUrl] = balance;
log(` - ${mintUrl}: ${balance} sats`);
}
return {
balance: totalBalance,
mint_balances: mintBalances,
message: `Wallet balance for ${username}: ${totalBalance} sats`,
};
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : String(error);
throw new Error(`Failed to get wallet balance: ${errorMessage}`);
}
}
/**
* Add the wallet-balance command to the MCP server
* @param server MCP server to add the command to
*/
export function addWalletBalanceCommand(server: McpServer) {
server.tool(
"wallet_balance",
"Get the balance of a user's wallet",
{
username: z.string().describe("Username to check balance for"),
},
async ({ username }) => {
const result = await getWalletBalance(username);
return {
content: [
{
type: "text",
text: result.message,
},
],
// Include detailed balance information in the response
wallet_balance: {
total: result.balance,
mints: result.mint_balances,
},
};
}
);
}