Skip to main content

generateErc1167(implementation: Uint8Array): Uint8Array

Generates ERC-1167 minimal proxy bytecode for gas-efficient contract cloning. Creates 55-byte creation code that deploys 45-byte runtime code delegating all calls to the implementation. Parameters:
  • implementation: Uint8Array - 20-byte implementation address
Returns: Uint8Array - 55-byte creation code Throws:
  • Error - If implementation address is not 20 bytes
Example:
import { generateErc1167 } from '@tevm/voltaire/Proxy';
import { Address } from '@tevm/voltaire/Address';

const implementation = Address.from('0x1234567890123456789012345678901234567890');
const bytecode = generateErc1167(implementation);

console.log('Creation code size:', bytecode.length); // 55 bytes
console.log('Runtime code size:', 45); // After deployment
console.log('Bytecode:', '0x' + Buffer.from(bytecode).toString('hex'));
Defined in: src/primitives/Proxy/generateErc1167.js

Bytecode Structure

Creation Code (10 bytes)

3d602d80600a3d3981f3
OpcodeValueDescription
3dRETURNDATASIZEPush 0 (gas optimization)
60 2dPUSH1 45Runtime code length
80DUP1Duplicate length
60 0aPUSH1 10Creation code length
3dRETURNDATASIZEPush 0
39CODECOPYCopy runtime code to memory
81DUP2Duplicate length
f3RETURNReturn runtime code

Runtime Code (45 bytes)

363d3d373d3d3d363d73[implementation]5af43d82803e903d91602b57fd5bf3
OpcodeDescription
363d3d37Copy calldata to memory
3d3d3d363d73Prepare DELEGATECALL
[implementation]20-byte implementation address
5af4DELEGATECALL to implementation
3d82803eCopy returndata
903d91602b57Check success
fd5bf3REVERT or RETURN

Gas Efficiency

Deployment Cost Comparison

// Regular contract deployment: ~50,000+ gas
const regularDeploy = await deploy(contractBytecode); // Large bytecode

// ERC-1167 clone: ~40,000 gas
const cloneDeploy = await deploy(generateErc1167(implementation)); // 55 bytes

// Savings: ~20% deployment cost

Clone Factory Pattern

import { generateErc1167 } from '@tevm/voltaire/Proxy';
import { Address } from '@tevm/voltaire/Address';

class MinimalCloneFactory {
  proxyBytecode: Uint8Array;

  constructor(implementationAddress: string) {
    this.proxyBytecode = generateErc1167(
      Address.from(implementationAddress)
    );
  }

  async clone(initData?: Uint8Array): Promise<string> {
    // Deploy minimal proxy
    const proxyAddress = await this.deploy(this.proxyBytecode);

    // Initialize clone if needed
    if (initData) {
      await this.call(proxyAddress, initData);
    }

    return proxyAddress;
  }

  async deploy(bytecode: Uint8Array): Promise<string> {
    // Deploy using CREATE or CREATE2
    const tx = await signer.sendTransaction({
      data: '0x' + Buffer.from(bytecode).toString('hex')
    });
    const receipt = await tx.wait();
    return receipt.contractAddress;
  }

  async call(address: string, data: Uint8Array): Promise<void> {
    await signer.sendTransaction({
      to: address,
      data: '0x' + Buffer.from(data).toString('hex')
    });
  }
}

// Usage
const factory = new MinimalCloneFactory('0x1234...');
const clone1 = await factory.clone();
const clone2 = await factory.clone();
const clone3 = await factory.clone();
// Each clone costs ~40k gas vs ~200k+ for full deployment

CREATE2 Deterministic Clones

import { generateErc1167 } from '@tevm/voltaire/Proxy';
import { Address } from '@tevm/voltaire/Address';
import { keccak256 } from '@tevm/voltaire/crypto';

// Generate bytecode
const implementation = Address.from('0x1234...');
const bytecode = generateErc1167(implementation);

// Calculate deterministic address
const factory = Address.from('0xabcd...');
const salt = keccak256(new TextEncoder().encode('my-salt'));
const cloneAddress = Address.calculateCreate2Address(
  factory,
  salt,
  bytecode
);

console.log('Clone will be deployed at:', cloneAddress.toHex());

// Deploy with CREATE2
await deployCreate2(factory, salt, bytecode);

Use Cases

  • Token Factories: Deploy minimal token clones
  • NFT Collections: Gas-efficient NFT contract instances
  • Wallet Clones: Account abstraction wallet instances
  • Marketplace Listings: Per-listing contract instances
  • DAO Modules: Modular DAO component instances

See Also