CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3-eth-contract

Web3 module to interact with Ethereum smart contracts with TypeScript type safety

Pending
Overview
Eval results
Files

contract-management.mddocs/

Contract Creation and Management

Core contract instantiation, configuration management, and Web3 context integration for the web3-eth-contract library.

Capabilities

Contract Constructor

Creates a new contract instance with all its methods and events defined in its ABI.

/**
 * Creates a new contract instance with all its methods and events defined in its ABI
 * @param jsonInterface - The JSON interface (ABI) for the contract to instantiate
 * @param address - The address of the smart contract to call (optional)
 * @param options - The options of the contract used as fallbacks for calls and transactions
 * @param context - The context of the contract used for customizing behavior
 */
class Contract<Abi extends ContractAbi> {
  constructor(jsonInterface: Abi);
  constructor(jsonInterface: Abi, context: Web3ContractContext | Web3Context);
  constructor(jsonInterface: Abi, address: Address, contextOrReturnFormat?: Web3ContractContext | Web3Context | DataFormat);
  constructor(jsonInterface: Abi, options: ContractInitOptions, contextOrReturnFormat?: Web3ContractContext | Web3Context | DataFormat);
  constructor(jsonInterface: Abi, address: Address, options: ContractInitOptions, contextOrReturnFormat?: Web3ContractContext | Web3Context | DataFormat);
}

Usage Examples:

import { Contract } from "web3-eth-contract";

// Basic contract with ABI only
const abi = [...] as const;
const contract = new Contract(abi);

// Contract with address
const contract = new Contract(abi, "0x1234567890123456789012345678901234567890");

// Contract with options
const contract = new Contract(abi, {
  from: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
  gas: 1000000,
  gasPrice: "20000000000"
});

// Contract with address and options
const contract = new Contract(
  abi,
  "0x1234567890123456789012345678901234567890",
  {
    from: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
    gas: 1000000
  }
);

Contract Options

The options object for the contract instance containing fallback values for transactions.

interface ContractOptions {
  /** The address of the smart contract */
  address?: Address;
  /** The JSON interface (ABI) for the contract */
  jsonInterface?: ContractAbi;
  /** Default gas limit for contract interactions */
  gas?: Numbers;
  /** Default gas price in wei */
  gasPrice?: Numbers;
  /** Default from address for transactions */
  from?: Address;
  /** Contract bytecode input (alias for data) */
  input?: HexString;
  /** Contract bytecode data */
  data?: HexString;
}

interface ContractInitOptions extends ContractOptions {
  /** Web3 provider for the contract */
  provider?: SupportedProviders;
}

Usage Examples:

// Access and modify contract options
console.log(contract.options.address);
console.log(contract.options.gas);

// Set default values
contract.options.from = "0x1234567890123456789012345678901234567891";
contract.options.gasPrice = "20000000000";
contract.options.gas = 5000000;

// These will be used as fallbacks for method calls
const receipt = await contract.methods.someMethod().send(); // Uses options.from and options.gas

Contract Context

Web3 context configuration for customizing contract behavior.

type Web3ContractContext = Partial<{
  /** Web3 provider instance */
  provider: SupportedProviders;
  /** Web3 request manager for RPC calls */
  requestManager: Web3RequestManager;
  /** Web3 configuration settings */
  config: Web3Configuration;
}>;

Sync with Context

Control whether contract defaults sync with global Web3 context defaults.

class Contract<Abi extends ContractAbi> {
  /** Set to true if you want contracts' defaults to sync with global defaults */
  syncWithContext: boolean;
}

Usage Examples:

// Enable syncing with global context
contract.syncWithContext = true;

// Now contract will automatically use global Web3 settings
// Changes to web3.defaultAccount, web3.defaultGasPrice, etc. will be reflected

Address Management

Get and set the contract address after instantiation.

class Contract<Abi extends ContractAbi> {
  /** The address of the smart contract */
  readonly options: ContractOptions & {
    address?: Address;
  };
}

Usage Examples:

// Check current contract address
if (contract.options.address) {
  console.log("Contract deployed at:", contract.options.address);
} else {
  console.log("Contract not yet deployed");
}

// Set address after deployment
contract.options.address = deployedContractAddress;

Clone Contract

Create a new contract instance with the same ABI but different address or options.

class Contract<Abi extends ContractAbi> {
  /**
   * Creates a new contract instance with the same ABI
   * @param address - New contract address
   * @param options - New contract options
   * @returns New Contract instance
   */
  clone(address?: Address, options?: ContractOptions): Contract<Abi>;
}

Usage Examples:

// Clone contract for different address
const proxyContract = contract.clone("0x9876543210987654321098765432109876543210");

// Clone with different options
const contractWithDifferentDefaults = contract.clone(undefined, {
  from: "0xdifferentaddress",
  gas: 2000000
});

Transaction Middleware

Access to transaction middleware for advanced transaction processing.

class Contract<Abi extends ContractAbi> {
  /**
   * Set transaction middleware for advanced transaction processing
   * @param transactionMiddleware - The middleware instance to set
   */
  setTransactionMiddleware(transactionMiddleware: TransactionMiddleware): void;
  
  /**
   * Get the current transaction middleware
   * @returns The transaction middleware instance if set
   */
  getTransactionMiddleware(): TransactionMiddleware | undefined;
}

Usage Examples:

// Set transaction middleware
const customMiddleware = new CustomTransactionMiddleware();
contract.setTransactionMiddleware(customMiddleware);

// Check if transaction middleware is configured
const middleware = contract.getTransactionMiddleware();
if (middleware) {
  console.log("Transaction middleware is configured");
} else {
  console.log("No transaction middleware set");
}

Method Data Decoding

Decode transaction input data to understand which method was called and with what parameters.

class Contract<Abi extends ContractAbi> {
  /**
   * Decode method data from transaction input to reveal method name and parameters
   * @param data - The transaction input data to decode
   * @returns Decoded parameters with method information
   */
  decodeMethodData(data: HexString): DecodedParams & { __method__: string };
}

Usage Examples:

// Decode transaction data
const transactionData = "0xa9059cbb000000000000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdef0000000000000000000000000000000000000000000000000de0b6b3a7640000";

const decoded = contract.decodeMethodData(transactionData);
console.log("Method called:", decoded.__method__); // "transfer(address,uint256)"
console.log("Number of parameters:", decoded.__length__); // 2
console.log("To address:", decoded.to || decoded[0]);
console.log("Amount:", decoded.amount || decoded[1]);

// Access parameters by name (if available in ABI)
if (decoded.to && decoded.amount) {
  console.log(`Transfer ${decoded.amount} tokens to ${decoded.to}`);
}

// Access parameters by index (always available)
console.log(`Parameter 0: ${decoded[0]}`);
console.log(`Parameter 1: ${decoded[1]}`);

Error Handling

// Web3 contract-specific errors
class Web3ContractError extends Web3Error {
  constructor(message: string, receipt?: TransactionReceipt);
}

class ContractExecutionError extends Web3ContractError {
  constructor(receipt: TransactionReceipt);
}

class ContractTransactionDataAndInputError extends Web3ContractError {
  constructor();
}

Install with Tessl CLI

npx tessl i tessl/npm-web3-eth-contract

docs

contract-deployment.md

contract-management.md

encoding-utilities.md

event-handling.md

index.md

method-execution.md

tile.json