Address.test.js 12 KB

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