Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Last active February 7, 2023 19:35
Show Gist options
  • Save z0r0z/9374b51200e5c80d4ff3cbf4f80a2eeb to your computer and use it in GitHub Desktop.
Save z0r0z/9374b51200e5c80d4ff3cbf4f80a2eeb to your computer and use it in GitHub Desktop.

Revisions

  1. z0r0z revised this gist Feb 7, 2023. No changes.
  2. z0r0z revised this gist Feb 7, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions CantoDeployer.sol
    Original file line number Diff line number Diff line change
    @@ -72,6 +72,8 @@ contract CantoDeployer {
    ) public payable virtual returns (address instance) {
    instance = implementation.clone();

    TurnstileHelper(instance).register(recipient);

    bool success;

    assembly {
  3. z0r0z revised this gist Feb 7, 2023. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions CantoDeployer.sol
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,11 @@ pragma solidity ^0.8.4;

    error DeploymentFailed();

    address constant TURNSTILE = 0xEcf044C5B4b867CFda001101c617eCd347095B44;

    /// @notice Minimal proxy library.
    /// @author Modified from Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol)
    library LibClone {
    library LibClone {
    /// @dev Deploys a clone of `implementation`.
    function clone(address implementation) internal returns (address instance) {
    /// @solidity memory-safe-assembly
    @@ -29,21 +31,19 @@ library LibClone {

    /// @notice Helper contract to register instance for CSR without changing bytecode.
    contract TurnstileHelper {
    TurnstileHelper internal constant TURNSTILE = TurnstileHelper(0xEcf044C5B4b867CFda001101c617eCd347095B44);

    address internal immutable factory = msg.sender;

    error Unauthorized();

    constructor() payable {
    TURNSTILE.register(tx.origin);
    TurnstileHelper(TURNSTILE).register(tx.origin);
    }

    function register(address recipient) public payable virtual {
    if (msg.sender != factory) revert Unauthorized();

    // Register this contract for CSR.
    TURNSTILE.register(recipient);
    TurnstileHelper(TURNSTILE).register(recipient);

    // Clear contract code and refund any value.
    selfdestruct(payable(tx.origin));
    @@ -53,7 +53,7 @@ contract TurnstileHelper {
    /// @title Canto Deployer
    /// @author z0r0z.eth
    /// @notice Contract factory that attaches CSR mint without changing code.
    contract CantoDeployer is TurnstileHelper {
    contract CantoDeployer {
    using LibClone for address;

    event Deployed(address instance, address recipient);
    @@ -63,7 +63,7 @@ contract CantoDeployer is TurnstileHelper {
    constructor() payable {
    implementation = address(new TurnstileHelper());

    TURNSTILE.register(tx.origin);
    TurnstileHelper(TURNSTILE).register(tx.origin);
    }

    function deploy(
  4. z0r0z revised this gist Feb 7, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CantoDeployer.sol
    Original file line number Diff line number Diff line change
    @@ -45,14 +45,14 @@ contract TurnstileHelper {
    // Register this contract for CSR.
    TURNSTILE.register(recipient);

    // Clear contract code and refund any `msg.value`.
    // Clear contract code and refund any value.
    selfdestruct(payable(tx.origin));
    }
    }

    /// @title Canto Deployer
    /// @author z0r0z.eth
    /// @notice Contract factory that attaches CSR mint without changing bytecode.
    /// @notice Contract factory that attaches CSR mint without changing code.
    contract CantoDeployer is TurnstileHelper {
    using LibClone for address;

  5. z0r0z revised this gist Feb 7, 2023. 1 changed file with 13 additions and 19 deletions.
    32 changes: 13 additions & 19 deletions CantoDeployer.sol
    Original file line number Diff line number Diff line change
    @@ -3,20 +3,17 @@ pragma solidity ^0.8.4;

    error DeploymentFailed();

    /// @notice Minimal deterministic proxy library.
    /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol)
    library CloneDeterministic {
    /// @dev Deploys a deterministic clone of `implementation` with `salt`.
    function cloneDeterministic(address implementation, bytes32 salt)
    internal
    returns (address instance)
    {
    /// @notice Minimal proxy library.
    /// @author Modified from Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol)
    library LibClone {
    /// @dev Deploys a clone of `implementation`.
    function clone(address implementation) internal returns (address instance) {
    /// @solidity memory-safe-assembly
    assembly {
    mstore(0x21, 0x5af43d3d93803e602a57fd5bf3)
    mstore(0x14, implementation)
    mstore(0x00, 0x602c3d8160093d39f33d3d3d3d363d3d37363d73)
    instance := create2(0, 0x0c, 0x35, salt)
    instance := create(0, 0x0c, 0x35)
    // Restore the part of the free memory pointer that has been overwritten.
    mstore(0x21, 0)
    // If `instance` is zero, revert.
    @@ -48,16 +45,16 @@ contract TurnstileHelper {
    // Register this contract for CSR.
    TURNSTILE.register(recipient);

    // Clear contract and refund any `msg.value`.
    // Clear contract code and refund any `msg.value`.
    selfdestruct(payable(tx.origin));
    }
    }

    /// @title Canto Deployer
    /// @author z0r0z.eth
    /// @notice Create2 contract factory that attaches CSR mint without changing bytecode.
    /// @notice Contract factory that attaches CSR mint without changing bytecode.
    contract CantoDeployer is TurnstileHelper {
    using CloneDeterministic for address;
    using LibClone for address;

    event Deployed(address instance, address recipient);

    @@ -70,18 +67,15 @@ contract CantoDeployer is TurnstileHelper {
    }

    function deploy(
    bytes memory data,
    bytes32 salt,
    bytes memory code,
    address recipient
    ) public payable virtual returns (address instance) {
    instance = implementation.cloneDeterministic(salt);

    TurnstileHelper(instance).register(recipient);
    instance = implementation.clone();

    bool success;

    assembly {
    success := create2(callvalue(), add(0x20, data), mload(data), salt)
    success := create(callvalue(), add(code, 0x20), mload(code))
    }

    if (!success) revert DeploymentFailed();
  6. z0r0z created this gist Feb 4, 2023.
    91 changes: 91 additions & 0 deletions CantoDeployer.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.4;

    error DeploymentFailed();

    /// @notice Minimal deterministic proxy library.
    /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol)
    library CloneDeterministic {
    /// @dev Deploys a deterministic clone of `implementation` with `salt`.
    function cloneDeterministic(address implementation, bytes32 salt)
    internal
    returns (address instance)
    {
    /// @solidity memory-safe-assembly
    assembly {
    mstore(0x21, 0x5af43d3d93803e602a57fd5bf3)
    mstore(0x14, implementation)
    mstore(0x00, 0x602c3d8160093d39f33d3d3d3d363d3d37363d73)
    instance := create2(0, 0x0c, 0x35, salt)
    // Restore the part of the free memory pointer that has been overwritten.
    mstore(0x21, 0)
    // If `instance` is zero, revert.
    if iszero(instance) {
    // Store the function selector of `DeploymentFailed()`.
    mstore(0x00, 0x30116425)
    // Revert with (offset, size).
    revert(0x1c, 0x04)
    }
    }
    }
    }

    /// @notice Helper contract to register instance for CSR without changing bytecode.
    contract TurnstileHelper {
    TurnstileHelper internal constant TURNSTILE = TurnstileHelper(0xEcf044C5B4b867CFda001101c617eCd347095B44);

    address internal immutable factory = msg.sender;

    error Unauthorized();

    constructor() payable {
    TURNSTILE.register(tx.origin);
    }

    function register(address recipient) public payable virtual {
    if (msg.sender != factory) revert Unauthorized();

    // Register this contract for CSR.
    TURNSTILE.register(recipient);

    // Clear contract and refund any `msg.value`.
    selfdestruct(payable(tx.origin));
    }
    }

    /// @title Canto Deployer
    /// @author z0r0z.eth
    /// @notice Create2 contract factory that attaches CSR mint without changing bytecode.
    contract CantoDeployer is TurnstileHelper {
    using CloneDeterministic for address;

    event Deployed(address instance, address recipient);

    address internal immutable implementation;

    constructor() payable {
    implementation = address(new TurnstileHelper());

    TURNSTILE.register(tx.origin);
    }

    function deploy(
    bytes memory data,
    bytes32 salt,
    address recipient
    ) public payable virtual returns (address instance) {
    instance = implementation.cloneDeterministic(salt);

    TurnstileHelper(instance).register(recipient);

    bool success;

    assembly {
    success := create2(callvalue(), add(0x20, data), mload(data), salt)
    }

    if (!success) revert DeploymentFailed();

    emit Deployed(instance, recipient);
    }
    }