Skip to content

Commit

Permalink
Merge 1c638ed into 6299f96
Browse files Browse the repository at this point in the history
  • Loading branch information
danoctavian committed May 12, 2020
2 parents 6299f96 + 1c638ed commit b2cd6cf
Show file tree
Hide file tree
Showing 21 changed files with 1,462 additions and 813 deletions.
49 changes: 7 additions & 42 deletions contracts/ClaimsReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import "./ClaimsData.sol";
import "./Governance.sol";
import "./Claims.sol";
import "./Pool1.sol";
import "./interfaces/IPooledStaking.sol";


contract ClaimsReward is Iupgradable {
Expand All @@ -38,6 +39,7 @@ contract ClaimsReward is Iupgradable {
Pool2 internal p2;
PoolData internal pd;
Governance internal gv;
IPooledStaking internal pooledStaking;

uint private constant DECIMAL1E18 = uint(10) ** 18;

Expand All @@ -53,6 +55,7 @@ contract ClaimsReward is Iupgradable {
pd = PoolData(ms.getLatestAddress("PD"));
qd = QuotationData(ms.getLatestAddress("QD"));
gv = Governance(ms.getLatestAddress("GV"));
pooledStaking = IPooledStaking(ms.getLatestAddress("PS"));
}

/// @dev Decides the next course of action for a given claim.
Expand Down Expand Up @@ -207,8 +210,6 @@ contract ClaimsReward is Iupgradable {
*/
function claimAllPendingReward(uint records) public isMemberAndcheckPause {
_claimRewardToBeDistributed(records);
_claimStakeCommission(records);
tf.unlockStakerUnlockableTokens(msg.sender);
uint gvReward = gv.claimReward(msg.sender, records);
if (gvReward > 0) {
require(tk.transfer(msg.sender, gvReward));
Expand All @@ -221,13 +222,12 @@ contract ClaimsReward is Iupgradable {
* @return total reward amount of the user
*/
function getAllPendingRewardOfUser(address _add) public view returns(uint total) {
require(!pooledStaking.hasPendingActions(), "Pooled staking actions are not all processed yet.");
uint caReward = getRewardToBeDistributedByUser(_add);
uint commissionEarned = td.getStakerTotalEarnedStakeCommission(_add);
uint commissionReedmed = td.getStakerTotalReedmedStakeCommission(_add);
uint unlockableStakedTokens = tf.getStakerAllUnlockableStakedTokens(_add);
uint pooledStakingReward = pooledStaking.stakerReward(_add);
uint stakedTokens = pooledStaking.stakerStake(_add);
uint governanceReward = gv.getPendingReward(_add);
total = caReward.add(unlockableStakedTokens).add(commissionEarned.
sub(commissionReedmed)).add(governanceReward);
total = caReward.add(stakedTokens).add(pooledStakingReward).add(governanceReward);
}

/// @dev Rewards/Punishes users who participated in Claims assessment.
Expand Down Expand Up @@ -434,39 +434,4 @@ contract ClaimsReward is Iupgradable {
else
cd.setRewardDistributedIndexMV(msg.sender, lastClaimed);
}

/**
* @dev Function used to claim the commission earned by the staker.
*/
function _claimStakeCommission(uint _records) internal {
uint total=0;
uint len = td.getStakerStakedContractLength(msg.sender);
uint lastCompletedStakeCommission = td.lastCompletedStakeCommission(msg.sender);
uint commissionEarned;
uint commissionRedeemed;
uint maxCommission;
uint lastCommisionRedeemed = len;
uint counter;
uint i;

for (i = lastCompletedStakeCommission; i < len && counter < _records; i++) {
commissionRedeemed = td.getStakerRedeemedStakeCommission(msg.sender, i);
commissionEarned = td.getStakerEarnedStakeCommission(msg.sender, i);
maxCommission = td.getStakerInitialStakedAmountOnContract(
msg.sender, i).mul(td.stakerMaxCommissionPer()).div(100);
if (lastCommisionRedeemed == len && maxCommission != commissionEarned)
lastCommisionRedeemed = i;
td.pushRedeemedStakeCommissions(msg.sender, i, commissionEarned.sub(commissionRedeemed));
total = total.add(commissionEarned.sub(commissionRedeemed));
counter++;
}
if(lastCommisionRedeemed == len)
td.setLastCompletedStakeCommissionIndex(msg.sender, i);
else
td.setLastCompletedStakeCommissionIndex(msg.sender, lastCommisionRedeemed);

if (total > 0)
require(tk.transfer(msg.sender, total)); //solhint-disable-line

}
}
1 change: 1 addition & 0 deletions contracts/Quotation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ contract Quotation is Iupgradable {
tf.lockCN(coverNoteAmount, coverPeriod, cid, from);
qd.addInTotalSumAssured(coverCurr, coverDetails[0]);
qd.addInTotalSumAssuredSC(scAddress, coverCurr, coverDetails[0]);

if (tf.getTotalStakedTokensOnSmartContract(scAddress) > 0)
tf.updateStakerCommissions(scAddress, coverDetails[2]);

Expand Down
81 changes: 44 additions & 37 deletions contracts/TokenController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pragma solidity 0.5.7;
import "./Iupgradable.sol";
import "./external/ERC1132/IERC1132.sol";
import "./NXMToken.sol";
import "./interfaces/IPooledStaking.sol";


contract TokenController is IERC1132, Iupgradable {
Expand All @@ -26,22 +27,24 @@ contract TokenController is IERC1132, Iupgradable {
event Burned(address indexed member, bytes32 lockedUnder, uint256 amount);

NXMToken public token;

IPooledStaking public pooledStaking;

/**
* @dev Just for interface
*/
function changeDependentContractAddress() public {
token = NXMToken(ms.tokenAddress());
pooledStaking = IPooledStaking(ms.getLatestAddress('PS'));
}

/**
* @dev to change the operator address
* @dev to change the operator address
* @param _newOperator is the new address of operator
*/
function changeOperator(address _newOperator) public onlyInternal {
token.changeOperator(_newOperator);
}

/**
* @dev Locks a specified amount of tokens,
* for a specified reason and time
Expand Down Expand Up @@ -75,7 +78,7 @@ contract TokenController is IERC1132, Iupgradable {
_lock(_of, _reason, _amount, _time);
return true;
}

/**
* @dev Extends lock for a specified reason and time
* @param _reason The reason to lock tokens
Expand All @@ -102,7 +105,7 @@ contract TokenController is IERC1132, Iupgradable {
_extendLock(_of, _reason, _time);
return true;
}

/**
* @dev Increase number of tokens locked for a specified reason
* @param _reason The reason to lock tokens
Expand All @@ -111,23 +114,23 @@ contract TokenController is IERC1132, Iupgradable {
function increaseLockAmount(bytes32 _reason, uint256 _amount)
public
returns (bool)
{
{
_increaseLockAmount(msg.sender, _reason, _amount);
return true;
}

/**
* @dev burns tokens of an address
* @dev burns tokens of an address
* @param _of is the address to burn tokens of
* @param amount is the amount to burn
* @return the boolean status of the burning process
*/
function burnFrom (address _of, uint amount) public onlyInternal returns (bool) {
return token.burnFrom(_of, amount);
}

/**
* @dev Burns locked tokens of a user
* @dev Burns locked tokens of a user
* @param _of address whose tokens are to be burned
* @param _reason lock reason for which tokens are to be burned
* @param _amount amount of tokens to burn
Expand All @@ -144,17 +147,17 @@ contract TokenController is IERC1132, Iupgradable {
*/
function reduceLock(address _of, bytes32 _reason, uint256 _time) public onlyInternal {
_reduceLock(_of, _reason, _time);
}
}

/**
* @dev Released locked tokens of an address locked for a specific reason
* @param _of address whose tokens are to be released from lock
* @param _reason reason of the lock
* @param _amount amount of tokens to release
*/
function releaseLockedTokens(address _of, bytes32 _reason, uint256 _amount)
public
onlyInternal
function releaseLockedTokens(address _of, bytes32 _reason, uint256 _amount)
public
onlyInternal
{
_releaseLockedTokens(_of, _reason, _amount);
}
Expand Down Expand Up @@ -185,7 +188,7 @@ contract TokenController is IERC1132, Iupgradable {
}

/**
* @dev Lock the user's tokens
* @dev Lock the user's tokens
* @param _of user's address.
*/
function lockForMemberVote(address _of, uint _days) public onlyInternal {
Expand All @@ -209,7 +212,7 @@ contract TokenController is IERC1132, Iupgradable {
locked[_of][lockReason[_of][i]].claimed = true;
emit Unlocked(_of, lockReason[_of][i], lockedTokens);
}
}
}

if (unlockableTokens > 0)
require(token.transfer(_of, unlockableTokens));
Expand All @@ -218,7 +221,7 @@ contract TokenController is IERC1132, Iupgradable {
/**
* @dev Gets the validity of locked tokens of a specified address
* @param _of The address to query the validity
* @param reason reason for which tokens were locked
* @param reason reason for which tokens were locked
*/
function getLockedTokensValidity(address _of, bytes32 reason)
public
Expand All @@ -239,7 +242,7 @@ contract TokenController is IERC1132, Iupgradable {
{
for (uint256 i = 0; i < lockReason[_of].length; i++) {
unlockableTokens = unlockableTokens.add(_tokensUnlockable(_of, lockReason[_of][i]));
}
}
}

/**
Expand Down Expand Up @@ -292,7 +295,11 @@ contract TokenController is IERC1132, Iupgradable {
}

/**
* @dev Returns total tokens held by an address (locked + transferable)
* @dev Returns the total amount of tokens held by an address:
* transferable + locked + staked for pooled staking - pending burns.
* Used by Claims and Governance in member voting to calculate the user's vote weight.
*
* @param _of The address to query the total balance of
* @param _of The address to query the total balance of
*/
function totalBalanceOf(address _of)
Expand All @@ -304,28 +311,28 @@ contract TokenController is IERC1132, Iupgradable {

for (uint256 i = 0; i < lockReason[_of].length; i++) {
amount = amount.add(_tokensLocked(_of, lockReason[_of][i]));
}
}

amount = amount.add(pooledStaking.stakerProcessedStake(_of));
}

/**
* @dev Returns the total locked tokens at time
* Returns the total amount of locked and staked tokens at a given time. Used by MemberRoles to check eligibility
* for withdraw / switch membership. Includes tokens locked for Claim Assessment and staked for Risk Assessment.
* Does not take into account pending burns.
*
* @param _of member whose locked tokens are to be calculate
* @param _time timestamp when the tokens should be locked
*/
function totalLockedBalance(address _of, uint256 _time) public view returns (uint256 amount) {
amount = _totalLockedBalance(_of, _time);
}

/**
* @dev Internal function to returns the total locked tokens at time
* @param _of member whose locked tokens are to be calculate
* @param _time timestamp when the tokens should be locked
*/
function _totalLockedBalance(address _of, uint256 _time) internal view returns (uint256 amount) {
for (uint256 i = 0; i < lockReason[_of].length; i++) {
amount = amount.add(_tokensLockedAtTime(_of, lockReason[_of][i], _time));
}
}

amount = amount.add(pooledStaking.stakerStake(_of));
}

/**
* @dev Locks a specified amount of tokens against an address,
Expand All @@ -348,7 +355,7 @@ contract TokenController is IERC1132, Iupgradable {
locked[_of][_reason] = LockToken(_amount, validUntil, false);
emit Locked(_of, _reason, _amount, validUntil);
}

/**
* @dev Returns tokens locked for a specified address for a
* specified reason
Expand Down Expand Up @@ -381,7 +388,7 @@ contract TokenController is IERC1132, Iupgradable {
if (locked[_of][_reason].validity > _time)
amount = locked[_of][_reason].amount;
}

/**
* @dev Extends lock for a specified reason and time
* @param _of The address whose tokens are locked
Expand All @@ -407,7 +414,7 @@ contract TokenController is IERC1132, Iupgradable {
locked[_of][_reason].validity = locked[_of][_reason].validity.sub(_time);
emit Locked(_of, _reason, locked[_of][_reason].amount, locked[_of][_reason].validity);
}

/**
* @dev Increase number of tokens locked for a specified reason
* @param _of The address whose tokens are locked
Expand All @@ -434,18 +441,18 @@ contract TokenController is IERC1132, Iupgradable {
}

/**
* @dev Burns locked tokens of a user
* @dev Burns locked tokens of a user
* @param _of address whose tokens are to be burned
* @param _reason lock reason for which tokens are to be burned
* @param _amount amount of tokens to burn
*/
function _burnLockedTokens(address _of, bytes32 _reason, uint256 _amount) internal {
uint256 amount = _tokensLocked(_of, _reason);
require(amount >= _amount);

if (amount == _amount)
locked[_of][_reason].claimed = true;

locked[_of][_reason].amount = locked[_of][_reason].amount.sub(_amount);
token.burn(_amount);
emit Burned(_of, _reason, _amount);
Expand All @@ -457,8 +464,8 @@ contract TokenController is IERC1132, Iupgradable {
* @param _reason reason of the lock
* @param _amount amount of tokens to release
*/
function _releaseLockedTokens(address _of, bytes32 _reason, uint256 _amount)
internal
function _releaseLockedTokens(address _of, bytes32 _reason, uint256 _amount)
internal
{
uint256 amount = _tokensLocked(_of, _reason);
require(amount >= _amount);
Expand All @@ -470,4 +477,4 @@ contract TokenController is IERC1132, Iupgradable {
require(token.transfer(_of, _amount));
emit Unlocked(_of, _reason, _amount);
}
}
}
Loading

0 comments on commit b2cd6cf

Please sign in to comment.