From 8575e7de55fb77e802b0e08f153726796406b206 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Tue, 14 Sep 2021 09:04:39 +0200 Subject: [PATCH] Stop voting with 0 tokens (#65) --- .../contracts/contracts/VotingContract.sol | 1 + .../contracts/test/1.votingContract.test.ts | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/contracts/contracts/VotingContract.sol b/packages/contracts/contracts/VotingContract.sol index 2884d4c..cee70da 100644 --- a/packages/contracts/contracts/VotingContract.sol +++ b/packages/contracts/contracts/VotingContract.sol @@ -79,6 +79,7 @@ contract VotingContract { string calldata description, uint256 voteAmount ) public { + require(voteAmount > 0, 'token amount must not be 0'); require(token.balanceOf(msg.sender) >= voteAmount, 'not enough token'); VotingRoom memory newVotingRoom; newVotingRoom.startBlock = block.number; diff --git a/packages/contracts/test/1.votingContract.test.ts b/packages/contracts/test/1.votingContract.test.ts index af73b6a..019540e 100644 --- a/packages/contracts/test/1.votingContract.test.ts +++ b/packages/contracts/test/1.votingContract.test.ts @@ -81,12 +81,12 @@ const getSignedMessages = async ( return { messages, signedMessages } } -async function fixture([alice, firstAddress, secondAddress]: any[], provider: JsonRpcProvider) { +async function fixture([alice, firstAddress, secondAddress, noTokensAddress]: any[], provider: JsonRpcProvider) { const erc20 = await deployContract(alice, ERC20Mock, ['MSNT', 'Mock SNT', alice.address, 100000]) await erc20.transfer(firstAddress.address, 10000) await erc20.transfer(secondAddress.address, 10000) const contract = await deployContract(alice, VotingContract, [erc20.address, 1000]) - return { contract, alice, firstAddress, secondAddress, provider } + return { contract, alice, firstAddress, secondAddress, provider, noTokensAddress } } before(async function () { @@ -116,6 +116,22 @@ describe('Contract', () => { contract.initializeVotingRoom('test', 'short desc', BigNumber.from(10000000000000)) ).to.be.revertedWith('not enough token') }) + + it('no tokens address', async () => { + const { contract, noTokensAddress } = await loadFixture(fixture) + const noTokensContract = contract.connect(noTokensAddress) + await expect( + noTokensContract.initializeVotingRoom('test', 'short desc', BigNumber.from(10)) + ).to.be.revertedWith('not enough token') + }) + + it("can't start voting with 0 tokens", async () => { + const { contract, noTokensAddress } = await loadFixture(fixture) + const noTokensContract = contract.connect(noTokensAddress) + await expect(noTokensContract.initializeVotingRoom('test', 'short desc', BigNumber.from(0))).to.be.revertedWith( + 'token amount must not be 0' + ) + }) }) it('gets', async () => {