Skip to content

Commit

Permalink
GenesisProtocol - optimise redeem function (#517)
Browse files Browse the repository at this point in the history
* Change redeem function

* update redeem func

* reputation reward only PBV

* Fix lostreputation.

* redeem returns rewards per roll

* Update redeemer signature

* Redeemer from contributionReward only if it was excuted

* staker() -> getStaker()
  • Loading branch information
orenyodfat authored Aug 14, 2018
1 parent 20ffd32 commit 2f7b216
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 341 deletions.
314 changes: 124 additions & 190 deletions contracts/VotingMachines/GenesisProtocol.sol

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions contracts/universalSchemes/ContributionReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,8 @@ contract ContributionReward is UniversalScheme {
return organizationsProposals[_avatar][_proposalId].externalToken;
}

function getProposalExecutionTime(bytes32 _proposalId, address _avatar) public view returns (uint) {
return organizationsProposals[_avatar][_proposalId].executionTime;
}

}
47 changes: 27 additions & 20 deletions contracts/utils/Redeemer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,50 @@ contract Redeemer {
* It tries to redeem proposal rewards from the contribution rewards scheme.
* This function does not emit events.
* A client should listen to GenesisProtocol and ContributionReward redemption events
* to monitor redemption operations.
* to monitor redemption operations.
* @param _proposalId the ID of the voting in the voting machine
* @param _avatar address of the controller
* @param _beneficiary beneficiary
* @return result boolean array for the following redeem types:
* result[0] -execute -true or false
* result[1]- redeem reputation and stakingTokens(GEN) from GenesisProtocol
* result[2] -redeem daoBounty(GEN) from GenesisProtocol
* result[3]- reputation - from ContributionReward
* result[4]- nativeTokenReward - from ContributionReward
* result[5]- Ether - from ContributionReward
* result[6]- ExternalToken - from ContributionReward
* @return gpRewards array
* gpRewards[0] - stakerTokenAmount
* gpRewards[1] - stakerReputationAmount
* gpRewards[2] - voterTokenAmount
* gpRewards[3] - voterReputationAmount
* gpRewards[4] - proposerReputationAmount
* @return gpDaoBountyReward array
* gpDaoBountyReward[0] - staker dao bounty reward -
* will be zero for the case there is not enough tokens in avatar for the reward.
* gpDaoBountyReward[1] - staker potential dao bounty reward.
* @return executed bool true or false
* @return crResults array
* crResults[0]- reputation - from ContributionReward
* crResults[1]- nativeTokenReward - from ContributionReward
* crResults[2]- Ether - from ContributionReward
* crResults[3]- ExternalToken - from ContributionReward
*/
function redeem(bytes32 _proposalId,address _avatar,address _beneficiary)
external
returns(bool[7] result)
returns(uint[5] gpRewards,
uint[2] gpDaoBountyReward,
bool executed,
bool[4] crResults)
{
GenesisProtocol.ProposalState pState = genesisProtocol.state(_proposalId);
// solium-disable-next-line operator-whitespace
if ((pState == GenesisProtocol.ProposalState.PreBoosted)||
(pState == GenesisProtocol.ProposalState.Boosted)||
(pState == GenesisProtocol.ProposalState.QuietEndingPeriod)) {
result[0] = genesisProtocol.execute(_proposalId);
executed = genesisProtocol.execute(_proposalId);
}
pState = genesisProtocol.state(_proposalId);
if ((pState == GenesisProtocol.ProposalState.Executed) ||
(pState == GenesisProtocol.ProposalState.Closed)) {
result[1] = genesisProtocol.redeem(_proposalId,_beneficiary);
uint daoBountyAmount = genesisProtocol.getRedeemableTokensStakerBounty(_proposalId,_beneficiary);
if ((daoBountyAmount > 0) && (genesisProtocol.stakingToken().balanceOf(_avatar) >= daoBountyAmount)) {
result[2] = genesisProtocol.redeemDaoBounty(_proposalId,_beneficiary);
}
//redeem from contributionReward only if it is positive decision
if (genesisProtocol.winningVote(_proposalId) == genesisProtocol.YES()) {
(result[3],result[4],result[5],result[6]) = contributionRewardRedeem(_proposalId,_avatar);
gpRewards = genesisProtocol.redeem(_proposalId,_beneficiary);
(gpDaoBountyReward[0],gpDaoBountyReward[1]) = genesisProtocol.redeemDaoBounty(_proposalId,_beneficiary);
//redeem from contributionReward only if it executed
if (contributionReward.getProposalExecutionTime(_proposalId,_avatar) > 0) {
(crResults[0],crResults[1],crResults[2],crResults[3]) = contributionRewardRedeem(_proposalId,_avatar);
}
}
}
Expand All @@ -69,7 +77,6 @@ contract Redeemer {
bool[4] memory whatToRedeem;
whatToRedeem[0] = true; //reputation
whatToRedeem[1] = true; //nativeToken

uint periodsToPay = contributionReward.getPeriodsToPay(_proposalId,_avatar,2);
uint ethReward = contributionReward.getProposalEthReward(_proposalId,_avatar);
uint externalTokenReward = contributionReward.getProposalExternalTokenReward(_proposalId,_avatar);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"tsconfig.json"
],
"config": {
"gasLimit": "6100000"
"gasLimit": "5900000"
},
"scripts": {
"test": "cross-conf-env run-with-ganache --ganache-cmd 'npm run ganache' 'npm run truffle compile && npm run truffle migrate && npm run truffle test'",
Expand Down
2 changes: 1 addition & 1 deletion test/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const ARC_GAS_LIMIT = 6100000;
const ARC_GAS_LIMIT = 5900000;

module.exports = { ARC_GAS_LIMIT };
19 changes: 16 additions & 3 deletions test/contributionreward.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,24 @@ contract('ContributionReward', function(accounts) {
await testSetup.contributionRewardParams.votingMachine.genesisProtocol.vote(proposalId,1,{from:accounts[0]});
await helpers.increaseTime(periodLength+1);
var arcUtils = await Redeemer.new(testSetup.contributionReward.address,testSetup.contributionRewardParams.votingMachine.genesisProtocol.address);
var redeemRewards = await arcUtils.redeem.call(proposalId,testSetup.org.avatar.address,accounts[0]);
assert.equal(redeemRewards[0][3],100); //redeemRewards[0] gpRewards
assert.equal(redeemRewards[0][4],61);
assert.equal(redeemRewards[1][0],0); //daoBountyRewards
assert.equal(redeemRewards[1][1],0); //daoBountyRewards
assert.equal(redeemRewards[2],false); //isExecuted
assert.equal(redeemRewards[3][0],true); //redeemRewards[3] crResultArray
await arcUtils.redeem(proposalId,testSetup.org.avatar.address,accounts[0]);
var eth = web3.eth.getBalance(otherAvatar.address);
assert.equal(eth.toNumber(),ethReward);
assert.equal(await testSetup.org.reputation.reputationOf(otherAvatar.address),reputationReward);
assert.equal(await testSetup.org.token.balanceOf(otherAvatar.address),nativeTokenReward);
var reputation = await testSetup.org.reputation.reputationOf(accounts[0]);
assert.equal(reputation,1141);
var reputationGainAsVoter = 0;
var proposingRepRewardConstA=60;
var proposingRepRewardConstB=1;
var reputationGainAsProposer = (proposingRepRewardConstA*1000 + proposingRepRewardConstB*1000)/1000;
assert.equal(reputation, 1000+reputationGainAsVoter + reputationGainAsProposer);
});
it("execute proposeContributionReward via genesisProtocol and redeem using Redeemer for negative proposal", async function() {
var standardTokenMock = await StandardTokenMock.new(accounts[0],1000);
Expand Down Expand Up @@ -547,9 +558,11 @@ contract('ContributionReward', function(accounts) {
assert.equal(await testSetup.org.reputation.reputationOf(otherAvatar.address),0);
assert.equal(await testSetup.org.token.balanceOf(otherAvatar.address),0);
var reputation = await testSetup.org.reputation.reputationOf(accounts[0]);
assert.equal(reputation,1080);
//no reputation reward for proposer for negative proposal.
//reputation reward for a single voter = 0
assert.equal(reputation, 1000);
});

it("execute proposeContributionReward mint reputation with period 0 ", async function() {
var testSetup = await setup(accounts);
var reputationReward = 12;
Expand Down
Loading

0 comments on commit 2f7b216

Please sign in to comment.