Clones.behaviour.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. module.exports = function shouldBehaveLikeClone() {
  4. const assertProxyInitialization = function ({ value, balance }) {
  5. it('initializes the proxy', async function () {
  6. const dummy = await ethers.getContractAt('DummyImplementation', this.proxy);
  7. expect(await dummy.value()).to.equal(value);
  8. });
  9. it('has expected balance', async function () {
  10. expect(await ethers.provider.getBalance(this.proxy)).to.equal(balance);
  11. });
  12. };
  13. describe('initialization without parameters', function () {
  14. describe('non payable', function () {
  15. const expectedInitializedValue = 10n;
  16. beforeEach(async function () {
  17. this.initializeData = await this.implementation.interface.encodeFunctionData('initializeNonPayable');
  18. });
  19. describe('when not sending balance', function () {
  20. beforeEach('creating proxy', async function () {
  21. this.proxy = await this.createClone(this.initializeData);
  22. });
  23. assertProxyInitialization({
  24. value: expectedInitializedValue,
  25. balance: 0,
  26. });
  27. });
  28. describe('when sending some balance', function () {
  29. const value = 10n ** 6n;
  30. it('reverts', async function () {
  31. await expect(this.createClone(this.initializeData, { value })).to.be.reverted;
  32. });
  33. });
  34. });
  35. describe('payable', function () {
  36. const expectedInitializedValue = 100n;
  37. beforeEach(async function () {
  38. this.initializeData = await this.implementation.interface.encodeFunctionData('initializePayable');
  39. });
  40. describe('when not sending balance', function () {
  41. beforeEach('creating proxy', async function () {
  42. this.proxy = await this.createClone(this.initializeData);
  43. });
  44. assertProxyInitialization({
  45. value: expectedInitializedValue,
  46. balance: 0,
  47. });
  48. });
  49. describe('when sending some balance', function () {
  50. const value = 10n ** 6n;
  51. beforeEach('creating proxy', async function () {
  52. this.proxy = await this.createClone(this.initializeData, { value });
  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 = 10n;
  64. beforeEach(async function () {
  65. this.initializeData = await this.implementation.interface.encodeFunctionData('initializeNonPayableWithValue', [
  66. expectedInitializedValue,
  67. ]);
  68. });
  69. describe('when not sending balance', function () {
  70. beforeEach('creating proxy', async function () {
  71. this.proxy = await this.createClone(this.initializeData);
  72. });
  73. assertProxyInitialization({
  74. value: expectedInitializedValue,
  75. balance: 0,
  76. });
  77. });
  78. describe('when sending some balance', function () {
  79. const value = 10n ** 6n;
  80. it('reverts', async function () {
  81. await expect(this.createClone(this.initializeData, { value })).to.be.reverted;
  82. });
  83. });
  84. });
  85. describe('payable', function () {
  86. const expectedInitializedValue = 42n;
  87. beforeEach(function () {
  88. this.initializeData = this.implementation.interface.encodeFunctionData('initializePayableWithValue', [
  89. expectedInitializedValue,
  90. ]);
  91. });
  92. describe('when not sending balance', function () {
  93. beforeEach('creating proxy', async function () {
  94. this.proxy = await this.createClone(this.initializeData);
  95. });
  96. assertProxyInitialization({
  97. value: expectedInitializedValue,
  98. balance: 0,
  99. });
  100. });
  101. describe('when sending some balance', function () {
  102. const value = 10n ** 6n;
  103. beforeEach('creating proxy', async function () {
  104. this.proxy = await this.createClone(this.initializeData, { value });
  105. });
  106. assertProxyInitialization({
  107. value: expectedInitializedValue,
  108. balance: value,
  109. });
  110. });
  111. });
  112. });
  113. };