Skip to content

Commit

Permalink
test: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybuidl committed Mar 11, 2024
1 parent a9e8ead commit 99fe8f0
Showing 1 changed file with 63 additions and 65 deletions.
128 changes: 63 additions & 65 deletions contracts/test/arbitration/staking-neo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,51 +79,47 @@ describe("Staking", async () => {

// Sets up the happy path for Neo
await nft.safeMint(deployer);
await nft.safeMint(juror.address);
await sortition.changeMaxStakePerJuror(PNK(10_000));
await sortition.changeMaxTotalStaked(PNK(20_000));
};

const reachGeneratingPhaseFromStaking = async () => {
const createDisputeAndReachGeneratingPhaseFromStaking = async () => {
const arbitrationCost = ETH(0.5);
await resolver.createDisputeForTemplate(extraData, "", "", 2, { value: arbitrationCost });
await network.provider.send("evm_increaseTime", [3600]);
await reachGeneratingPhaseFromStaking();
};

const reachGeneratingPhaseFromStaking = async () => {
await network.provider.send("evm_increaseTime", [3600]); // Wait for minStakingTime
await network.provider.send("evm_mine");

expect(await sortition.phase()).to.be.equal(0); // Staking
await sortition.passPhase(); // Staking -> Generating
expect(await sortition.phase()).to.be.equal(1); // Generating
};

const reachDrawingPhase = async () => {
expect(await sortition.phase()).to.be.equal(0); // Staking
const arbitrationCost = ETH(0.1).mul(3);

await core.createCourt(1, false, PNK(1000), 1000, ETH(0.1), 3, [0, 0, 0, 0], 3, [1]); // Parent - general court, Classic dispute kit

await pnk.approve(core.address, PNK(4000));
await core.setStake(1, PNK(2000));
await core.setStake(2, PNK(2000));

expect(await sortition.getJurorCourtIDs(deployer)).to.be.deep.equal([BigNumber.from("1"), BigNumber.from("2")]);

await resolver.createDisputeForTemplate(extraData, "", "", 2, { value: arbitrationCost });

await network.provider.send("evm_increaseTime", [2000]); // Wait for minStakingTime
await network.provider.send("evm_mine");
const drawFromGeneratingPhase = async () => {
expect(await sortition.phase()).to.be.equal(1); // Generating

const lookahead = await sortition.rngLookahead();
await sortition.passPhase(); // Staking -> Generating
for (let index = 0; index < lookahead; index++) {
await network.provider.send("evm_mine");
}

balanceBefore = await pnk.balanceOf(deployer);
};

const reachStakingPhaseAfterDrawing = async () => {
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
await sortition.passPhase(); // Generating -> Drawing
expect(await sortition.phase()).to.be.equal(2); // Drawing

await core.draw(0, 10);
};

const drawAndReachStakingPhaseFromGenerating = async () => {
await drawFromGeneratingPhase();

await network.provider.send("evm_increaseTime", [3600]); // Ensures that maxDrawingTime has passed
await network.provider.send("evm_mine");

await sortition.passPhase(); // Drawing -> Staking
expect(await sortition.phase()).to.be.equal(0); // Staking
};
Expand Down Expand Up @@ -219,7 +215,7 @@ describe("Staking", async () => {

describe("When stakes are delayed", () => {
beforeEach("Setup", async () => {
await reachGeneratingPhaseFromStaking();
await createDisputeAndReachGeneratingPhaseFromStaking();
});

it("Should be able to unstake", async () => {
Expand All @@ -228,7 +224,7 @@ describe("Staking", async () => {
.withArgs(juror.address, 1, PNK(0))
.to.not.emit(sortition, "StakeSet");
expect(await sortition.totalStaked()).to.be.equal(PNK(1000));
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
expect(await sortition.executeDelayedStakes(10))
.to.emit(sortition, "StakeSet")
.withArgs(juror.address, 1, PNK(0));
Expand Down Expand Up @@ -256,28 +252,28 @@ describe("Staking", async () => {

describe("When stakes are delayed", () => {
it("Should not be able to stake more than maxStakePerJuror", async () => {
await reachGeneratingPhaseFromStaking();
await createDisputeAndReachGeneratingPhaseFromStaking();
await pnk.connect(juror).approve(core.address, PNK(5000));
await expect(core.connect(juror).setStake(1, PNK(5000))).to.be.revertedWithCustomError(
core,
"StakingMoreThanMaxStakePerJuror"
);
expect(await sortition.totalStaked()).to.be.equal(PNK(0));
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
await expect(sortition.executeDelayedStakes(10)).to.revertedWith("No delayed stake to execute.");
expect(await sortition.totalStaked()).to.be.equal(PNK(0));
});

it("Should be able to stake exactly maxStakePerJuror", async () => {
await pnk.connect(juror).approve(core.address, PNK(5000));
await core.connect(juror).setStake(1, PNK(1000));
await reachGeneratingPhaseFromStaking();
await createDisputeAndReachGeneratingPhaseFromStaking();
expect(await core.connect(juror).setStake(1, PNK(2000)))
.to.emit(sortition, "StakeDelayedAlreadyTransferred")
.withArgs(juror.address, 1, PNK(2000))
.to.not.emit(sortition, "StakeSet");
expect(await sortition.totalStaked()).to.be.equal(PNK(1000));
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
expect(await sortition.executeDelayedStakes(10))
.to.emit(sortition, "StakeSet")
.withArgs(juror.address, 1, PNK(2000));
Expand Down Expand Up @@ -317,7 +313,7 @@ describe("Staking", async () => {

describe("When stakes are delayed", () => {
beforeEach("Setup", async () => {
await reachGeneratingPhaseFromStaking();
await createDisputeAndReachGeneratingPhaseFromStaking();
});

it("Should not be able to stake more than maxTotalStaked", async () => {
Expand All @@ -327,7 +323,7 @@ describe("Staking", async () => {
"StakingMoreThanMaxTotalStaked"
);
expect(await sortition.totalStaked()).to.be.equal(PNK(2000));
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
await expect(sortition.executeDelayedStakes(10)).to.revertedWith("No delayed stake to execute.");
expect(await sortition.totalStaked()).to.be.equal(PNK(2000));
});
Expand All @@ -338,7 +334,7 @@ describe("Staking", async () => {
.to.emit(sortition, "StakeDelayedAlreadyTransferred")
.withArgs(juror.address, 1, PNK(1000));
expect(await sortition.totalStaked()).to.be.equal(PNK(2000)); // Not updated until the delayed stake is executed
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
await expect(await sortition.executeDelayedStakes(10))
.to.emit(sortition, "StakeSet")
.withArgs(juror.address, 1, PNK(1000));
Expand Down Expand Up @@ -398,14 +394,29 @@ describe("Staking", async () => {
});

describe("When outside the Staking phase", async () => {
const createSubcourtStakeAndCreateDispute = async () => {
expect(await sortition.phase()).to.be.equal(0); // Staking
await core.createCourt(1, false, PNK(1000), 1000, ETH(0.1), 3, [0, 0, 0, 0], 3, [1]); // Parent - general court, Classic dispute kit

await pnk.approve(core.address, PNK(4000));
await core.setStake(1, PNK(2000));
await core.setStake(2, PNK(2000));
expect(await sortition.getJurorCourtIDs(deployer)).to.be.deep.equal([BigNumber.from("1"), BigNumber.from("2")]);

const arbitrationCost = ETH(0.1).mul(3);
await resolver.createDisputeForTemplate(extraData, "", "", 2, { value: arbitrationCost });
};

describe("When stake is increased once", async () => {
before("Setup", async () => {
await deploy();
await reachDrawingPhase();
await createSubcourtStakeAndCreateDispute();
await reachGeneratingPhaseFromStaking();
balanceBefore = await pnk.balanceOf(deployer);
});

it("Should be outside the Staking phase", async () => {
expect(await sortition.phase()).to.be.equal(1); // Drawing
expect(await sortition.phase()).to.be.equal(1); // Generating
expect(await sortition.getJurorBalance(deployer, 2)).to.be.deep.equal([PNK(4000), 0, PNK(2000), 2]);
});

Expand Down Expand Up @@ -435,7 +446,7 @@ describe("Staking", async () => {

describe("When the Phase passes back to Staking", () => {
before("Setup", async () => {
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
balanceBefore = await pnk.balanceOf(deployer);
});

Expand Down Expand Up @@ -468,11 +479,13 @@ describe("Staking", async () => {
describe("When stake is decreased once", async () => {
before("Setup", async () => {
await deploy();
await reachDrawingPhase();
await createSubcourtStakeAndCreateDispute();
await reachGeneratingPhaseFromStaking();
balanceBefore = await pnk.balanceOf(deployer);
});

it("Should be outside the Staking phase", async () => {
expect(await sortition.phase()).to.be.equal(1); // Drawing
expect(await sortition.phase()).to.be.equal(1); // Generating
expect(await sortition.getJurorBalance(deployer, 2)).to.be.deep.equal([PNK(4000), 0, PNK(2000), 2]);
});

Expand Down Expand Up @@ -501,7 +514,7 @@ describe("Staking", async () => {

describe("When the Phase passes back to Staking", () => {
before("Setup", async () => {
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
balanceBefore = await pnk.balanceOf(deployer);
});

Expand Down Expand Up @@ -531,11 +544,13 @@ describe("Staking", async () => {
describe("When stake is decreased then increased back", async () => {
before("Setup", async () => {
await deploy();
await reachDrawingPhase();
await createSubcourtStakeAndCreateDispute();
await reachGeneratingPhaseFromStaking();
balanceBefore = await pnk.balanceOf(deployer);
});

it("Should be outside the Staking phase", async () => {
expect(await sortition.phase()).to.be.equal(1); // Drawing
expect(await sortition.phase()).to.be.equal(1); // Generating
expect(await sortition.getJurorBalance(deployer, 2)).to.be.deep.equal([PNK(4000), 0, PNK(2000), 2]);
});

Expand Down Expand Up @@ -588,7 +603,7 @@ describe("Staking", async () => {

describe("When the Phase passes back to Staking", () => {
before("Setup", async () => {
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
balanceBefore = await pnk.balanceOf(deployer);
});

Expand Down Expand Up @@ -618,11 +633,13 @@ describe("Staking", async () => {
describe("When stake is increased then decreased back", async () => {
before("Setup", async () => {
await deploy();
await reachDrawingPhase();
await createSubcourtStakeAndCreateDispute();
await reachGeneratingPhaseFromStaking();
balanceBefore = await pnk.balanceOf(deployer);
});

it("Should be outside the Staking phase", async () => {
expect(await sortition.phase()).to.be.equal(1); // Drawing
expect(await sortition.phase()).to.be.equal(1); // Generating
expect(await sortition.getJurorBalance(deployer, 2)).to.be.deep.equal([PNK(4000), 0, PNK(2000), 2]);
});

Expand Down Expand Up @@ -677,7 +694,7 @@ describe("Staking", async () => {

describe("When the Phase passes back to Staking", () => {
before("Setup", async () => {
await reachStakingPhaseAfterDrawing();
await drawAndReachStakingPhaseFromGenerating();
balanceBefore = await pnk.balanceOf(deployer);
});

Expand Down Expand Up @@ -714,41 +731,22 @@ describe("Staking", async () => {
});

it("Should unstake from all courts", async () => {
const arbitrationCost = ETH(0.1).mul(3);

await core.createCourt(1, false, PNK(1000), 1000, ETH(0.1), 3, [0, 0, 0, 0], 3, [1]); // Parent - general court, Classic dispute kit

await pnk.approve(core.address, PNK(4000));
await core.setStake(1, PNK(2000));
await core.setStake(2, PNK(2000));

expect(await sortition.getJurorCourtIDs(deployer)).to.be.deep.equal([1, 2]);

await resolver.createDisputeForTemplate(extraData, "", "", 2, { value: arbitrationCost });

await network.provider.send("evm_increaseTime", [2000]); // Wait for minStakingTime
await network.provider.send("evm_mine");

const lookahead = await sortition.rngLookahead();
await sortition.passPhase(); // Staking -> Generating
for (let index = 0; index < lookahead; index++) {
await network.provider.send("evm_mine");
}
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
await sortition.passPhase(); // Generating -> Drawing

await core.draw(0, 5000);
await createDisputeAndReachGeneratingPhaseFromStaking();
await drawAndReachStakingPhaseFromGenerating();

await core.passPeriod(0); // Evidence -> Voting
await core.passPeriod(0); // Voting -> Appeal
await core.passPeriod(0); // Appeal -> Execution

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

expect(await sortition.getJurorCourtIDs(deployer)).to.be.deep.equal([1, 2]);

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

expect(await sortition.getJurorCourtIDs(deployer)).to.be.deep.equal([]);
});
});
Expand Down

0 comments on commit 99fe8f0

Please sign in to comment.