> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# generateErc1167

> Generate ERC-1167 minimal proxy bytecode

## `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:**

```typescript theme={null}
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](https://github.com/evmts/voltaire/blob/main/src/primitives/Proxy/generateErc1167.js)

## Bytecode Structure

### Creation Code (10 bytes)

```
3d602d80600a3d3981f3
```

| Opcode | Value          | Description                 |
| ------ | -------------- | --------------------------- |
| 3d     | RETURNDATASIZE | Push 0 (gas optimization)   |
| 60 2d  | PUSH1 45       | Runtime code length         |
| 80     | DUP1           | Duplicate length            |
| 60 0a  | PUSH1 10       | Creation code length        |
| 3d     | RETURNDATASIZE | Push 0                      |
| 39     | CODECOPY       | Copy runtime code to memory |
| 81     | DUP2           | Duplicate length            |
| f3     | RETURN         | Return runtime code         |

### Runtime Code (45 bytes)

```
363d3d373d3d3d363d73[implementation]5af43d82803e903d91602b57fd5bf3
```

| Opcode            | Description                    |
| ----------------- | ------------------------------ |
| 363d3d37          | Copy calldata to memory        |
| 3d3d3d363d73      | Prepare DELEGATECALL           |
| \[implementation] | 20-byte implementation address |
| 5af4              | DELEGATECALL to implementation |
| 3d82803e          | Copy returndata                |
| 903d91602b57      | Check success                  |
| fd5bf3            | REVERT or RETURN               |

## Gas Efficiency

### Deployment Cost Comparison

```typescript theme={null}
// 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

```typescript theme={null}
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

```typescript theme={null}
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

* [parseErc1167](/primitives/proxy/parse-erc1167) - Extract implementation from deployed clone
* [isErc1167](/primitives/proxy/is-erc1167) - Check if bytecode is ERC-1167
* [generateErc3448](/primitives/proxy/generate-erc3448) - MetaProxy with metadata
* [ERC-1167 Specification](https://eips.ethereum.org/EIPS/eip-1167)
