Skip to content

Commit

Permalink
FixedReputationAllocation get redeemEnableTime as initialise param (#550
Browse files Browse the repository at this point in the history
)

* locking4reputation : remove lockid from redeem function

* FixedReputationAllocation : add redeemEnableTime
  • Loading branch information
orenyodfat authored Oct 8, 2018
1 parent 2e6d233 commit 78d90ae
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 66 deletions.
15 changes: 10 additions & 5 deletions contracts/schemes/FixReputationAllocation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ contract FixedReputationAllocation is Ownable {
bool public isEnable;
uint public numberOfBeneficiaries;
uint public beneficiaryReward;
uint public redeemEnableTime;

/**
* @dev initialize
* @param _avatar the avatar to mint reputation from
* @param _reputationReward the total reputation this contract will reward
* @param _redeemEnableTime time to enable redeem
*/
function initialize(Avatar _avatar, uint _reputationReward) external onlyOwner {
function initialize(Avatar _avatar, uint _reputationReward, uint _redeemEnableTime) external onlyOwner {
require(avatar == Avatar(0), "can be called only one time");
require(_avatar != Avatar(0), "avatar cannot be zero");
reputationReward = _reputationReward;
redeemEnableTime = _redeemEnableTime;
avatar = _avatar;
}

Expand All @@ -47,10 +50,12 @@ contract FixedReputationAllocation is Ownable {
function redeem(address _beneficiary) public returns(bool) {
require(isEnable, "require to be enable");
require(beneficiaries[_beneficiary], "require _beneficiary to exist in the beneficiaries map");
// solium-disable-next-line security/no-block-members
require(now > redeemEnableTime, "require now > redeemEnableTime");
require(ControllerInterface(avatar.owner()).mintReputation(beneficiaryReward, _beneficiary, avatar), "mint reputation should success");

emit Redeem(_beneficiary, beneficiaryReward);

return true;
}

Expand All @@ -60,11 +65,11 @@ contract FixedReputationAllocation is Ownable {
*/
function addBeneficiary(address _beneficiary) public onlyOwner {
require(!isEnable, "can add beneficiary only if not already enable");

if (!beneficiaries[_beneficiary]) {
beneficiaries[_beneficiary] = true;
numberOfBeneficiaries++;

emit BeneficiaryAddressAdded(_beneficiary);
}
}
Expand Down
15 changes: 7 additions & 8 deletions contracts/schemes/Locking4Reputation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract Locking4Reputation {
using RealMath for int216;
using RealMath for int256;

event Redeem(bytes32 indexed _lockingId, address indexed _beneficiary, uint _amount);
event Redeem(address indexed _beneficiary, uint _amount);
event Release(bytes32 indexed _lockingId, address indexed _beneficiary, uint _amount);
event Lock(address indexed _locker, bytes32 indexed _lockingId, uint _amount, uint _period);

Expand Down Expand Up @@ -42,24 +42,23 @@ contract Locking4Reputation {
/**
* @dev redeem reputation function
* @param _beneficiary the beneficiary for the release
* @param _lockingId the locking id to release
* @return bool
*/
function redeem(address _beneficiary, bytes32 _lockingId) public returns(bool) {
function redeem(address _beneficiary) public returns(bool) {
// solium-disable-next-line security/no-block-members
require(block.timestamp >= lockingEndTime, "check the lock period pass");
require(scores[_beneficiary] > 0, "score should be > 0");
uint score = scores[_beneficiary];
scores[_beneficiary] = 0;
int256 repRelation = int216(score).toReal().mul(int216(reputationReward).toReal());
uint reputation = uint256(repRelation.div(int216(totalScore).toReal()).fromReal());

//check that the reputation is sum zero
reputationRewardLeft = reputationRewardLeft.sub(reputation);
require(ControllerInterface(avatar.owner()).mintReputation(reputation, _beneficiary, avatar), "mint reputation should success");
emit Redeem(_lockingId, _beneficiary, reputation);

emit Redeem(_beneficiary, reputation);

return true;
}

Expand Down Expand Up @@ -134,7 +133,7 @@ contract Locking4Reputation {
require(avatar == Avatar(0), "can be called only one time");
require(_avatar != Avatar(0), "avatar cannot be zero");
require(_lockingEndTime > _lockingStartTime, "locking end time should be greater than locking start time");

reputationReward = _reputationReward;
reputationRewardLeft = _reputationReward;
lockingEndTime = _lockingEndTime;
Expand Down
26 changes: 10 additions & 16 deletions test/externallocking4reputation.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,38 +110,33 @@ contract('ExternalLocking4Reputation', accounts => {
it("redeem", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.externalLocking4Reputation.lock();
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
await helpers.increaseTime(3001);
tx = await testSetup.externalLocking4Reputation.redeem(accounts[0],lockingId);
tx = await testSetup.externalLocking4Reputation.redeem(accounts[0]);
assert.equal(tx.logs.length,1);
assert.equal(tx.logs[0].event,"Redeem");
assert.equal(tx.logs[0].args._lockingId,lockingId);
assert.equal(tx.logs[0].args._amount,100);
assert.equal(tx.logs[0].args._beneficiary,accounts[0]);
assert.equal(await testSetup.org.reputation.balanceOf(accounts[0]),1000+100);
});

it("redeem score ", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.externalLocking4Reputation.lock({from:accounts[0]});
var lockingId1 = await helpers.getValueFromLogs(tx, '_lockingId',1);
tx = await testSetup.externalLocking4Reputation.lock({from:accounts[2]});
var lockingId2 = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.externalLocking4Reputation.lock({from:accounts[0]});
await testSetup.externalLocking4Reputation.lock({from:accounts[2]});
await helpers.increaseTime(3001);
await testSetup.externalLocking4Reputation.redeem(accounts[0],lockingId1);
await testSetup.externalLocking4Reputation.redeem(accounts[2],lockingId2);
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
await testSetup.externalLocking4Reputation.redeem(accounts[2]);
assert.equal(await testSetup.org.reputation.balanceOf(accounts[0]),1000+25);
assert.equal(await testSetup.org.reputation.balanceOf(accounts[2]),75);
});

it("redeem cannot redeem twice", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.externalLocking4Reputation.lock();
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.externalLocking4Reputation.lock();
await helpers.increaseTime(3001);
await testSetup.externalLocking4Reputation.redeem(accounts[0],lockingId);
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
try {
await testSetup.externalLocking4Reputation.redeem(accounts[0],lockingId);
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
assert(false, "cannot redeem twice");
} catch(error) {
helpers.assertVMException(error);
Expand All @@ -150,11 +145,10 @@ contract('ExternalLocking4Reputation', accounts => {

it("redeem before lockingEndTime should revert", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.externalLocking4Reputation.lock();
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.externalLocking4Reputation.lock();
await helpers.increaseTime(50);
try {
await testSetup.externalLocking4Reputation.redeem(accounts[0],lockingId);
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
assert(false, "redeem before lockingEndTime should revert");
} catch(error) {
helpers.assertVMException(error);
Expand Down
31 changes: 28 additions & 3 deletions test/fixreputationallocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ const ControllerCreator = artifacts.require("./ControllerCreator.sol");
const constants = require('./constants');
var FixedReputationAllocation = artifacts.require("./FixedReputationAllocation.sol");

const setup = async function (accounts,_repAllocation = 300,_initialize = true) {
const setup = async function (accounts,_repAllocation = 300,_initialize = true,_redeemEnableTime = 3000) {
var testSetup = new helpers.TestSetup();
var controllerCreator = await ControllerCreator.new({gas: constants.ARC_GAS_LIMIT});
testSetup.daoCreator = await DaoCreator.new(controllerCreator.address,{gas:constants.ARC_GAS_LIMIT});
testSetup.org = await helpers.setupOrganization(testSetup.daoCreator,accounts[0],0,0);

testSetup.fixedReputationAllocation = await FixedReputationAllocation.new();
if (_initialize === true) {
var block = await web3.eth.getBlock("latest");
testSetup.redeemEnableTime = block.timestamp + _redeemEnableTime;
await testSetup.fixedReputationAllocation.initialize(testSetup.org.avatar.address,
_repAllocation);
_repAllocation,
testSetup.redeemEnableTime);
}

var permissions = "0x00000000";
Expand All @@ -26,6 +29,7 @@ contract('FixedReputationAllocation', accounts => {
let testSetup = await setup(accounts);
assert.equal(await testSetup.fixedReputationAllocation.reputationReward(),300);
assert.equal(await testSetup.fixedReputationAllocation.isEnable(),false);
assert.equal(await testSetup.fixedReputationAllocation.redeemEnableTime(),testSetup.redeemEnableTime);
});

it("add beneficiary", async () => {
Expand Down Expand Up @@ -61,6 +65,7 @@ contract('FixedReputationAllocation', accounts => {
assert.equal(await testSetup.fixedReputationAllocation.beneficiaryReward(),300/accounts.length);
var beneficiaryReward;
var reputation;
await helpers.increaseTime(3001);
for (var i = 0 ;i< accounts.length ;i++) {
tx = await testSetup.fixedReputationAllocation.redeem(accounts[i]);
assert.equal(tx.logs.length,1);
Expand All @@ -86,6 +91,23 @@ contract('FixedReputationAllocation', accounts => {
}
});

it("cannot redeem before redeemEnableTime", async () => {
let testSetup = await setup(accounts);
await testSetup.fixedReputationAllocation.addBeneficiaries(accounts);
assert.equal(await testSetup.fixedReputationAllocation.numberOfBeneficiaries(),accounts.length);
assert.equal(await testSetup.fixedReputationAllocation.beneficiaryReward(),0);
await testSetup.fixedReputationAllocation.enable();
try {
await testSetup.fixedReputationAllocation.redeem(accounts[0]);
assert(false, "cannot redeem if not initialize");
} catch(error) {
helpers.assertVMException(error);
}
await helpers.increaseTime(3001);
await testSetup.fixedReputationAllocation.redeem(accounts[0]);


});

it("redeem without enable should revert", async () => {
let testSetup = await setup(accounts);
Expand Down Expand Up @@ -125,7 +147,8 @@ contract('FixedReputationAllocation', accounts => {
let testSetup = await setup(accounts);
try {
await testSetup.fixedReputationAllocation.initialize(testSetup.org.avatar.address,
100);
100,
100);
assert(false, "cannot initialize twice");
} catch(error) {
helpers.assertVMException(error);
Expand All @@ -136,13 +159,15 @@ contract('FixedReputationAllocation', accounts => {
var fixedReputationAllocation = await FixedReputationAllocation.new();
try {
await fixedReputationAllocation.initialize(accounts[0],
100,
100,
{from:accounts[1]});
assert(false, "initialize is onlyOwner");
} catch(error) {
helpers.assertVMException(error);
}
await fixedReputationAllocation.initialize(accounts[0],
100,
100,
{from:accounts[0]});
});
Expand Down
28 changes: 11 additions & 17 deletions test/lockingeth4reputation.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,39 +154,34 @@ contract('LockingEth4Reputation', accounts => {

it("redeem", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.lockingEth4Reputation.lock(100,{value:web3.utils.toWei('1', "ether")});
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.lockingEth4Reputation.lock(100,{value:web3.utils.toWei('1', "ether")});
await helpers.increaseTime(3001);
tx = await testSetup.lockingEth4Reputation.redeem(accounts[0],lockingId);
var tx = await testSetup.lockingEth4Reputation.redeem(accounts[0]);
assert.equal(tx.logs.length,1);
assert.equal(tx.logs[0].event,"Redeem");
assert.equal(tx.logs[0].args._lockingId,lockingId);
assert.equal(tx.logs[0].args._amount,100);
assert.equal(tx.logs[0].args._beneficiary,accounts[0]);
assert.equal(await testSetup.org.reputation.balanceOf(accounts[0]),1000+100);
});

it("redeem score ", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.lockingEth4Reputation.lock(100,{from:accounts[0],value:web3.utils.toWei('1', "ether")});
var lockingId1 = await helpers.getValueFromLogs(tx, '_lockingId',1);
tx = await testSetup.lockingEth4Reputation.lock(300,{from:accounts[1],value:web3.utils.toWei('1', "ether")});
var lockingId2 = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.lockingEth4Reputation.lock(100,{from:accounts[0],value:web3.utils.toWei('1', "ether")});
await testSetup.lockingEth4Reputation.lock(300,{from:accounts[1],value:web3.utils.toWei('1', "ether")});
await helpers.increaseTime(3001);
await testSetup.lockingEth4Reputation.redeem(accounts[0],lockingId1);
await testSetup.lockingEth4Reputation.redeem(accounts[1],lockingId2);
await testSetup.lockingEth4Reputation.redeem(accounts[0]);
await testSetup.lockingEth4Reputation.redeem(accounts[1]);
assert.equal(await testSetup.org.reputation.balanceOf(accounts[0]),1000+25);
assert.equal(await testSetup.org.reputation.balanceOf(accounts[1]),75);
});

it("redeem cannot redeem twice", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.lockingEth4Reputation.lock(100,{value:web3.utils.toWei('1', "ether")});
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.lockingEth4Reputation.lock(100,{value:web3.utils.toWei('1', "ether")});
await helpers.increaseTime(3001);
await testSetup.lockingEth4Reputation.redeem(accounts[0],lockingId);
await testSetup.lockingEth4Reputation.redeem(accounts[0]);
try {
await testSetup.lockingEth4Reputation.redeem(accounts[0],lockingId);
await testSetup.lockingEth4Reputation.redeem(accounts[0]);
assert(false, "cannot redeem twice");
} catch(error) {
helpers.assertVMException(error);
Expand All @@ -195,11 +190,10 @@ contract('LockingEth4Reputation', accounts => {

it("redeem before lockingEndTime should revert", async () => {
let testSetup = await setup(accounts);
var tx = await testSetup.lockingEth4Reputation.lock(100,{value:web3.utils.toWei('1', "ether")});
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
await testSetup.lockingEth4Reputation.lock(100,{value:web3.utils.toWei('1', "ether")});
await helpers.increaseTime(50);
try {
await testSetup.lockingEth4Reputation.redeem(accounts[0],lockingId);
await testSetup.lockingEth4Reputation.redeem(accounts[0]);
assert(false, "redeem before lockingEndTime should revert");
} catch(error) {
helpers.assertVMException(error);
Expand Down
Loading

0 comments on commit 78d90ae

Please sign in to comment.