Clones.behaviour.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = (await createClone(this.implementation, initializeData)).address;
  24. });
  25. assertProxyInitialization({
  26. value: expectedInitializedValue,
  27. balance: 0,
  28. });
  29. });
  30. describe('when sending some balance', function () {
  31. const value = 10e5;
  32. it('reverts', async function () {
  33. await expectRevert.unspecified(createClone(this.implementation, initializeData, { value }));
  34. });
  35. });
  36. });
  37. describe('payable', function () {
  38. const expectedInitializedValue = 100;
  39. const initializeData = new DummyImplementation('').contract.methods['initializePayable()']().encodeABI();
  40. describe('when not sending balance', function () {
  41. beforeEach('creating proxy', async function () {
  42. this.proxy = (await createClone(this.implementation, initializeData)).address;
  43. });
  44. assertProxyInitialization({
  45. value: expectedInitializedValue,
  46. balance: 0,
  47. });
  48. });
  49. describe('when sending some balance', function () {
  50. const value = 10e5;
  51. beforeEach('creating proxy', async function () {
  52. this.proxy = (await createClone(this.implementation, initializeData, { value })).address;
  53. });
  54. assertProxyInitialization({
  55. value: expectedInitializedValue,
  56. balance: value,
  57. });
  58. });
  59. });
  60. });
  61. describe('initialization with parameters', function () {
  62. describe('non payable', function () {
  63. const expectedInitializedValue = 10;
  64. const initializeData = new DummyImplementation('').contract.methods
  65. .initializeNonPayableWithValue(expectedInitializedValue)
  66. .encodeABI();
  67. describe('when not sending balance', function () {
  68. beforeEach('creating proxy', async function () {
  69. this.proxy = (await createClone(this.implementation, initializeData)).address;
  70. });
  71. assertProxyInitialization({
  72. value: expectedInitializedValue,
  73. balance: 0,
  74. });
  75. });
  76. describe('when sending some balance', function () {
  77. const value = 10e5;
  78. it('reverts', async function () {
  79. await expectRevert.unspecified(createClone(this.implementation, initializeData, { value }));
  80. });
  81. });
  82. });
  83. describe('payable', function () {
  84. const expectedInitializedValue = 42;
  85. const initializeData = new DummyImplementation('').contract.methods
  86. .initializePayableWithValue(expectedInitializedValue)
  87. .encodeABI();
  88. describe('when not sending balance', function () {
  89. beforeEach('creating proxy', async function () {
  90. this.proxy = (await createClone(this.implementation, initializeData)).address;
  91. });
  92. assertProxyInitialization({
  93. value: expectedInitializedValue,
  94. balance: 0,
  95. });
  96. });
  97. describe('when sending some balance', function () {
  98. const value = 10e5;
  99. beforeEach('creating proxy', async function () {
  100. this.proxy = (await createClone(this.implementation, initializeData, { value })).address;
  101. });
  102. assertProxyInitialization({
  103. value: expectedInitializedValue,
  104. balance: value,
  105. });
  106. });
  107. });
  108. });
  109. };