Address.test.js 14 KB

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