Clones.sol 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <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. library Clones {
  15. /**
  16. * @dev Deploys and returns the address of a clone that mimics the behaviour of `master`.
  17. *
  18. * This function uses the create opcode, which should never revert.
  19. */
  20. function clone(address master) internal returns (address instance) {
  21. // solhint-disable-next-line no-inline-assembly
  22. assembly {
  23. let ptr := mload(0x40)
  24. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  25. mstore(add(ptr, 0x14), shl(0x60, master))
  26. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  27. instance := create(0, ptr, 0x37)
  28. }
  29. require(instance != address(0), "ERC1167: create failed");
  30. }
  31. /**
  32. * @dev Deploys and returns the address of a clone that mimics the behaviour of `master`.
  33. *
  34. * This function uses the create2 opcode and a `salt` to deterministically deploy
  35. * the clone. Using the same `master` and `salt` multiple time will revert, since
  36. * the clones cannot be deployed twice at the same address.
  37. */
  38. function cloneDeterministic(address master, bytes32 salt) internal returns (address instance) {
  39. // solhint-disable-next-line no-inline-assembly
  40. assembly {
  41. let ptr := mload(0x40)
  42. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  43. mstore(add(ptr, 0x14), shl(0x60, master))
  44. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  45. instance := create2(0, ptr, 0x37, salt)
  46. }
  47. require(instance != address(0), "ERC1167: create2 failed");
  48. }
  49. /**
  50. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  51. */
  52. function predictDeterministicAddress(address master, bytes32 salt, address deployer) internal pure returns (address predicted) {
  53. // solhint-disable-next-line no-inline-assembly
  54. assembly {
  55. let ptr := mload(0x40)
  56. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  57. mstore(add(ptr, 0x14), shl(0x60, master))
  58. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
  59. mstore(add(ptr, 0x38), shl(0x60, deployer))
  60. mstore(add(ptr, 0x4c), salt)
  61. mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
  62. predicted := keccak256(add(ptr, 0x37), 0x55)
  63. }
  64. }
  65. /**
  66. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  67. */
  68. function predictDeterministicAddress(address master, bytes32 salt) internal view returns (address predicted) {
  69. return predictDeterministicAddress(master, salt, address(this));
  70. }
  71. }