Address.test.js 9.9 KB

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