Clones.sol 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (proxy/Clones.sol)
  3. pragma solidity ^0.8.0;
  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. * _Available since v3.4._
  16. */
  17. library Clones {
  18. /**
  19. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  20. *
  21. * This function uses the create opcode, which should never revert.
  22. */
  23. function clone(address implementation) internal returns (address instance) {
  24. /// @solidity memory-safe-assembly
  25. assembly {
  26. // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
  27. // of the `implementation` address with the bytecode before the address.
  28. mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
  29. // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
  30. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
  31. instance := create(0, 0x09, 0x37)
  32. }
  33. require(instance != address(0), "ERC1167: create failed");
  34. }
  35. /**
  36. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  37. *
  38. * This function uses the create2 opcode and a `salt` to deterministically deploy
  39. * the clone. Using the same `implementation` and `salt` multiple time will revert, since
  40. * the clones cannot be deployed twice at the same address.
  41. */
  42. function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
  43. /// @solidity memory-safe-assembly
  44. assembly {
  45. // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
  46. // of the `implementation` address with the bytecode before the address.
  47. mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
  48. // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
  49. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
  50. instance := create2(0, 0x09, 0x37, salt)
  51. }
  52. require(instance != address(0), "ERC1167: create2 failed");
  53. }
  54. /**
  55. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  56. */
  57. function predictDeterministicAddress(
  58. address implementation,
  59. bytes32 salt,
  60. address deployer
  61. ) internal pure returns (address predicted) {
  62. /// @solidity memory-safe-assembly
  63. assembly {
  64. let ptr := mload(0x40)
  65. mstore(add(ptr, 0x38), deployer)
  66. mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
  67. mstore(add(ptr, 0x14), implementation)
  68. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
  69. mstore(add(ptr, 0x58), salt)
  70. mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
  71. predicted := keccak256(add(ptr, 0x43), 0x55)
  72. }
  73. }
  74. /**
  75. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  76. */
  77. function predictDeterministicAddress(
  78. address implementation,
  79. bytes32 salt
  80. ) internal view returns (address predicted) {
  81. return predictDeterministicAddress(implementation, salt, address(this));
  82. }
  83. }