Address.test.js 12 KB

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