Address.test.js 14 KB

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