1
1
import { ethers , getNamedAccounts , network , deployments } from "hardhat" ;
2
+ const { anyValue } = require ( "@nomicfoundation/hardhat-chai-matchers/withArgs" ) ;
2
3
import { BigNumber } from "ethers" ;
3
4
import {
4
5
PNK ,
@@ -15,8 +16,8 @@ import exp from "constants";
15
16
/* eslint-disable no-unused-expressions */
16
17
17
18
describe ( "Staking" , async ( ) => {
18
- const ONE_TENTH_ETH = BigNumber . from ( 10 ) . pow ( 17 ) ;
19
- const ONE_THOUSAND_PNK = BigNumber . from ( 10 ) . pow ( 21 ) ;
19
+ const ETH = ( amount : number ) => ethers . utils . parseUnits ( amount . toString ( ) ) ;
20
+ const PNK = ETH ;
20
21
21
22
// 2nd court, 3 jurors, 1 dispute kit
22
23
const extraData =
@@ -49,13 +50,13 @@ describe("Staking", async () => {
49
50
50
51
const reachDrawingPhase = async ( ) => {
51
52
expect ( await sortition . phase ( ) ) . to . be . equal ( 0 ) ; // Staking
52
- const arbitrationCost = ONE_TENTH_ETH . mul ( 3 ) ;
53
+ const arbitrationCost = ETH ( 0.1 ) . mul ( 3 ) ;
53
54
54
- await core . createCourt ( 1 , false , ONE_THOUSAND_PNK , 1000 , ONE_TENTH_ETH , 3 , [ 0 , 0 , 0 , 0 ] , 3 , [ 1 ] ) ; // Parent - general court, Classic dispute kit
55
+ await core . createCourt ( 1 , false , PNK ( 1000 ) , 1000 , ETH ( 0.1 ) , 3 , [ 0 , 0 , 0 , 0 ] , 3 , [ 1 ] ) ; // Parent - general court, Classic dispute kit
55
56
56
- await pnk . approve ( core . address , ONE_THOUSAND_PNK . mul ( 4 ) ) ;
57
- await core . setStake ( 1 , ONE_THOUSAND_PNK . mul ( 2 ) ) ;
58
- await core . setStake ( 2 , ONE_THOUSAND_PNK . mul ( 2 ) ) ;
57
+ await pnk . approve ( core . address , PNK ( 4000 ) ) ;
58
+ await core . setStake ( 1 , PNK ( 2000 ) ) ;
59
+ await core . setStake ( 2 , PNK ( 2000 ) ) ;
59
60
60
61
expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
61
62
@@ -73,6 +74,13 @@ describe("Staking", async () => {
73
74
balanceBefore = await pnk . balanceOf ( deployer ) ;
74
75
} ;
75
76
77
+ const reachStakingPhaseAfterDrawing = async ( ) => {
78
+ await randomizer . relay ( rng . address , 0 , ethers . utils . randomBytes ( 32 ) ) ;
79
+ await sortition . passPhase ( ) ; // Generating -> Drawing
80
+ await core . draw ( 0 , 5000 ) ;
81
+ await sortition . passPhase ( ) ; // Drawing -> Staking
82
+ } ;
83
+
76
84
describe ( "When decreasing then increasing back stake" , async ( ) => {
77
85
before ( "Setup" , async ( ) => {
78
86
await deploy ( ) ;
@@ -82,9 +90,9 @@ describe("Staking", async () => {
82
90
it ( "Should be outside the Staking phase" , async ( ) => {
83
91
expect ( await sortition . phase ( ) ) . to . be . equal ( 1 ) ; // Drawing
84
92
expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
85
- ONE_THOUSAND_PNK . mul ( 4 ) ,
93
+ PNK ( 4000 ) ,
86
94
BigNumber . from ( 0 ) ,
87
- ONE_THOUSAND_PNK . mul ( 2 ) ,
95
+ PNK ( 2000 ) ,
88
96
BigNumber . from ( 2 ) ,
89
97
] ) ;
90
98
} ) ;
@@ -93,40 +101,58 @@ describe("Staking", async () => {
93
101
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
94
102
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
95
103
expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ;
96
- await expect ( core . setStake ( 2 , ONE_THOUSAND_PNK . mul ( 1 ) ) )
104
+ await expect ( core . setStake ( 2 , PNK ( 1000 ) ) )
97
105
. to . emit ( core , "StakeDelayedNotTransferred" )
98
- . withArgs ( deployer , 2 , ONE_THOUSAND_PNK . mul ( 1 ) ) ;
99
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
106
+ . withArgs ( deployer , 2 , PNK ( 1000 ) ) ;
100
107
expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
101
- ONE_THOUSAND_PNK . mul ( 4 ) ,
108
+ PNK ( 4000 ) ,
102
109
BigNumber . from ( 0 ) ,
103
- ONE_THOUSAND_PNK . mul ( 2 ) ,
110
+ PNK ( 2000 ) ,
104
111
BigNumber . from ( 2 ) ,
105
112
] ) ; // stake unchanged, delayed
106
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 ) ;
107
115
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
108
116
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
109
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , ONE_THOUSAND_PNK . mul ( 1 ) , false ] ) ;
117
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 1000 ) , false ] ) ;
110
118
} ) ;
111
119
112
120
it ( "Should delay the stake increase back to the previous amount" , async ( ) => {
113
121
balanceBefore = await pnk . balanceOf ( deployer ) ;
114
122
expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
115
- await expect ( core . setStake ( 2 , ONE_THOUSAND_PNK . mul ( 2 ) ) )
123
+ await expect ( core . setStake ( 2 , PNK ( 2000 ) ) )
116
124
. to . emit ( core , "StakeDelayedNotTransferred" )
117
- . withArgs ( deployer , 2 , ONE_THOUSAND_PNK . mul ( 2 ) ) ;
118
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
125
+ . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
119
126
expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
120
- ONE_THOUSAND_PNK . mul ( 4 ) ,
127
+ PNK ( 4000 ) ,
121
128
BigNumber . from ( 0 ) ,
122
- ONE_THOUSAND_PNK . mul ( 2 ) ,
129
+ PNK ( 2000 ) ,
123
130
BigNumber . from ( 2 ) ,
124
131
] ) ; // stake unchanged, delayed
125
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 ) ;
126
134
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
127
135
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
128
136
expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
129
- expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , ONE_THOUSAND_PNK . mul ( 2 ) , false ] ) ;
137
+ expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 2000 ) , false ] ) ;
138
+ } ) ;
139
+
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
130
156
} ) ;
131
157
} ) ;
132
158
@@ -139,52 +165,70 @@ describe("Staking", async () => {
139
165
it ( "Should be outside the Staking phase" , async ( ) => {
140
166
expect ( await sortition . phase ( ) ) . to . be . equal ( 1 ) ; // Drawing
141
167
expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
142
- ONE_THOUSAND_PNK . mul ( 4 ) ,
168
+ PNK ( 4000 ) ,
143
169
BigNumber . from ( 0 ) ,
144
- ONE_THOUSAND_PNK . mul ( 2 ) ,
170
+ PNK ( 2000 ) ,
145
171
BigNumber . from ( 2 ) ,
146
172
] ) ;
147
173
} ) ;
148
174
149
175
it ( "Should transfer PNK but delay the stake increase" , async ( ) => {
150
176
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
151
177
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
152
- await pnk . approve ( core . address , ONE_THOUSAND_PNK . mul ( 1 ) ) ;
178
+ await pnk . approve ( core . address , PNK ( 1000 ) ) ;
153
179
expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ;
154
- await expect ( core . setStake ( 2 , ONE_THOUSAND_PNK . mul ( 3 ) ) )
180
+ await expect ( core . setStake ( 2 , PNK ( 3000 ) ) )
155
181
. to . emit ( core , "StakeDelayedAlreadyTransferred" )
156
- . withArgs ( deployer , 2 , ONE_THOUSAND_PNK . mul ( 3 ) ) ;
157
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
182
+ . withArgs ( deployer , 2 , PNK ( 3000 ) ) ;
158
183
expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
159
- ONE_THOUSAND_PNK . mul ( 5 ) ,
184
+ PNK ( 5000 ) ,
160
185
BigNumber . from ( 0 ) ,
161
- ONE_THOUSAND_PNK . mul ( 3 ) ,
186
+ PNK ( 3000 ) ,
162
187
BigNumber . from ( 2 ) ,
163
188
] ) ; // stake has changed immediately, WARNING: this is misleading because it's not actually added to the SortitionSumTree
164
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore . sub ( ONE_THOUSAND_PNK ) ) ; // PNK is transferred out of the juror's account
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 ) ;
165
191
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
166
192
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
167
- expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , ONE_THOUSAND_PNK . mul ( 3 ) , true ] ) ;
193
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 3000 ) , true ] ) ;
168
194
} ) ;
169
195
170
196
it ( "Should cancel out the stake decrease back" , async ( ) => {
171
197
balanceBefore = await pnk . balanceOf ( deployer ) ;
172
198
expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
173
- await expect ( core . setStake ( 2 , ONE_THOUSAND_PNK . mul ( 2 ) ) )
199
+ await expect ( core . setStake ( 2 , PNK ( 2000 ) ) )
174
200
. to . emit ( core , "StakeDelayedNotTransferred" )
175
- . withArgs ( deployer , 2 , ONE_THOUSAND_PNK . mul ( 2 ) ) ;
176
- expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
201
+ . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
177
202
expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
178
- ONE_THOUSAND_PNK . mul ( 4 ) ,
203
+ PNK ( 4000 ) ,
179
204
BigNumber . from ( 0 ) ,
180
- ONE_THOUSAND_PNK . mul ( 2 ) ,
205
+ PNK ( 2000 ) ,
181
206
BigNumber . from ( 2 ) ,
182
207
] ) ; // stake has changed immediately
183
- expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore . add ( ONE_THOUSAND_PNK ) ) ; // PNK is sent back to the juror
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 ) ;
184
210
expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
185
211
expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
186
212
expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
187
- expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , ONE_THOUSAND_PNK . mul ( 2 ) , false ] ) ;
213
+ expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ deployer , 2 , PNK ( 2000 ) , false ] ) ;
214
+ } ) ;
215
+
216
+ it ( "Should execute the delayed stakes but the stakes should remain the same" , async ( ) => {
217
+ await reachStakingPhaseAfterDrawing ( ) ;
218
+ balanceBefore = await pnk . balanceOf ( deployer ) ;
219
+ await expect ( sortition . executeDelayedStakes ( 10 ) ) . to . emit ( core , "StakeSet" ) . withArgs ( deployer , 2 , PNK ( 2000 ) ) ;
220
+ expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
221
+ PNK ( 4000 ) ,
222
+ PNK ( 300 ) , // we're the only juror so we are drawn 3 times
223
+ PNK ( 2000 ) ,
224
+ BigNumber . from ( 2 ) ,
225
+ ] ) ; // stake unchanged, delayed
226
+ expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
227
+ expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
228
+ expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 3 ) ;
229
+ expect ( await sortition . delayedStakes ( 1 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 1st delayed stake got deleted
230
+ expect ( await sortition . delayedStakes ( 2 ) ) . to . be . deep . equal ( [ ethers . constants . AddressZero , 0 , 0 , false ] ) ; // the 2nd delayed stake got deleted
231
+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 0 ) ; // no delayed stakes left
188
232
} ) ;
189
233
} ) ;
190
234
} ) ;
@@ -195,13 +239,13 @@ describe("Staking", async () => {
195
239
} ) ;
196
240
197
241
it ( "Should unstake from all courts" , async ( ) => {
198
- const arbitrationCost = ONE_TENTH_ETH . mul ( 3 ) ;
242
+ const arbitrationCost = ETH ( 0.1 ) . mul ( 3 ) ;
199
243
200
- await core . createCourt ( 1 , false , ONE_THOUSAND_PNK , 1000 , ONE_TENTH_ETH , 3 , [ 0 , 0 , 0 , 0 ] , 3 , [ 1 ] ) ; // Parent - general court, Classic dispute kit
244
+ await core . createCourt ( 1 , false , PNK ( 1000 ) , 1000 , ETH ( 0.1 ) , 3 , [ 0 , 0 , 0 , 0 ] , 3 , [ 1 ] ) ; // Parent - general court, Classic dispute kit
201
245
202
- await pnk . approve ( core . address , ONE_THOUSAND_PNK . mul ( 4 ) ) ;
203
- await core . setStake ( 1 , ONE_THOUSAND_PNK . mul ( 2 ) ) ;
204
- await core . setStake ( 2 , ONE_THOUSAND_PNK . mul ( 2 ) ) ;
246
+ await pnk . approve ( core . address , PNK ( 4000 ) ) ;
247
+ await core . setStake ( 1 , PNK ( 2000 ) ) ;
248
+ await core . setStake ( 2 , PNK ( 2000 ) ) ;
205
249
206
250
expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
207
251
@@ -224,7 +268,7 @@ describe("Staking", async () => {
224
268
await core . passPeriod ( 0 ) ; // Voting -> Appeal
225
269
await core . passPeriod ( 0 ) ; // Appeal -> Execution
226
270
227
- await sortition . passPhase ( ) ; // Freezing -> Staking. Change so we don't deal with delayed stakes
271
+ await sortition . passPhase ( ) ; // Drawing -> Staking. Change so we don't deal with delayed stakes
228
272
229
273
expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
230
274
0 commit comments