Address.test.js 9.9 KB

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