# ImmutableClone

[Git Source](https://github.com/traderjoe-xyz/joe-v2/blob/16f011d25e6bf6d0a0c479974345b623d491104f/src/libraries/ImmutableClone.sol)

**Authors:**\
Trader Joe, Solady (<https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol>), Minimal proxy by 0age (<https://github.com/0age>), Clones with immutable args by wighawag, zefram.eth, Saw-mon & Natalie\
(<https://github.com/Saw-mon-and-Natalie/clones-with-immutable-args>)

Minimal immutable proxy library.

*Minimal proxy:*\
*Although the sw0nt pattern saves 5 gas over the erc-1167 pattern during runtime,*\
*it is not supported out-of-the-box on Etherscan. Hence, we choose to use the 0age pattern,*\
*which saves 4 gas over the erc-1167 pattern during runtime, and has the smallest bytecode.*

*Clones with immutable args (CWIA):*\
*The implementation of CWIA here doesn't implements a `receive()` as it is not needed for LB.*

## Functions

### cloneDeterministic

*Deploys a deterministic clone of `implementation` using immutable arguments encoded in `data`, with `salt`*

```solidity
function cloneDeterministic(address implementation, bytes memory data, bytes32 salt)
    internal
    returns (address instance);
```

**Parameters**

| Name             | Type      | Description                       |
| ---------------- | --------- | --------------------------------- |
| `implementation` | `address` | The address of the implementation |
| `data`           | `bytes`   | The encoded immutable arguments   |
| `salt`           | `bytes32` | The salt                          |

### initCodeHash

\---------------------------------------------------------------------------------------------------+

| CREATION (10 bytes)                                                                                  |
| ---------------------------------------------------------------------------------------------------- |
| Opcode                                                                                               |
| ---------------------------------------------------------------------------------------------------  |
| 61 runSize                                                                                           |
| 3d                                                                                                   |
| 81                                                                                                   |
| 60 offset                                                                                            |
| 3d                                                                                                   |
| 39                                                                                                   |
| f3                                                                                                   |
| ---------------------------------------------------------------------------------------------------  |
| RUNTIME (98 bytes + extraLength)                                                                     |
| ---------------------------------------------------------------------------------------------------  |
| Opcode                                                                                               |
| ---------------------------------------------------------------------------------------------------  |
|                                                                                                      |
| ::: copy calldata to memory ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   |
| 36                                                                                                   |
| 3d                                                                                                   |
| 3d                                                                                                   |
| 37                                                                                                   |
|                                                                                                      |
| ::: keep some values in stack ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   |
| 3d                                                                                                   |
| 3d                                                                                                   |
| 3d                                                                                                   |
| 3d                                                                                                   |
| 61 extra                                                                                             |
|                                                                                                      |
| ::: copy extra data to memory ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   |
| 80                                                                                                   |
| 60 0x35                                                                                              |
| 36                                                                                                   |
| 39                                                                                                   |
|                                                                                                      |
| ::: delegate call to the implementation contract :::::::::::::::::::::::::::::::::::::::::::::::::   |
| 36                                                                                                   |
| 01                                                                                                   |
| 3d                                                                                                   |
| 73 addr                                                                                              |
| 5a                                                                                                   |
| f4                                                                                                   |
|                                                                                                      |
| ::: copy return data to memory :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   |
| 3d                                                                                                   |
| 3d                                                                                                   |
| 93                                                                                                   |
| 80                                                                                                   |
| 3e                                                                                                   |
|                                                                                                      |
| 60 0x33                                                                                              |
| 57                                                                                                   |
|                                                                                                      |
| ::: revert :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   |
| fd                                                                                                   |
|                                                                                                      |
| ::: return :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   |
| 5b                                                                                                   |
| f3                                                                                                   |
| ---------------------------------------------------------------------------------------------------+ |

*Returns the initialization code hash of the clone of `implementation`*\
*using immutable arguments encoded in `data`.*\
*Used for mining vanity addresses with create2crunch.*

```solidity
function initCodeHash(address implementation, bytes memory data) internal pure returns (bytes32 hash);
```

**Parameters**

| Name             | Type      | Description                                 |
| ---------------- | --------- | ------------------------------------------- |
| `implementation` | `address` | The address of the implementation contract. |
| `data`           | `bytes`   | The encoded immutable arguments.            |

**Returns**

| Name   | Type      | Description                   |
| ------ | --------- | ----------------------------- |
| `hash` | `bytes32` | The initialization code hash. |

### predictDeterministicAddress

*Returns the address of the deterministic clone of`implementation` using immutable arguments encoded in `data`, with `salt`, by `deployer`.*

```solidity
function predictDeterministicAddress(address implementation, bytes memory data, bytes32 salt, address deployer)
    internal
    pure
    returns (address predicted);
```

**Parameters**

| Name             | Type      | Description                                    |
| ---------------- | --------- | ---------------------------------------------- |
| `implementation` | `address` | The address of the implementation.             |
| `data`           | `bytes`   | The immutable arguments of the implementation. |
| `salt`           | `bytes32` | The salt used to compute the address.          |
| `deployer`       | `address` | The address of the deployer.                   |

**Returns**

| Name        | Type      | Description            |
| ----------- | --------- | ---------------------- |
| `predicted` | `address` | The predicted address. |

### predictDeterministicAddress

*Returns the address when a contract with initialization code hash,`hash`, is deployed with `salt`, by `deployer`.*

```solidity
function predictDeterministicAddress(bytes32 hash, bytes32 salt, address deployer)
    internal
    pure
    returns (address predicted);
```

**Parameters**

| Name       | Type      | Description                           |
| ---------- | --------- | ------------------------------------- |
| `hash`     | `bytes32` | The initialization code hash.         |
| `salt`     | `bytes32` | The salt used to compute the address. |
| `deployer` | `address` | The address of the deployer.          |

**Returns**

| Name        | Type      | Description            |
| ----------- | --------- | ---------------------- |
| `predicted` | `address` | The predicted address. |

## Errors

### DeploymentFailed

```solidity
error DeploymentFailed();
```

### PackedDataTooBig

```solidity
error PackedDataTooBig();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.lfj.gg/contracts/libraries/immutableclone.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
