Address.test.js 10 KB

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