Added quadratic voting
This commit is contained in:
parent
2979fbcc2e
commit
f7985fb57c
|
@ -27,7 +27,10 @@ contract SingleChoice is Controlled {
|
|||
|
||||
string public question;
|
||||
string[] public choices;
|
||||
|
||||
int[] public result;
|
||||
int[] public qvResult;
|
||||
|
||||
bytes32 uid;
|
||||
|
||||
function SingleChoice(address _controller, bytes _rlpDefinition, uint salt) {
|
||||
|
@ -55,6 +58,7 @@ contract SingleChoice is Controlled {
|
|||
}
|
||||
|
||||
result.length = choices.length;
|
||||
qvResult.length = choices.length;
|
||||
}
|
||||
|
||||
function pollType() public constant returns (bytes32) {
|
||||
|
@ -71,7 +75,18 @@ contract SingleChoice is Controlled {
|
|||
function deltaVote(int _amount, bytes32 _ballot) onlyController returns (bool _succes) {
|
||||
if (!isValid(_ballot)) return false;
|
||||
uint v = uint(_ballot) / (2**248);
|
||||
|
||||
result[v] += _amount;
|
||||
|
||||
int qv;
|
||||
if (_amount < 0) {
|
||||
qv = -sqrt(-_amount);
|
||||
} else {
|
||||
qv = sqrt(_amount);
|
||||
}
|
||||
|
||||
qvResult[v] += qv;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -82,6 +97,15 @@ contract SingleChoice is Controlled {
|
|||
function getBallot(uint _option) constant returns(bytes32) {
|
||||
return bytes32((_option * (2**248)) + (uint(keccak256(uid, _option)) & (2**248 -1)));
|
||||
}
|
||||
|
||||
function sqrt(int256 x) public pure returns (int256 y) {
|
||||
int256 z = (x + 1) / 2;
|
||||
y = x;
|
||||
while (z < y) {
|
||||
y = z;
|
||||
z = (x / z + z) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -59,13 +59,13 @@ describe("VotingDapp", function () {
|
|||
|
||||
web3.eth.getAccounts().then((acc) => {
|
||||
accounts = acc;
|
||||
return SNT.methods.generateTokens(accounts[0], 123456).send()
|
||||
return SNT.methods.generateTokens(accounts[0], 6).send()
|
||||
}).then((receipt) => {
|
||||
return SNT.methods.generateTokens(accounts[1], 789012).send()
|
||||
return SNT.methods.generateTokens(accounts[1], 12).send()
|
||||
}).then((receipt) => {
|
||||
return SNT.methods.generateTokens(accounts[2], 345678).send()
|
||||
return SNT.methods.generateTokens(accounts[2], 10).send()
|
||||
}).then((receipt) => {
|
||||
return SNT.methods.generateTokens(accounts[3], 901234).send()
|
||||
return SNT.methods.generateTokens(accounts[3], 7).send()
|
||||
}).then((receipt) => {
|
||||
done();
|
||||
});
|
||||
|
@ -148,18 +148,25 @@ describe("VotingDapp", function () {
|
|||
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 )
|
||||
let quadraticVotesByBallotYES = await pollContract.methods.qvResult(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
|
||||
// Will contain state of the poll
|
||||
// console.dir(poll);
|
||||
|
||||
// Contains how many votes has a ballot
|
||||
// console.log(tokenVotesByBallotYES);
|
||||
|
||||
// Contains how many votes has a ballot using quadratic voting
|
||||
//console.log(quadraticVotesByBallotYES);
|
||||
|
||||
// Contains how many voters voted for that option
|
||||
// console.log(votersByBallotYES);
|
||||
|
||||
// ===================================================
|
||||
// Unvote
|
||||
receipt = await PollManager.methods.unvote(pollId).send({from: accounts[0]});
|
||||
assert.equal(!!receipt.events.Unvote, true, "Unvote not triggered");
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue