@@ -197,10 +197,10 @@ describe('Staking:Stakes', () => {
197
197
const tx1 = await staking . connect ( indexer . signer ) . unstake ( tokensToUnstake )
198
198
const receipt1 = await tx1 . wait ( )
199
199
const event1 : Event = receipt1 . events . pop ( )
200
- const tokensLockedUntil1 = event1 . args [ 2 ]
200
+ const tokensLockedUntil1 = event1 . args [ 'until' ]
201
201
202
- // Move forward
203
- await advanceBlockTo ( tokensLockedUntil1 )
202
+ // Move forward before the tokens are unlocked for withdrawal
203
+ await advanceBlockTo ( tokensLockedUntil1 . sub ( 5 ) )
204
204
205
205
// Calculate locking time for tokens taking into account the previous unstake request
206
206
const currentBlock = await latestBlock ( )
@@ -215,9 +215,57 @@ describe('Staking:Stakes', () => {
215
215
// Unstake (2)
216
216
const tx2 = await staking . connect ( indexer . signer ) . unstake ( tokensToUnstake )
217
217
const receipt2 = await tx2 . wait ( )
218
+
219
+ // Verify events
218
220
const event2 : Event = receipt2 . events . pop ( )
219
- const tokensLockedUntil2 = event2 . args [ 2 ]
220
- expect ( expectedLockedUntil ) . eq ( tokensLockedUntil2 )
221
+ expect ( event2 . args [ 'until' ] ) . eq ( expectedLockedUntil )
222
+
223
+ // Verify state
224
+ const afterIndexerStake = await staking . stakes ( indexer . address )
225
+ expect ( afterIndexerStake . tokensLocked ) . eq ( tokensToUnstake . mul ( 2 ) ) // we unstaked two times
226
+ expect ( afterIndexerStake . tokensLockedUntil ) . eq ( expectedLockedUntil )
227
+ } )
228
+
229
+ it ( 'should unstake and withdraw if some tokens are unthawed' , async function ( ) {
230
+ const tokensToUnstake = toGRT ( '10' )
231
+ const thawingPeriod = toBN ( await staking . thawingPeriod ( ) )
232
+
233
+ const beforeIndexerBalance = await grt . balanceOf ( indexer . address )
234
+
235
+ // Unstake (1)
236
+ const tx1 = await staking . connect ( indexer . signer ) . unstake ( tokensToUnstake )
237
+ const receipt1 = await tx1 . wait ( )
238
+ const event1 : Event = receipt1 . events . pop ( )
239
+ const tokensLockedUntil1 = event1 . args [ 'until' ]
240
+
241
+ // Move forward after the tokens are unlocked for withdrawal
242
+ await advanceBlockTo ( tokensLockedUntil1 )
243
+
244
+ // Calculate locking time for tokens taking into account some tokens are withdraweable
245
+ const currentBlock = await latestBlock ( )
246
+ const expectedLockedUntil = currentBlock . add ( thawingPeriod ) . add ( toBN ( '1' ) )
247
+
248
+ // Unstake (2)
249
+ const tx2 = await staking . connect ( indexer . signer ) . unstake ( tokensToUnstake )
250
+ const receipt2 = await tx2 . wait ( )
251
+
252
+ // Verify events
253
+ const unstakeEvent : Event = receipt2 . events . pop ( )
254
+ const withdrawEvent : Event = receipt2 . events . pop ( )
255
+ expect ( unstakeEvent . args [ 'until' ] ) . eq ( expectedLockedUntil )
256
+ expect ( withdrawEvent . args [ 'tokens' ] ) . eq ( tokensToUnstake )
257
+
258
+ // Verify state
259
+ const afterIndexerStake = await staking . stakes ( indexer . address )
260
+ const afterIndexerBalance = await grt . balanceOf ( indexer . address )
261
+ expect ( afterIndexerStake . tokensLocked ) . eq ( tokensToUnstake )
262
+ expect ( afterIndexerStake . tokensLockedUntil ) . eq ( expectedLockedUntil )
263
+ expect ( afterIndexerBalance ) . eq ( beforeIndexerBalance . add ( tokensToUnstake ) )
264
+ } )
265
+
266
+ it ( 'reject unstake zero tokens' , async function ( ) {
267
+ const tx = staking . connect ( indexer . signer ) . unstake ( toGRT ( '0' ) )
268
+ await expect ( tx ) . revertedWith ( 'Cannot unstake zero tokens' )
221
269
} )
222
270
223
271
it ( 'reject unstake more than available tokens' , async function ( ) {
0 commit comments