Skip to content

Commit

Permalink
Use totalInfluence to determine voting rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Jan 22, 2021
1 parent 793f1ea commit e07d9ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
11 changes: 7 additions & 4 deletions contracts/extensions/VotingBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ abstract contract VotingBase is ColonyExtension, PatriciaTreeProofs {

function getInfluence(uint256 _motionId, address _user) public view virtual returns (uint256);

function getTotalInfluence(uint256 _motionId) public view virtual returns (uint256);

function postReveal(uint256 _motionId, address _user) internal virtual;

function postClaim(uint256 _motionId, address _user) internal virtual;
Expand Down Expand Up @@ -588,13 +590,14 @@ abstract contract VotingBase is ColonyExtension, PatriciaTreeProofs {

/// @notice Get the voter reward
/// @param _motionId The id of the motion
/// @param _voterRep The reputation the voter has in the domain
/// @param _voterInfluence The influence the voter has in the domain
/// @return The voter reward
function getVoterReward(uint256 _motionId, uint256 _voterRep) public view returns (uint256) {
function getVoterReward(uint256 _motionId, uint256 _voterInfluence) public view returns (uint256) {
Motion storage motion = motions[_motionId];
uint256 fractionUserReputation = wdiv(_voterRep, motion.maxVotes);
uint256 totalInfluence = getTotalInfluence(_motionId);
uint256 fractionUserInfluence = wdiv(_voterInfluence, totalInfluence);
uint256 totalStake = add(motion.stakes[YAY], motion.stakes[NAY]);
return wmul(wmul(fractionUserReputation, totalStake), voterRewardFraction);
return wmul(wmul(fractionUserInfluence, totalStake), voterRewardFraction);
}

/// @notice Get the staker reward
Expand Down
14 changes: 14 additions & 0 deletions contracts/extensions/VotingReputation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ contract VotingReputation is VotingBase {

// [rootHash][skillId][user] => reputationBalance
mapping (bytes32 => mapping (uint256 => mapping (address => uint256))) influences;
// [rootHash][skillId] => reputationBalance
mapping (bytes32 => mapping (uint256 => uint256)) totalInfluences;

// Public

Expand All @@ -53,15 +55,27 @@ contract VotingReputation is VotingBase {
) public {
Motion storage motion = motions[_motionId];
uint256 userRep = getReputationFromProof(_motionId, msg.sender, _key, _value, _branchMask, _siblings);

if (influences[motion.rootHash][motion.skillId][msg.sender] == 0) {
totalInfluences[motion.rootHash][motion.skillId] = add(totalInfluences[motion.rootHash][motion.skillId], userRep);
}

influences[motion.rootHash][motion.skillId][msg.sender] = userRep;
}

/// @param _motionId The id of the motion
/// @param _user The user in question
function getInfluence(uint256 _motionId, address _user) public view override returns (uint256) {
Motion storage motion = motions[_motionId];
return influences[motion.rootHash][motion.skillId][_user];
}

/// @param _motionId The id of the motion
function getTotalInfluence(uint256 _motionId) public view override returns (uint256) {
Motion storage motion = motions[_motionId];
return totalInfluences[motion.rootHash][motion.skillId];
}

function postReveal(uint256 _motionId, address _user) internal override {}
function postClaim(uint256 _motionId, address _user) internal override {}

Expand Down
13 changes: 13 additions & 0 deletions contracts/extensions/VotingToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ contract VotingToken is VotingBase {

// [motionId][user] => tokenBalance
mapping (uint256 => mapping (address => uint256)) influences;
// [motionId] => tokenBalance
mapping (uint256 => uint256) totalInfluences;

// [motionId] => lockId
mapping (uint256 => uint256) locks;
Expand All @@ -51,14 +53,25 @@ contract VotingToken is VotingBase {
);

uint256 balance = tokenLocking.getUserLock(token, msg.sender).balance;

if (influences[_motionId][msg.sender] == 0) {
totalInfluences[_motionId] = add(totalInfluences[_motionId], balance);
}

influences[_motionId][msg.sender] = balance;
}

/// @param _motionId The id of the motion
/// @param _user The user in question
function getInfluence(uint256 _motionId, address _user) public view override returns (uint256) {
return influences[_motionId][_user];
}

/// @param _motionId The id of the motion
function getTotalInfluence(uint256 _motionId) public view override returns (uint256) {
return totalInfluences[_motionId];
}

function postReveal(uint256 _motionId, address _user) internal override {
colony.unlockTokenForUser(_user, locks[_motionId]);
}
Expand Down
9 changes: 6 additions & 3 deletions test/extensions/voting-rep.js
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,8 @@ contract("Voting Reputation", (accounts) => {
const user0LockPost = await tokenLocking.getUserLock(token.address, USER0);
const user1LockPost = await tokenLocking.getUserLock(token.address, USER1);

const loserStake = REQUIRED_STAKE.divn(10).muln(8); // Take out voter comp
const motion = await voting.getMotion(motionId);
const loserStake = REQUIRED_STAKE.sub(new BN(motion.paidVoterComp));
const expectedReward0 = loserStake.divn(3).muln(2); // (stake * .8) * (winPct = 1/3 * 2)
const expectedReward1 = REQUIRED_STAKE.add(loserStake.divn(3)); // stake + ((stake * .8) * (1 - (winPct = 2/3 * 2))

Expand Down Expand Up @@ -1410,7 +1411,8 @@ contract("Voting Reputation", (accounts) => {
const user1LockPost = await tokenLocking.getUserLock(token.address, USER1);
const user2LockPost = await tokenLocking.getUserLock(token.address, USER2);

const loserStake = REQUIRED_STAKE.divn(10).muln(8); // Take out voter comp
const motion = await voting.getMotion(motionId);
const loserStake = REQUIRED_STAKE.sub(new BN(motion.paidVoterComp));
const expectedReward0 = loserStake.divn(3).muln(2); // (stake * .8) * (winPct = 1/3 * 2)
const expectedReward1 = REQUIRED_STAKE.add(loserStake.divn(3)).divn(3).muln(2); // stake + ((stake * .8) * (1 - (winPct = 2/3 * 2))
const expectedReward2 = REQUIRED_STAKE.add(loserStake.divn(3)).divn(3); // stake + ((stake * .8) * (1 - (winPct = 2/3 * 2))
Expand Down Expand Up @@ -1453,7 +1455,8 @@ contract("Voting Reputation", (accounts) => {
const user1LockPost = await tokenLocking.getUserLock(token.address, USER1);
const user2LockPost = await tokenLocking.getUserLock(token.address, USER2);

const loserStake = REQUIRED_STAKE.divn(10).muln(8); // Take out voter comp
const motion = await voting.getMotion(motionId);
const loserStake = REQUIRED_STAKE.sub(new BN(motion.paidVoterComp));
const expectedReward0 = loserStake.divn(3).muln(2).divn(3).muln(2); // (stake * .8) * (winPct = 1/3 * 2)
const expectedReward1 = REQUIRED_STAKE.add(loserStake.divn(3)); // stake + ((stake * .8) * (1 - (winPct = 2/3 * 2))
const expectedReward2 = loserStake.divn(3).muln(2).divn(3); // (stake * .8) * (winPct = 1/3 * 2)
Expand Down
2 changes: 1 addition & 1 deletion test/extensions/voting-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ contract("Voting Token", (accounts) => {
const motion = await voting.getMotion(motionId);
const loserStake = requiredStake.sub(new BN(motion.paidVoterComp));
const expectedReward0 = loserStake.divn(3).muln(2).addn(1); // (stake * .8) * (winPct = 1/3 * 2) + dust
const expectedReward1 = requiredStake.add(loserStake.divn(3)); // stake + ((stake * .8) * (1 - (winPct = 2/3 * 2))
const expectedReward1 = requiredStake.add(loserStake.divn(3)).addn(1); // stake + ((stake * .8) * (1 - (winPct = 2/3 * 2)) + dust

expect(new BN(user0LockPost.balance).sub(new BN(user0LockPre.balance))).to.eq.BN(expectedReward0);
expect(new BN(user1LockPost.balance).sub(new BN(user1LockPre.balance))).to.eq.BN(expectedReward1);
Expand Down

0 comments on commit e07d9ce

Please sign in to comment.