Refactor BaseFeeManager
This commit is contained in:
parent
16be3f8229
commit
cf81b767ba
|
@ -43,54 +43,13 @@ contract BaseFeeManager is EternalStorage, FeeTypes {
|
|||
return uintStorage[keccak256(abi.encodePacked("foreignFee"))];
|
||||
}
|
||||
|
||||
function distributeFeeFromAffirmation(uint256 _fee) external {
|
||||
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_FOREIGN);
|
||||
}
|
||||
function distributeFeeFromAffirmation(uint256 _fee) external;
|
||||
|
||||
function distributeFeeFromSignatures(uint256 _fee) external {
|
||||
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_HOME);
|
||||
}
|
||||
function distributeFeeFromSignatures(uint256 _fee) external;
|
||||
|
||||
function getFeeManagerMode() public pure returns(bytes4);
|
||||
|
||||
function random(uint256 _count) public view returns(uint256) {
|
||||
return uint256(blockhash(block.number.sub(1))) % _count;
|
||||
}
|
||||
|
||||
function distributeFeeProportionally(uint256 _fee, bytes32 _direction) internal {
|
||||
IRewardableValidators validators = rewardableValidatorContract();
|
||||
address[] memory validatorList = validators.validatorList();
|
||||
uint256 feePerValidator = _fee.div(validatorList.length);
|
||||
|
||||
uint256 randomValidatorIndex;
|
||||
uint256 diff = _fee.sub(feePerValidator.mul(validatorList.length));
|
||||
if (diff > 0) {
|
||||
randomValidatorIndex = random(validatorList.length);
|
||||
}
|
||||
|
||||
for (uint256 i = 0; i < validatorList.length; i++) {
|
||||
uint256 feeToDistribute = feePerValidator;
|
||||
if (diff > 0 && randomValidatorIndex == i) {
|
||||
feeToDistribute = feeToDistribute.add(diff);
|
||||
}
|
||||
address rewardAddress = validators.getValidatorRewardAddress(validatorList[i]);
|
||||
onFeeDistribution(rewardAddress, feeToDistribute, _direction);
|
||||
}
|
||||
}
|
||||
|
||||
function onFeeDistribution(address _rewardAddress, uint256 _fee, bytes32 _direction) internal {
|
||||
if (_direction == REWARD_FOR_TRANSFERRING_FROM_FOREIGN) {
|
||||
onAffirmationFeeDistribution(_rewardAddress, _fee);
|
||||
} else {
|
||||
onSignatureFeeDistribution(_rewardAddress, _fee);
|
||||
}
|
||||
}
|
||||
|
||||
function onAffirmationFeeDistribution(address _rewardAddress, uint256 _fee) internal;
|
||||
|
||||
function onSignatureFeeDistribution(address _rewardAddress, uint256 _fee) internal;
|
||||
|
||||
function rewardableValidatorContract() public view returns(IRewardableValidators) {
|
||||
return IRewardableValidators(addressStorage[keccak256(abi.encodePacked("validatorContract"))]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
pragma solidity 0.4.24;
|
||||
|
||||
import "./BaseFeeManager.sol";
|
||||
import "../IBlockReward.sol";
|
||||
|
||||
contract BlockRewardFeeManager is BaseFeeManager {
|
||||
|
||||
function distributeFeeFromAffirmation(uint256 _fee) external {
|
||||
distributeFeeFromBlockReward(_fee);
|
||||
}
|
||||
|
||||
function distributeFeeFromSignatures(uint256 _fee) external {
|
||||
distributeFeeFromBlockReward(_fee);
|
||||
}
|
||||
|
||||
function distributeFeeFromBlockReward(uint256 _fee) internal {
|
||||
IBlockReward blockReward = _blockRewardContract();
|
||||
blockReward.addBridgeTokenFeeReceivers(_fee);
|
||||
}
|
||||
|
||||
function _blockRewardContract() internal view returns(IBlockReward) {
|
||||
return IBlockReward(addressStorage[keccak256(abi.encodePacked("blockRewardContract"))]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
pragma solidity 0.4.24;
|
||||
|
||||
import "./BaseFeeManager.sol";
|
||||
import "../IRewardableValidators.sol";
|
||||
|
||||
contract ValidatorsFeeManager is BaseFeeManager {
|
||||
|
||||
function distributeFeeFromAffirmation(uint256 _fee) external {
|
||||
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_FOREIGN);
|
||||
}
|
||||
|
||||
function distributeFeeFromSignatures(uint256 _fee) external {
|
||||
distributeFeeProportionally(_fee, REWARD_FOR_TRANSFERRING_FROM_HOME);
|
||||
}
|
||||
|
||||
function rewardableValidatorContract() internal view returns(IRewardableValidators) {
|
||||
return IRewardableValidators(addressStorage[keccak256(abi.encodePacked("validatorContract"))]);
|
||||
}
|
||||
|
||||
function distributeFeeProportionally(uint256 _fee, bytes32 _direction) internal {
|
||||
IRewardableValidators validators = rewardableValidatorContract();
|
||||
address[] memory validatorList = validators.validatorList();
|
||||
uint256 feePerValidator = _fee.div(validatorList.length);
|
||||
|
||||
uint256 randomValidatorIndex;
|
||||
uint256 diff = _fee.sub(feePerValidator.mul(validatorList.length));
|
||||
if (diff > 0) {
|
||||
randomValidatorIndex = random(validatorList.length);
|
||||
}
|
||||
|
||||
for (uint256 i = 0; i < validatorList.length; i++) {
|
||||
uint256 feeToDistribute = feePerValidator;
|
||||
if (diff > 0 && randomValidatorIndex == i) {
|
||||
feeToDistribute = feeToDistribute.add(diff);
|
||||
}
|
||||
address rewardAddress = validators.getValidatorRewardAddress(validatorList[i]);
|
||||
onFeeDistribution(rewardAddress, feeToDistribute, _direction);
|
||||
}
|
||||
}
|
||||
|
||||
function onFeeDistribution(address _rewardAddress, uint256 _fee, bytes32 _direction) internal {
|
||||
if (_direction == REWARD_FOR_TRANSFERRING_FROM_FOREIGN) {
|
||||
onAffirmationFeeDistribution(_rewardAddress, _fee);
|
||||
} else {
|
||||
onSignatureFeeDistribution(_rewardAddress, _fee);
|
||||
}
|
||||
}
|
||||
|
||||
function onAffirmationFeeDistribution(address _rewardAddress, uint256 _fee) internal;
|
||||
|
||||
function onSignatureFeeDistribution(address _rewardAddress, uint256 _fee) internal;
|
||||
}
|
|
@ -1,36 +1,10 @@
|
|||
pragma solidity 0.4.24;
|
||||
|
||||
import "../BaseFeeManager.sol";
|
||||
import "../../IBlockReward.sol";
|
||||
import "../BlockRewardFeeManager.sol";
|
||||
|
||||
contract FeeManagerErcToErcPOSDAO is BaseFeeManager {
|
||||
contract FeeManagerErcToErcPOSDAO is BlockRewardFeeManager {
|
||||
|
||||
function getFeeManagerMode() public pure returns(bytes4) {
|
||||
return bytes4(keccak256(abi.encodePacked("manages-both-directions")));
|
||||
}
|
||||
|
||||
function blockRewardContract() internal view returns(IBlockReward) {
|
||||
return IBlockReward(addressStorage[keccak256(abi.encodePacked("blockRewardContract"))]);
|
||||
}
|
||||
|
||||
function distributeFeeFromAffirmation(uint256 _fee) external {
|
||||
distributeFeeFromBlockReward(_fee);
|
||||
}
|
||||
|
||||
function distributeFeeFromSignatures(uint256 _fee) external {
|
||||
distributeFeeFromBlockReward(_fee);
|
||||
}
|
||||
|
||||
function distributeFeeFromBlockReward(uint256 _fee) internal {
|
||||
IBlockReward blockReward = blockRewardContract();
|
||||
blockReward.addBridgeTokenFeeReceivers(_fee);
|
||||
}
|
||||
|
||||
function isPOSDAOFeeManager() public pure returns(bool) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function onAffirmationFeeDistribution(address _rewardAddress, uint256 _fee) internal {}
|
||||
|
||||
function onSignatureFeeDistribution(address _rewardAddress, uint256 _fee) internal {}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
pragma solidity 0.4.24;
|
||||
|
||||
import "../BaseFeeManager.sol";
|
||||
import "../../IBlockReward.sol";
|
||||
import "../Sacrifice.sol";
|
||||
import "../ValidatorsFeeManager.sol";
|
||||
|
||||
|
||||
contract FeeManagerErcToNative is BaseFeeManager {
|
||||
contract FeeManagerErcToNative is ValidatorsFeeManager {
|
||||
|
||||
function getFeeManagerMode() public pure returns(bytes4) {
|
||||
return bytes4(keccak256(abi.encodePacked("manages-both-directions")));
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
pragma solidity 0.4.24;
|
||||
|
||||
import "../BaseFeeManager.sol";
|
||||
import "../../IBurnableMintableERC677Token.sol";
|
||||
import "../Sacrifice.sol";
|
||||
import "../ValidatorsFeeManager.sol";
|
||||
|
||||
|
||||
contract FeeManagerNativeToErc is BaseFeeManager {
|
||||
contract FeeManagerNativeToErc is ValidatorsFeeManager {
|
||||
|
||||
function getFeeManagerMode() public pure returns(bytes4) {
|
||||
return bytes4(keccak256(abi.encodePacked("manages-one-direction")));
|
||||
|
|
Loading…
Reference in New Issue