Skip to content

Commit 1a4a5f7

Browse files
committed
test: refactored and improved
1 parent 236d1d3 commit 1a4a5f7

File tree

1 file changed

+120
-107
lines changed

1 file changed

+120
-107
lines changed

contracts/test/arbitration/staking.ts

+120-107
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "../../typechain-types";
1212
import { expect } from "chai";
1313
import exp from "constants";
14+
import { it } from "mocha";
1415

1516
/* eslint-disable no-unused-vars */
1617
/* eslint-disable no-unused-expressions */
@@ -81,138 +82,147 @@ describe("Staking", async () => {
8182
await sortition.passPhase(); // Drawing -> Staking
8283
};
8384

84-
describe("When decreasing then increasing back stake", async () => {
85+
describe("When stake is decreased then increased back", async () => {
8586
before("Setup", async () => {
8687
await deploy();
8788
await reachDrawingPhase();
8889
});
8990

9091
it("Should be outside the Staking phase", async () => {
9192
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]);
9894
});
9995

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+
});
118117
});
119118

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+
});
138140
});
139141

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+
});
156163
});
157164
});
158165

159-
describe("When increasing then decreasing back stake", async () => {
166+
describe("When stake is increased then decreased back", async () => {
160167
before("Setup", async () => {
161168
await deploy();
162169
await reachDrawingPhase();
163170
});
164171

165172
it("Should be outside the Staking phase", async () => {
166173
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]);
173175
});
174176

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+
});
194199
});
195200

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+
});
214222
});
223+
});
215224

225+
describe("When the Phase passes back to Staking", () => {
216226
it("Should execute the delayed stakes but the stakes should remain the same", async () => {
217227
await reachStakingPhaseAfterDrawing();
218228
balanceBefore = await pnk.balanceOf(deployer);
@@ -221,15 +231,18 @@ describe("Staking", async () => {
221231
PNK(4000),
222232
PNK(300), // we're the only juror so we are drawn 3 times
223233
PNK(2000),
224-
BigNumber.from(2),
234+
2,
225235
]); // stake unchanged, delayed
226-
expect(await pnk.balanceOf(deployer)).to.be.equal(balanceBefore); // No PNK transfer yet
227236
expect(await sortition.delayedStakeWriteIndex()).to.be.equal(2);
228237
expect(await sortition.delayedStakeReadIndex()).to.be.equal(3);
229238
expect(await sortition.delayedStakes(1)).to.be.deep.equal([ethers.constants.AddressZero, 0, 0, false]); // the 1st delayed stake got deleted
230239
expect(await sortition.delayedStakes(2)).to.be.deep.equal([ethers.constants.AddressZero, 0, 0, false]); // the 2nd delayed stake got deleted
231240
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(0); // no delayed stakes left
232241
});
242+
243+
it("Should not transfer any PNK", async () => {
244+
expect(await pnk.balanceOf(deployer)).to.be.equal(balanceBefore); // No PNK transfer yet
245+
});
233246
});
234247
});
235248

@@ -247,7 +260,7 @@ describe("Staking", async () => {
247260
await core.setStake(1, PNK(2000));
248261
await core.setStake(2, PNK(2000));
249262

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]);
251264

252265
await core.functions["createDispute(uint256,bytes)"](2, extraData, { value: arbitrationCost });
253266

@@ -270,7 +283,7 @@ describe("Staking", async () => {
270283

271284
await sortition.passPhase(); // Drawing -> Staking. Change so we don't deal with delayed stakes
272285

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]);
274287

275288
await core.execute(0, 0, 1); // 1 iteration should unstake from both courts
276289

0 commit comments

Comments
 (0)