permamind
Version:
An MCP server that provides an immortal memory layer for AI agents and clients
69 lines (68 loc) • 2.78 kB
JavaScript
import { z } from "zod";
import { ToolCommand } from "../../core/index.js";
import { resolveToken } from "../utils/TokenResolver.js";
export class GetTokenBalancesCommand extends ToolCommand {
context;
metadata = {
description: "Get all token balances for all holders of a token. Returns a list of all addresses that hold the token and their respective balances.",
name: "getTokenBalances",
openWorldHint: false,
readOnlyHint: true,
title: "Get Token Balances",
};
parametersSchema = z.object({
confirmed: z
.boolean()
.optional()
.describe("Set to true to confirm resolved token"),
processId: z.string().describe("The AO token process ID, name, or ticker"),
});
constructor(context) {
super();
this.context = context;
}
async execute(args) {
try {
// Dynamic import to avoid circular dependencies
const { read } = await import("../../../process.js");
// Resolve token processId if needed
const tokenResolution = await resolveToken(args.processId, this.context.hubId);
if (!tokenResolution.resolved) {
return JSON.stringify({
error: "Token resolution failed",
message: tokenResolution.verificationMessage,
success: false,
suggestion: "Use saveTokenMapping to register this token or provide a valid processId",
});
}
if (tokenResolution.requiresVerification && !args.confirmed) {
return JSON.stringify({
instruction: "Add 'confirmed: true' to your request to proceed with this token",
message: tokenResolution.verificationMessage,
requiresConfirmation: true,
resolvedToken: tokenResolution.value,
success: false,
});
}
const processId = tokenResolution.value;
// Get all balances
const tags = [{ name: "Action", value: "Balances" }];
const result = await read(processId, tags);
return JSON.stringify({
balances: result?.Data ? JSON.parse(result.Data) : null,
message: `All token holder balances retrieved`,
query: {
processId,
},
result,
success: true,
});
}
catch (error) {
return JSON.stringify({
error: `Balances query failed: ${error instanceof Error ? error.message : "Unknown error"}`,
success: false,
});
}
}
}