draft-ERC4337Utils.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. const { ethers, entrypoint } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { packValidationData, UserOperation } = require('../../helpers/erc4337');
  5. const { MAX_UINT48 } = require('../../helpers/constants');
  6. const time = require('../../helpers/time');
  7. const ADDRESS_ONE = '0x0000000000000000000000000000000000000001';
  8. const fixture = async () => {
  9. const [authorizer, sender, factory, paymaster, other] = await ethers.getSigners();
  10. const utils = await ethers.deployContract('$ERC4337Utils');
  11. const SIG_VALIDATION_SUCCESS = await utils.$SIG_VALIDATION_SUCCESS();
  12. const SIG_VALIDATION_FAILED = await utils.$SIG_VALIDATION_FAILED();
  13. return { utils, authorizer, sender, factory, paymaster, other, SIG_VALIDATION_SUCCESS, SIG_VALIDATION_FAILED };
  14. };
  15. describe('ERC4337Utils', function () {
  16. beforeEach(async function () {
  17. Object.assign(this, await loadFixture(fixture));
  18. });
  19. describe('parseValidationData', function () {
  20. it('parses the validation data', async function () {
  21. const authorizer = this.authorizer;
  22. const validUntil = 0x12345678n;
  23. const validAfter = 0x9abcdef0n;
  24. const validationData = packValidationData(validAfter, validUntil, authorizer);
  25. await expect(this.utils.$parseValidationData(validationData)).to.eventually.deep.equal([
  26. authorizer.address,
  27. validAfter,
  28. validUntil,
  29. ]);
  30. });
  31. it('returns an type(uint48).max if until is 0', async function () {
  32. const authorizer = this.authorizer;
  33. const validAfter = 0x12345678n;
  34. const validationData = packValidationData(validAfter, 0, authorizer);
  35. await expect(this.utils.$parseValidationData(validationData)).to.eventually.deep.equal([
  36. authorizer.address,
  37. validAfter,
  38. MAX_UINT48,
  39. ]);
  40. });
  41. it('parse canonical values', async function () {
  42. await expect(this.utils.$parseValidationData(this.SIG_VALIDATION_SUCCESS)).to.eventually.deep.equal([
  43. ethers.ZeroAddress,
  44. 0n,
  45. MAX_UINT48,
  46. ]);
  47. await expect(this.utils.$parseValidationData(this.SIG_VALIDATION_FAILED)).to.eventually.deep.equal([
  48. ADDRESS_ONE,
  49. 0n,
  50. MAX_UINT48,
  51. ]);
  52. });
  53. });
  54. describe('packValidationData', function () {
  55. it('packs the validation data', async function () {
  56. const authorizer = this.authorizer;
  57. const validUntil = 0x12345678n;
  58. const validAfter = 0x9abcdef0n;
  59. const validationData = packValidationData(validAfter, validUntil, authorizer);
  60. await expect(
  61. this.utils.$packValidationData(ethers.Typed.address(authorizer), validAfter, validUntil),
  62. ).to.eventually.equal(validationData);
  63. });
  64. it('packs the validation data (bool)', async function () {
  65. const success = false;
  66. const validUntil = 0x12345678n;
  67. const validAfter = 0x9abcdef0n;
  68. const validationData = packValidationData(validAfter, validUntil, false);
  69. await expect(
  70. this.utils.$packValidationData(ethers.Typed.bool(success), validAfter, validUntil),
  71. ).to.eventually.equal(validationData);
  72. });
  73. it('packing reproduced canonical values', async function () {
  74. await expect(
  75. this.utils.$packValidationData(ethers.Typed.address(ethers.ZeroAddress), 0n, 0n),
  76. ).to.eventually.equal(this.SIG_VALIDATION_SUCCESS);
  77. await expect(this.utils.$packValidationData(ethers.Typed.bool(true), 0n, 0n)).to.eventually.equal(
  78. this.SIG_VALIDATION_SUCCESS,
  79. );
  80. await expect(this.utils.$packValidationData(ethers.Typed.address(ADDRESS_ONE), 0n, 0n)).to.eventually.equal(
  81. this.SIG_VALIDATION_FAILED,
  82. );
  83. await expect(this.utils.$packValidationData(ethers.Typed.bool(false), 0n, 0n)).to.eventually.equal(
  84. this.SIG_VALIDATION_FAILED,
  85. );
  86. });
  87. });
  88. describe('combineValidationData', function () {
  89. const validUntil1 = 0x12345678n;
  90. const validAfter1 = 0x9abcdef0n;
  91. const validUntil2 = 0x87654321n;
  92. const validAfter2 = 0xabcdef90n;
  93. it('combines the validation data', async function () {
  94. const validationData1 = packValidationData(validAfter1, validUntil1, ethers.ZeroAddress);
  95. const validationData2 = packValidationData(validAfter2, validUntil2, ethers.ZeroAddress);
  96. const expected = packValidationData(validAfter2, validUntil1, true);
  97. // check symmetry
  98. await expect(this.utils.$combineValidationData(validationData1, validationData2)).to.eventually.equal(expected);
  99. await expect(this.utils.$combineValidationData(validationData2, validationData1)).to.eventually.equal(expected);
  100. });
  101. for (const [authorizer1, authorizer2] of [
  102. [ethers.ZeroAddress, '0xbf023313b891fd6000544b79e353323aa94a4f29'],
  103. ['0xbf023313b891fd6000544b79e353323aa94a4f29', ethers.ZeroAddress],
  104. ]) {
  105. it('returns SIG_VALIDATION_FAILURE if one of the authorizers is not address(0)', async function () {
  106. const validationData1 = packValidationData(validAfter1, validUntil1, authorizer1);
  107. const validationData2 = packValidationData(validAfter2, validUntil2, authorizer2);
  108. const expected = packValidationData(validAfter2, validUntil1, false);
  109. // check symmetry
  110. await expect(this.utils.$combineValidationData(validationData1, validationData2)).to.eventually.equal(expected);
  111. await expect(this.utils.$combineValidationData(validationData2, validationData1)).to.eventually.equal(expected);
  112. });
  113. }
  114. });
  115. describe('getValidationData', function () {
  116. it('returns the validation data with valid validity range', async function () {
  117. const aggregator = this.authorizer;
  118. const validAfter = 0;
  119. const validUntil = MAX_UINT48;
  120. const validationData = packValidationData(validAfter, validUntil, aggregator);
  121. await expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, false]);
  122. });
  123. it('returns the validation data with invalid validity range (expired)', async function () {
  124. const aggregator = this.authorizer;
  125. const validAfter = 0;
  126. const validUntil = 1;
  127. const validationData = packValidationData(validAfter, validUntil, aggregator);
  128. await expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, true]);
  129. });
  130. it('returns the validation data with invalid validity range (not yet valid)', async function () {
  131. const aggregator = this.authorizer;
  132. const validAfter = MAX_UINT48;
  133. const validUntil = MAX_UINT48;
  134. const validationData = packValidationData(validAfter, validUntil, aggregator);
  135. await expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, true]);
  136. });
  137. it('returns address(0) and false for validationData = 0', async function () {
  138. await expect(this.utils.$getValidationData(0n)).to.eventually.deep.equal([ethers.ZeroAddress, false]);
  139. });
  140. });
  141. describe('hash', function () {
  142. it('returns the operation hash with specified entrypoint and chainId', async function () {
  143. const userOp = new UserOperation({ sender: this.sender, nonce: 1 });
  144. const chainId = await ethers.provider.getNetwork().then(({ chainId }) => chainId);
  145. const otherChainId = 0xdeadbeef;
  146. // check that helper matches entrypoint logic
  147. await expect(entrypoint.getUserOpHash(userOp.packed)).to.eventually.equal(userOp.hash(entrypoint, chainId));
  148. // check library against helper
  149. await expect(this.utils.$hash(userOp.packed, entrypoint, chainId)).to.eventually.equal(
  150. userOp.hash(entrypoint, chainId),
  151. );
  152. await expect(this.utils.$hash(userOp.packed, entrypoint, otherChainId)).to.eventually.equal(
  153. userOp.hash(entrypoint, otherChainId),
  154. );
  155. });
  156. });
  157. describe('userOp values', function () {
  158. describe('intiCode', function () {
  159. beforeEach(async function () {
  160. this.userOp = new UserOperation({
  161. sender: this.sender,
  162. nonce: 1,
  163. verificationGas: 0x12345678n,
  164. factory: this.factory,
  165. factoryData: '0x123456',
  166. });
  167. this.emptyUserOp = new UserOperation({
  168. sender: this.sender,
  169. nonce: 1,
  170. });
  171. });
  172. it('returns factory', async function () {
  173. await expect(this.utils.$factory(this.userOp.packed)).to.eventually.equal(this.factory);
  174. await expect(this.utils.$factory(this.emptyUserOp.packed)).to.eventually.equal(ethers.ZeroAddress);
  175. });
  176. it('returns factoryData', async function () {
  177. await expect(this.utils.$factoryData(this.userOp.packed)).to.eventually.equal('0x123456');
  178. await expect(this.utils.$factoryData(this.emptyUserOp.packed)).to.eventually.equal('0x');
  179. });
  180. });
  181. it('returns verificationGasLimit', async function () {
  182. const userOp = new UserOperation({ sender: this.sender, nonce: 1, verificationGas: 0x12345678n });
  183. await expect(this.utils.$verificationGasLimit(userOp.packed)).to.eventually.equal(userOp.verificationGas);
  184. });
  185. it('returns callGasLimit', async function () {
  186. const userOp = new UserOperation({ sender: this.sender, nonce: 1, callGas: 0x12345678n });
  187. await expect(this.utils.$callGasLimit(userOp.packed)).to.eventually.equal(userOp.callGas);
  188. });
  189. it('returns maxPriorityFeePerGas', async function () {
  190. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxPriorityFee: 0x12345678n });
  191. await expect(this.utils.$maxPriorityFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  192. });
  193. it('returns maxFeePerGas', async function () {
  194. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxFeePerGas: 0x12345678n });
  195. await expect(this.utils.$maxFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxFeePerGas);
  196. });
  197. it('returns gasPrice', async function () {
  198. const userOp = new UserOperation({
  199. sender: this.sender,
  200. nonce: 1,
  201. maxPriorityFee: 0x12345678n,
  202. maxFeePerGas: 0x87654321n,
  203. });
  204. await expect(this.utils.$gasPrice(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  205. });
  206. describe('paymasterAndData', function () {
  207. beforeEach(async function () {
  208. this.userOp = new UserOperation({
  209. sender: this.sender,
  210. nonce: 1,
  211. paymaster: this.paymaster,
  212. paymasterVerificationGasLimit: 0x12345678n,
  213. paymasterPostOpGasLimit: 0x87654321n,
  214. paymasterData: '0xbeefcafe',
  215. });
  216. this.emptyUserOp = new UserOperation({
  217. sender: this.sender,
  218. nonce: 1,
  219. });
  220. });
  221. it('returns paymaster', async function () {
  222. await expect(this.utils.$paymaster(this.userOp.packed)).to.eventually.equal(this.userOp.paymaster);
  223. await expect(this.utils.$paymaster(this.emptyUserOp.packed)).to.eventually.equal(ethers.ZeroAddress);
  224. });
  225. it('returns verificationGasLimit', async function () {
  226. await expect(this.utils.$paymasterVerificationGasLimit(this.userOp.packed)).to.eventually.equal(
  227. this.userOp.paymasterVerificationGasLimit,
  228. );
  229. await expect(this.utils.$paymasterVerificationGasLimit(this.emptyUserOp.packed)).to.eventually.equal(0n);
  230. });
  231. it('returns postOpGasLimit', async function () {
  232. await expect(this.utils.$paymasterPostOpGasLimit(this.userOp.packed)).to.eventually.equal(
  233. this.userOp.paymasterPostOpGasLimit,
  234. );
  235. await expect(this.utils.$paymasterPostOpGasLimit(this.emptyUserOp.packed)).to.eventually.equal(0n);
  236. });
  237. it('returns data', async function () {
  238. await expect(this.utils.$paymasterData(this.userOp.packed)).to.eventually.equal(this.userOp.paymasterData);
  239. await expect(this.utils.$paymasterData(this.emptyUserOp.packed)).to.eventually.equal('0x');
  240. });
  241. });
  242. });
  243. describe('stake management', function () {
  244. const unstakeDelaySec = 3600n;
  245. beforeEach(async function () {
  246. await this.authorizer.sendTransaction({ to: this.utils, value: ethers.parseEther('1') });
  247. });
  248. it('deposit & withdraw', async function () {
  249. await expect(entrypoint.balanceOf(this.utils)).to.eventually.equal(0n);
  250. // deposit
  251. await expect(this.utils.$depositTo(this.utils, 42n)).to.changeEtherBalances(
  252. [this.utils, entrypoint],
  253. [-42n, 42n],
  254. );
  255. await expect(entrypoint.balanceOf(this.utils)).to.eventually.equal(42n);
  256. // withdraw
  257. await expect(this.utils.$withdrawTo(this.other, 17n)).to.changeEtherBalances(
  258. [entrypoint, this.other],
  259. [-17n, 17n],
  260. );
  261. await expect(entrypoint.balanceOf(this.utils)).to.eventually.equal(25n); // 42 - 17
  262. });
  263. it('stake, unlock & withdraw stake', async function () {
  264. await expect(entrypoint.deposits(this.utils)).to.eventually.deep.equal([0n, false, 0n, 0n, 0n]);
  265. // stake
  266. await expect(this.utils.$addStake(42n, unstakeDelaySec)).to.changeEtherBalances(
  267. [this.utils, entrypoint],
  268. [-42n, 42n],
  269. );
  270. await expect(entrypoint.deposits(this.utils)).to.eventually.deep.equal([0n, true, 42n, unstakeDelaySec, 0n]);
  271. // unlock
  272. const unlockTx = this.utils.$unlockStake();
  273. await expect(unlockTx).to.changeEtherBalances([this.utils, entrypoint], [0n, 0n]); // no ether movement
  274. const timestamp = await time.clockFromReceipt.timestamp(unlockTx);
  275. await expect(entrypoint.deposits(this.utils)).to.eventually.deep.equal([
  276. 0n,
  277. false,
  278. 42n,
  279. unstakeDelaySec,
  280. timestamp + unstakeDelaySec,
  281. ]);
  282. // wait
  283. await time.increaseBy.timestamp(unstakeDelaySec);
  284. // withdraw stake
  285. await expect(this.utils.$withdrawStake(this.other)).to.changeEtherBalances(
  286. [this.utils, entrypoint, this.other],
  287. [0n, -42n, 42n],
  288. );
  289. await expect(entrypoint.deposits(this.utils)).to.eventually.deep.equal([0n, false, 0n, 0n, 0n]);
  290. });
  291. });
  292. });