Skip to content

Commit

Permalink
removed duplicates between test utils and playbook utils
Browse files Browse the repository at this point in the history
  • Loading branch information
peersky committed Jan 16, 2025
1 parent 1d0daac commit 222fce3
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 749 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-deers-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rankify-contracts': patch
---

removed duplicates between test utils and playbook utils
12 changes: 11 additions & 1 deletion playbook/instance/discussionTopics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,21 @@ const proposals = {
'Expand Ensure the assistant is accessible to users with disabilities by providing alternative input methods and output formats. to include more detailed visualizations and customization options, allowing users to tailor the experience to their preferences.',
'Address potential privacy concerns related to Ensure the assistant is accessible to users with disabilities by providing alternative input methods and output formats. by implementing robust data anonymization and user consent mechanisms.',
],
'player9-id': [
'Implement a smart notification system that learns from user behavior and prioritizes important alerts.',
"Create customizable notification categories inspired by player2-id's focus mode concept.",
'Add machine learning capabilities to predict the best times for different types of notifications based on user activity patterns.',
"Integrate with the task manager to provide intelligent reminders, building on player1-id's organization features.",
'Develop a unified notification center with smart filtering and grouping options.',
"Further enhance the unified notification center by incorporating player7-id's recommendation engine for better alert prioritization.",
'Expand the notification system to include collaborative features, allowing team members to coordinate through smart alerts.',
'Implement advanced privacy controls for notification content and user activity data.',
],
} as const;

