Clones.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (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. let ptr := mload(0x40)
  27. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  28. mstore(add(ptr, 0x14), shl(0x60, implementation))
  29. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  30. instance := create(0, ptr, 0x37)
  31. }
  32. require(instance != address(0), "ERC1167: create failed");
  33. }
  34. /**
  35. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  36. *
  37. * This function uses the create2 opcode and a `salt` to deterministically deploy
  38. * the clone. Using the same `implementation` and `salt` multiple time will revert, since
  39. * the clones cannot be deployed twice at the same address.
  40. */
  41. function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
  42. /// @solidity memory-safe-assembly
  43. assembly {
  44. let ptr := mload(0x40)
  45. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  46. mstore(add(ptr, 0x14), shl(0x60, implementation))
  47. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  48. instance := create2(0, ptr, 0x37, salt)
  49. }
  50. require(instance != address(0), "ERC1167: create2 failed");
  51. }
  52. /**
  53. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  54. */
  55. function predictDeterministicAddress(
  56. address implementation,
  57. bytes32 salt,
  58. address deployer
  59. ) internal pure returns (address predicted) {
  60. /// @solidity memory-safe-assembly
  61. assembly {
  62. let ptr := mload(0x40)
  63. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  64. mstore(add(ptr, 0x14), shl(0x60, implementation))
  65. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
  66. mstore(add(ptr, 0x38), shl(0x60, deployer))
  67. mstore(add(ptr, 0x4c), salt)
  68. mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
  69. predicted := keccak256(add(ptr, 0x37), 0x55)
  70. }
  71. }
  72. /**
  73. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  74. */
  75. function predictDeterministicAddress(address implementation, bytes32 salt)
  76. internal
  77. view
  78. returns (address predicted)
  79. {
  80. return predictDeterministicAddress(implementation, salt, address(this));
  81. }
  82. }