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

Commit a1dd3be

Browse files
authored
Merge pull request #351 from maticnetwork/pre-upgrade-fixes
Pre upgrade fixes
2 parents 47c585f + f561c21 commit a1dd3be

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

contracts/staking/EventsHub.sol

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,33 @@ contract EventsHub is Initializable {
4242
address indexed user,
4343
uint256 indexed amount,
4444
uint256 tokens,
45-
uint256 burnId
45+
uint256 nonce
4646
);
4747

4848
function logShareBurnedWithId(
4949
uint256 validatorId,
5050
address user,
5151
uint256 amount,
5252
uint256 tokens,
53-
uint256 burnId
53+
uint256 nonce
5454
) public onlyValidatorContract(validatorId) {
55-
emit ShareBurnedWithId(validatorId, user, amount, tokens, burnId);
55+
emit ShareBurnedWithId(validatorId, user, amount, tokens, nonce);
56+
}
57+
58+
event DelegatorUnstakeWithId(
59+
uint256 indexed validatorId,
60+
address indexed user,
61+
uint256 amount,
62+
uint256 nonce
63+
);
64+
65+
function logDelegatorUnstakedWithId(
66+
uint256 validatorId,
67+
address user,
68+
uint256 amount,
69+
uint256 nonce
70+
) public onlyValidatorContract(validatorId) {
71+
emit DelegatorUnstakeWithId(validatorId, user, amount, nonce);
5672
}
5773

5874
event RewardParams(

contracts/staking/stakeManager/StakeManager.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ contract StakeManager is
107107
delegationEnabled = true;
108108
}
109109

110+
function isOwner() public view returns (bool) {
111+
address _owner;
112+
bytes32 position = keccak256("matic.network.proxy.owner");
113+
assembly {
114+
_owner := sload(position)
115+
}
116+
return msg.sender == _owner;
117+
}
118+
110119
/**
111120
Public View Methods
112121
*/

contracts/staking/validatorShare/ValidatorShare.sol

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
2020
// 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
2121
uint256 constant EXCHANGE_RATE_HIGH_PRECISION = 10**29;
2222
uint256 constant MAX_COMMISION_RATE = 100;
23+
uint256 constant REWARD_PRECISION = 10**25;
2324

2425
StakingInfo public stakingLogger;
2526
IStakeManager public stakeManager;
@@ -186,8 +187,9 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
186187

187188
function unstakeClaimTokens() public {
188189
DelegatorUnbond memory unbond = unbonds[msg.sender];
189-
_unstakeClaimTokens(unbond);
190+
uint256 amount = _unstakeClaimTokens(unbond);
190191
delete unbonds[msg.sender];
192+
stakingLogger.logDelegatorUnstaked(validatorId, msg.sender, amount);
191193
}
192194

