contracts/test/votingdapp.js

167 lines
5.3 KiB
JavaScript
Raw Normal View History

const utils = require('../utils/testUtils')
const assert = require('assert');
const SingleChoice = require('Embark/contracts/SingleChoice');
const BN = web3.utils.BN;
var _ = require('lodash');
var rlp = require('rlp');
config({
contracts: {
"MiniMeTokenFactory": {
"gasLimit": 4000000
},
"MiniMeToken": {
"deploy": false,
},
"SNT":{
"instanceOf": "MiniMeToken",
"args": [
"$MiniMeTokenFactory",
utils.zeroAddress,
0,
"TestMiniMeToken",
18,
"TST",
true
],
"gasLimit": 4000000
},
2018-06-26 19:41:28 +00:00
"SingleChoiceFactory": {
"deploy": false
},
"PollManager": {
"deploy": true,
"args": ["$MiniMeTokenFactory", "$SNT"]
}
}
});
singleChoiceDef = (question, options) => {
var d = [
new Buffer(question),
_.map(options, function(o) {
return new Buffer(o);
})
];
var b= rlp.encode(d);
var rlpDefinition = '0x' + b.toString('hex');
return rlpDefinition;
}
describe("VotingDapp", function () {
this.timeout(0);
let accounts;
before(function(done) {
web3.eth.getAccounts().then((acc) => {
accounts = acc;
return SNT.methods.generateTokens(accounts[0], 123456).send()
}).then((receipt) => {
return SNT.methods.generateTokens(accounts[1], 789012).send()
}).then((receipt) => {
return SNT.methods.generateTokens(accounts[2], 345678).send()
}).then((receipt) => {
return SNT.methods.generateTokens(accounts[3], 901234).send()
}).then((receipt) => {
done();
});
});
it("Test", async () => {
const blockNumber = await web3.eth.getBlockNumber();
const question = singleChoiceDef("Move from Slack to Status Desktop", [ "Yes", "No" ]);
let receipt;
// ===================================================
// Creating a proposal without holding SNT SHOULD FAIL!
try {
receipt = await PollManager.methods.addPoll(
blockNumber,
blockNumber + 10,
question)
.send({from: accounts[8]});
assert.fail('should have reverted before');
} catch(error) {
utils.assertJump(error);
}
// ===================================================
// Creating a proposal as a SNT holder
receipt = await PollManager.methods.addPoll(
blockNumber,
blockNumber + 10,
question)
.send({from: accounts[0]});
assert.equal(!!receipt.events.PollCreated, true, "PollCreated not triggered");
const pollId = receipt.events.PollCreated.returnValues.idPoll;
let poll = await PollManager.methods.poll(pollId).call();
SingleChoice.options.address = poll._pollContract;
const pollContract = SingleChoice;
// Options are represented as a hex value
const Yes = await SingleChoice.methods.getBallot(0).call();
const No = await SingleChoice.methods.getBallot(1).call();
// ===================================================
// Determining if I can vote por a proposal
let canVote = await PollManager.methods.canVote(pollId).call({from: accounts[0]});
assert.equal(canVote, true, "User should be able to vote");
// ===================================================
// Voting
receipt = await PollManager.methods.vote(pollId, Yes).send({from: accounts[0]});
assert.equal(!!receipt.events.Vote, true, "Vote not triggered");
// ===================================================
// Getting what option the voter selected
let myVote = await PollManager.methods.getVote(pollId, accounts[0]).call();
assert.equal(myVote._ballot, Yes, "Vote is different from selected");
// ===================================================
// Voting when you're not a SNT holder SHOULD FAIL!
try {
receipt = await PollManager.methods.vote(pollId, Yes)
.send({from: accounts[8]});
assert.fail('should have reverted before');
} catch(error) {
utils.assertJump(error);
}
// ===================================================
// Getting proposal information
poll = await PollManager.methods.poll(pollId).call();
let votersByBallotYES = await PollManager.methods.getVotesByBallot(pollId, Yes).call();
let tokenVotesByBallotYES = await pollContract.methods.result(0).call(); // 0 == Yes (because it is the initial option )
// console.dir(poll); // Will contain state of the poll
// console.log(tokenVotesByBallotYES); // Contains how many votes has a ballot
// console.log(votersByBallotYES); // Contains how many voters voted for that option
// ===================================================
// Unvote
receipt = await PollManager.methods.unvote(pollId).send({from: accounts[0]});
assert.equal(!!receipt.events.Unvote, true, "Unvote not triggered");
});
});