Skip to content

Commit

Permalink
[ValidatorSet] Add details for StakingRewardDistributed event (#133)
Browse files Browse the repository at this point in the history
* Fix contract

* Fix test
  • Loading branch information
nxqbao authored Dec 28, 2022
1 parent c93c6ae commit 463022d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
9 changes: 7 additions & 2 deletions contracts/interfaces/validator/ICoinbaseExecution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ interface ICoinbaseExecution is ISlashingExecution {
);

/// @dev Emitted when the amount of RON reward is distributed to staking contract.
event StakingRewardDistributed(uint256 amount);
event StakingRewardDistributed(uint256 totalAmount, address[] consensusAddrs, uint256[] amounts);
/// @dev Emitted when the contracts fails when distributing the amount of RON to the staking contract.
event StakingRewardDistributionFailed(uint256 amount, uint256 contractBalance);
event StakingRewardDistributionFailed(
uint256 totalAmount,
address[] consensusAddrs,
uint256[] amounts,
uint256 contractBalance
);

/// @dev Emitted when the epoch is wrapped up.
event WrappedUpEpoch(uint256 indexed periodNumber, uint256 indexed epochNumber, bool periodEnding);
Expand Down
9 changes: 7 additions & 2 deletions contracts/ronin/validator/CoinbaseExecution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,16 @@ abstract contract CoinbaseExecution is
if (_totalDelegatingReward > 0) {
if (_unsafeSendRON(payable(address(_staking)), _totalDelegatingReward)) {
_staking.recordRewards(_currentValidators, _delegatingRewards, _period);
emit StakingRewardDistributed(_totalDelegatingReward);
emit StakingRewardDistributed(_totalDelegatingReward, _currentValidators, _delegatingRewards);
return;
}

emit StakingRewardDistributionFailed(_totalDelegatingReward, address(this).balance);
emit StakingRewardDistributionFailed(
_totalDelegatingReward,
_currentValidators,
_delegatingRewards,
address(this).balance
);
}
}

Expand Down
15 changes: 13 additions & 2 deletions test/helpers/ronin-validator-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,24 @@ export const expects = {
);
},

emitStakingRewardDistributedEvent: async function (tx: ContractTransaction, expectingAmount: BigNumberish) {
emitStakingRewardDistributedEvent: async function (
tx: ContractTransaction,
expectingTotalAmount: BigNumberish,
expectingValidators: string[] | undefined,
expectingAmounts: BigNumberish[] | undefined
) {
await expectEvent(
contractInterface,
'StakingRewardDistributed',
tx,
(event) => {
expect(event.args[0], 'invalid distributing reward').eq(expectingAmount);
expect(event.args[0], 'invalid total distributing reward').eq(expectingTotalAmount);
if (expectingValidators) {
expect(event.args[1], 'invalid validator list').eql(expectingValidators);
}
if (expectingAmounts) {
expect(event.args[2], 'invalid amount list').eql(expectingAmounts);
}
},
1
);
Expand Down
11 changes: 9 additions & 2 deletions test/validator/RoninValidatorSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ describe('Ronin Validator Set test', () => {
await expect(tx!).emit(roninValidatorSet, 'WrappedUpEpoch').withArgs(lastPeriod, epoch, true);
lastPeriod = await roninValidatorSet.currentPeriod();
await RoninValidatorSet.expects.emitValidatorSetUpdatedEvent(tx!, lastPeriod, currentValidatorSet);
await RoninValidatorSet.expects.emitStakingRewardDistributedEvent(tx!, 5148); // (5000 + 100 + 100) * 99%
await RoninValidatorSet.expects.emitStakingRewardDistributedEvent(
tx!,
5148,
currentValidatorSet,
[5148, 0, 0, 0].map((_) => BigNumber.from(_))
); // (5000 + 100 + 100) * 99%
await RoninValidatorSet.expects.emitMiningRewardDistributedEvent(
tx!,
consensusAddr.address,
Expand Down Expand Up @@ -521,7 +526,9 @@ describe('Ronin Validator Set test', () => {
await RoninValidatorSet.expects.emitValidatorSetUpdatedEvent(tx!, lastPeriod, currentValidatorSet);
await RoninValidatorSet.expects.emitStakingRewardDistributedEvent(
tx!,
blockProducerBonusPerBlock.add(100).div(100).mul(99)
blockProducerBonusPerBlock.add(100).div(100).mul(99),
currentValidatorSet,
[blockProducerBonusPerBlock.add(100).div(100).mul(99), 0, 0, 0].map((_) => BigNumber.from(_))
);

const balanceDiff = (await treasury.getBalance()).sub(balance);
Expand Down

0 comments on commit 463022d

Please sign in to comment.