Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #351 from maticnetwork/pre-upgrade-fixes
Browse files Browse the repository at this point in the history
Pre upgrade fixes
  • Loading branch information
jdkanani authored Apr 5, 2021
2 parents 47c585f + f561c21 commit a1dd3be
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
22 changes: 19 additions & 3 deletions contracts/staking/EventsHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,33 @@ contract EventsHub is Initializable {
address indexed user,
uint256 indexed amount,
uint256 tokens,
uint256 burnId
uint256 nonce

This comment has been minimized.

Copy link
@daniel81825

daniel81825 Aug 27, 2021

Accept

);

function logShareBurnedWithId(
uint256 validatorId,
address user,
uint256 amount,
uint256 tokens,
uint256 burnId
uint256 nonce
) public onlyValidatorContract(validatorId) {
emit ShareBurnedWithId(validatorId, user, amount, tokens, burnId);
emit ShareBurnedWithId(validatorId, user, amount, tokens, nonce);
}

event DelegatorUnstakeWithId(
uint256 indexed validatorId,
address indexed user,
uint256 amount,
uint256 nonce

This comment has been minimized.

Copy link
@daniel81825

daniel81825 Aug 27, 2021

Accept

);

function logDelegatorUnstakedWithId(
uint256 validatorId,
address user,
uint256 amount,
uint256 nonce
) public onlyValidatorContract(validatorId) {
emit DelegatorUnstakeWithId(validatorId, user, amount, nonce);
}

event RewardParams(
Expand Down
9 changes: 9 additions & 0 deletions contracts/staking/stakeManager/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ contract StakeManager is
delegationEnabled = true;
}

function isOwner() public view returns (bool) {
address _owner;
bytes32 position = keccak256("matic.network.proxy.owner");
assembly {
_owner := sload(position)
}
return msg.sender == _owner;
}

/**
Public View Methods
*/
Expand Down
16 changes: 10 additions & 6 deletions contracts/staking/validatorShare/ValidatorShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
// maximum matic possible, even if rate will be 1 and all matic will be staken in one go, it will result in 10 ^ 58 shares
uint256 constant EXCHANGE_RATE_HIGH_PRECISION = 10**29;
uint256 constant MAX_COMMISION_RATE = 100;
uint256 constant REWARD_PRECISION = 10**25;

StakingInfo public stakingLogger;
IStakeManager public stakeManager;
Expand Down Expand Up @@ -186,8 +187,9 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I

function unstakeClaimTokens() public {
DelegatorUnbond memory unbond = unbonds[msg.sender];
_unstakeClaimTokens(unbond);
uint256 amount = _unstakeClaimTokens(unbond);
delete unbonds[msg.sender];
stakingLogger.logDelegatorUnstaked(validatorId, msg.sender, amount);
}

