formatUnits
Convert Ethereum token amounts from wei to human-readable units like ether or gwei. Specify the value and decimal places or unit name for accurate formatting.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes | The value to format | |
| unit | Yes | The number of decimals or unit name (e.g., 'gwei', 18) |
Implementation Reference
- src/tools/core.ts:349-368 (handler)The handler function that executes the formatUnits tool logic: takes a value (in wei or similar) and unit (decimals or name like 'gwei'), uses ethers.formatUnits to convert to human-readable format, and returns it as text content.async ({ value, unit }) => { try { const formattedValue = ethers.formatUnits(value, unit); return { content: [{ type: "text", text: formattedValue }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error formatting units: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/core.ts:341-348 (schema)Zod input schema defining parameters: value (string) and unit (string or number).{ value: z.string().describe( "The value to format" ), unit: z.union([z.string(), z.number()]).describe( "The number of decimals or unit name (e.g., 'gwei', 18)" ) },
- src/tools/core.ts:339-369 (registration)The server.tool registration call that defines and registers the formatUnits tool with its schema and handler within the registerCoreTools function.server.tool( "formatUnits", { value: z.string().describe( "The value to format" ), unit: z.union([z.string(), z.number()]).describe( "The number of decimals or unit name (e.g., 'gwei', 18)" ) }, async ({ value, unit }) => { try { const formattedValue = ethers.formatUnits(value, unit); return { content: [{ type: "text", text: formattedValue }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error formatting units: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/tools/index.ts:23-23 (registration)Call to registerCoreTools in the central registerAllTools function, which includes the formatUnits tool among core tools.registerCoreTools(server, ethersService);