Address.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. const { balance, constants, ether, expectRevert, send, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Address = artifacts.require('$Address');
  4. const EtherReceiver = artifacts.require('EtherReceiverMock');
  5. const CallReceiverMock = artifacts.require('CallReceiverMock');
  6. contract('Address', function (accounts) {
  7. const [recipient, other] = accounts;
  8. beforeEach(async function () {
  9. this.mock = await Address.new();
  10. });
  11. describe('sendValue', function () {
  12. beforeEach(async function () {
  13. this.recipientTracker = await balance.tracker(recipient);
  14. });
  15. context('when sender contract has no funds', function () {
  16. it('sends 0 wei', async function () {
  17. await this.mock.$sendValue(other, 0);
  18. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  19. });
  20. it('reverts when sending non-zero amounts', async function () {
  21. await expectRevert(this.mock.$sendValue(other, 1), 'Address: insufficient balance');
  22. });
  23. });
  24. context('when sender contract has funds', function () {
  25. const funds = ether('1');
  26. beforeEach(async function () {
  27. await send.ether(other, this.mock.address, funds);
  28. });
  29. it('sends 0 wei', async function () {
  30. await this.mock.$sendValue(recipient, 0);
  31. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  32. });
  33. it('sends non-zero amounts', async function () {
  34. await this.mock.$sendValue(recipient, funds.subn(1));
  35. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds.subn(1));
  36. });
  37. it('sends the whole balance', async function () {
  38. await this.mock.$sendValue(recipient, funds);
  39. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds);
  40. expect(await balance.current(this.mock.address)).to.be.bignumber.equal('0');
  41. });
  42. it('reverts when sending more than the balance', async function () {
  43. await expectRevert(this.mock.$sendValue(recipient, funds.addn(1)), 'Address: insufficient balance');
  44. });
  45. context('with contract recipient', function () {
  46. beforeEach(async function () {
  47. this.target = await EtherReceiver.new();
  48. });
  49. it('sends funds', async function () {
  50. const tracker = await balance.tracker(this.target.address);
  51. await this.target.setAcceptEther(true);
  52. await this.mock.$sendValue(this.target.address, funds);
  53. expect(await tracker.delta()).to.be.bignumber.equal(funds);
  54. });
  55. it('reverts on recipient revert', async function () {
  56. await this.target.setAcceptEther(false);
  57. await expectRevert(
  58. this.mock.$sendValue(this.target.address, funds),
  59. 'Address: unable to send value, recipient may have reverted',
  60. );
  61. });
  62. });
  63. });
  64. });
  65. describe('functionCall', function () {
  66. beforeEach(async function () {
  67. this.target = await CallReceiverMock.new();
  68. });
  69. context('with valid contract receiver', function () {
  70. it('calls the requested function', async function () {
  71. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  72. const receipt = await this.mock.$functionCall(this.target.address, abiEncodedCall);
  73. expectEvent(receipt, 'return$functionCall_address_bytes', {
  74. ret0: web3.eth.abi.encodeParameters(['string'], ['0x1234']),
  75. });
  76. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  77. });
  78. it('reverts when the called function reverts with no reason', async function () {
  79. const abiEncodedCall = this.target.contract.methods.mockFunctionRevertsNoReason().encodeABI();
  80. await expectRevert(
  81. this.mock.$functionCall(this.target.address, abiEncodedCall),
  82. 'Address: low-level call failed',
  83. );
  84. });
  85. it('reverts when the called function reverts, bubbling up the revert reason', async function () {
  86. const abiEncodedCall = this.target.contract.methods.mockFunctionRevertsReason().encodeABI();
  87. await expectRevert(this.mock.$functionCall(this.target.address, abiEncodedCall), 'CallReceiverMock: reverting');
  88. });
  89. it('reverts when the called function runs out of gas', async function () {
  90. const abiEncodedCall = this.target.contract.methods.mockFunctionOutOfGas().encodeABI();
  91. await expectRevert(
  92. this.mock.$functionCall(this.target.address, abiEncodedCall, { gas: '120000' }),
  93. 'Address: low-level call failed',
  94. );
  95. });
  96. it('reverts when the called function throws', async function () {
  97. const abiEncodedCall = this.target.contract.methods.mockFunctionThrows().encodeABI();
  98. await expectRevert.unspecified(this.mock.$functionCall(this.target.address, abiEncodedCall));
  99. });
  100. it('reverts when function does not exist', async function () {
  101. const abiEncodedCall = web3.eth.abi.encodeFunctionCall(
  102. {
  103. name: 'mockFunctionDoesNotExist',
  104. type: 'function',
  105. inputs: [],
  106. },
  107. [],
  108. );
  109. await expectRevert(
  110. this.mock.$functionCall(this.target.address, abiEncodedCall),
  111. 'Address: low-level call failed',
  112. );
  113. });
  114. });
  115. context('with non-contract receiver', function () {
  116. it('reverts when address is not a contract', async function () {
  117. const [recipient] = accounts;
  118. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  119. await expectRevert(this.mock.$functionCall(recipient, abiEncodedCall), 'Address: call to non-contract');
  120. });
  121. });
  122. });
  123. describe('functionCallWithValue', function () {
  124. beforeEach(async function () {
  125. this.target = await CallReceiverMock.new();
  126. });
  127. context('with zero value', function () {
  128. it('calls the requested function', async function () {
  129. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  130. const receipt = await this.mock.$functionCallWithValue(this.target.address, abiEncodedCall, 0);
  131. expectEvent(receipt, 'return$functionCallWithValue_address_bytes_uint256', {
  132. ret0: web3.eth.abi.encodeParameters(['string'], ['0x1234']),
  133. });
  134. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  135. });
  136. });
  137. context('with non-zero value', function () {
  138. const amount = ether('1.2');
  139. it('reverts if insufficient sender balance', async function () {
  140. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  141. await expectRevert(
  142. this.mock.$functionCallWithValue(this.target.address, abiEncodedCall, amount),
  143. 'Address: insufficient balance for call',
  144. );
  145. });
  146. it('calls the requested function with existing value', async function () {
  147. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  148. const tracker = await balance.tracker(this.target.address);
  149. await send.ether(other, this.mock.address, amount);
  150. const receipt = await this.mock.$functionCallWithValue(this.target.address, abiEncodedCall, amount);
  151. expectEvent(receipt, 'return$functionCallWithValue_address_bytes_uint256', {
  152. ret0: web3.eth.abi.encodeParameters(['string'], ['0x1234']),
  153. });
  154. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  155. expect(await tracker.delta()).to.be.bignumber.equal(amount);
  156. });
  157. it('calls the requested function with transaction funds', async function () {
  158. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  159. const tracker = await balance.tracker(this.target.address);
  160. expect(await balance.current(this.mock.address)).to.be.bignumber.equal('0');
  161. const receipt = await this.mock.$functionCallWithValue(this.target.address, abiEncodedCall, amount, {
  162. from: other,
  163. value: amount,
  164. });
  165. expectEvent(receipt, 'return$functionCallWithValue_address_bytes_uint256', {
  166. ret0: web3.eth.abi.encodeParameters(['string'], ['0x1234']),
  167. });
  168. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  169. expect(await tracker.delta()).to.be.bignumber.equal(amount);
  170. });
  171. it('reverts when calling non-payable functions', async function () {
  172. const abiEncodedCall = this.target.contract.methods.mockFunctionNonPayable().encodeABI();
  173. await send.ether(other, this.mock.address, amount);
  174. await expectRevert(
  175. this.mock.$functionCallWithValue(this.target.address, abiEncodedCall, amount),
  176. 'Address: low-level call with value failed',
  177. );
  178. });
  179. });
  180. });
  181. describe('functionStaticCall', function () {
  182. beforeEach(async function () {
  183. this.target = await CallReceiverMock.new();
  184. });
  185. it('calls the requested function', async function () {
  186. const abiEncodedCall = this.target.contract.methods.mockStaticFunction().encodeABI();
  187. expect(await this.mock.$functionStaticCall(this.target.address, abiEncodedCall)).to.be.equal(
  188. web3.eth.abi.encodeParameters(['string'], ['0x1234']),
  189. );
  190. });
  191. it('reverts on a non-static function', async function () {
  192. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  193. await expectRevert(
  194. this.mock.$functionStaticCall(this.target.address, abiEncodedCall),
  195. 'Address: low-level static call failed',
  196. );
  197. });
  198. it('bubbles up revert reason', async function () {
  199. const abiEncodedCall = this.target.contract.methods.mockFunctionRevertsReason().encodeABI();
  200. await expectRevert(
  201. this.mock.$functionStaticCall(this.target.address, abiEncodedCall),
  202. 'CallReceiverMock: reverting',
  203. );
  204. });
  205. it('reverts when address is not a contract', async function () {
  206. const [recipient] = accounts;
  207. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  208. await expectRevert(this.mock.$functionStaticCall(recipient, abiEncodedCall), 'Address: call to non-contract');
  209. });
  210. });
  211. describe('functionDelegateCall', function () {
  212. beforeEach(async function () {
  213. this.target = await CallReceiverMock.new();
  214. });
  215. it('delegate calls the requested function', async function () {
  216. // pseudorandom values
  217. const slot = '0x93e4c53af435ddf777c3de84bb9a953a777788500e229a468ea1036496ab66a0';
  218. const value = '0x6a465d1c49869f71fb65562bcbd7e08c8044074927f0297127203f2a9924ff5b';
  219. const abiEncodedCall = this.target.contract.methods.mockFunctionWritesStorage(slot, value).encodeABI();
  220. expect(await web3.eth.getStorageAt(this.mock.address, slot)).to.be.equal(constants.ZERO_BYTES32);
  221. expectEvent(
  222. await this.mock.$functionDelegateCall(this.target.address, abiEncodedCall),
  223. 'return$functionDelegateCall_address_bytes',
  224. { ret0: web3.eth.abi.encodeParameters(['string'], ['0x1234']) },
  225. );
  226. expect(await web3.eth.getStorageAt(this.mock.address, slot)).to.be.equal(value);
  227. });
  228. it('bubbles up revert reason', async function () {
  229. const abiEncodedCall = this.target.contract.methods.mockFunctionRevertsReason().encodeABI();
  230. await expectRevert(
  231. this.mock.$functionDelegateCall(this.target.address, abiEncodedCall),
  232. 'CallReceiverMock: reverting',
  233. );
  234. });
  235. it('reverts when address is not a contract', async function () {
  236. const [recipient] = accounts;
  237. const abiEncodedCall = this.target.contract.methods.mockFunction().encodeABI();
  238. await expectRevert(this.mock.$functionDelegateCall(recipient, abiEncodedCall), 'Address: call to non-contract');
  239. });
  240. });
  241. });