Skip to content

Commit

Permalink
issue ERC20 staking rewards (native rewards TBD)
Browse files Browse the repository at this point in the history
  • Loading branch information
feuGeneA committed Sep 6, 2024
1 parent 48132e9 commit fc11272
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 4 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions contracts/staking/ERC20TokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ contract ERC20TokenStakingManager is
function _unlock(uint256 value, address to) internal virtual override {
_getERC20StakingManagerStorage()._token.safeTransfer(to, value);
}

function _reward(address account, uint256 amount) internal virtual override {
ERC20TokenStakingManagerStorage storage $ = _getERC20StakingManagerStorage();
$._token.mint(account, amount);
}
}
5 changes: 5 additions & 0 deletions contracts/staking/NativeTokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ contract NativeTokenStakingManager is
function _unlock(uint256 value, address to) internal virtual override {
payable(to).sendValue(value);
}

// solhint-disable-next-line no-empty-blocks
function _reward(address account, uint256 amount) internal virtual override {
// TODO: call the native minter precompile to mint `amount` for `account`
}
}
9 changes: 9 additions & 0 deletions contracts/staking/PoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager

function completeEndValidation(uint32 messageIndex) external {
Validator memory validator = _completeEndValidation(messageIndex);

PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage();
uint256 rewardAmount = $._rewardCalculator.calculateReward(
validator.weight, validator.startedAt, validator.endedAt, 0, 0
);

_reward(validator.owner, rewardAmount);

_unlock(validator.weight, validator.owner);
}

Expand Down Expand Up @@ -136,4 +144,5 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager

function _lock(uint256 value) internal virtual returns (uint256);
function _unlock(uint256 value, address to) internal virtual;
function _reward(address account, uint256 amount) internal virtual;
}
3 changes: 2 additions & 1 deletion contracts/staking/tests/ERC20TokenStakingManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ERC20TokenStakingManager} from "../ERC20TokenStakingManager.sol";
import {ValidatorManagerSettings} from "../interfaces/IValidatorManager.sol";
import {PoSValidatorManagerSettings} from "../interfaces/IPoSValidatorManager.sol";
import {IRewardCalculator} from "../interfaces/IRewardCalculator.sol";
import {ExampleRewardCalculator} from "../ExampleRewardCalculator.sol";
import {ICMInitializable} from "../../utilities/ICMInitializable.sol";
import {ExampleERC20} from "@mocks/ExampleERC20.sol";
import {IERC20Mintable} from "../interfaces/IERC20Mintable.sol";
Expand Down Expand Up @@ -37,7 +38,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
minimumStakeAmount: DEFAULT_MINIMUM_STAKE,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
rewardCalculator: IRewardCalculator(address(0))
rewardCalculator: IRewardCalculator(new ExampleRewardCalculator(DEFAULT_REWARD_RATE))
}),
token
);
Expand Down
3 changes: 2 additions & 1 deletion contracts/staking/tests/NativeTokenStakingManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {NativeTokenStakingManager} from "../NativeTokenStakingManager.sol";
import {ValidatorManagerSettings} from "../interfaces/IValidatorManager.sol";
import {PoSValidatorManagerSettings} from "../interfaces/IPoSValidatorManager.sol";
import {IRewardCalculator} from "../interfaces/IRewardCalculator.sol";
import {ExampleRewardCalculator} from "../ExampleRewardCalculator.sol";
import {ICMInitializable} from "../../utilities/ICMInitializable.sol";

// TODO: Remove this once all unit tests implemented
Expand All @@ -30,7 +31,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
minimumStakeAmount: DEFAULT_MINIMUM_STAKE,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
rewardCalculator: IRewardCalculator(address(0))
rewardCalculator: IRewardCalculator(new ExampleRewardCalculator(DEFAULT_REWARD_RATE))
})
);
validatorManager = app;
Expand Down
1 change: 1 addition & 0 deletions contracts/staking/tests/PoSValidatorManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {ValidatorMessages} from "../ValidatorMessages.sol";

abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint64 public constant DEFAULT_UPTIME = uint64(100);
uint64 public constant DEFAULT_REWARD_RATE = uint64(10);

PoSValidatorManager public posValidatorManager;

Expand Down

0 comments on commit fc11272

Please sign in to comment.