Proxy.behaviour.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { getSlot, ImplementationSlot } = require('../helpers/erc1967');
  3. const { expect } = require('chai');
  4. const DummyImplementation = artifacts.require('DummyImplementation');
  5. module.exports = function shouldBehaveLikeProxy(createProxy, proxyAdminAddress, proxyCreator) {
  6. it('cannot be initialized with a non-contract address', async function () {
  7. const nonContractAddress = proxyCreator;
  8. const initializeData = Buffer.from('');
  9. await expectRevert.unspecified(
  10. createProxy(nonContractAddress, proxyAdminAddress, initializeData, {
  11. from: proxyCreator,
  12. }),
  13. );
  14. });
  15. before('deploy implementation', async function () {
  16. this.implementation = web3.utils.toChecksumAddress((await DummyImplementation.new()).address);
  17. });
  18. const assertProxyInitialization = function ({ value, balance }) {
  19. it('sets the implementation address', async function () {
  20. const implementationSlot = await getSlot(this.proxy, ImplementationSlot);
  21. const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
  22. expect(implementationAddress).to.be.equal(this.implementation);
  23. });
  24. it('initializes the proxy', async function () {
  25. const dummy = new DummyImplementation(this.proxy);
  26. expect(await dummy.value()).to.be.bignumber.equal(value.toString());
  27. });
  28. it('has expected balance', async function () {
  29. expect(await web3.eth.getBalance(this.proxy)).to.be.bignumber.equal(balance.toString());
  30. });
  31. };
  32. describe('without initialization', function () {
  33. const initializeData = Buffer.from('');
  34. describe('when not sending balance', function () {
  35. beforeEach('creating proxy', async function () {
  36. this.proxy = (
  37. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  38. from: proxyCreator,
  39. })
  40. ).address;
  41. });
  42. assertProxyInitialization({ value: 0, balance: 0 });
  43. });
  44. describe('when sending some balance', function () {
  45. const value = 10e5;
  46. beforeEach('creating proxy', async function () {
  47. this.proxy = (
  48. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  49. from: proxyCreator,
  50. value,
  51. })
  52. ).address;
  53. });
  54. assertProxyInitialization({ value: 0, balance: value });
  55. });
  56. });
  57. describe('initialization without parameters', function () {
  58. describe('non payable', function () {
  59. const expectedInitializedValue = 10;
  60. const initializeData = new DummyImplementation('').contract.methods['initializeNonPayable()']().encodeABI();
  61. describe('when not sending balance', function () {
  62. beforeEach('creating proxy', async function () {
  63. this.proxy = (
  64. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  65. from: proxyCreator,
  66. })
  67. ).address;
  68. });
  69. assertProxyInitialization({
  70. value: expectedInitializedValue,
  71. balance: 0,
  72. });
  73. });
  74. describe('when sending some balance', function () {
  75. const value = 10e5;
  76. it('reverts', async function () {
  77. await expectRevert.unspecified(
  78. createProxy(this.implementation, proxyAdminAddress, initializeData, { from: proxyCreator, value }),
  79. );
  80. });
  81. });
  82. });
  83. describe('payable', function () {
  84. const expectedInitializedValue = 100;
  85. const initializeData = new DummyImplementation('').contract.methods['initializePayable()']().encodeABI();
  86. describe('when not sending balance', function () {
  87. beforeEach('creating proxy', async function () {
  88. this.proxy = (
  89. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  90. from: proxyCreator,
  91. })
  92. ).address;
  93. });
  94. assertProxyInitialization({
  95. value: expectedInitializedValue,
  96. balance: 0,
  97. });
  98. });
  99. describe('when sending some balance', function () {
  100. const value = 10e5;
  101. beforeEach('creating proxy', async function () {
  102. this.proxy = (
  103. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  104. from: proxyCreator,
  105. value,
  106. })
  107. ).address;
  108. });
  109. assertProxyInitialization({
  110. value: expectedInitializedValue,
  111. balance: value,
  112. });
  113. });
  114. });
  115. });
  116. describe('initialization with parameters', function () {
  117. describe('non payable', function () {
  118. const expectedInitializedValue = 10;
  119. const initializeData = new DummyImplementation('').contract.methods
  120. .initializeNonPayableWithValue(expectedInitializedValue)
  121. .encodeABI();
  122. describe('when not sending balance', function () {
  123. beforeEach('creating proxy', async function () {
  124. this.proxy = (
  125. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  126. from: proxyCreator,
  127. })
  128. ).address;
  129. });
  130. assertProxyInitialization({
  131. value: expectedInitializedValue,
  132. balance: 0,
  133. });
  134. });
  135. describe('when sending some balance', function () {
  136. const value = 10e5;
  137. it('reverts', async function () {
  138. await expectRevert.unspecified(
  139. createProxy(this.implementation, proxyAdminAddress, initializeData, { from: proxyCreator, value }),
  140. );
  141. });
  142. });
  143. });
  144. describe('payable', function () {
  145. const expectedInitializedValue = 42;
  146. const initializeData = new DummyImplementation('').contract.methods
  147. .initializePayableWithValue(expectedInitializedValue)
  148. .encodeABI();
  149. describe('when not sending balance', function () {
  150. beforeEach('creating proxy', async function () {
  151. this.proxy = (
  152. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  153. from: proxyCreator,
  154. })
  155. ).address;
  156. });
  157. assertProxyInitialization({
  158. value: expectedInitializedValue,
  159. balance: 0,
  160. });
  161. });
  162. describe('when sending some balance', function () {
  163. const value = 10e5;
  164. beforeEach('creating proxy', async function () {
  165. this.proxy = (
  166. await createProxy(this.implementation, proxyAdminAddress, initializeData, {
  167. from: proxyCreator,
  168. value,
  169. })
  170. ).address;
  171. });
  172. assertProxyInitialization({
  173. value: expectedInitializedValue,
  174. balance: value,
  175. });
  176. });
  177. });
  178. describe('reverting initialization', function () {
  179. const initializeData = new DummyImplementation('').contract.methods.reverts().encodeABI();
  180. it('reverts', async function () {
  181. await expectRevert(
  182. createProxy(this.implementation, proxyAdminAddress, initializeData, { from: proxyCreator }),
  183. 'DummyImplementation reverted',
  184. );
  185. });
  186. });
  187. });
  188. };