Clones.sol 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 `implementation`.
  19. *
  20. * This function uses the create opcode, which should never revert.
  21. */
  22. function clone(address implementation) internal returns (address instance) {
  23. assembly {
  24. let ptr := mload(0x40)
  25. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  26. mstore(add(ptr, 0x14), shl(0x60, implementation))
  27. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
  28. instance := create(0, ptr, 0x37)
  29. }
  30. require(instance != address(0), "ERC1167: create failed");
  31. }
  32. /**
  33. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  34. *
  35. * This function uses the create2 opcode and a `salt` to deterministically deploy
  36. * the clone. Using the same `implementation` and `salt` multiple time will revert, since
  37. * the clones cannot be deployed twice at the same address.
  38. */
  39. function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
  40. assembly {
  41. let ptr := mload(0x40)
  42. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  43. mstore(add(ptr, 0x14), shl(0x60, implementation))
  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(
  53. address implementation,
  54. bytes32 salt,
  55. address deployer
  56. ) internal pure returns (address predicted) {
  57. assembly {
  58. let ptr := mload(0x40)
  59. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
  60. mstore(add(ptr, 0x14), shl(0x60, implementation))
  61. mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
  62. mstore(add(ptr, 0x38), shl(0x60, deployer))
  63. mstore(add(ptr, 0x4c), salt)
  64. mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
  65. predicted := keccak256(add(ptr, 0x37), 0x55)
  66. }
  67. }
  68. /**
  69. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  70. */
  71. function predictDeterministicAddress(address implementation, bytes32 salt)
  72. internal
  73. view
  74. returns (address predicted)
  75. {
  76. return predictDeterministicAddress(implementation, salt, address(this));
  77. }
  78. }