193195
function slash(
@@ -251,8 +253,9 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
251253

252254
function unstakeClaimTokens_new(uint256 unbondNonce) public {
253255
DelegatorUnbond memory unbond = unbonds_new[msg.sender][unbondNonce];
254-
_unstakeClaimTokens(unbond);
256+
uint256 amount = _unstakeClaimTokens(unbond);
255257
delete unbonds_new[msg.sender][unbondNonce];
258+
_getOrCacheEventsHub().logDelegatorUnstakedWithId(validatorId, msg.sender, amount, unbondNonce);
256259
}
257260

258261
/**
@@ -291,7 +294,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
291294
return (shares, _withdrawPoolShare);
292295
}
293296

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

305308
require(stakeManager.transferFunds(validatorId, _amount, msg.sender), "Insufficent rewards");
306-
stakingLogger.logDelegatorUnstaked(validatorId, msg.sender, _amount);
309+
310+
return _amount;
307311
}
308312

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

323327
if (totalShares != 0) {
324-
_rewardPerShare = _rewardPerShare.add(accumulatedReward.mul(_getRatePrecision()).div(totalShares));
328+
_rewardPerShare = _rewardPerShare.add(accumulatedReward.mul(REWARD_PRECISION).div(totalShares));
325329
}
326330
}
327331

@@ -340,7 +344,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
340344
return 0;
341345
}
342346

343-
return _rewardPerShare.sub(_initialRewardPerShare).mul(shares).div(_getRatePrecision());
347+
return _rewardPerShare.sub(_initialRewardPerShare).mul(shares).div(REWARD_PRECISION);
344348
}
345349

346350
function _withdrawReward(address user) private returns (uint256) {

test/units/staking/ValidatorShare.test.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ contract('ValidatorShare', async function() {
750750
userTotalStaked,
751751
totalStaked,
752752
shares,
753-
burnId,
753+
nonce,
754754
withdrawalExchangeRate = ExchangeRatePrecision
755755
}) {
756756
if (minClaimAmount) {
@@ -763,14 +763,14 @@ contract('ValidatorShare', async function() {
763763
})
764764
}
765765

766-
if (burnId) {
766+
if (nonce) {
767767
it('must emit ShareBurnedWithId', async function() {
768768
await expectEvent.inTransaction(this.receipt.tx, EventsHub, 'ShareBurnedWithId', {
769769
validatorId: validatorId,
770770
tokens: shares,
771771
amount: returnedStake,
772772
user: user,
773-
burnId
773+
nonce
774774
})
775775
})
776776
} else {
@@ -1006,7 +1006,7 @@ contract('ValidatorShare', async function() {
10061006
validatorId: '8',
10071007
user: Alice,
10081008
userTotalStaked: halfStake,
1009-
burnId: '1',
1009+
nonce: '1',
10101010
totalStaked: halfStake.add(ValidatorDefaultStake)
10111011
})
10121012
})
@@ -1025,7 +1025,7 @@ contract('ValidatorShare', async function() {
10251025
validatorId: '8',
10261026
user: Alice,
10271027
userTotalStaked: '0',
1028-
burnId: '2',
1028+
nonce: '2',
10291029
totalStaked: ValidatorDefaultStake
10301030
})
10311031
})
@@ -1049,7 +1049,7 @@ contract('ValidatorShare', async function() {
10491049
initialBalance: new BN(0),
10501050
validatorId: '8',
10511051
user: Alice,
1052-
burnId: '1',
1052+
nonce: '1',
10531053
userTotalStaked: halfStake,
10541054
totalStaked: halfStake.add(validatorHalfStake)
10551055
})
@@ -1069,7 +1069,7 @@ contract('ValidatorShare', async function() {
10691069
validatorId: '8',
10701070
user: Alice,
10711071
userTotalStaked: '0',
1072-
burnId: '2',
1072+
nonce: '2',
10731073
totalStaked: validatorHalfStake
10741074
})
10751075
})
@@ -1785,11 +1785,12 @@ contract('ValidatorShare', async function() {
17851785
})
17861786
})
17871787

1788-
it('must emit DelegatorUnstaked', async function() {
1789-
await expectEvent.inTransaction(this.receipt.tx, StakingInfo, 'DelegatorUnstaked', {
1788+
it('must emit DelegatorUnstakeWithId', async function() {
1789+
await expectEvent.inTransaction(this.receipt.tx, EventsHub, 'DelegatorUnstakeWithId', {
17901790
validatorId: this.validatorId,
17911791
user: this.user,
1792-
amount: this.claimAmount
1792+
amount: this.claimAmount,
1793+
nonce: '1'
17931794
})
17941795
})
17951796

@@ -1799,11 +1800,12 @@ contract('ValidatorShare', async function() {
17991800
})
18001801
})
18011802

1802-
it('must emit DelegatorUnstaked', async function() {
1803-
await expectEvent.inTransaction(this.receipt.tx, StakingInfo, 'DelegatorUnstaked', {
1803+
it('must emit DelegatorUnstakeWithId', async function() {
1804+
await expectEvent.inTransaction(this.receipt.tx, EventsHub, 'DelegatorUnstakeWithId', {
18041805
validatorId: this.validatorId,
18051806
user: this.user,
1806-
amount: this.claimAmount
1807+
amount: this.claimAmount,
1808+
nonce: '2'
18071809
})
18081810
})
18091811

0 commit comments

Comments
 (0)