2021-08-04 15:47:10 +02:00
|
|
|
import { BigNumber } from '@ethersproject/bignumber'
|
2023-03-15 16:02:48 +01:00
|
|
|
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
|
|
|
|
import { Contract } from 'ethers'
|
|
|
|
import { ERC20Mock } from '../abi'
|
2021-06-17 13:33:35 +02:00
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
import { time, loadFixture } from '@nomicfoundation/hardhat-network-helpers'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import { ethers } from 'hardhat'
|
|
|
|
|
|
|
|
const { utils } = ethers
|
2021-06-17 13:33:35 +02:00
|
|
|
|
2021-08-26 18:57:05 +02:00
|
|
|
const publicKeys = [
|
|
|
|
'0x0d9cb350e1dc415303e2816a21b0a439530725b4b2b42d2948e967cb211eab89d5',
|
|
|
|
'0xe84e64498172551d998a220e1d8e5893c818ee9aa90bdb855aec0c9e65e89014b8',
|
|
|
|
]
|
|
|
|
|
2021-08-27 14:53:14 +02:00
|
|
|
const typedData = {
|
|
|
|
types: {
|
|
|
|
Vote: [
|
|
|
|
{ name: 'roomIdAndType', type: 'uint256' },
|
|
|
|
{ name: 'sntAmount', type: 'uint256' },
|
|
|
|
{ name: 'voter', type: 'address' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
domain: {
|
|
|
|
name: 'Voting Contract',
|
|
|
|
version: '1',
|
|
|
|
chainId: 0,
|
|
|
|
verifyingContract: '',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
const getSignedVotes = async (
|
|
|
|
firstSigner: SignerWithAddress,
|
|
|
|
secondSigner: SignerWithAddress,
|
|
|
|
thirdSigner: SignerWithAddress
|
|
|
|
): Promise<{ voter: string; roomIdAndType: BigNumber; sntAmount: BigNumber; r: string; vs: string }[]> => {
|
2021-07-06 13:59:28 +02:00
|
|
|
const votes = [
|
|
|
|
{
|
2023-03-15 16:02:48 +01:00
|
|
|
voter: firstSigner,
|
2021-07-06 13:59:28 +02:00
|
|
|
vote: 1,
|
|
|
|
sntAmount: BigNumber.from(100),
|
|
|
|
sessionID: 1,
|
|
|
|
},
|
|
|
|
{
|
2023-03-15 16:02:48 +01:00
|
|
|
voter: secondSigner,
|
2021-07-06 13:59:28 +02:00
|
|
|
vote: 0,
|
|
|
|
sntAmount: BigNumber.from(100),
|
|
|
|
sessionID: 1,
|
|
|
|
},
|
|
|
|
{
|
2023-03-15 16:02:48 +01:00
|
|
|
voter: thirdSigner,
|
2021-07-06 13:59:28 +02:00
|
|
|
vote: 1,
|
|
|
|
sntAmount: BigNumber.from(100),
|
|
|
|
sessionID: 1,
|
|
|
|
},
|
|
|
|
]
|
2021-08-27 14:53:14 +02:00
|
|
|
const messages: [string, BigNumber, BigNumber][] = votes.map((vote) => [
|
2021-07-06 13:59:28 +02:00
|
|
|
vote.voter.address,
|
|
|
|
BigNumber.from(vote.sessionID).mul(2).add(vote.vote),
|
|
|
|
vote.sntAmount,
|
|
|
|
])
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
const signedMessages = await Promise.all(
|
|
|
|
messages.map(async (msg, idx) => {
|
|
|
|
const message = { roomIdAndType: msg[1].toHexString(), sntAmount: msg[2].toHexString(), voter: msg[0] }
|
|
|
|
const signature = await votes[idx].voter._signTypedData(typedData.domain, typedData.types, message)
|
|
|
|
const sig = utils.splitSignature(signature)
|
|
|
|
return { voter: msg[0], roomIdAndType: msg[1], sntAmount: msg[2], r: sig.r, vs: sig._vs }
|
|
|
|
})
|
|
|
|
)
|
2021-07-06 13:59:28 +02:00
|
|
|
|
2021-08-27 14:53:14 +02:00
|
|
|
return signedMessages
|
2021-07-06 13:59:28 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
const voteAndFinalize = async (room: number, type: number, signer: SignerWithAddress, contract: Contract) => {
|
2021-08-27 14:53:14 +02:00
|
|
|
const vote: [string, BigNumber, BigNumber] = [
|
|
|
|
signer.address,
|
|
|
|
BigNumber.from(room).mul(2).add(type),
|
|
|
|
BigNumber.from(100),
|
|
|
|
]
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
const message = { roomIdAndType: vote[1].toHexString(), sntAmount: vote[2].toHexString(), voter: vote[0] }
|
|
|
|
const signature = await signer._signTypedData(typedData.domain, typedData.types, message)
|
|
|
|
const sig = utils.splitSignature(signature)
|
2021-08-27 14:53:14 +02:00
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
await contract.castVotes([{ voter: vote[0], roomIdAndType: vote[1], sntAmount: vote[2], r: sig.r, vs: sig._vs }])
|
|
|
|
await time.increase(10000)
|
2021-07-12 15:17:32 +02:00
|
|
|
await contract.finalizeVotingRoom(room)
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
async function fixture() {
|
|
|
|
const [firstSigner, secondSigner, thirdSigner] = await ethers.getSigners()
|
|
|
|
|
|
|
|
const Erc20ContractFactory = await ethers.getContractFactory(ERC20Mock.abi, ERC20Mock.bytecode)
|
|
|
|
const erc20Contract = await Erc20ContractFactory.deploy('MSNT', 'Mock SNT', firstSigner.address, 100000)
|
|
|
|
|
|
|
|
await erc20Contract.transfer(secondSigner.address, 10000)
|
|
|
|
await erc20Contract.transfer(thirdSigner.address, 10000)
|
|
|
|
|
|
|
|
const votingContractFactory = await ethers.getContractFactory('VotingContract')
|
|
|
|
const votingContract = await votingContractFactory.deploy(erc20Contract.address)
|
|
|
|
|
|
|
|
const directoryContractFactory = await ethers.getContractFactory('Directory')
|
|
|
|
const directoryContract = await directoryContractFactory.deploy(votingContract.address)
|
|
|
|
|
|
|
|
await votingContract.setDirectory(directoryContract.address)
|
|
|
|
|
|
|
|
return { votingContract, directoryContract, firstSigner, secondSigner, thirdSigner }
|
2021-08-26 18:57:05 +02:00
|
|
|
}
|
2021-07-30 12:28:45 +02:00
|
|
|
|
2021-09-02 11:25:42 +02:00
|
|
|
before(async function () {
|
2023-03-15 16:02:48 +01:00
|
|
|
// this.timeout(10000)
|
|
|
|
const { votingContract: voting } = await loadFixture(fixture)
|
|
|
|
typedData.domain.chainId = 31337
|
|
|
|
typedData.domain.verifyingContract = voting.address
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('voting', () => {
|
|
|
|
it('check voters', async () => {
|
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
|
|
|
|
const messages = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
|
|
|
|
expect(await votingContract.listRoomVoters(1)).to.deep.eq([firstSigner.address])
|
|
|
|
await votingContract.castVotes(messages.slice(2))
|
|
|
|
expect(await votingContract.listRoomVoters(1)).to.deep.eq([firstSigner.address, thirdSigner.address])
|
|
|
|
})
|
2021-08-31 15:46:55 +02:00
|
|
|
})
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
describe('VotingContract', () => {
|
2021-07-12 15:17:32 +02:00
|
|
|
it('deploys properly', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, directoryContract } = await loadFixture(fixture)
|
|
|
|
await expect(await votingContract.directory()).to.eq(directoryContract.address)
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('only owner can change directory', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, secondSigner } = await loadFixture(fixture)
|
|
|
|
const differentSender = votingContract.connect(secondSigner)
|
|
|
|
await expect(differentSender.setDirectory(secondSigner.address)).to.be.revertedWith('Not owner')
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
|
2021-06-23 23:33:25 +02:00
|
|
|
describe('Voting Room', () => {
|
2021-07-12 15:17:32 +02:00
|
|
|
describe('initialization', () => {
|
|
|
|
it('initializes', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract } = await loadFixture(fixture)
|
|
|
|
await expect(await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100)))
|
|
|
|
.to.emit(votingContract, 'VotingRoomStarted')
|
2021-08-26 18:57:05 +02:00
|
|
|
.withArgs(1, publicKeys[0])
|
2023-03-15 16:02:48 +01:00
|
|
|
await expect(await votingContract.initializeVotingRoom(1, publicKeys[1], BigNumber.from(100)))
|
|
|
|
.to.emit(votingContract, 'VotingRoomStarted')
|
2021-08-26 18:57:05 +02:00
|
|
|
.withArgs(2, publicKeys[1])
|
2023-03-15 16:02:48 +01:00
|
|
|
await expect(votingContract.initializeVotingRoom(1, publicKeys[1], BigNumber.from(100))).to.be.revertedWith(
|
2021-07-14 12:18:00 +02:00
|
|
|
'vote already ongoing'
|
|
|
|
)
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
|
2021-07-30 12:28:45 +02:00
|
|
|
it('not enough token', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract } = await loadFixture(fixture)
|
2021-07-30 12:28:45 +02:00
|
|
|
await expect(
|
2023-03-15 16:02:48 +01:00
|
|
|
votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(10000000000000))
|
2021-07-30 12:28:45 +02:00
|
|
|
).to.be.revertedWith('not enough token')
|
|
|
|
})
|
|
|
|
|
2021-07-12 15:17:32 +02:00
|
|
|
describe('directory interaction', () => {
|
|
|
|
it('remove missing', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract } = await loadFixture(fixture)
|
|
|
|
await expect(votingContract.initializeVotingRoom(0, publicKeys[0], BigNumber.from(100))).to.be.revertedWith(
|
2021-08-26 18:57:05 +02:00
|
|
|
'Community not in directory'
|
|
|
|
)
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('add already in', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, directoryContract, firstSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 1, firstSigner, votingContract)
|
2021-07-12 15:17:32 +02:00
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([publicKeys[0]])
|
|
|
|
await expect(votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))).to.be.revertedWith(
|
2021-08-26 18:57:05 +02:00
|
|
|
'Community already in directory'
|
|
|
|
)
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
})
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('gets', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
2023-03-16 20:54:10 +01:00
|
|
|
const votingRoom1 = await votingContract.votingRooms(0)
|
2021-09-02 10:23:47 +02:00
|
|
|
expect(votingRoom1.slice(2)).to.deep.eq([
|
2021-06-23 23:33:25 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-06-23 23:33:25 +02:00
|
|
|
BigNumber.from(0),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(1),
|
2021-06-23 23:33:25 +02:00
|
|
|
])
|
2023-03-16 21:31:09 +01:00
|
|
|
const history = await votingContract.getVotingHistory(publicKeys[0])
|
2021-09-02 10:23:47 +02:00
|
|
|
expect(history.length).to.eq(1)
|
|
|
|
expect(history[0][2]).to.eq(1)
|
|
|
|
expect(history[0][4]).to.eq(publicKeys[0])
|
2021-06-23 23:33:25 +02:00
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[1], BigNumber.from(100))
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(1)).slice(2)).to.deep.eq([
|
2021-06-23 23:33:25 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[1],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-06-23 23:33:25 +02:00
|
|
|
BigNumber.from(0),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(2),
|
2021-06-23 23:33:25 +02:00
|
|
|
])
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-06-23 23:33:25 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-06-23 23:33:25 +02:00
|
|
|
BigNumber.from(0),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(1),
|
2021-06-23 23:33:25 +02:00
|
|
|
])
|
|
|
|
})
|
2021-09-02 10:23:47 +02:00
|
|
|
|
|
|
|
describe('history', () => {
|
|
|
|
it('saves to history', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 1, firstSigner, votingContract)
|
2023-03-16 21:31:09 +01:00
|
|
|
expect((await votingContract.getVotingHistory(publicKeys[0])).length).to.eq(1)
|
2023-03-15 16:02:48 +01:00
|
|
|
await time.increase(10000)
|
|
|
|
await votingContract.initializeVotingRoom(0, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(2, 0, firstSigner, votingContract)
|
2023-03-16 21:31:09 +01:00
|
|
|
const history = await votingContract.getVotingHistory(publicKeys[0])
|
2021-09-02 10:23:47 +02:00
|
|
|
expect(history.length).to.eq(2)
|
|
|
|
expect(history[0][2]).to.eq(1)
|
|
|
|
expect(history[0][3]).to.eq(true)
|
|
|
|
expect(history[0][4]).to.eq(publicKeys[0])
|
|
|
|
expect(history[0][5]).to.eq(BigNumber.from(100))
|
|
|
|
expect(history[0][6]).to.eq(BigNumber.from(0))
|
|
|
|
expect(history[0][7]).to.eq(BigNumber.from(1))
|
|
|
|
|
|
|
|
expect(history[1][2]).to.eq(0)
|
|
|
|
expect(history[1][3]).to.eq(true)
|
|
|
|
expect(history[1][4]).to.eq(publicKeys[0])
|
|
|
|
expect(history[1][5]).to.eq(BigNumber.from(100))
|
|
|
|
expect(history[1][6]).to.eq(BigNumber.from(0))
|
|
|
|
expect(history[1][7]).to.eq(BigNumber.from(2))
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can't start vote to fast", async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 1, firstSigner, votingContract)
|
|
|
|
await expect(votingContract.initializeVotingRoom(0, publicKeys[0], BigNumber.from(100))).to.be.revertedWith(
|
2021-09-02 10:23:47 +02:00
|
|
|
'Community was in a vote recently'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-12 15:17:32 +02:00
|
|
|
describe('finalization', () => {
|
|
|
|
it('finalizes', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-07-12 15:17:32 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-07-12 15:17:32 +02:00
|
|
|
BigNumber.from(0),
|
|
|
|
BigNumber.from(1),
|
|
|
|
])
|
2023-03-15 16:02:48 +01:00
|
|
|
await time.increase(2000)
|
|
|
|
await expect(await votingContract.finalizeVotingRoom(1))
|
|
|
|
.to.emit(votingContract, 'VotingRoomFinalized')
|
2021-08-26 18:57:05 +02:00
|
|
|
.withArgs(1, publicKeys[0], true, 1)
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-07-12 15:17:32 +02:00
|
|
|
1,
|
|
|
|
true,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-07-12 15:17:32 +02:00
|
|
|
BigNumber.from(0),
|
|
|
|
BigNumber.from(1),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('directory interaction', () => {
|
|
|
|
it('add community', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, directoryContract, secondSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 1, secondSigner, votingContract)
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-07-12 15:17:32 +02:00
|
|
|
1,
|
|
|
|
true,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(200),
|
2021-07-12 15:17:32 +02:00
|
|
|
BigNumber.from(0),
|
|
|
|
BigNumber.from(1),
|
|
|
|
])
|
2023-03-15 16:02:48 +01:00
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([publicKeys[0]])
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
2021-06-23 23:33:25 +02:00
|
|
|
|
2021-07-12 15:17:32 +02:00
|
|
|
it('remove community', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, directoryContract, secondSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 1, secondSigner, votingContract)
|
|
|
|
|
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([publicKeys[0]])
|
|
|
|
await time.increase(10000)
|
|
|
|
await votingContract.initializeVotingRoom(0, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(2, 1, secondSigner, votingContract)
|
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([])
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('failed add vote', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, directoryContract, secondSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 0, secondSigner, votingContract)
|
2021-07-12 15:17:32 +02:00
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([])
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('failed remove vote', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, directoryContract, secondSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(1, 1, secondSigner, votingContract)
|
|
|
|
|
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([publicKeys[0]])
|
|
|
|
await time.increase(10000)
|
|
|
|
await votingContract.initializeVotingRoom(0, publicKeys[0], BigNumber.from(100))
|
|
|
|
await voteAndFinalize(2, 0, secondSigner, votingContract)
|
|
|
|
expect(await directoryContract.getCommunities()).to.deep.eq([publicKeys[0]])
|
2021-07-12 15:17:32 +02:00
|
|
|
})
|
|
|
|
})
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('helpers', () => {
|
2023-03-16 21:31:09 +01:00
|
|
|
it('getActiveVotingRoom', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
2023-03-16 21:31:09 +01:00
|
|
|
expect((await votingContract.getActiveVotingRoom(publicKeys[0])).slice(2)).to.deep.eq([
|
2021-07-06 14:55:13 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(0),
|
|
|
|
BigNumber.from(1),
|
2023-03-15 16:02:48 +01:00
|
|
|
[firstSigner.address],
|
2021-07-06 14:55:13 +02:00
|
|
|
])
|
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
await time.increase(10000)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[1], BigNumber.from(100))
|
2023-03-16 21:31:09 +01:00
|
|
|
expect((await votingContract.getActiveVotingRoom(publicKeys[1])).slice(2)).to.deep.eq([
|
2021-07-06 14:55:13 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[1],
|
2021-07-14 12:18:00 +02:00
|
|
|
BigNumber.from(100),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(0),
|
|
|
|
BigNumber.from(2),
|
2023-03-15 16:02:48 +01:00
|
|
|
[firstSigner.address],
|
2021-07-06 14:55:13 +02:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2021-06-23 23:33:25 +02:00
|
|
|
it('get active votes', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
expect(await votingContract.getActiveVotingRooms()).to.deep.eq([BigNumber.from(1)])
|
|
|
|
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[1], BigNumber.from(100))
|
|
|
|
expect(await votingContract.getActiveVotingRooms()).to.deep.eq([BigNumber.from(1), BigNumber.from(2)])
|
|
|
|
await time.increase(2000)
|
|
|
|
await votingContract.finalizeVotingRoom(1)
|
|
|
|
expect(await votingContract.getActiveVotingRooms()).to.deep.eq([BigNumber.from(2)])
|
|
|
|
await votingContract.finalizeVotingRoom(2)
|
|
|
|
expect(await votingContract.getActiveVotingRooms()).to.deep.eq([])
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('voting', () => {
|
|
|
|
it('check voters', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
const votes = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
2021-06-23 23:33:25 +02:00
|
|
|
|
2023-03-15 16:02:48 +01:00
|
|
|
expect(await votingContract.listRoomVoters(1)).to.deep.eq([firstSigner.address])
|
|
|
|
await votingContract.castVotes(votes.slice(2))
|
|
|
|
expect(await votingContract.listRoomVoters(1)).to.deep.eq([firstSigner.address, thirdSigner.address])
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
|
|
|
|
2021-07-30 12:28:45 +02:00
|
|
|
it('not enough tokens', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, secondSigner } = await loadFixture(fixture)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
|
|
|
|
const vote: [string, BigNumber, BigNumber] = [
|
|
|
|
secondSigner.address,
|
|
|
|
BigNumber.from(1).mul(2).add(1),
|
|
|
|
BigNumber.from(100000000000),
|
|
|
|
]
|
|
|
|
const message = { roomIdAndType: vote[1].toHexString(), sntAmount: vote[2].toHexString(), voter: vote[0] }
|
|
|
|
const signature = await secondSigner._signTypedData(typedData.domain, typedData.types, message)
|
|
|
|
const sig = utils.splitSignature(signature)
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
await votingContract.castVotes([
|
|
|
|
{ voter: vote[0], roomIdAndType: vote[1], sntAmount: vote[2], r: sig.r, vs: sig._vs },
|
|
|
|
])
|
2021-07-30 12:28:45 +02:00
|
|
|
)
|
2023-03-15 16:02:48 +01:00
|
|
|
.to.emit(votingContract, 'NotEnoughToken')
|
|
|
|
.withArgs(1, secondSigner.address)
|
2021-07-30 12:28:45 +02:00
|
|
|
|
2023-03-16 20:54:10 +01:00
|
|
|
await expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-07-30 12:28:45 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-07-30 12:28:45 +02:00
|
|
|
BigNumber.from(100),
|
|
|
|
BigNumber.from(0),
|
|
|
|
BigNumber.from(1),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2021-06-23 23:33:25 +02:00
|
|
|
it('success', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
const votes = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await votingContract.castVotes(votes)
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-06-23 23:33:25 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-06-23 23:33:25 +02:00
|
|
|
BigNumber.from(200),
|
|
|
|
BigNumber.from(100),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(1),
|
2021-06-23 23:33:25 +02:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('double vote', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
const votes = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await votingContract.castVotes(votes)
|
|
|
|
await votingContract.castVotes(votes)
|
2023-03-16 20:54:10 +01:00
|
|
|
expect((await votingContract.votingRooms(0)).slice(2)).to.deep.eq([
|
2021-06-23 23:33:25 +02:00
|
|
|
1,
|
|
|
|
false,
|
2021-08-26 18:57:05 +02:00
|
|
|
publicKeys[0],
|
2021-06-23 23:33:25 +02:00
|
|
|
BigNumber.from(200),
|
|
|
|
BigNumber.from(100),
|
2021-07-06 14:55:13 +02:00
|
|
|
BigNumber.from(1),
|
2021-06-23 23:33:25 +02:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('none existent room', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
const votes = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await expect(votingContract.castVotes(votes)).to.be.reverted
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('old room', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
const votes = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await time.increase(2000)
|
|
|
|
await expect(votingContract.castVotes(votes)).to.be.reverted
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('wrong signature', async () => {
|
2023-03-15 16:02:48 +01:00
|
|
|
const { votingContract, firstSigner, secondSigner, thirdSigner } = await loadFixture(fixture)
|
|
|
|
const signedVotes = await getSignedVotes(firstSigner, secondSigner, thirdSigner)
|
|
|
|
await votingContract.initializeVotingRoom(1, publicKeys[0], BigNumber.from(100))
|
|
|
|
await time.increase(2000)
|
|
|
|
|
|
|
|
const wronglySignedMessages = signedVotes.map((msg) => {
|
|
|
|
return {
|
|
|
|
...msg,
|
|
|
|
r: '0x2d63286985277c440b9f01a987fbbc9bc9ca32cb4e9e55ee3ffcab4e67c211e6',
|
|
|
|
vs: '0x2d63286985277c440b9f01a987fbbc9bc9ca32cb4e9e55ee3ffcab4e67c211e6',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await votingContract.castVotes(wronglySignedMessages)
|
|
|
|
await expect(await votingContract.listRoomVoters(1)).to.deep.eq([firstSigner.address])
|
2021-06-23 23:33:25 +02:00
|
|
|
})
|
2021-06-17 13:33:35 +02:00
|
|
|
})
|
|
|
|
})
|