Clones.sol 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (proxy/Clones.sol)
  3. pragma solidity ^0.8.19;
  4. /**
  5. * @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
  6. * deploying minimal proxy contracts, also known as "clones".
  7. *
  8. * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
  9. * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
  10. *
  11. * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
  12. * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
  13. * deterministic method.
  14. */
  15. library Clones {
  16. /**
  17. * @dev A clone instance deployment failed.
  18. */
  19. error ERC1167FailedCreateClone();
  20. /**
  21. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  22. *
  23. * This function uses the create opcode, which should never revert.
  24. */
  25. function clone(address implementation) internal returns (address instance) {
  26. /// @solidity memory-safe-assembly
  27. assembly {
  28. // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
  29. // of the `implementation` address with the bytecode before the address.
  30. mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
  31. // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
  32. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
  33. instance := create(0, 0x09, 0x37)
  34. }
  35. if (instance == address(0)) {
  36. revert ERC1167FailedCreateClone();
  37. }
  38. }
  39. /**
  40. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  41. *
  42. * This function uses the create2 opcode and a `salt` to deterministically deploy
  43. * the clone. Using the same `implementation` and `salt` multiple time will revert, since
  44. * the clones cannot be deployed twice at the same address.
  45. */
  46. function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
  47. /// @solidity memory-safe-assembly
  48. assembly {
  49. // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
  50. // of the `implementation` address with the bytecode before the address.
  51. mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
  52. // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
  53. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
  54. instance := create2(0, 0x09, 0x37, salt)
  55. }
  56. if (instance == address(0)) {
  57. revert ERC1167FailedCreateClone();
  58. }
  59. }
  60. /**
  61. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  62. */
  63. function predictDeterministicAddress(
  64. address implementation,
  65. bytes32 salt,
  66. address deployer
  67. ) internal pure returns (address predicted) {
  68. /// @solidity memory-safe-assembly
  69. assembly {
  70. let ptr := mload(0x40)
  71. mstore(add(ptr, 0x38), deployer)
  72. mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
  73. mstore(add(ptr, 0x14), implementation)
  74. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
  75. mstore(add(ptr, 0x58), salt)
  76. mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
  77. predicted := keccak256(add(ptr, 0x43), 0x55)
  78. }
  79. }
  80. /**
  81. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  82. */
  83. function predictDeterministicAddress(
  84. address implementation,
  85. bytes32 salt
  86. ) internal view returns (address predicted) {
  87. return predictDeterministicAddress(implementation, salt, address(this));
  88. }
  89. }