diff --git a/contracts/schemes/FixReputationAllocation.sol b/contracts/schemes/FixReputationAllocation.sol index 4f66179e..dfd8a10e 100644 --- a/contracts/schemes/FixReputationAllocation.sol +++ b/contracts/schemes/FixReputationAllocation.sol @@ -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; } @@ -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; } @@ -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); } } diff --git a/contracts/schemes/Locking4Reputation.sol b/contracts/schemes/Locking4Reputation.sol index 87647c8b..908d3b1f 100644 --- a/contracts/schemes/Locking4Reputation.sol +++ b/contracts/schemes/Locking4Reputation.sol @@ -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); @@ -42,10 +42,9 @@ 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"); @@ -53,13 +52,13 @@ contract Locking4Reputation { 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; } @@ -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; diff --git a/test/externallocking4reputation.js b/test/externallocking4reputation.js index bcb36f02..a6da5a3d 100644 --- a/test/externallocking4reputation.js +++ b/test/externallocking4reputation.js @@ -110,12 +110,10 @@ 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); @@ -123,25 +121,22 @@ contract('ExternalLocking4Reputation', accounts => { 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); @@ -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); diff --git a/test/fixreputationallocation.js b/test/fixreputationallocation.js index e3b120a3..6cdeb5de 100644 --- a/test/fixreputationallocation.js +++ b/test/fixreputationallocation.js @@ -4,7 +4,7 @@ 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}); @@ -12,8 +12,11 @@ const setup = async function (accounts,_repAllocation = 300,_initialize = true) 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"; @@ -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 () => { @@ -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); @@ -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); @@ -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); @@ -136,6 +159,7 @@ contract('FixedReputationAllocation', accounts => { var fixedReputationAllocation = await FixedReputationAllocation.new(); try { await fixedReputationAllocation.initialize(accounts[0], + 100, 100, {from:accounts[1]}); assert(false, "initialize is onlyOwner"); @@ -143,6 +167,7 @@ contract('FixedReputationAllocation', accounts => { helpers.assertVMException(error); } await fixedReputationAllocation.initialize(accounts[0], + 100, 100, {from:accounts[0]}); }); diff --git a/test/lockingeth4reputation.js b/test/lockingeth4reputation.js index 0fc42b46..c1fc439e 100644 --- a/test/lockingeth4reputation.js +++ b/test/lockingeth4reputation.js @@ -154,13 +154,11 @@ 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); @@ -168,25 +166,22 @@ contract('LockingEth4Reputation', accounts => { 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); @@ -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); diff --git a/test/lockingtoken4reputation.js b/test/lockingtoken4reputation.js index 490e773d..6aa3d9b8 100644 --- a/test/lockingtoken4reputation.js +++ b/test/lockingtoken4reputation.js @@ -157,13 +157,11 @@ contract('LockingToken4Reputation', accounts => { it("redeem", async () => { let testSetup = await setup(accounts); - var tx = await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100); - var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1); + await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100); await helpers.increaseTime(3001); - tx = await testSetup.lockingToken4Reputation.redeem(accounts[0],lockingId); + var tx = await testSetup.lockingToken4Reputation.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); @@ -171,27 +169,24 @@ contract('LockingToken4Reputation', accounts => { it("redeem score ", async () => { let testSetup = await setup(accounts); - var tx = await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100 ,{from:accounts[0]}); - var lockingId1 = await helpers.getValueFromLogs(tx, '_lockingId',1); + await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100 ,{from:accounts[0]}); await testSetup.lockingToken.transfer(accounts[1],web3.utils.toWei('1', "ether")); await testSetup.lockingToken.approve(testSetup.lockingToken4Reputation.address,web3.utils.toWei('100', "ether"),{from:accounts[1]}); - tx = await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),300 ,{from:accounts[1]}); - var lockingId2 = await helpers.getValueFromLogs(tx, '_lockingId',1); + await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),300 ,{from:accounts[1]}); await helpers.increaseTime(3001); - await testSetup.lockingToken4Reputation.redeem(accounts[0],lockingId1); - await testSetup.lockingToken4Reputation.redeem(accounts[1],lockingId2); + await testSetup.lockingToken4Reputation.redeem(accounts[0]); + await testSetup.lockingToken4Reputation.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.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100); - var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1); + await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100); await helpers.increaseTime(3001); - await testSetup.lockingToken4Reputation.redeem(accounts[0],lockingId); + await testSetup.lockingToken4Reputation.redeem(accounts[0]); try { - await testSetup.lockingToken4Reputation.redeem(accounts[0],lockingId); + await testSetup.lockingToken4Reputation.redeem(accounts[0]); assert(false, "cannot redeem twice"); } catch(error) { helpers.assertVMException(error); @@ -200,11 +195,10 @@ contract('LockingToken4Reputation', accounts => { it("redeem before lockingEndTime should revert", async () => { let testSetup = await setup(accounts); - var tx = await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100); - var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1); + await testSetup.lockingToken4Reputation.lock(web3.utils.toWei('1', "ether"),100); await helpers.increaseTime(50); try { - await testSetup.lockingToken4Reputation.redeem(accounts[0],lockingId); + await testSetup.lockingToken4Reputation.redeem(accounts[0]); assert(false, "redeem before lockingEndTime should revert"); } catch(error) { helpers.assertVMException(error);