Clones.behaviour.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const DummyImplementation = artifacts.require('DummyImplementation');
  4. module.exports = function shouldBehaveLikeClone (createClone) {
  5. before('deploy implementation', async function () {
  6. this.implementation = web3.utils.toChecksumAddress((await DummyImplementation.new()).address);
  7. });
  8. const assertProxyInitialization = function ({ value, balance }) {
  9. it('initializes the proxy', async function () {
  10. const dummy = new DummyImplementation(this.proxy);
  11. expect(await dummy.value()).to.be.bignumber.equal(value.toString());
  12. });
  13. it('has expected balance', async function () {
  14. expect(await web3.eth.getBalance(this.proxy)).to.be.bignumber.equal(balance.toString());
  15. });
  16. };
  17. describe('initialization without parameters', function () {
  18. describe('non payable', function () {
  19. const expectedInitializedValue = 10;
  20. const initializeData = new DummyImplementation('').contract.methods['initializeNonPayable()']().encodeABI();
  21. describe('when not sending balance', function () {
  22. beforeEach('creating proxy', async function () {
  23. this.proxy = (
  24. await createClone(this.implementation, initializeData)
  25. ).address;
  26. });
  27. assertProxyInitialization({
  28. value: expectedInitializedValue,
  29. balance: 0,
  30. });
  31. });
  32. describe('when sending some balance', function () {
  33. const value = 10e5;
  34. it('reverts', async function () {
  35. await expectRevert.unspecified(
  36. createClone(this.implementation, initializeData, { value }),
  37. );
  38. });
  39. });
  40. });
  41. describe('payable', function () {
  42. const expectedInitializedValue = 100;
  43. const initializeData = new DummyImplementation('').contract.methods['initializePayable()']().encodeABI();
  44. describe('when not sending balance', function () {
  45. beforeEach('creating proxy', async function () {
  46. this.proxy = (
  47. await createClone(this.implementation, initializeData)
  48. ).address;
  49. });
  50. assertProxyInitialization({
  51. value: expectedInitializedValue,
  52. balance: 0,
  53. });
  54. });
  55. describe('when sending some balance', function () {
  56. const value = 10e5;
  57. beforeEach('creating proxy', async function () {
  58. this.proxy = (
  59. await createClone(this.implementation, initializeData, { value })
  60. ).address;
  61. });
  62. assertProxyInitialization({
  63. value: expectedInitializedValue,
  64. balance: value,
  65. });
  66. });
  67. });
  68. });
  69. describe('initialization with parameters', function () {
  70. describe('non payable', function () {
  71. const expectedInitializedValue = 10;
  72. const initializeData = new DummyImplementation('').contract
  73. .methods.initializeNonPayableWithValue(expectedInitializedValue).encodeABI();
  74. describe('when not sending balance', function () {
  75. beforeEach('creating proxy', async function () {
  76. this.proxy = (
  77. await createClone(this.implementation, initializeData)
  78. ).address;
  79. });
  80. assertProxyInitialization({
  81. value: expectedInitializedValue,
  82. balance: 0,
  83. });
  84. });
  85. describe('when sending some balance', function () {
  86. const value = 10e5;
  87. it('reverts', async function () {
  88. await expectRevert.unspecified(
  89. createClone(this.implementation, initializeData, { value }),
  90. );
  91. });
  92. });
  93. });
  94. describe('payable', function () {
  95. const expectedInitializedValue = 42;
  96. const initializeData = new DummyImplementation('').contract
  97. .methods.initializePayableWithValue(expectedInitializedValue).encodeABI();
  98. describe('when not sending balance', function () {
  99. beforeEach('creating proxy', async function () {
  100. this.proxy = (
  101. await createClone(this.implementation, initializeData)
  102. ).address;
  103. });
  104. assertProxyInitialization({
  105. value: expectedInitializedValue,
  106. balance: 0,
  107. });
  108. });
  109. describe('when sending some balance', function () {
  110. const value = 10e5;
  111. beforeEach('creating proxy', async function () {
  112. this.proxy = (
  113. await createClone(this.implementation, initializeData, { value })
  114. ).address;
  115. });
  116. assertProxyInitialization({
  117. value: expectedInitializedValue,
  118. balance: value,
  119. });
  120. });
  121. });
  122. });
  123. };