Skip to main content

Type Definition

Branded Uint8Array representing contract creation bytecode deployed during contract creation transactions.
import type { brand } from '@tevm/voltaire/brand';

export type InitCodeType = Uint8Array & {
  readonly [brand]: "InitCode";
};
Init code contains:
  1. Constructor logic
  2. Constructor arguments (if any)
  3. Runtime bytecode (returned by constructor)

Quick Reference

import * as InitCode from '@tevm/voltaire/InitCode';

// From hex string
const init = InitCode.from("0x608060405234801561001057600080fd5b50...");

// From Uint8Array
const init = InitCode.from(new Uint8Array([0x60, 0x80, ...]));

API Methods

Constructors

Analysis

Utilities

Usage Patterns

Deploying Contract

import * as InitCode from '@tevm/voltaire/InitCode';

// Get init code from compilation output
const init = InitCode.from(compiledOutput.bytecode);

// Estimate gas
const estimatedGas = InitCode.estimateGas(init);
console.log(`Estimated gas: ${estimatedGas}`);

// Deploy
const tx = await signer.sendTransaction({
  data: InitCode.toHex(init),
  gasLimit: estimatedGas + 50000n, // Add buffer
});

Analyzing Creation Bytecode

import * as InitCode from '@tevm/voltaire/InitCode';
import * as Bytecode from '@tevm/voltaire/Bytecode';

const init = InitCode.from(creationBytecode);

// Analyze constructor
const analysis = Bytecode.analyze(init);
console.log(`Constructor instructions: ${analysis.instructions.length}`);

// Find constructor length (usually ends with RETURN or STOP)
// then extract runtime code
const runtime = InitCode.extractRuntime(init, constructorLength);

Gas Estimation

import * as InitCode from '@tevm/voltaire/InitCode';

const init = InitCode.from("0x608060405234801561001057600080fd5b50...");

// Base gas cost (data + creation)
const gas = InitCode.estimateGas(init);

// Gas breakdown:
// - 21000: Base transaction cost
// - 32000: CREATE operation
// - 16 per non-zero byte
// - 4 per zero byte
console.log(`Total creation gas: ${gas}`);

Specification