export function getDiscussionForTurn(turn: number, proposerId: string) {
const proposerProposals = proposals[proposerId as keyof typeof proposals];
if (turn <= proposerProposals.length) {
if (proposerProposals && turn <= proposerProposals.length) {
return proposerProposals[turn - 1];
}

Expand Down
33 changes: 33 additions & 0 deletions playbook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,39 @@ export const mockProposals = async ({
return proposals;
};

const joinTypes = {
AttestJoiningGame: [
{ type: 'address', name: 'instance' },
{ type: 'address', name: 'participant' },
{ type: 'uint256', name: 'gameId' },
{ type: 'bytes32', name: 'hiddenSalt' },
],
};
export const signJoiningGame = async (
hre: HardhatRuntimeEnvironment,
verifier: string,
participant: string,
gameId: BigNumberish,
signer: SignerIdentity,
) => {
const { ethers } = hre;
let { chainId } = await ethers.provider.getNetwork();
const domain = {
name: RANKIFY_INSTANCE_CONTRACT_NAME,
version: RANKIFY_INSTANCE_CONTRACT_VERSION,
chainId,
verifyingContract: verifier,
};
const hiddenSalt = ethers.utils.hexZeroPad('0x123131231311', 32); // Pad to 32 bytes
const signature = await signer.wallet._signTypedData(domain, joinTypes, {
instance: verifier,
participant,
gameId,
hiddenSalt: ethers.utils.keccak256(hiddenSalt), // Hash the padded value
});
return { signature, hiddenSalt };
};

export default {
setupAddresses,
setupEnvironment,
Expand Down
3 changes: 2 additions & 1 deletion test/MAODistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { deployments, ethers, getNamedAccounts } from 'hardhat';
import hre from 'hardhat';
import { expect } from 'chai';
import { MAODistribution, DAODistributor, Rankify, RankifyDiamondInstance } from '../types';
import { AdrSetupResult, setupTest } from './utils';
import { setupTest } from './utils';
import { AdrSetupResult } from '../playbook/utils';
import { getCodeIdFromArtifact } from '../scripts/getCodeId';
import addDistribution from '../scripts/playbooks/addDistribution';
import { generateDistributorData } from '../scripts/libraries/generateDistributorData';
Expand Down
3 changes: 2 additions & 1 deletion test/RankToken.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ethers, getNamedAccounts, network } from 'hardhat';
import { expect } from 'chai';
import hre, { deployments } from 'hardhat';
import { AdrSetupResult, EnvSetupResult, RInstanceSettings, setupTest } from './utils';
import { setupTest } from './utils';
import { AdrSetupResult, EnvSetupResult, RInstanceSettings } from '../playbook/utils';
import { RankifyDiamondInstance, RankToken, Rankify } from '../types';
import addDistribution from '../scripts/playbooks/addDistribution';
import { getCodeIdFromArtifact } from '../scripts/getCodeId';
Expand Down
81 changes: 44 additions & 37 deletions test/RankifyInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
signJoiningGame,
} from '../playbook/utils';
import { setupTest } from './utils';
import { RInstanceSettings, mineBlocks, mockProposals, mockVotes, getPlayers } from './utils';
import { RInstanceSettings, mineBlocks, mockProposals, mockVotes, getPlayers } from '../playbook/utils';
import { expect } from 'chai';
import { time } from '@nomicfoundation/hardhat-network-helpers';
import { DistributableGovernanceERC20, Rankify, RankifyDiamondInstance, RankToken } from '../types/';
Expand All @@ -25,7 +25,7 @@ import { assert } from 'console';
import { solidityKeccak256 } from 'ethers/lib/utils';
import addDistribution from '../scripts/playbooks/addDistribution';
import hre from 'hardhat';
import { network } from 'hardhat';

const scriptName = path.basename(__filename);

import { getCodeIdFromArtifact } from '../scripts/getCodeId';
Expand All @@ -41,6 +41,7 @@ let rankToken: RankToken;
let govtToken: DistributableGovernanceERC20;

const createGame = async (
minGameTime: BigNumberish,
gameContract: RankifyDiamondInstance,
signer: SignerIdentity,
gameMaster: string,
Expand All @@ -57,7 +58,7 @@ const createGame = async (
timeToJoin: RInstanceSettings.RInstance_TIME_TO_JOIN,
nTurns: RInstance_MAX_TURNS,
voteCredits: RInstanceSettings.RInstance_VOTE_CREDITS,
minGameTime: RInstanceSettings.RInstance_MIN_GAME_TIME,
minGameTime: minGameTime,
};
await gameContract.connect(signer.wallet).createGame(params);
const gameId = await gameContract.getContractState().then(state => state.numGames);
Expand All @@ -81,7 +82,10 @@ const runToTheEnd = async (
}

const proposals = await mockValidProposals(players, gameContract, gameMaster, gameId, true);
if (isLastTurn) await time.increase(RInstanceSettings.RInstance_MIN_GAME_TIME + 1);
if (isLastTurn) {
const timeToEnd = await gameContract.getGameState(gameId).then(state => state.minGameTime);
await time.increase(timeToEnd.toNumber() + 1);
}
await gameContract.connect(gameMaster.wallet).endTurn(
gameId,
turn == 1 ? [] : votes?.map(vote => vote.vote),
Expand Down Expand Up @@ -125,25 +129,6 @@ const endTurn = async (gameId: BigNumberish, gameContract: RankifyDiamondInstanc
);
};

const runToOvertime = async (
gameId: BigNumberish,
gameContract: RankifyDiamondInstance,
gameMaster: SignerIdentity,
players: [SignerIdentity, SignerIdentity, ...SignerIdentity[]],
) => {
await runToLastTurn(gameId, gameContract, gameMaster, players, 'equal');

await mockValidVotes(players, gameContract, gameId, gameMaster, true, 'equal');
const proposals = await mockValidProposals(players, gameContract, gameMaster, gameId, true);
const turn = await gameContract.getTurn(gameId);
await gameContract.connect(gameMaster.wallet).endTurn(
gameId,
votes.map(vote => vote.vote),
proposals.map(prop => prop.proposal),
proposals.map((prop, idx) => idx),
);
};

const mockValidVotes = async (
players: [SignerIdentity, SignerIdentity, ...SignerIdentity[]],
gameContract: RankifyDiamondInstance,
Expand Down Expand Up @@ -175,7 +160,7 @@ const mockValidVotes = async (
const startGame = async (gameId: BigNumberish) => {
const currentT = await time.latest();
await time.setNextBlockTimestamp(currentT + Number(RInstanceSettings.RInstance_TIME_TO_JOIN) + 1);
await mineBlocks(RInstanceSettings.RInstance_TIME_TO_JOIN + 1);
await mineBlocks(RInstanceSettings.RInstance_TIME_TO_JOIN + 1, hre);
await rankifyInstance.connect(adr.gameMaster1.wallet).startGame(gameId);
};

Expand Down Expand Up @@ -226,7 +211,7 @@ const fillParty = async (
if (shiftTime) {
const currentT = await time.latest();
await time.setNextBlockTimestamp(currentT + Number(RInstanceSettings.RInstance_TIME_TO_JOIN) + 1);
await mineBlocks(1);
await mineBlocks(1, hre);
}
if (startGame && gameMaster) {
await rankifyInstance.connect(gameMaster.wallet).startGame(gameId);
Expand Down Expand Up @@ -481,7 +466,7 @@ describe(scriptName, () => {
gamePrice = commonParams.principalCost.mul(principalTimeConstant).div(minGameTime);

// Create the game
await createGame(rankifyInstance, adr.gameCreator1, adr.gameMaster1.wallet.address, 1);
await createGame(minGameTime, rankifyInstance, adr.gameCreator1, adr.gameMaster1.wallet.address, 1);
});

it('Should handle game creation costs and token distribution correctly', async () => {
Expand Down Expand Up @@ -511,7 +496,7 @@ describe(scriptName, () => {
it('can get game state', async () => {
const state = await rankifyInstance.getGameState(1);
expect(state.rank).to.be.equal(1);
expect(state.minGameTime).to.be.equal(RInstanceSettings.RInstance_MIN_GAME_TIME);
expect(state.minGameTime).to.be.equal(RInstanceSettings.PRINCIPAL_TIME_CONSTANT);
expect(state.createdBy).to.be.equal(adr.gameCreator1.wallet.address);
expect(state.numOngoingProposals).to.be.equal(0);
expect(state.numPrevProposals).to.be.equal(0);
Expand Down Expand Up @@ -759,7 +744,7 @@ describe(scriptName, () => {
).to.be.revertedWith('Game has not yet started');
});
it('Cannot be started if not enough players', async () => {
await mineBlocks(RInstanceSettings.RInstance_TIME_TO_JOIN + 1);
await mineBlocks(RInstanceSettings.RInstance_TIME_TO_JOIN + 1, hre);
await expect(rankifyInstance.connect(adr.gameMaster1.wallet).startGame(1)).to.be.revertedWith(
'startGame->Not enough players',
);
Expand All @@ -780,7 +765,7 @@ describe(scriptName, () => {
);
const currentT = await time.latest();
await time.setNextBlockTimestamp(currentT + Number(RInstanceSettings.RInstance_TIME_TO_JOIN) + 1);
await mineBlocks(1);
await mineBlocks(1, hre);
await expect(rankifyInstance.connect(adr.gameMaster1.wallet).startGame(1)).to.be.emit(
rankifyInstance,
'GameStarted',
Expand Down Expand Up @@ -905,11 +890,11 @@ describe(scriptName, () => {
await expect(endTurn(1, rankifyInstance)).to.be.revertedWith(
'Game duration less than minimum required time',
);
await time.increase(RInstanceSettings.RInstance_MIN_GAME_TIME - 100);
await time.setNextBlockTimestamp((await time.latest()) + RInstanceSettings.RInstance_MIN_GAME_TIME - 100);
await expect(endTurn(1, rankifyInstance)).to.be.revertedWith(
'Game duration less than minimum required time',
);
await time.increase(101);
await time.increase(await rankifyInstance.getGameState(1).then(state => state.minGameTime));
await expect(endTurn(1, rankifyInstance)).to.not.be.reverted;
});
it('Accepts only proposals and no votes', async () => {
Expand Down Expand Up @@ -948,7 +933,7 @@ describe(scriptName, () => {
).to.be.revertedWith('Only game master');
});
it('Can end turn if timeout reached with zero scores', async () => {
await mineBlocks(RInstanceSettings.RInstance_TIME_PER_TURN + 1);
await mineBlocks(RInstanceSettings.RInstance_TIME_PER_TURN + 1, hre);
await expect(rankifyInstance.connect(adr.gameMaster1.wallet).endTurn(1, [], [], []))
.to.be.emit(rankifyInstance, 'TurnEnded')
.withArgs(
Expand Down Expand Up @@ -1188,7 +1173,14 @@ describe(scriptName, () => {
});
describe('When another game of first rank is created', () => {
beforeEach(async () => {
await createGame(rankifyInstance, adr.gameCreator1, adr.gameMaster2.wallet.address, 1, true);
await createGame(
RInstanceSettings.RInstance_MIN_GAME_TIME,
rankifyInstance,
adr.gameCreator1,
adr.gameMaster2.wallet.address,
1,
true,
);
});
it('Reverts if players from another game tries to join', async () => {
const s1 = await signJoiningGame(
Expand Down Expand Up @@ -1300,7 +1292,8 @@ describe(scriptName, () => {
1,
true,
);
await time.increase(Number(RInstanceSettings.RInstance_MIN_GAME_TIME) + 1);
const timeToEnd = await rankifyInstance.getGameState(1).then(state => state.minGameTime);
await time.increase(timeToEnd.toNumber() + 1);
expect(
await rankifyInstance.connect(adr.gameMaster1.wallet).endTurn(
1,
Expand Down Expand Up @@ -1577,7 +1570,14 @@ describe(scriptName, () => {
describe('When there was multiple first rank games played so higher rank game can be filled', () => {
beforeEach(async () => {
for (let numGames = 0; numGames < RInstanceSettings.RInstance_MAX_PLAYERS; numGames++) {
const gameId = await createGame(rankifyInstance, adr.gameCreator1, adr.gameMaster1.wallet.address, 1, true);
const gameId = await createGame(
RInstanceSettings.RInstance_MIN_GAME_TIME,
rankifyInstance,
adr.gameCreator1,
adr.gameMaster1.wallet.address,
1,
true,
);
await fillParty(
getPlayers(adr, RInstanceSettings.RInstance_MIN_PLAYERS, numGames),
rankifyInstance,
Expand Down Expand Up @@ -1610,7 +1610,14 @@ describe(scriptName, () => {
});
describe('When game of next rank is created', () => {
beforeEach(async () => {
await createGame(rankifyInstance, adr.player1, adr.gameMaster1.wallet.address, 2, true);
await createGame(
RInstanceSettings.RInstance_MIN_GAME_TIME,
rankifyInstance,
adr.player1,
adr.gameMaster1.wallet.address,
2,
true,
);
});
it('Can be joined only by bearers of rank token', async () => {
const lastCreatedGameId = await rankifyInstance.getContractState().then(r => r.numGames);
Expand Down Expand Up @@ -1791,7 +1798,7 @@ describe(scriptName, () => {
minPlayerCnt: RInstance_MIN_PLAYERS,
voteCredits: RInstanceSettings.RInstance_VOTE_CREDITS,
nTurns: 2,
minGameTime: 3600,
minGameTime: RInstanceSettings.RInstance_MIN_GAME_TIME,
timePerTurn: RInstanceSettings.RInstance_TIME_PER_TURN,
timeToJoin: RInstanceSettings.RInstance_TIME_TO_JOIN,
};
Expand Down
Loading

0 comments on commit 222fce3

Please sign in to comment.