11import { ethers , getNamedAccounts , network , deployments } from "hardhat" ;
2+ const { anyValue } = require ( "@nomicfoundation/hardhat-chai-matchers/withArgs" ) ;
23import { BigNumber } from "ethers" ;
34import {
45 PNK ,
@@ -15,8 +16,8 @@ import exp from "constants";
1516/* eslint-disable no-unused-expressions */
1617
1718describe ( "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 ;
2021
2122 // 2nd court, 3 jurors, 1 dispute kit
2223 const extraData =
@@ -49,13 +50,13 @@ describe("Staking", async () => {
4950
5051 const reachDrawingPhase = async ( ) => {
5152 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 ) ;
5354
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
5556
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 ) ) ;
5960
6061 expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
6162
@@ -73,6 +74,13 @@ describe("Staking", async () => {
7374 balanceBefore = await pnk . balanceOf ( deployer ) ;
7475 } ;
7576
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+
7684 describe ( "When decreasing then increasing back stake" , async ( ) => {
7785 before ( "Setup" , async ( ) => {
7886 await deploy ( ) ;
@@ -82,9 +90,9 @@ describe("Staking", async () => {
8290 it ( "Should be outside the Staking phase" , async ( ) => {
8391 expect ( await sortition . phase ( ) ) . to . be . equal ( 1 ) ; // Drawing
8492 expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
85- ONE_THOUSAND_PNK . mul ( 4 ) ,
93+ PNK ( 4000 ) ,
8694 BigNumber . from ( 0 ) ,
87- ONE_THOUSAND_PNK . mul ( 2 ) ,
95+ PNK ( 2000 ) ,
8896 BigNumber . from ( 2 ) ,
8997 ] ) ;
9098 } ) ;
@@ -93,40 +101,58 @@ describe("Staking", async () => {
93101 expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
94102 expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
95103 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 ) ) )
97105 . 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 ) ) ;
100107 expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
101- ONE_THOUSAND_PNK . mul ( 4 ) ,
108+ PNK ( 4000 ) ,
102109 BigNumber . from ( 0 ) ,
103- ONE_THOUSAND_PNK . mul ( 2 ) ,
110+ PNK ( 2000 ) ,
104111 BigNumber . from ( 2 ) ,
105112 ] ) ; // stake unchanged, delayed
106113 expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
114+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 1 ) ;
107115 expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
108116 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 ] ) ;
110118 } ) ;
111119
112120 it ( "Should delay the stake increase back to the previous amount" , async ( ) => {
113121 balanceBefore = await pnk . balanceOf ( deployer ) ;
114122 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 ) ) )
116124 . 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 ) ) ;
119126 expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
120- ONE_THOUSAND_PNK . mul ( 4 ) ,
127+ PNK ( 4000 ) ,
121128 BigNumber . from ( 0 ) ,
122- ONE_THOUSAND_PNK . mul ( 2 ) ,
129+ PNK ( 2000 ) ,
123130 BigNumber . from ( 2 ) ,
124131 ] ) ; // stake unchanged, delayed
125132 expect ( await pnk . balanceOf ( deployer ) ) . to . be . equal ( balanceBefore ) ; // No PNK transfer yet
133+ expect ( await sortition . latestDelayedStakeIndex ( deployer , 2 ) ) . to . be . equal ( 2 ) ;
126134 expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
127135 expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
128136 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
130156 } ) ;
131157 } ) ;
132158
@@ -139,52 +165,70 @@ describe("Staking", async () => {
139165 it ( "Should be outside the Staking phase" , async ( ) => {
140166 expect ( await sortition . phase ( ) ) . to . be . equal ( 1 ) ; // Drawing
141167 expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
142- ONE_THOUSAND_PNK . mul ( 4 ) ,
168+ PNK ( 4000 ) ,
143169 BigNumber . from ( 0 ) ,
144- ONE_THOUSAND_PNK . mul ( 2 ) ,
170+ PNK ( 2000 ) ,
145171 BigNumber . from ( 2 ) ,
146172 ] ) ;
147173 } ) ;
148174
149175 it ( "Should transfer PNK but delay the stake increase" , async ( ) => {
150176 expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 0 ) ;
151177 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 ) ) ;
153179 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 ) ) )
155181 . 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 ) ) ;
158183 expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
159- ONE_THOUSAND_PNK . mul ( 5 ) ,
184+ PNK ( 5000 ) ,
160185 BigNumber . from ( 0 ) ,
161- ONE_THOUSAND_PNK . mul ( 3 ) ,
186+ PNK ( 3000 ) ,
162187 BigNumber . from ( 2 ) ,
163188 ] ) ; // 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 ) ;
165191 expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 1 ) ;
166192 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 ] ) ;
168194 } ) ;
169195
170196 it ( "Should cancel out the stake decrease back" , async ( ) => {
171197 balanceBefore = await pnk . balanceOf ( deployer ) ;
172198 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 ) ) )
174200 . 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 ) ) ;
177202 expect ( await core . getJurorBalance ( deployer , 2 ) ) . to . be . deep . equal ( [
178- ONE_THOUSAND_PNK . mul ( 4 ) ,
203+ PNK ( 4000 ) ,
179204 BigNumber . from ( 0 ) ,
180- ONE_THOUSAND_PNK . mul ( 2 ) ,
205+ PNK ( 2000 ) ,
181206 BigNumber . from ( 2 ) ,
182207 ] ) ; // 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 ) ;
184210 expect ( await sortition . delayedStakeWriteIndex ( ) ) . to . be . equal ( 2 ) ;
185211 expect ( await sortition . delayedStakeReadIndex ( ) ) . to . be . equal ( 1 ) ;
186212 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
188232 } ) ;
189233 } ) ;
190234 } ) ;
@@ -195,13 +239,13 @@ describe("Staking", async () => {
195239 } ) ;
196240
197241 it ( "Should unstake from all courts" , async ( ) => {
198- const arbitrationCost = ONE_TENTH_ETH . mul ( 3 ) ;
242+ const arbitrationCost = ETH ( 0.1 ) . mul ( 3 ) ;
199243
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
201245
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 ) ) ;
205249
206250 expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
207251
@@ -224,7 +268,7 @@ describe("Staking", async () => {
224268 await core . passPeriod ( 0 ) ; // Voting -> Appeal
225269 await core . passPeriod ( 0 ) ; // Appeal -> Execution
226270
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
228272
229273 expect ( await core . getJurorCourtIDs ( deployer ) ) . to . be . deep . equal ( [ BigNumber . from ( "1" ) , BigNumber . from ( "2" ) ] ) ;
230274
0 commit comments