Skip to content

Commit b6e6341

Browse files
authored
lockingtoken4reputation.sol : multi token support (#563)
* lockingtoken4reputation.sol : multi token support * use truffle "5.0.0-beta.1"
1 parent dcaed7e commit b6e6341

File tree

8 files changed

+126
-37
lines changed

8 files changed

+126
-37
lines changed

contracts/schemes/ExternalLocking4Reputation.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
7272
require(registrar[_beneficiary],"beneficiary should be register");
7373
beneficiary = _beneficiary;
7474
}
75-
require(externalLockers[beneficiary] == false, "claiming twice is not allowed");
75+
require(externalLockers[beneficiary] == false, "claiming twice for the same beneficiary is not allowed");
7676
externalLockers[beneficiary] = true;
7777
// solium-disable-next-line security/no-low-level-calls
7878
bool result = externalLockingContract.call(abi.encodeWithSignature(getBalanceFuncSignature, beneficiary));
@@ -86,7 +86,7 @@ contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
8686
default { lockedAmount := mload(0) }
8787
}
8888

89-
return super._lock(lockedAmount, 1, beneficiary);
89+
return super._lock(lockedAmount, 1, beneficiary,1,1);
9090
}
9191

9292
/**

contracts/schemes/Locking4Reputation.sol

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ contract Locking4Reputation {
8686
* @param _amount the amount to lock
8787
* @param _period the locking period
8888
* @param _locker the locker
89+
* @param _numerator price numerator
90+
* @param _denominator price denominator
8991
* @return lockingId
9092
*/
91-
function _lock(uint _amount, uint _period, address _locker) internal returns(bytes32 lockingId) {
93+
function _lock(uint _amount, uint _period, address _locker,uint _numerator,uint _denominator) internal returns(bytes32 lockingId) {
9294
require(_amount > 0, "locking amount should be > 0");
9395
require(_period <= maxLockingPeriod, "locking period should be <= maxLockingPeriod");
9496
require(_period > 0, "locking period should be > 0");
@@ -106,7 +108,8 @@ contract Locking4Reputation {
106108
locker.releaseTime = now + _period;
107109
totalLocked += _amount;
108110
totalLockedLeft = totalLocked;
109-
uint score = _period.mul(_amount);
111+
uint score = _period.mul(_amount).mul(_numerator).div(_denominator);
112+
require(score>0,"score must me > 0");
110113
scores[_locker] = scores[_locker].add(score);
111114
totalScore = totalScore.add(score);
112115

contracts/schemes/LockingEth4Reputation.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ contract LockingEth4Reputation is Locking4Reputation, Ownable {
6060
* @return lockingId the unique Id
6161
*/
6262
function lock(uint _period) public payable returns(bytes32 lockingId) {
63-
return super._lock(msg.value, _period, msg.sender);
63+
return super._lock(msg.value, _period, msg.sender,1,1);
6464
}
6565

6666
}

contracts/schemes/LockingToken4Reputation.sol

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
pragma solidity ^0.4.25;
22

33
import "./Locking4Reputation.sol";
4+
import "./PriceOracleInterface.sol";
45
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
6+
import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
57

68

79
/**
810
* @title A scheme for locking ERC20 Tokens for reputation
911
*/
1012

1113
contract LockingToken4Reputation is Locking4Reputation, Ownable {
12-
StandardToken public token;
14+
15+
PriceOracleInterface public priceOracleContract;
16+
// lockingId => token
17+
mapping(bytes32 => StandardToken) public lockedTokens;
18+
19+
event LockToken(bytes32 indexed _lockingId, address indexed _token, uint _numerator, uint _denominator);
1320

1421
/**
1522
* @dev initialize
@@ -22,7 +29,8 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
2229
* @param _redeemEnableTime redeem enable time .
2330
* redeem reputation can be done after this time.
2431
* @param _maxLockingPeriod maximum locking period allowed.
25-
* @param _token the locking token
32+
* @param _priceOracleContract the price oracle contract which the locked token will be
33+
* validated against
2634
*/
2735
function initialize(
2836
Avatar _avatar,
@@ -31,11 +39,11 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
3139
uint _lockingEndTime,
3240
uint _redeemEnableTime,
3341
uint _maxLockingPeriod,
34-
StandardToken _token)
42+
PriceOracleInterface _priceOracleContract)
3543
external
3644
onlyOwner
3745
{
38-
token = _token;
46+
priceOracleContract = _priceOracleContract;
3947
super._initialize(
4048
_avatar,
4149
_reputationReward,
@@ -53,7 +61,7 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
5361
*/
5462
function release(address _beneficiary,bytes32 _lockingId) public returns(bool) {
5563
uint amount = super._release(_beneficiary, _lockingId);
56-
require(token.transfer(_beneficiary, amount), "transfer should success");
64+
require(lockedTokens[_lockingId].transfer(_beneficiary, amount), "transfer should success");
5765

5866
return true;
5967
}
@@ -62,12 +70,25 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
6270
* @dev lock function
6371
* @param _amount the amount to lock
6472
* @param _period the locking period
73+
* @param _token the token to lock - this should be whitelisted at the priceOracleContract
6574
* @return lockingId
6675
*/
67-
function lock(uint _amount, uint _period) public returns(bytes32) {
68-
require(token.transferFrom(msg.sender, address(this), _amount), "transferFrom should success");
76+
function lock(uint _amount, uint _period,StandardToken _token) public returns(bytes32 lockingId) {
6977

70-
return super._lock(_amount, _period, msg.sender);
71-
}
78+
uint numerator;
79+
uint denominator;
80+
81+
(numerator,denominator) = priceOracleContract.getPrice(address(_token));
82+
83+
require(numerator > 0,"numerator should be > 0");
84+
require(denominator > 0,"denominator should be > 0");
7285

86+
require(_token.transferFrom(msg.sender, address(this), _amount), "transferFrom should success");
87+
88+
lockingId = super._lock(_amount, _period, msg.sender,numerator,denominator);
89+
90+
lockedTokens[lockingId] = _token;
91+
92+
emit LockToken(lockingId,address(_token),numerator,denominator);
93+
}
7394
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pragma solidity ^0.4.25;
2+
3+
interface PriceOracleInterface {
4+
5+
function getPrice(address token) external view returns (uint, uint);
6+
7+
}

contracts/test/PriceOracleMock.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.4.25;
2+
3+
import "../schemes/PriceOracleInterface.sol";
4+
5+
6+
contract PriceOracleMock is PriceOracleInterface {
7+
8+
struct Price {
9+
uint numerator;
10+
uint denominator;
11+
}
12+
// user => amount
13+
mapping (address => Price) public tokenPrices;
14+
15+
16+
function getPrice(address token) public view returns (uint, uint) {
17+
Price memory price = tokenPrices[token];
18+
return (price.numerator, price.denominator);
19+
}
20+
21+
function setTokenPrice(address token,uint numerator,uint denominator) public {
22+
tokenPrices[token] = Price(numerator,denominator);
23+
}
24+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"solc": "^0.4.24",
5656
"solc-cli": "^0.3.0",
5757
"solium": "^1.1.8",
58-
"truffle": "beta",
58+
"truffle": "5.0.0-beta.1",
5959
"uint32": "^0.2.1"
6060
},
6161
"repository": {

0 commit comments

Comments
 (0)