Clones.sol 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. /**
  4. * @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
  5. * deploying minimal proxy contracts, also known as "clones".
  6. *
  7. * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
  8. * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
  9. *
  10. * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
  11. * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
  12. * deterministic method.
  13. *
  14. * _Available since v3.4._
  15. */
  16. library Clones {
  17. /**
  18. * @dev Deploys and returns the address of a clone that mimics the behaviour of `master`.
  19. *
  20. * This function uses the create opcode, which should never revert.
  21. */
  22. function clone(address master) internal returns (address instance) {
  23. // solhint-disable-next-line no-inline-assembly
  24. assembly {
  25. let ptr := mload(0x40)
  26. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  27. mstore(add(ptr, 0x14), shl(0x60, master))
  28. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  29. instance := create(0, ptr, 0x37)
  30. }
  31. require(instance != address(0), "ERC1167: create failed");
  32. }
  33. /**
  34. * @dev Deploys and returns the address of a clone that mimics the behaviour of `master`.
  35. *
  36. * This function uses the create2 opcode and a `salt` to deterministically deploy
  37. * the clone. Using the same `master` and `salt` multiple time will revert, since
  38. * the clones cannot be deployed twice at the same address.
  39. */
  40. function cloneDeterministic(address master, bytes32 salt) internal returns (address instance) {
  41. // solhint-disable-next-line no-inline-assembly
  42. assembly {
  43. let ptr := mload(0x40)
  44. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  45. mstore(add(ptr, 0x14), shl(0x60, master))
  46. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  47. instance := create2(0, ptr, 0x37, salt)
  48. }
  49. require(instance != address(0), "ERC1167: create2 failed");
  50. }
  51. /**
  52. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  53. */
  54. function predictDeterministicAddress(address master, bytes32 salt, address deployer) internal pure returns (address predicted) {
  55. // solhint-disable-next-line no-inline-assembly
  56. assembly {
  57. let ptr := mload(0x40)
  58. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  59. mstore(add(ptr, 0x14), shl(0x60, master))
  60. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
  61. mstore(add(ptr, 0x38), shl(0x60, deployer))
  62. mstore(add(ptr, 0x4c), salt)
  63. mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
  64. predicted := keccak256(add(ptr, 0x37), 0x55)
  65. }
  66. }
  67. /**
  68. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  69. */
  70. function predictDeterministicAddress(address master, bytes32 salt) internal view returns (address predicted) {
  71. return predictDeterministicAddress(master, salt, address(this));
  72. }
  73. }