get_account_pnl
Access a trader's profit and loss (P&L) and performance data on Polymarket using their Ethereum address.
Instructions
Get a trader's P&L and performance metrics from the Beefy P&L subgraph
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| account | Yes | Ethereum address of the trader (lowercase) |
Implementation Reference
- src/index.ts:168-204 (registration)Tool registration for 'get_account_pnl' using server.registerTool. Registers the tool with input schema (zod: 'account' as Ethereum address string) and the handler function.
server.registerTool( "get_account_pnl", { description: "Get a trader's P&L and performance metrics from the Beefy P&L subgraph", inputSchema: { account: z.string().describe("Ethereum address of the trader (lowercase)"), }, }, async ({ account }) => { try { const query = `{ account(id: "${account.toLowerCase()}") { id creationTimestamp lastTradedTimestamp isActive numTrades collateralVolume totalRealizedPnl totalUnrealizedPnl totalFeesPaid winRate profitFactor maxDrawdown numWinningPositions numLosingPositions totalProfitsSum totalLossesSum } }`; const data = await querySubgraph(SUBGRAPHS.beefy_pnl.ipfsHash, query); return textResult(data); } catch (error) { return errorResult(error); } } ); - src/index.ts:176-203 (handler)Handler function for get_account_pnl. Accepts { account }, builds a GraphQL query against the Beefy P&L subgraph's 'account' entity, and returns P&L metrics including totalRealizedPnl, totalUnrealizedPnl, totalFeesPaid, winRate, profitFactor, maxDrawdown, numWinningPositions, numLosingPositions, totalProfitsSum, totalLossesSum.
async ({ account }) => { try { const query = `{ account(id: "${account.toLowerCase()}") { id creationTimestamp lastTradedTimestamp isActive numTrades collateralVolume totalRealizedPnl totalUnrealizedPnl totalFeesPaid winRate profitFactor maxDrawdown numWinningPositions numLosingPositions totalProfitsSum totalLossesSum } }`; const data = await querySubgraph(SUBGRAPHS.beefy_pnl.ipfsHash, query); return textResult(data); } catch (error) { return errorResult(error); } } - src/index.ts:172-174 (schema)Input schema for get_account_pnl: expects 'account' as a zod string (Ethereum address). No output schema defined — results are JSON-stringified.
inputSchema: { account: z.string().describe("Ethereum address of the trader (lowercase)"), }, - src/index.ts:198-198 (helper)The tool queries the 'beefy_pnl' subgraph (IPFS hash QmbHwcGkumWdyTK2jYWXV3vX4WyinftEGbuwi7hDkhPWqG) via the querySubgraph helper.
const data = await querySubgraph(SUBGRAPHS.beefy_pnl.ipfsHash, query); - src/graphClient.ts:12-59 (helper)The querySubgraph helper function used by the handler to execute the GraphQL query against the Graph API gateway.
export async function querySubgraph( ipfsHash: string, query: string, variables?: Record<string, unknown> ): Promise<unknown> { const apiKey = process.env.GRAPH_API_KEY; if (!apiKey) { throw new GraphClientError( "GRAPH_API_KEY environment variable is required. " + "Get one at https://thegraph.com/studio/apikeys/" ); } const url = `https://gateway.thegraph.com/api/${apiKey}/deployments/id/${ipfsHash}`; const body: Record<string, unknown> = { query }; if (variables && Object.keys(variables).length > 0) { body.variables = variables; } const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!response.ok) { throw new GraphClientError( `Graph API returned HTTP ${response.status}: ${response.statusText}`, response.status ); } const json = (await response.json()) as { data?: unknown; errors?: unknown[]; }; if (json.errors && json.errors.length > 0) { throw new GraphClientError( `GraphQL errors: ${JSON.stringify(json.errors)}`, undefined, json.errors ); } return json.data; }