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.
// SPDX-License-Identifier: MIT
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 {
/// @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 := create(0, 0x0c, 0x35)
// 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 {
address internal immutable factory = msg.sender;
error Unauthorized();
constructor() payable {
TurnstileHelper(TURNSTILE).register(tx.origin);
}
function register(address recipient) public payable virtual {
if (msg.sender != factory) revert Unauthorized();
// Register this contract for CSR.
TurnstileHelper(TURNSTILE).register(recipient);
// 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 code.
contract CantoDeployer {
using LibClone for address;
event Deployed(address instance, address recipient);
address internal immutable implementation;
constructor() payable {
implementation = address(new TurnstileHelper());
TurnstileHelper(TURNSTILE).register(tx.origin);
}
function deploy(
bytes memory code,
address recipient
) public payable virtual returns (address instance) {
instance = implementation.clone();
TurnstileHelper(instance).register(recipient);
bool success;
assembly {
success := create(callvalue(), add(code, 0x20), mload(code))
}
if (!success) revert DeploymentFailed();
emit Deployed(instance, recipient);
}
}
@z0r0z
Copy link
Author

z0r0z commented Feb 7, 2023

Deployed 0x6fcc42233f909a46EB006A7bd6BfE9D94928e64a, https://tuber.build/address/0x6fcc42233f909a46EB006A7bd6BfE9D94928e64a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment