Address.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. const coder = ethers.AbiCoder.defaultAbiCoder();
  6. async function fixture() {
  7. const [recipient, other] = await ethers.getSigners();
  8. const mock = await ethers.deployContract('$Address');
  9. const target = await ethers.deployContract('CallReceiverMock');
  10. const targetEther = await ethers.deployContract('EtherReceiverMock');
  11. return { recipient, other, mock, target, targetEther };
  12. }
  13. describe('Address', function () {
  14. beforeEach(async function () {
  15. Object.assign(this, await loadFixture(fixture));
  16. });
  17. describe('sendValue', function () {
  18. describe('when sender contract has no funds', function () {
  19. it('sends 0 wei', async function () {
  20. await expect(this.mock.$sendValue(this.other, 0)).to.changeEtherBalance(this.recipient, 0);
  21. });
  22. it('reverts when sending non-zero amounts', async function () {
  23. await expect(this.mock.$sendValue(this.other, 1))
  24. .to.be.revertedWithCustomError(this.mock, 'AddressInsufficientBalance')
  25. .withArgs(this.mock);
  26. });
  27. });
  28. describe('when sender contract has funds', function () {
  29. const funds = ethers.parseEther('1');
  30. beforeEach(async function () {
  31. await this.other.sendTransaction({ to: this.mock, value: funds });
  32. });
  33. describe('with EOA recipient', function () {
  34. it('sends 0 wei', async function () {
  35. await expect(this.mock.$sendValue(this.recipient, 0)).to.changeEtherBalance(this.recipient, 0);
  36. });
  37. it('sends non-zero amounts', async function () {
  38. await expect(this.mock.$sendValue(this.recipient, funds - 1n)).to.changeEtherBalance(
  39. this.recipient,
  40. funds - 1n,
  41. );
  42. });
  43. it('sends the whole balance', async function () {
  44. await expect(this.mock.$sendValue(this.recipient, funds)).to.changeEtherBalance(this.recipient, funds);
  45. expect(await ethers.provider.getBalance(this.mock)).to.equal(0n);
  46. });
  47. it('reverts when sending more than the balance', async function () {
  48. await expect(this.mock.$sendValue(this.recipient, funds + 1n))
  49. .to.be.revertedWithCustomError(this.mock, 'AddressInsufficientBalance')
  50. .withArgs(this.mock);
  51. });
  52. });
  53. describe('with contract recipient', function () {
  54. it('sends funds', async function () {
  55. await this.targetEther.setAcceptEther(true);
  56. await expect(this.mock.$sendValue(this.targetEther, funds)).to.changeEtherBalance(this.targetEther, funds);
  57. });
  58. it('reverts on recipient revert', async function () {
  59. await this.targetEther.setAcceptEther(false);
  60. await expect(this.mock.$sendValue(this.targetEther, funds)).to.be.revertedWithCustomError(
  61. this.mock,
  62. 'FailedInnerCall',
  63. );
  64. });
  65. });
  66. });
  67. });
  68. describe('functionCall', function () {
  69. describe('with valid contract receiver', function () {
  70. it('calls the requested function', async function () {
  71. const call = this.target.interface.encodeFunctionData('mockFunction');
  72. await expect(this.mock.$functionCall(this.target, call))
  73. .to.emit(this.target, 'MockFunctionCalled')
  74. .to.emit(this.mock, 'return$functionCall')
  75. .withArgs(coder.encode(['string'], ['0x1234']));
  76. });
  77. it('calls the requested empty return function', async function () {
  78. const call = this.target.interface.encodeFunctionData('mockFunctionEmptyReturn');
  79. await expect(this.mock.$functionCall(this.target, call)).to.emit(this.target, 'MockFunctionCalled');
  80. });
  81. it('reverts when the called function reverts with no reason', async function () {
  82. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsNoReason');
  83. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWithCustomError(
  84. this.mock,
  85. 'FailedInnerCall',
  86. );
  87. });
  88. it('reverts when the called function reverts, bubbling up the revert reason', async function () {
  89. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsReason');
  90. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWith('CallReceiverMock: reverting');
  91. });
  92. it('reverts when the called function runs out of gas', async function () {
  93. const call = this.target.interface.encodeFunctionData('mockFunctionOutOfGas');
  94. await expect(this.mock.$functionCall(this.target, call, { gasLimit: 120_000n })).to.be.revertedWithCustomError(
  95. this.mock,
  96. 'FailedInnerCall',
  97. );
  98. });
  99. it('reverts when the called function throws', async function () {
  100. const call = this.target.interface.encodeFunctionData('mockFunctionThrows');
  101. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWithPanic(PANIC_CODES.ASSERTION_ERROR);
  102. });
  103. it('reverts when function does not exist', async function () {
  104. const interface = new ethers.Interface(['function mockFunctionDoesNotExist()']);
  105. const call = interface.encodeFunctionData('mockFunctionDoesNotExist');
  106. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWithCustomError(
  107. this.mock,
  108. 'FailedInnerCall',
  109. );
  110. });
  111. });
  112. describe('with non-contract receiver', function () {
  113. it('reverts when address is not a contract', async function () {
  114. const call = this.target.interface.encodeFunctionData('mockFunction');
  115. await expect(this.mock.$functionCall(this.recipient, call))
  116. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  117. .withArgs(this.recipient);
  118. });
  119. });
  120. });
  121. describe('functionCallWithValue', function () {
  122. describe('with zero value', function () {
  123. it('calls the requested function', async function () {
  124. const call = this.target.interface.encodeFunctionData('mockFunction');
  125. await expect(this.mock.$functionCallWithValue(this.target, call, 0))
  126. .to.emit(this.target, 'MockFunctionCalled')
  127. .to.emit(this.mock, 'return$functionCallWithValue')
  128. .withArgs(coder.encode(['string'], ['0x1234']));
  129. });
  130. });
  131. describe('with non-zero value', function () {
  132. const value = ethers.parseEther('1.2');
  133. it('reverts if insufficient sender balance', async function () {
  134. const call = this.target.interface.encodeFunctionData('mockFunction');
  135. await expect(this.mock.$functionCallWithValue(this.target, call, value))
  136. .to.be.revertedWithCustomError(this.mock, 'AddressInsufficientBalance')
  137. .withArgs(this.mock);
  138. });
  139. it('calls the requested function with existing value', async function () {
  140. await this.other.sendTransaction({ to: this.mock, value });
  141. const call = this.target.interface.encodeFunctionData('mockFunction');
  142. const tx = await this.mock.$functionCallWithValue(this.target, call, value);
  143. await expect(tx).to.changeEtherBalance(this.target, value);
  144. await expect(tx)
  145. .to.emit(this.target, 'MockFunctionCalled')
  146. .to.emit(this.mock, 'return$functionCallWithValue')
  147. .withArgs(coder.encode(['string'], ['0x1234']));
  148. });
  149. it('calls the requested function with transaction funds', async function () {
  150. expect(await ethers.provider.getBalance(this.mock)).to.equal(0n);
  151. const call = this.target.interface.encodeFunctionData('mockFunction');
  152. const tx = await this.mock.connect(this.other).$functionCallWithValue(this.target, call, value, { value });
  153. await expect(tx).to.changeEtherBalance(this.target, value);
  154. await expect(tx)
  155. .to.emit(this.target, 'MockFunctionCalled')
  156. .to.emit(this.mock, 'return$functionCallWithValue')
  157. .withArgs(coder.encode(['string'], ['0x1234']));
  158. });
  159. it('reverts when calling non-payable functions', async function () {
  160. await this.other.sendTransaction({ to: this.mock, value });
  161. const call = this.target.interface.encodeFunctionData('mockFunctionNonPayable');
  162. await expect(this.mock.$functionCallWithValue(this.target, call, value)).to.be.revertedWithCustomError(
  163. this.mock,
  164. 'FailedInnerCall',
  165. );
  166. });
  167. });
  168. });
  169. describe('functionStaticCall', function () {
  170. it('calls the requested function', async function () {
  171. const call = this.target.interface.encodeFunctionData('mockStaticFunction');
  172. expect(await this.mock.$functionStaticCall(this.target, call)).to.equal(coder.encode(['string'], ['0x1234']));
  173. });
  174. it('reverts on a non-static function', async function () {
  175. const call = this.target.interface.encodeFunctionData('mockFunction');
  176. await expect(this.mock.$functionStaticCall(this.target, call)).to.be.revertedWithCustomError(
  177. this.mock,
  178. 'FailedInnerCall',
  179. );
  180. });
  181. it('bubbles up revert reason', async function () {
  182. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsReason');
  183. await expect(this.mock.$functionStaticCall(this.target, call)).to.be.revertedWith('CallReceiverMock: reverting');
  184. });
  185. it('reverts when address is not a contract', async function () {
  186. const call = this.target.interface.encodeFunctionData('mockFunction');
  187. await expect(this.mock.$functionStaticCall(this.recipient, call))
  188. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  189. .withArgs(this.recipient);
  190. });
  191. });
  192. describe('functionDelegateCall', function () {
  193. it('delegate calls the requested function', async function () {
  194. const slot = ethers.hexlify(ethers.randomBytes(32));
  195. const value = ethers.hexlify(ethers.randomBytes(32));
  196. const call = this.target.interface.encodeFunctionData('mockFunctionWritesStorage', [slot, value]);
  197. expect(await ethers.provider.getStorage(this.mock, slot)).to.equal(ethers.ZeroHash);
  198. await expect(await this.mock.$functionDelegateCall(this.target, call))
  199. .to.emit(this.mock, 'return$functionDelegateCall')
  200. .withArgs(coder.encode(['string'], ['0x1234']));
  201. expect(await ethers.provider.getStorage(this.mock, slot)).to.equal(value);
  202. });
  203. it('bubbles up revert reason', async function () {
  204. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsReason');
  205. await expect(this.mock.$functionDelegateCall(this.target, call)).to.be.revertedWith(
  206. 'CallReceiverMock: reverting',
  207. );
  208. });
  209. it('reverts when address is not a contract', async function () {
  210. const call = this.target.interface.encodeFunctionData('mockFunction');
  211. await expect(this.mock.$functionDelegateCall(this.recipient, call))
  212. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  213. .withArgs(this.recipient);
  214. });
  215. });
  216. describe('verifyCallResult', function () {
  217. it('returns returndata on success', async function () {
  218. const returndata = '0x123abc';
  219. expect(await this.mock.$verifyCallResult(true, returndata)).to.equal(returndata);
  220. });
  221. });
  222. });