Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Oct 17, 2024
1 parent 0ec2e46 commit 2898981
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions packages/zevm-app-contracts/test/xp-nft/zeta-xp-gov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const HARDHAT_CHAIN_ID = 1337;

const encodeTag = (tag: string) => ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["string"], [tag]));

enum VoteType {
AGAINST = 0,
FOR = 1,
ABSTAIN = 2,
}

describe("ZetaXPGov", () => {
let zetaGov: ZetaXPGov,
zetaXP: ZetaXP_V2,
Expand Down Expand Up @@ -118,7 +124,7 @@ describe("ZetaXPGov", () => {

it("Should be able to vote and meet quorum", async () => {
const user1 = addrs[0];
const user2 = addrs[1]; // Añadimos un segundo usuario para alcanzar el quórum
const user2 = addrs[1];

// Mint NFTs to both users
const nftId1 = await mintNFTToUser(user1);
Expand All @@ -142,8 +148,8 @@ describe("ZetaXPGov", () => {
await ethers.provider.send("evm_mine", []); // Mine the next block

// Both users vote for the proposal using their NFTs
await zetaGov.connect(user1).castVote(proposalId, 1); // Assuming 1 is a vote in favor
await zetaGov.connect(user2).castVote(proposalId, 1); // Second user votes in favor
await zetaGov.connect(user1).castVote(proposalId, VoteType.FOR);
await zetaGov.connect(user2).castVote(proposalId, VoteType.FOR);

// Optionally, increase the block number to simulate time passing and end the voting period
await ethers.provider.send("evm_increaseTime", [50400]); // Fast forward 1 week to end the voting period
Expand Down Expand Up @@ -171,4 +177,65 @@ describe("ZetaXPGov", () => {
expect(votes.againstVotes).to.equal(0);
expect(votes.forVotes).to.equal(6);
});

it("Should be able to vote acording to voting power", async () => {
const user1 = addrs[0];
const user2 = addrs[1];
const user3 = addrs[2];

// Mint NFTs to both users
const nftId1 = await mintNFTToUser(user1);
await setLevelToNFT(nftId1, 3);

const nftId2 = await mintNFTToUser(user2);
await setLevelToNFT(nftId2, 2);

const nftId3 = await mintNFTToUser(user3);
await setLevelToNFT(nftId3, 1);

// Create a proposal to vote on
const targets = ["0x0000000000000000000000000000000000000000"];
const values = [0];
const calldatas = ["0x"];
const description = "Proposal #1";

const proposeTx = await zetaGov.connect(signer).propose(targets, values, calldatas, description);
const proposeReceipt = await proposeTx.wait();
const proposalId = proposeReceipt.events?.find((e) => e.event === "ProposalCreated")?.args?.proposalId;

// Increase the time and mine blocks to move to the voting phase
await ethers.provider.send("evm_increaseTime", [7200]); // Fast forward 2 hours to ensure voting delay is over
await ethers.provider.send("evm_mine", []); // Mine the next block

// Both users vote for the proposal using their NFTs
await zetaGov.connect(user1).castVote(proposalId, VoteType.FOR);
await zetaGov.connect(user2).castVote(proposalId, VoteType.ABSTAIN);
await zetaGov.connect(user3).castVote(proposalId, VoteType.AGAINST);

// Optionally, increase the block number to simulate time passing and end the voting period
await ethers.provider.send("evm_increaseTime", [50400]); // Fast forward 1 week to end the voting period
await ethers.provider.send("evm_mine", []); // Mine the next block

// Queue the proposal after voting period is over
const descriptionHash = ethers.utils.id(description);
await zetaGov.connect(signer).queue(targets, values, calldatas, descriptionHash);

// Increase time to meet the timelock delay
await ethers.provider.send("evm_increaseTime", [3600]); // Fast forward 1 hour to meet timelock delay
await ethers.provider.send("evm_mine", []); // Mine the next block

// Execute the proposal after the timelock delay has passed
const executeTx = await zetaGov.connect(signer).execute(targets, values, calldatas, descriptionHash);
await executeTx.wait();

// Assertions
const proposalState = await zetaGov.state(proposalId);
expect(proposalState).to.equal(7); // Assuming 7 means 'executed'

// Get the proposal votes after the voting period
const votes = await zetaGov.proposalVotes(proposalId);
expect(votes.abstainVotes).to.equal(2);
expect(votes.againstVotes).to.equal(1);
expect(votes.forVotes).to.equal(3);
});
});

0 comments on commit 2898981

Please sign in to comment.