MultipleInheritanceInitializableMocks.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Initializable} from "../proxy/utils/Initializable.sol";
  4. // Sample contracts showing upgradeability with multiple inheritance.
  5. // Child contract inherits from Father and Mother contracts, and Father extends from Gramps.
  6. //
  7. // Human
  8. // / \
  9. // | Gramps
  10. // | |
  11. // Mother Father
  12. // | |
  13. // -- Child --
  14. /**
  15. * Sample base initializable contract that is a human
  16. */
  17. contract SampleHuman is Initializable {
  18. bool public isHuman;
  19. function initialize() public initializer {
  20. __SampleHuman_init();
  21. }
  22. // solhint-disable-next-line func-name-mixedcase
  23. function __SampleHuman_init() internal onlyInitializing {
  24. __SampleHuman_init_unchained();
  25. }
  26. // solhint-disable-next-line func-name-mixedcase
  27. function __SampleHuman_init_unchained() internal onlyInitializing {
  28. isHuman = true;
  29. }
  30. }
  31. /**
  32. * Sample base initializable contract that defines a field mother
  33. */
  34. contract SampleMother is Initializable, SampleHuman {
  35. uint256 public mother;
  36. function initialize(uint256 value) public initializer {
  37. __SampleMother_init(value);
  38. }
  39. // solhint-disable-next-line func-name-mixedcase
  40. function __SampleMother_init(uint256 value) internal onlyInitializing {
  41. __SampleHuman_init();
  42. __SampleMother_init_unchained(value);
  43. }
  44. // solhint-disable-next-line func-name-mixedcase
  45. function __SampleMother_init_unchained(uint256 value) internal onlyInitializing {
  46. mother = value;
  47. }
  48. }
  49. /**
  50. * Sample base initializable contract that defines a field gramps
  51. */
  52. contract SampleGramps is Initializable, SampleHuman {
  53. string public gramps;
  54. function initialize(string memory value) public initializer {
  55. __SampleGramps_init(value);
  56. }
  57. // solhint-disable-next-line func-name-mixedcase
  58. function __SampleGramps_init(string memory value) internal onlyInitializing {
  59. __SampleHuman_init();
  60. __SampleGramps_init_unchained(value);
  61. }
  62. // solhint-disable-next-line func-name-mixedcase
  63. function __SampleGramps_init_unchained(string memory value) internal onlyInitializing {
  64. gramps = value;
  65. }
  66. }
  67. /**
  68. * Sample base initializable contract that defines a field father and extends from gramps
  69. */
  70. contract SampleFather is Initializable, SampleGramps {
  71. uint256 public father;
  72. function initialize(string memory _gramps, uint256 _father) public initializer {
  73. __SampleFather_init(_gramps, _father);
  74. }
  75. // solhint-disable-next-line func-name-mixedcase
  76. function __SampleFather_init(string memory _gramps, uint256 _father) internal onlyInitializing {
  77. __SampleGramps_init(_gramps);
  78. __SampleFather_init_unchained(_father);
  79. }
  80. // solhint-disable-next-line func-name-mixedcase
  81. function __SampleFather_init_unchained(uint256 _father) internal onlyInitializing {
  82. father = _father;
  83. }
  84. }
  85. /**
  86. * Child extends from mother, father (gramps)
  87. */
  88. contract SampleChild is Initializable, SampleMother, SampleFather {
  89. uint256 public child;
  90. function initialize(uint256 _mother, string memory _gramps, uint256 _father, uint256 _child) public initializer {
  91. __SampleChild_init(_mother, _gramps, _father, _child);
  92. }
  93. // solhint-disable-next-line func-name-mixedcase
  94. function __SampleChild_init(
  95. uint256 _mother,
  96. string memory _gramps,
  97. uint256 _father,
  98. uint256 _child
  99. ) internal onlyInitializing {
  100. __SampleMother_init(_mother);
  101. __SampleFather_init(_gramps, _father);
  102. __SampleChild_init_unchained(_child);
  103. }
  104. // solhint-disable-next-line func-name-mixedcase
  105. function __SampleChild_init_unchained(uint256 _child) internal onlyInitializing {
  106. child = _child;
  107. }
  108. }