function slash(
Expand Down Expand Up @@ -251,8 +253,9 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I

function unstakeClaimTokens_new(uint256 unbondNonce) public {
DelegatorUnbond memory unbond = unbonds_new[msg.sender][unbondNonce];
_unstakeClaimTokens(unbond);
uint256 amount = _unstakeClaimTokens(unbond);
delete unbonds_new[msg.sender][unbondNonce];
_getOrCacheEventsHub().logDelegatorUnstakedWithId(validatorId, msg.sender, amount, unbondNonce);
}

/**
Expand Down Expand Up @@ -291,7 +294,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
return (shares, _withdrawPoolShare);
}

function _unstakeClaimTokens(DelegatorUnbond memory unbond) private {
function _unstakeClaimTokens(DelegatorUnbond memory unbond) private returns(uint256) {
uint256 shares = unbond.shares;
require(
unbond.withdrawEpoch.add(stakeManager.withdrawalDelay()) <= stakeManager.epoch() && shares > 0,
Expand All @@ -303,7 +306,8 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
withdrawPool = withdrawPool.sub(_amount);

require(stakeManager.transferFunds(validatorId, _amount, msg.sender), "Insufficent rewards");
stakingLogger.logDelegatorUnstaked(validatorId, msg.sender, _amount);

return _amount;
}

function _getRatePrecision() private view returns (uint256) {
Expand All @@ -321,7 +325,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
uint256 totalShares = totalSupply();

if (totalShares != 0) {
_rewardPerShare = _rewardPerShare.add(accumulatedReward.mul(_getRatePrecision()).div(totalShares));
_rewardPerShare = _rewardPerShare.add(accumulatedReward.mul(REWARD_PRECISION).div(totalShares));
}
}

Expand All @@ -340,7 +344,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
return 0;
}

return _rewardPerShare.sub(_initialRewardPerShare).mul(shares).div(_getRatePrecision());
return _rewardPerShare.sub(_initialRewardPerShare).mul(shares).div(REWARD_PRECISION);
}

function _withdrawReward(address user) private returns (uint256) {
Expand Down
28 changes: 15 additions & 13 deletions test/units/staking/ValidatorShare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ contract('ValidatorShare', async function() {
userTotalStaked,
totalStaked,
shares,
burnId,
nonce,
withdrawalExchangeRate = ExchangeRatePrecision
}) {
if (minClaimAmount) {
Expand All @@ -763,14 +763,14 @@ contract('ValidatorShare', async function() {
})
}

if (burnId) {
if (nonce) {
it('must emit ShareBurnedWithId', async function() {
await expectEvent.inTransaction(this.receipt.tx, EventsHub, 'ShareBurnedWithId', {
validatorId: validatorId,
tokens: shares,
amount: returnedStake,
user: user,
burnId
nonce
})
})
} else {
Expand Down Expand Up @@ -1006,7 +1006,7 @@ contract('ValidatorShare', async function() {
validatorId: '8',
user: Alice,
userTotalStaked: halfStake,
burnId: '1',
nonce: '1',
totalStaked: halfStake.add(ValidatorDefaultStake)
})
})
Expand All @@ -1025,7 +1025,7 @@ contract('ValidatorShare', async function() {
validatorId: '8',
user: Alice,
userTotalStaked: '0',
burnId: '2',
nonce: '2',
totalStaked: ValidatorDefaultStake
})
})
Expand All @@ -1049,7 +1049,7 @@ contract('ValidatorShare', async function() {
initialBalance: new BN(0),
validatorId: '8',
user: Alice,
burnId: '1',
nonce: '1',
userTotalStaked: halfStake,
totalStaked: halfStake.add(validatorHalfStake)
})
Expand All @@ -1069,7 +1069,7 @@ contract('ValidatorShare', async function() {
validatorId: '8',
user: Alice,
userTotalStaked: '0',
burnId: '2',
nonce: '2',
totalStaked: validatorHalfStake
})
})
Expand Down Expand Up @@ -1785,11 +1785,12 @@ contract('ValidatorShare', async function() {
})
})

it('must emit DelegatorUnstaked', async function() {
await expectEvent.inTransaction(this.receipt.tx, StakingInfo, 'DelegatorUnstaked', {
it('must emit DelegatorUnstakeWithId', async function() {
await expectEvent.inTransaction(this.receipt.tx, EventsHub, 'DelegatorUnstakeWithId', {
validatorId: this.validatorId,
user: this.user,
amount: this.claimAmount
amount: this.claimAmount,
nonce: '1'
})
})

Expand All @@ -1799,11 +1800,12 @@ contract('ValidatorShare', async function() {
})
})

it('must emit DelegatorUnstaked', async function() {
await expectEvent.inTransaction(this.receipt.tx, StakingInfo, 'DelegatorUnstaked', {
it('must emit DelegatorUnstakeWithId', async function() {
await expectEvent.inTransaction(this.receipt.tx, EventsHub, 'DelegatorUnstakeWithId', {
validatorId: this.validatorId,
user: this.user,
amount: this.claimAmount
amount: this.claimAmount,
nonce: '2'
})
})

Expand Down

0 comments on commit a1dd3be

Please sign in to comment.