Skip to content

Commit

Permalink
lockingtoken4reputation.sol : multi token support (#563)
Browse files Browse the repository at this point in the history
* lockingtoken4reputation.sol : multi token support

* use truffle "5.0.0-beta.1"
  • Loading branch information
orenyodfat authored Nov 27, 2018
1 parent dcaed7e commit b6e6341
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 37 deletions.
4 changes: 2 additions & 2 deletions contracts/schemes/ExternalLocking4Reputation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
require(registrar[_beneficiary],"beneficiary should be register");
beneficiary = _beneficiary;
}
require(externalLockers[beneficiary] == false, "claiming twice is not allowed");
require(externalLockers[beneficiary] == false, "claiming twice for the same beneficiary is not allowed");
externalLockers[beneficiary] = true;
// solium-disable-next-line security/no-low-level-calls
bool result = externalLockingContract.call(abi.encodeWithSignature(getBalanceFuncSignature, beneficiary));
Expand All @@ -86,7 +86,7 @@ contract ExternalLocking4Reputation is Locking4Reputation, Ownable {
default { lockedAmount := mload(0) }
}

return super._lock(lockedAmount, 1, beneficiary);
return super._lock(lockedAmount, 1, beneficiary,1,1);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions contracts/schemes/Locking4Reputation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ contract Locking4Reputation {
* @param _amount the amount to lock
* @param _period the locking period
* @param _locker the locker
* @param _numerator price numerator
* @param _denominator price denominator
* @return lockingId
*/
function _lock(uint _amount, uint _period, address _locker) internal returns(bytes32 lockingId) {
function _lock(uint _amount, uint _period, address _locker,uint _numerator,uint _denominator) internal returns(bytes32 lockingId) {
require(_amount > 0, "locking amount should be > 0");
require(_period <= maxLockingPeriod, "locking period should be <= maxLockingPeriod");
require(_period > 0, "locking period should be > 0");
Expand All @@ -106,7 +108,8 @@ contract Locking4Reputation {
locker.releaseTime = now + _period;
totalLocked += _amount;
totalLockedLeft = totalLocked;
uint score = _period.mul(_amount);
uint score = _period.mul(_amount).mul(_numerator).div(_denominator);
require(score>0,"score must me > 0");
scores[_locker] = scores[_locker].add(score);
totalScore = totalScore.add(score);

Expand Down
2 changes: 1 addition & 1 deletion contracts/schemes/LockingEth4Reputation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ contract LockingEth4Reputation is Locking4Reputation, Ownable {
* @return lockingId the unique Id
*/
function lock(uint _period) public payable returns(bytes32 lockingId) {
return super._lock(msg.value, _period, msg.sender);
return super._lock(msg.value, _period, msg.sender,1,1);
}

}
39 changes: 30 additions & 9 deletions contracts/schemes/LockingToken4Reputation.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
pragma solidity ^0.4.25;

import "./Locking4Reputation.sol";
import "./PriceOracleInterface.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";


/**
* @title A scheme for locking ERC20 Tokens for reputation
*/

contract LockingToken4Reputation is Locking4Reputation, Ownable {
StandardToken public token;

PriceOracleInterface public priceOracleContract;
// lockingId => token
mapping(bytes32 => StandardToken) public lockedTokens;

event LockToken(bytes32 indexed _lockingId, address indexed _token, uint _numerator, uint _denominator);

/**
* @dev initialize
Expand All @@ -22,7 +29,8 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
* @param _redeemEnableTime redeem enable time .
* redeem reputation can be done after this time.
* @param _maxLockingPeriod maximum locking period allowed.
* @param _token the locking token
* @param _priceOracleContract the price oracle contract which the locked token will be
* validated against
*/
function initialize(
Avatar _avatar,
Expand All @@ -31,11 +39,11 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
uint _lockingEndTime,
uint _redeemEnableTime,
uint _maxLockingPeriod,
StandardToken _token)
PriceOracleInterface _priceOracleContract)
external
onlyOwner
{
token = _token;
priceOracleContract = _priceOracleContract;
super._initialize(
_avatar,
_reputationReward,
Expand All @@ -53,7 +61,7 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
*/
function release(address _beneficiary,bytes32 _lockingId) public returns(bool) {
uint amount = super._release(_beneficiary, _lockingId);
require(token.transfer(_beneficiary, amount), "transfer should success");
require(lockedTokens[_lockingId].transfer(_beneficiary, amount), "transfer should success");

return true;
}
Expand All @@ -62,12 +70,25 @@ contract LockingToken4Reputation is Locking4Reputation, Ownable {
* @dev lock function
* @param _amount the amount to lock
* @param _period the locking period
* @param _token the token to lock - this should be whitelisted at the priceOracleContract
* @return lockingId
*/
function lock(uint _amount, uint _period) public returns(bytes32) {
require(token.transferFrom(msg.sender, address(this), _amount), "transferFrom should success");
function lock(uint _amount, uint _period,StandardToken _token) public returns(bytes32 lockingId) {

return super._lock(_amount, _period, msg.sender);
}
uint numerator;
uint denominator;

(numerator,denominator) = priceOracleContract.getPrice(address(_token));

require(numerator > 0,"numerator should be > 0");
require(denominator > 0,"denominator should be > 0");

require(_token.transferFrom(msg.sender, address(this), _amount), "transferFrom should success");

lockingId = super._lock(_amount, _period, msg.sender,numerator,denominator);

lockedTokens[lockingId] = _token;

emit LockToken(lockingId,address(_token),numerator,denominator);
}
}
7 changes: 7 additions & 0 deletions contracts/schemes/PriceOracleInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity ^0.4.25;

interface PriceOracleInterface {

function getPrice(address token) external view returns (uint, uint);

}
24 changes: 24 additions & 0 deletions contracts/test/PriceOracleMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma solidity ^0.4.25;

import "../schemes/PriceOracleInterface.sol";


contract PriceOracleMock is PriceOracleInterface {

struct Price {
uint numerator;
uint denominator;
}
// user => amount
mapping (address => Price) public tokenPrices;


function getPrice(address token) public view returns (uint, uint) {
Price memory price = tokenPrices[token];
return (price.numerator, price.denominator);
}

function setTokenPrice(address token,uint numerator,uint denominator) public {
tokenPrices[token] = Price(numerator,denominator);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"solc": "^0.4.24",
"solc-cli": "^0.3.0",
"solium": "^1.1.8",
"truffle": "beta",
"truffle": "5.0.0-beta.1",
"uint32": "^0.2.1"
},
"repository": {
Expand Down
Loading

0 comments on commit b6e6341

Please sign in to comment.