CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3-core-helpers

Core helper utilities providing standardized error handling and data formatting for the Web3.js ecosystem

Pending
Overview
Eval results
Files

index.mddocs/

Web3 Core Helpers

Web3 Core Helpers is an internal utility package that provides standardized error handling and data formatting capabilities for the Web3.js ecosystem. It contains essential error creators and formatters that ensure consistent behavior across all Web3.js sub-packages when interacting with Ethereum nodes.

Package Information

  • Package Name: web3-core-helpers
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install web3-core-helpers

Core Imports

const { errors, formatters } = require("web3-core-helpers");

ES6/TypeScript:

import { errors, formatters } from "web3-core-helpers";

Basic Usage

const { errors, formatters } = require("web3-core-helpers");

// Create standardized errors
const connectionError = errors.InvalidConnection("wss://localhost:8546");
const paramError = errors.InvalidNumberOfParams(2, 3, "eth_getBalance");

// Format data for Ethereum nodes
const formattedAddress = formatters.inputAddressFormatter("0xd46e8dd67c5d32be8058bb8eb970870f07244567");
const formattedBlockNumber = formatters.inputBlockNumberFormatter("latest");

// Format data from Ethereum nodes
const formattedTransaction = formatters.outputTransactionFormatter({
  blockNumber: "0x5bad55",
  value: "0xde0b6b3a7640000"
});

Architecture

Web3 Core Helpers is organized around two main functional areas:

  • Error Handling: Standardized error creation with consistent messaging and metadata across Web3.js
  • Data Formatting: Bidirectional formatters for converting between JavaScript types and Ethereum node formats
  • Provider Base Classes: TypeScript base classes for different provider types (WebSocket, HTTP, IPC)

Capabilities

Error Handling

Complete error handling system providing standardized error creation for connection issues, validation failures, transaction problems, and contract interactions.

// Connection & Provider Errors
function ErrorResponse(result: Error): Error;
function InvalidConnection(host: string, event?: WebSocketEvent): ConnectionError;
function InvalidProvider(): Error;
function ConnectionTimeout(ms: string): Error;

// Transaction Errors  
function TransactionError(message: string, receipt: object): TransactionError;
function RevertInstructionError(reason: string, signature: string): RevertInstructionError;
function TransactionOutOfGasError(receipt: object): TransactionError;

// Contract Errors
function ContractMissingABIError(): Error;
function ContractNoAddressDefinedError(): Error;

Error Handling

Data Formatting

Comprehensive data formatting utilities for converting between JavaScript types and Ethereum node formats, with separate input and output formatters for different data types.

// Input Formatters (JS -> Ethereum node format)
function inputAddressFormatter(address: string): string;
function inputBlockNumberFormatter(blockNumber: string | number): string | number;
function inputTransactionFormatter(options: object): object;
function txInputFormatter(options: object): object;
function inputSignFormatter(data: string): string;

// Output Formatters (Ethereum node -> JS format)  
function outputBigNumberFormatter(number: string | number): string;
function outputTransactionFormatter(tx: object, hexFormat?: boolean): object;
function outputBlockFormatter(block: object, hexFormat?: boolean): object;
function outputLogFormatter(log: object): object;

// Utility Functions
function isPredefinedBlockNumber(blockNumber: string): boolean;

Data Formatting

Types

interface ConnectionError extends Error {
  code: string | undefined;
  reason: string | undefined;
}

interface TransactionError extends Error {
  receipt: object;
}

interface RevertInstructionError extends Error {
  reason: string;
  signature: string;
}

interface WebSocketEvent {
  code?: number;
  reason?: string;
}

interface JsonRpcPayload {
  jsonrpc: string;
  method: string;
  params?: any[];
  id?: string | number;
}

interface JsonRpcResponse {
  jsonrpc: string;
  id: string | number;
  result?: any;
  error?: {
    readonly code?: number;
    readonly data?: unknown;
    readonly message: string;
  };
}

Install with Tessl CLI

npx tessl i tessl/npm-web3-core-helpers

docs

errors.md

formatters.md

index.md

tile.json