@@ -11,6 +11,7 @@ import {
11
11
} from "../../typechain-types" ;
12
12
import { expect } from "chai" ;
13
13
import exp from "constants" ;
14
+ import { it } from "mocha" ;
14
15
15
16
/* eslint-disable no-unused-vars */
16
17
/* eslint-disable no-unused-expressions */
@@ -81,138 +82,147 @@ describe("Staking", async () => {
81
82
await sortition . passPhase ( ) ; // Drawing -> Staking
82
83
} ;
83
84
84
- describe ( "When decreasing then increasing back stake " , async ( ) => {
85
+ describe ( "When stake is decreased then increased back" , async ( ) => {
85
86
before ( "Setup" , async ( ) => {
86
87
await deploy ( ) ;
87
88
await reachDrawingPhase ( ) ;
88
89
} ) ;
89
90
90
91
it ( "Should be outside the Staking phase" , async ( ) => {
91
92
expect ( await sortition . phase ( ) ) . to . be . equal ( 1 ) ; // Drawing
92
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
93
- PNK ( 4000 ) ,
94
- BigNumber . from ( 0 ) ,
95
- PNK ( 2000 ) ,
96
- BigNumber . from ( 2 ) ,
97
- ] ) ;
93
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [ PNK ( 4000 ) , 0 , PNK ( 2000 ) , 2 ] ) ;
98
94
} ) ;
99
95
100
- it ( "Should delay the stake decrease" , async ( ) => {
101
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
102
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
103
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ;
104
- await expect ( core . setStake ( 2 , PNK ( 1000 ) ) )
105
- . to . emit ( core , "StakeDelayedNotTransferred" )
106
- . withArgs ( deployer , 2 , PNK ( 1000 ) ) ;
107
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
108
- PNK ( 4000 ) ,
109
- BigNumber . from ( 0 ) ,
110
- PNK ( 2000 ) ,
111
- BigNumber . from ( 2 ) ,
112
- ] ) ; // stake unchanged, delayed
113
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
114
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
115
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
116
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
117
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 1000 ) , false ] ) ;
96
+ describe ( "When stake is decreased" , async ( ) => {
97
+ it ( "Should delay the stake decrease" , async ( ) => {
98
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
99
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
100
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ;
101
+ await expect ( core . setStake ( 2 , PNK ( 1000 ) ) )
102
+ . to . emit ( core , "StakeDelayedNotTransferred" )
103
+ . withArgs ( deployer , 2 , PNK ( 1000 ) ) ;
104
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [ PNK ( 4000 ) , 0 , PNK ( 2000 ) , 2 ] ) ; // stake unchanged, delayed
105
+ } ) ;
106
+
107
+ it ( "Should not transfer any PNK" , async ( ) => {
108
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
109
+ } ) ;
110
+
111
+ it ( "Should store the delayed stake for later" , async ( ) => {
112
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
113
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
114
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
115
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 1000 ) , false ] ) ;
116
+ } ) ;
118
117
} ) ;
119
118
120
- it ( "Should delay the stake increase back to the previous amount" , async ( ) => {
121
- balanceBefore = await pnk . balanceOf ( deployer ) ;
122
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
123
- await expect ( core . setStake ( 2 , PNK ( 2000 ) ) )
124
- . to . emit ( core , "StakeDelayedNotTransferred" )
125
- . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
126
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
127
- PNK ( 4000 ) ,
128
- BigNumber . from ( 0 ) ,
129
- PNK ( 2000 ) ,
130
- BigNumber . from ( 2 ) ,
131
- ] ) ; // stake unchanged, delayed
132
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
133
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
134
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
135
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
136
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
137
- expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 2000 ) , false ] ) ;
119
+ describe ( "When stake is increased back to the previous amount" , ( ) => {
120
+ it ( "Should delay the stake increase" , async ( ) => {
121
+ balanceBefore = await pnk . balanceOf ( deployer ) ;
122
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
123
+ await expect ( core . setStake ( 2 , PNK ( 2000 ) ) )
124
+ . to . emit ( core , "StakeDelayedNotTransferred" )
125
+ . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
126
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [ PNK ( 4000 ) , 0 , PNK ( 2000 ) , 2 ] ) ; // stake unchanged, delayed
127
+ } ) ;
128
+
129
+ it ( "Should not transfer any PNK" , async ( ) => {
130
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
131
+ } ) ;
132
+
133
+ it ( "Should store the delayed stake for later" , async ( ) => {
134
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
135
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
136
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
137
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
138
+ expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 2000 ) , false ] ) ;
139
+ } ) ;
138
140
} ) ;
139
141
140
- it ( "Should execute the delayed stakes but the stakes should remain the same" , async ( ) => {
141
- await reachStakingPhaseAfterDrawing ( ) ;
142
- balanceBefore = await pnk . balanceOf ( deployer ) ;
143
- await expect ( sortition . executeDelayedStakes ( 10 ) ) . to . emit ( core , "StakeSet" ) . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
144
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
145
- PNK ( 4000 ) ,
146
- PNK ( 300 ) , // we're the only juror so we are drawn 3 times
147
- PNK ( 2000 ) ,
148
- BigNumber . from ( 2 ) ,
149
- ] ) ; // stake unchanged, delayed
150
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
151
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
152
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 3 ) ;
153
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
154
- expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 2nd delayed stake got deleted
155
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ; // no delayed stakes left
142
+ describe ( "When the Phase passes back to Staking" , ( ) => {
143
+ it ( "Should execute the delayed stakes but the stakes should remain the same" , async ( ) => {
144
+ await reachStakingPhaseAfterDrawing ( ) ;
145
+ balanceBefore = await pnk . balanceOf ( deployer ) ;
146
+ await expect ( sortition . executeDelayedStakes ( 10 ) ) . to . emit ( core , "StakeSet" ) . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
147
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
148
+ PNK ( 4000 ) ,
149
+ PNK ( 300 ) , // we're the only juror so we are drawn 3 times
150
+ PNK ( 2000 ) ,
151
+ 2 ,
152
+ ] ) ; // stake unchanged, delayed
153
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
154
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 3 ) ;
155
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
156
+ expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 2nd delayed stake got deleted
157
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ; // no delayed stakes left
158
+ } ) ;
159
+
160
+ it ( "Should not transfer any PNK" , async ( ) => {
161
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
162
+ } ) ;
156
163
} ) ;
157
164
} ) ;
158
165
159
- describe ( "When increasing then decreasing back stake " , async ( ) => {
166
+ describe ( "When stake is increased then decreased back" , async ( ) => {
160
167
before ( "Setup" , async ( ) => {
161
168
await deploy ( ) ;
162
169
await reachDrawingPhase ( ) ;
163
170
} ) ;
164
171
165
172
it ( "Should be outside the Staking phase" , async ( ) => {
166
173
expect ( await sortition . phase ( ) ) . to . be . equal ( 1 ) ; // Drawing
167
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
168
- PNK ( 4000 ) ,
169
- BigNumber . from ( 0 ) ,
170
- PNK ( 2000 ) ,
171
- BigNumber . from ( 2 ) ,
172
- ] ) ;
174
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [ PNK ( 4000 ) , 0 , PNK ( 2000 ) , 2 ] ) ;
173
175
} ) ;
174
176
175
- it ( "Should transfer PNK but delay the stake increase" , async ( ) => {
176
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
177
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
178
- await pnk . approve ( core . address , PNK ( 1000 ) ) ;
179
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ;
180
- await expect ( core . setStake ( 2 , PNK ( 3000 ) ) )
181
- . to . emit ( core , "StakeDelayedAlreadyTransferred" )
182
- . withArgs ( deployer , 2 , PNK ( 3000 ) ) ;
183
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
184
- PNK ( 5000 ) ,
185
- BigNumber . from ( 0 ) ,
186
- PNK ( 3000 ) ,
187
- BigNumber . from ( 2 ) ,
188
- ] ) ; // stake has changed immediately, WARNING: this is misleading because it's not actually added to the SortitionSumTree
189
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore . sub ( PNK ( 1000 ) ) ) ; // PNK is transferred out of the juror's account
190
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
191
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
192
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
193
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 3000 ) , true ] ) ;
177
+ describe ( "When stake is increased" , ( ) => {
178
+ it ( "Should transfer PNK but delay the stake increase" , async ( ) => {
179
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
180
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
181
+ await pnk . approve ( core . address , PNK ( 1000 ) ) ;
182
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ;
183
+ await expect ( core . setStake ( 2 , PNK ( 3000 ) ) )
184
+ . to . emit ( core , "StakeDelayedAlreadyTransferred" )
185
+ . withArgs ( deployer , 2 , PNK ( 3000 ) ) ;
186
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [ PNK ( 5000 ) , 0 , PNK ( 3000 ) , 2 ] ) ; // stake has changed immediately, WARNING: this is misleading because it's not actually added to the SortitionSumTree
187
+ } ) ;
188
+
189
+ it ( "Should transfer some PNK out of the juror's account" , async ( ) => {
190
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore . sub ( PNK ( 1000 ) ) ) ; // PNK is transferred out of the juror's account
191
+ } ) ;
192
+
193
+ it ( "Should store the delayed stake for later" , async ( ) => {
194
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
195
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
196
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
197
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 3000 ) , true ] ) ;
198
+ } ) ;
194
199
} ) ;
195
200
196
- it ( "Should cancel out the stake decrease back" , async ( ) => {
197
- balanceBefore = await pnk . balanceOf ( deployer ) ;
198
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
199
- await expect ( core . setStake ( 2 , PNK ( 2000 ) ) )
200
- . to . emit ( core , "StakeDelayedNotTransferred" )
201
- . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
202
- expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
203
- PNK ( 4000 ) ,
204
- BigNumber . from ( 0 ) ,
205
- PNK ( 2000 ) ,
206
- BigNumber . from ( 2 ) ,
207
- ] ) ; // stake has changed immediately
208
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore . add ( PNK ( 1000 ) ) ) ; // PNK is sent back to the juror
209
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
210
- expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
211
- expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
212
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
213
- expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 2000 ) , false ] ) ;
201
+ describe ( "When stake is decreased back to the previous amount" , ( ) => {
202
+ it ( "Should cancel out the stake decrease back" , async ( ) => {
203
+ balanceBefore = await pnk . balanceOf ( deployer ) ;
204
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
205
+ await expect ( core . setStake ( 2 , PNK ( 2000 ) ) )
206
+ . to . emit ( core , "StakeDelayedNotTransferred" )
207
+ . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
208
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [ PNK ( 4000 ) , 0 , PNK ( 2000 ) , 2 ] ) ; // stake has changed immediately
209
+ } ) ;
210
+
211
+ it ( "Should transfer back some PNK to the juror" , async ( ) => {
212
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore . add ( PNK ( 1000 ) ) ) ; // PNK is sent back to the juror
213
+ } ) ;
214
+
215
+ it ( "Should store the delayed stake for later" , async ( ) => {
216
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
217
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
218
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
219
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
220
+ expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 2000 ) , false ] ) ;
221
+ } ) ;
214
222
} ) ;
223
+ } ) ;
215
224
225
+ describe ( "When the Phase passes back to Staking" , ( ) => {
216
226
it ( "Should execute the delayed stakes but the stakes should remain the same" , async ( ) => {
217
227
await reachStakingPhaseAfterDrawing ( ) ;
218
228
balanceBefore = await pnk . balanceOf ( deployer ) ;
@@ -221,15 +231,18 @@ describe("Staking", async () => {
221
231
PNK ( 4000 ) ,
222
232
PNK ( 300 ) , // we're the only juror so we are drawn 3 times
223
233
PNK ( 2000 ) ,
224
- BigNumber . from ( 2 ) ,
234
+ 2 ,
225
235
] ) ; // stake unchanged, delayed
226
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
227
236
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
228
237
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 3 ) ;
229
238
expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
230
239
expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 2nd delayed stake got deleted
231
240
expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ; // no delayed stakes left
232
241
} ) ;
242
+
243
+ it ( "Should not transfer any PNK" , async ( ) => {
244
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
245
+ } ) ;
233
246
} ) ;
234
247
} ) ;
235
248
@@ -247,7 +260,7 @@ describe("Staking", async () => {
247
260
await core . setStake ( 1 , PNK ( 2000 ) ) ;
248
261
await core . setStake ( 2 , PNK ( 2000 ) ) ;
249
262
250
- expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
263
+ expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ 1 , 2 ] ) ;
251
264
252
265
await core . functions [ "createDispute(uint256,bytes)" ] ( 2 , extraData , { value : arbitrationCost } ) ;
253
266
@@ -270,7 +283,7 @@ describe("Staking", async () => {
270
283
271
284
await sortition . passPhase ( ) ; // Drawing -> Staking. Change so we don't deal with delayed stakes
272
285
273
- expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
286
+ expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ 1 , 2 ] ) ;
274
287
275
288
await core . execute ( 0 , 0 , 1 ) ; // 1 iteration should unstake from both courts
276
289
0 commit comments