Address.test.js 13 KB

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