@@ -4,15 +4,16 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
4
4
5
5
const { packValidationData, UserOperation } = require ( '../../helpers/erc4337' ) ;
6
6
const { MAX_UINT48 } = require ( '../../helpers/constants' ) ;
7
+ const time = require ( '../../helpers/time' ) ;
7
8
const ADDRESS_ONE = '0x0000000000000000000000000000000000000001' ;
8
9
9
10
const fixture = async ( ) => {
10
- const [ authorizer , sender , factory , paymaster ] = await ethers . getSigners ( ) ;
11
+ const [ authorizer , sender , factory , paymaster , other ] = await ethers . getSigners ( ) ;
11
12
const utils = await ethers . deployContract ( '$ERC4337Utils' ) ;
12
13
const SIG_VALIDATION_SUCCESS = await utils . $SIG_VALIDATION_SUCCESS ( ) ;
13
14
const SIG_VALIDATION_FAILED = await utils . $SIG_VALIDATION_FAILED ( ) ;
14
15
15
- return { utils, authorizer, sender, factory, paymaster, SIG_VALIDATION_SUCCESS , SIG_VALIDATION_FAILED } ;
16
+ return { utils, authorizer, sender, factory, paymaster, other , SIG_VALIDATION_SUCCESS , SIG_VALIDATION_FAILED } ;
16
17
} ;
17
18
18
19
describe ( 'ERC4337Utils' , function ( ) {
@@ -284,4 +285,68 @@ describe('ERC4337Utils', function () {
284
285
} ) ;
285
286
} ) ;
286
287
} ) ;
288
+
289
+ describe ( 'stake management' , function ( ) {
290
+ const unstakeDelaySec = 3600n ;
291
+
292
+ beforeEach ( async function ( ) {
293
+ await this . authorizer . sendTransaction ( { to : this . utils , value : ethers . parseEther ( '1' ) } ) ;
294
+ } ) ;
295
+
296
+ it ( 'deposit & withdraw' , async function ( ) {
297
+ await expect ( entrypoint . balanceOf ( this . utils ) ) . to . eventually . equal ( 0n ) ;
298
+
299
+ // deposit
300
+ await expect ( this . utils . $depositTo ( this . utils , 42n ) ) . to . changeEtherBalances (
301
+ [ this . utils , entrypoint ] ,
302
+ [ - 42n , 42n ] ,
303
+ ) ;
304
+
305
+ await expect ( entrypoint . balanceOf ( this . utils ) ) . to . eventually . equal ( 42n ) ;
306
+
307
+ // withdraw
308
+ await expect ( this . utils . $withdrawTo ( this . other , 17n ) ) . to . changeEtherBalances (
309
+ [ entrypoint , this . other ] ,
310
+ [ - 17n , 17n ] ,
311
+ ) ;
312
+
313
+ await expect ( entrypoint . balanceOf ( this . utils ) ) . to . eventually . equal ( 25n ) ; // 42 - 17
314
+ } ) ;
315
+
316
+ it ( 'stake, unlock & withdraw stake' , async function ( ) {
317
+ await expect ( entrypoint . deposits ( this . utils ) ) . to . eventually . deep . equal ( [ 0n , false , 0n , 0n , 0n ] ) ;
318
+
319
+ // stake
320
+ await expect ( this . utils . $addStake ( 42n , unstakeDelaySec ) ) . to . changeEtherBalances (
321
+ [ this . utils , entrypoint ] ,
322
+ [ - 42n , 42n ] ,
323
+ ) ;
324
+
325
+ await expect ( entrypoint . deposits ( this . utils ) ) . to . eventually . deep . equal ( [ 0n , true , 42n , unstakeDelaySec , 0n ] ) ;
326
+
327
+ // unlock
328
+ const unlockTx = this . utils . $unlockStake ( ) ;
329
+ await expect ( unlockTx ) . to . changeEtherBalances ( [ this . utils , entrypoint ] , [ 0n , 0n ] ) ; // no ether movement
330
+
331
+ const timestamp = await time . clockFromReceipt . timestamp ( unlockTx ) ;
332
+ await expect ( entrypoint . deposits ( this . utils ) ) . to . eventually . deep . equal ( [
333
+ 0n ,
334
+ false ,
335
+ 42n ,
336
+ unstakeDelaySec ,
337
+ timestamp + unstakeDelaySec ,
338
+ ] ) ;
339
+
340
+ // wait
341
+ await time . increaseBy . timestamp ( unstakeDelaySec ) ;
342
+
343
+ // withdraw stake
344
+ await expect ( this . utils . $withdrawStake ( this . other ) ) . to . changeEtherBalances (
345
+ [ this . utils , entrypoint , this . other ] ,
346
+ [ 0n , - 42n , 42n ] ,
347
+ ) ;
348
+
349
+ await expect ( entrypoint . deposits ( this . utils ) ) . to . eventually . deep . equal ( [ 0n , false , 0n , 0n , 0n ] ) ;
350
+ } ) ;
351
+ } ) ;
287
352
} ) ;
0 commit comments