Address.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, 0n)).to.changeEtherBalance(this.recipient, 0n);
  21. });
  22. it('reverts when sending non-zero amounts', async function () {
  23. await expect(this.mock.$sendValue(this.other, 1n))
  24. .to.be.revertedWithCustomError(this.mock, 'InsufficientBalance')
  25. .withArgs(0n, 1n);
  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, 0n)).to.changeEtherBalance(this.recipient, 0n);
  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, 'InsufficientBalance')
  50. .withArgs(funds, funds + 1n);
  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. 'FailedCall',
  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(this.mock, 'FailedCall');
  84. });
  85. it('reverts when the called function reverts, bubbling up the revert reason', async function () {
  86. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsReason');
  87. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWith('CallReceiverMock: reverting');
  88. });
  89. it('reverts when the called function runs out of gas', async function () {
  90. const call = this.target.interface.encodeFunctionData('mockFunctionOutOfGas');
  91. await expect(this.mock.$functionCall(this.target, call, { gasLimit: 120_000n })).to.be.revertedWithCustomError(
  92. this.mock,
  93. 'FailedCall',
  94. );
  95. });
  96. it('reverts when the called function throws', async function () {
  97. const call = this.target.interface.encodeFunctionData('mockFunctionThrows');
  98. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWithPanic(PANIC_CODES.ASSERTION_ERROR);
  99. });
  100. it('reverts when function does not exist', async function () {
  101. const call = new ethers.Interface(['function mockFunctionDoesNotExist()']).encodeFunctionData(
  102. 'mockFunctionDoesNotExist',
  103. );
  104. await expect(this.mock.$functionCall(this.target, call)).to.be.revertedWithCustomError(this.mock, 'FailedCall');
  105. });
  106. });
  107. describe('with non-contract receiver', function () {
  108. it('reverts when address is not a contract', async function () {
  109. const call = this.target.interface.encodeFunctionData('mockFunction');
  110. await expect(this.mock.$functionCall(this.recipient, call))
  111. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  112. .withArgs(this.recipient);
  113. });
  114. });
  115. });
  116. describe('functionCallWithValue', function () {
  117. describe('with zero value', function () {
  118. it('calls the requested function', async function () {
  119. const call = this.target.interface.encodeFunctionData('mockFunction');
  120. await expect(this.mock.$functionCallWithValue(this.target, call, 0n))
  121. .to.emit(this.target, 'MockFunctionCalled')
  122. .to.emit(this.mock, 'return$functionCallWithValue')
  123. .withArgs(coder.encode(['string'], ['0x1234']));
  124. });
  125. });
  126. describe('with non-zero value', function () {
  127. const value = ethers.parseEther('1.2');
  128. it('reverts if insufficient sender balance', async function () {
  129. const call = this.target.interface.encodeFunctionData('mockFunction');
  130. await expect(this.mock.$functionCallWithValue(this.target, call, value))
  131. .to.be.revertedWithCustomError(this.mock, 'InsufficientBalance')
  132. .withArgs(0n, value);
  133. });
  134. it('calls the requested function with existing value', async function () {
  135. await this.other.sendTransaction({ to: this.mock, value });
  136. const call = this.target.interface.encodeFunctionData('mockFunction');
  137. const tx = await this.mock.$functionCallWithValue(this.target, call, value);
  138. await expect(tx).to.changeEtherBalance(this.target, value);
  139. await expect(tx)
  140. .to.emit(this.target, 'MockFunctionCalled')
  141. .to.emit(this.mock, 'return$functionCallWithValue')
  142. .withArgs(coder.encode(['string'], ['0x1234']));
  143. });
  144. it('calls the requested function with transaction funds', async function () {
  145. expect(await ethers.provider.getBalance(this.mock)).to.equal(0n);
  146. const call = this.target.interface.encodeFunctionData('mockFunction');
  147. const tx = await this.mock.connect(this.other).$functionCallWithValue(this.target, call, value, { value });
  148. await expect(tx).to.changeEtherBalance(this.target, value);
  149. await expect(tx)
  150. .to.emit(this.target, 'MockFunctionCalled')
  151. .to.emit(this.mock, 'return$functionCallWithValue')
  152. .withArgs(coder.encode(['string'], ['0x1234']));
  153. });
  154. it('reverts when calling non-payable functions', async function () {
  155. await this.other.sendTransaction({ to: this.mock, value });
  156. const call = this.target.interface.encodeFunctionData('mockFunctionNonPayable');
  157. await expect(this.mock.$functionCallWithValue(this.target, call, value)).to.be.revertedWithCustomError(
  158. this.mock,
  159. 'FailedCall',
  160. );
  161. });
  162. });
  163. });
  164. describe('functionStaticCall', function () {
  165. it('calls the requested function', async function () {
  166. const call = this.target.interface.encodeFunctionData('mockStaticFunction');
  167. expect(await this.mock.$functionStaticCall(this.target, call)).to.equal(coder.encode(['string'], ['0x1234']));
  168. });
  169. it('reverts on a non-static function', async function () {
  170. const call = this.target.interface.encodeFunctionData('mockFunction');
  171. await expect(this.mock.$functionStaticCall(this.target, call)).to.be.revertedWithCustomError(
  172. this.mock,
  173. 'FailedCall',
  174. );
  175. });
  176. it('bubbles up revert reason', async function () {
  177. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsReason');
  178. await expect(this.mock.$functionStaticCall(this.target, call)).to.be.revertedWith('CallReceiverMock: reverting');
  179. });
  180. it('reverts when address is not a contract', async function () {
  181. const call = this.target.interface.encodeFunctionData('mockFunction');
  182. await expect(this.mock.$functionStaticCall(this.recipient, call))
  183. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  184. .withArgs(this.recipient);
  185. });
  186. });
  187. describe('functionDelegateCall', function () {
  188. it('delegate calls the requested function', async function () {
  189. const slot = ethers.hexlify(ethers.randomBytes(32));
  190. const value = ethers.hexlify(ethers.randomBytes(32));
  191. const call = this.target.interface.encodeFunctionData('mockFunctionWritesStorage', [slot, value]);
  192. expect(await ethers.provider.getStorage(this.mock, slot)).to.equal(ethers.ZeroHash);
  193. await expect(await this.mock.$functionDelegateCall(this.target, call))
  194. .to.emit(this.mock, 'return$functionDelegateCall')
  195. .withArgs(coder.encode(['string'], ['0x1234']));
  196. expect(await ethers.provider.getStorage(this.mock, slot)).to.equal(value);
  197. });
  198. it('bubbles up revert reason', async function () {
  199. const call = this.target.interface.encodeFunctionData('mockFunctionRevertsReason');
  200. await expect(this.mock.$functionDelegateCall(this.target, call)).to.be.revertedWith(
  201. 'CallReceiverMock: reverting',
  202. );
  203. });
  204. it('reverts when address is not a contract', async function () {
  205. const call = this.target.interface.encodeFunctionData('mockFunction');
  206. await expect(this.mock.$functionDelegateCall(this.recipient, call))
  207. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  208. .withArgs(this.recipient);
  209. });
  210. });
  211. describe('verifyCallResult', function () {
  212. it('returns returndata on success', async function () {
  213. const returndata = '0x123abc';
  214. expect(await this.mock.$verifyCallResult(true, returndata)).to.equal(returndata);
  215. });
  216. });
  217. });