Skip to content

Commit dcaed7e

Browse files
authored
ExternalLocking4Rep : add registrar (#562)
1 parent 94149aa commit dcaed7e

File tree

2 files changed

+87
-38
lines changed

2 files changed

+87
-38
lines changed

contracts/schemes/ExternalLocking4Reputation.sol

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
1010

1111
contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
1212

13+
event Register(address indexed _beneficiary);
14+
1315
address public externalLockingContract;
1416
string public getBalanceFuncSignature;
1517

1618
// locker -> bool
1719
mapping(address => bool) public externalLockers;
20+
// beneficiary -> bool
21+
mapping(address => bool) public registrar;
1822

1923
/**
2024
* @dev initialize
2125
* @param _avatar the avatar to mint reputation from
2226
* @param _reputationReward the total reputation this contract will reward
2327
* for the token locking
24-
* @param _lockingStartTime locking starting period time.
25-
* @param _lockingEndTime the locking end time.
26-
* locking is disable after this time.
28+
* @param _claimingStartTime claiming starting period time.
29+
* @param _claimingEndTime the claiming end time.
30+
* claiming is disable after this time.
2731
* @param _redeemEnableTime redeem enable time .
2832
* redeem reputation can be done after this time.
2933
* @param _externalLockingContract the contract which lock the token.
@@ -33,36 +37,45 @@ contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
3337
function initialize(
3438
Avatar _avatar,
3539
uint _reputationReward,
36-
uint _lockingStartTime,
37-
uint _lockingEndTime,
40+
uint _claimingStartTime,
41+
uint _claimingEndTime,
3842
uint _redeemEnableTime,
3943
address _externalLockingContract,
4044
string _getBalanceFuncSignature)
4145
external
4246
onlyOwner
4347
{
44-
require(_lockingEndTime > _lockingStartTime, "_lockingEndTime should be greater than _lockingStartTime");
48+
require(_claimingEndTime > _claimingStartTime, "_claimingEndTime should be greater than _claimingStartTime");
4549
externalLockingContract = _externalLockingContract;
4650
getBalanceFuncSignature = _getBalanceFuncSignature;
4751
super._initialize(
4852
_avatar,
4953
_reputationReward,
50-
_lockingStartTime,
51-
_lockingEndTime,
54+
_claimingStartTime,
55+
_claimingEndTime,
5256
_redeemEnableTime,
5357
1);
5458
}
5559

5660
/**
57-
* @dev lock function
58-
* @return lockingId
61+
* @dev claim function
62+
* @param _beneficiary the beneficiary address to claim for
63+
* if _beneficiary == 0 the claim will be for the msg.sender.
64+
* @return claimId
5965
*/
60-
function lock() public returns(bytes32) {
66+
function claim(address _beneficiary) public returns(bytes32) {
6167
require(avatar != Avatar(0), "should initialize first");
62-
require(externalLockers[msg.sender] == false, "locking twice is not allowed");
63-
externalLockers[msg.sender] = true;
68+
address beneficiary;
69+
if (_beneficiary == address(0)) {
70+
beneficiary = msg.sender;
71+
} else {
72+
require(registrar[_beneficiary],"beneficiary should be register");
73+
beneficiary = _beneficiary;
74+
}
75+
require(externalLockers[beneficiary] == false, "claiming twice is not allowed");
76+
externalLockers[beneficiary] = true;
6477
// solium-disable-next-line security/no-low-level-calls
65-
bool result = externalLockingContract.call(abi.encodeWithSignature(getBalanceFuncSignature, msg.sender));
78+
bool result = externalLockingContract.call(abi.encodeWithSignature(getBalanceFuncSignature, beneficiary));
6679
uint lockedAmount;
6780
// solium-disable-next-line security/no-inline-assembly
6881
assembly {
@@ -73,6 +86,15 @@ contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
7386
default { lockedAmount := mload(0) }
7487
}
7588

76-
return super._lock(lockedAmount, 1, msg.sender);
89+
return super._lock(lockedAmount, 1, beneficiary);
90+
}
91+
92+
/**
93+
* @dev register function
94+
* register for external locking claim
95+
*/
96+
function register() public {
97+
registrar[msg.sender] = true;
98+
emit Register(msg.sender);
7799
}
78100
}

test/externallocking4reputation.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const constants = require('./constants');
55
var ExternalLocking4Reputation = artifacts.require("./ExternalLocking4Reputation.sol");
66
var ExternalTokenLockerMock = artifacts.require("./ExternalTokenLockerMock.sol");
77

8-
const setup = async function (accounts,_repAllocation = 100,_lockingStartTime = 0,_lockingEndTime = 3000,_redeemEnableTime = 3000,_initialize = true) {
8+
const setup = async function (accounts,_repAllocation = 100,_claimingStartTime = 0,_claimingEndTime = 3000,_redeemEnableTime = 3000,_initialize = true) {
99
var testSetup = new helpers.TestSetup();
1010
var controllerCreator = await ControllerCreator.new({gas: constants.ARC_GAS_LIMIT});
1111
testSetup.daoCreator = await DaoCreator.new(controllerCreator.address,{gas:constants.ARC_GAS_LIMIT});
1212
testSetup.org = await helpers.setupOrganization(testSetup.daoCreator,accounts[0],1000,1000);
1313
var block = await web3.eth.getBlock("latest");
14-
testSetup.lockingEndTime = block.timestamp + _lockingEndTime;
15-
testSetup.lockingStartTime = block.timestamp + _lockingStartTime;
14+
testSetup.lockingEndTime = block.timestamp + _claimingEndTime;
15+
testSetup.lockingStartTime = block.timestamp + _claimingStartTime;
1616
testSetup.redeemEnableTime = block.timestamp + _redeemEnableTime;
1717
testSetup.extetnalTokenLockerMock = await ExternalTokenLockerMock.new();
1818
await testSetup.extetnalTokenLockerMock.lock(100,{from:accounts[0]});
@@ -46,9 +46,9 @@ contract('ExternalLocking4Reputation', accounts => {
4646
assert.equal(await testSetup.externalLocking4Reputation.getBalanceFuncSignature(),"lockedTokenBalances(address)");
4747
});
4848

49-
it("lock", async () => {
49+
it("claim", async () => {
5050
let testSetup = await setup(accounts);
51-
var tx = await testSetup.externalLocking4Reputation.lock();
51+
var tx = await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
5252
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
5353
assert.equal(tx.logs.length,1);
5454
assert.equal(tx.logs[0].event,"Lock");
@@ -58,52 +58,79 @@ contract('ExternalLocking4Reputation', accounts => {
5858
assert.equal(tx.logs[0].args._locker,accounts[0]);
5959
});
6060

61-
it("cannot lock before set parameters", async () => {
61+
it("claim on behalf of a beneficiary", async () => {
62+
let testSetup = await setup(accounts);
63+
var tx = await testSetup.externalLocking4Reputation.register({from:accounts[1]});
64+
assert.equal(tx.logs.length,1);
65+
assert.equal(tx.logs[0].event,"Register");
66+
assert.equal(tx.logs[0].args._beneficiary,accounts[1]);
67+
tx = await testSetup.externalLocking4Reputation.claim(accounts[1]);
68+
var lockingId = await helpers.getValueFromLogs(tx, '_lockingId',1);
69+
assert.equal(tx.logs.length,1);
70+
assert.equal(tx.logs[0].event,"Lock");
71+
assert.equal(tx.logs[0].args._lockingId,lockingId);
72+
assert.equal(tx.logs[0].args._amount,200);
73+
assert.equal(tx.logs[0].args._period,1);
74+
assert.equal(tx.logs[0].args._locker,accounts[1]);
75+
});
76+
77+
it("cannot claim on behalf of a beneficiary if not register", async () => {
78+
let testSetup = await setup(accounts);
79+
try {
80+
await testSetup.externalLocking4Reputation.claim(accounts[1]);
81+
assert(false, "cannot claim on behalf of a beneficiary if not register");
82+
} catch(error) {
83+
helpers.assertVMException(error);
84+
}
85+
});
86+
87+
88+
it("cannot claim before set parameters", async () => {
6289
let testSetup = await setup(accounts,100,0,3000,3000,false);
6390
try {
64-
await testSetup.externalLocking4Reputation.lock();
91+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
6592
assert(false, "cannot lock before set parameters");
6693
} catch(error) {
6794
helpers.assertVMException(error);
6895
}
6996
});
7097

71-
it("lock with value == 0 should revert", async () => {
98+
it("claim with value == 0 should revert", async () => {
7299
let testSetup = await setup(accounts);
73100
try {
74-
await testSetup.externalLocking4Reputation.lock({from:accounts[4]});
101+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS,{from:accounts[4]});
75102
assert(false, "lock with value == 0 should revert");
76103
} catch(error) {
77104
helpers.assertVMException(error);
78105
}
79106
});
80107

81-
it("lock after _lockingEndTime should revert", async () => {
108+
it("claim after _claimingEndTime should revert", async () => {
82109
let testSetup = await setup(accounts);
83110
await helpers.increaseTime(3001);
84111
try {
85-
await testSetup.externalLocking4Reputation.lock();
86-
assert(false, "lock after _lockingEndTime should revert");
112+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
113+
assert(false, "lock after _claimingEndTime should revert");
87114
} catch(error) {
88115
helpers.assertVMException(error);
89116
}
90117
});
91118

92-
it("lock before start should revert", async () => {
119+
it("claim before start should revert", async () => {
93120
let testSetup = await setup(accounts,100,100);
94121
try {
95-
await testSetup.externalLocking4Reputation.lock();
122+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
96123
assert(false, "lock before start should revert");
97124
} catch(error) {
98125
helpers.assertVMException(error);
99126
}
100127
});
101128

102-
it("cannot lock twice for the same user", async () => {
129+
it("cannot claim twice for the same user", async () => {
103130
let testSetup = await setup(accounts);
104-
await testSetup.externalLocking4Reputation.lock();
131+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
105132
try {
106-
await testSetup.externalLocking4Reputation.lock();
133+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
107134
assert(false, "cannot lock twice for the same user");
108135
} catch(error) {
109136
helpers.assertVMException(error);
@@ -112,7 +139,7 @@ contract('ExternalLocking4Reputation', accounts => {
112139

113140
it("redeem", async () => {
114141
let testSetup = await setup(accounts);
115-
var tx = await testSetup.externalLocking4Reputation.lock();
142+
var tx = await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
116143
await helpers.increaseTime(3001);
117144
tx = await testSetup.externalLocking4Reputation.redeem(accounts[0]);
118145
assert.equal(tx.logs.length,1);
@@ -124,8 +151,8 @@ contract('ExternalLocking4Reputation', accounts => {
124151

125152
it("redeem score ", async () => {
126153
let testSetup = await setup(accounts);
127-
await testSetup.externalLocking4Reputation.lock({from:accounts[0]});
128-
await testSetup.externalLocking4Reputation.lock({from:accounts[2]});
154+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS,{from:accounts[0]});
155+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS,{from:accounts[2]});
129156
await helpers.increaseTime(3001);
130157
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
131158
await testSetup.externalLocking4Reputation.redeem(accounts[2]);
@@ -135,7 +162,7 @@ contract('ExternalLocking4Reputation', accounts => {
135162

136163
it("redeem cannot redeem twice", async () => {
137164
let testSetup = await setup(accounts);
138-
await testSetup.externalLocking4Reputation.lock();
165+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
139166
await helpers.increaseTime(3001);
140167
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
141168
try {
@@ -148,7 +175,7 @@ contract('ExternalLocking4Reputation', accounts => {
148175

149176
it("redeem before lockingEndTime should revert", async () => {
150177
let testSetup = await setup(accounts);
151-
await testSetup.externalLocking4Reputation.lock();
178+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
152179
await helpers.increaseTime(50);
153180
try {
154181
await testSetup.externalLocking4Reputation.redeem(accounts[0]);
@@ -160,7 +187,7 @@ contract('ExternalLocking4Reputation', accounts => {
160187

161188
it("redeem before redeemEnableTime should revert", async () => {
162189
let testSetup = await setup(accounts,100,0,3000,4000,true);
163-
await testSetup.externalLocking4Reputation.lock();
190+
await testSetup.externalLocking4Reputation.claim(helpers.NULL_ADDRESS);
164191
await helpers.increaseTime(3500);
165192
try {
166193
await testSetup.externalLocking4Reputation.redeem(accounts[0]);

0 commit comments

Comments
 (0)