Skip to content

Commit

Permalink
fix: use treasury percentage, adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriivanchev committed Jun 28, 2024
1 parent 373963b commit 3e49f89
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 112 deletions.
38 changes: 17 additions & 21 deletions contracts/facets/FeeCalculatorFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ contract FeeCalculatorFacet is IFeeCalculator {

/// @notice Construct a new FeeCalculator contract
/// @param _precision The precision for every fee calculator
/// @param _validatorRewardsPercentage The percentage for the validator rewards
function initFeeCalculator(uint256 _precision, uint256 _validatorRewardsPercentage) external override {
function initFeeCalculator(uint256 _precision) external override {
LibFeeCalculator.Storage storage fcs = LibFeeCalculator
.feeCalculatorStorage();
require(!fcs.initialized, "FeeCalculatorFacet: already initialized");
require(
_precision >= 10,
"FeeCalculatorFacet: precision must not be single-digit"
);
require(
_validatorRewardsPercentage < _precision,
"FeeCalculatorFacet: percentages must be less than precision"
);
);
fcs.initialized = true;
fcs.precision = _precision;
fcs.validatorRewardsPercentage = _validatorRewardsPercentage;
}

/// @return The current precision for service fee calculations of tokens
Expand All @@ -47,20 +41,20 @@ contract FeeCalculatorFacet is IFeeCalculator {
emit ServiceFeeSet(msg.sender, _token, _serviceFeePercentage);
}

/// @notice updates the validator rewards percentage
/// @param _validatorRewardsPercentage The validator rewards percentage
function updateValidatorRewardsPercentage(uint256 _validatorRewardsPercentage)
/// @notice updates the treasury rewards percentage
/// @param _treasuryPercentage The treasury rewards percentage
function setTreasuryPercentage(uint256 _treasuryPercentage)
external
override
{
LibDiamond.enforceIsContractOwner();
LibFeeCalculator.updateValidatorRewardsPercentage(_validatorRewardsPercentage);
emit UpdateValidatorRewardsPercentage(msg.sender, _validatorRewardsPercentage);
LibFeeCalculator.setTreasuryPercentage(_treasuryPercentage);
emit TreasuryPercentageSet(msg.sender, _treasuryPercentage);
}

/// @notice The current validator rewards percentage
function validatorRewardsPercentage() external view override returns (uint256) {
return LibFeeCalculator.validatorRewardsPercentage();
/// @notice The current treasury rewards percentage
function treasuryPercentage() external view override returns (uint256) {
return LibFeeCalculator.treasuryPercentage();
}

/// @param _account The address of a validator
Expand Down Expand Up @@ -111,7 +105,8 @@ contract FeeCalculatorFacet is IFeeCalculator {
}

/// @notice Sends out the reward accumulated by the member for the specified token
/// to the member admin and treasury
/// to the member admin and a predefined percentage of the rewards is allocated
/// to the treasury.
function claim(address _token, address _member)
external
override
Expand All @@ -127,17 +122,18 @@ contract FeeCalculatorFacet is IFeeCalculator {
uint256 claimableAmount = LibFeeCalculator.claimReward(_member, _token);
address memberAdmin = LibGovernance.memberAdmin(_member);
address treasury = LibGovernance.treasury();
uint256 validatorClaimableAmount = (claimableAmount * fcs.validatorRewardsPercentage) / fcs.precision;
uint256 treasuryClaimableAmount = (claimableAmount * fcs.treasuryPercentage) / fcs.precision;

IERC20(_token).safeTransfer(memberAdmin, validatorClaimableAmount);
IERC20(_token).safeTransfer(treasury, claimableAmount - validatorClaimableAmount);
IERC20(_token).safeTransfer(memberAdmin, claimableAmount - treasuryClaimableAmount);
IERC20(_token).safeTransfer(treasury, treasuryClaimableAmount);

emit Claim(_member, memberAdmin, _token, claimableAmount);
}


/// @notice Sends out the reward accumulated by the members for the specified tokens
/// to the members admin and treasury
/// to the members admin and a predefined percentage of the rewards is allocated
/// to the treasury.
function claimMultiple(address[] calldata _tokens, address[] calldata _members)
external
override
Expand Down
17 changes: 8 additions & 9 deletions contracts/facets/GovernanceV2Facet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,20 @@ contract GovernanceV2Facet is IGovernanceV2 {
} else {
LibFeeCalculator.Storage storage fcs = LibFeeCalculator
.feeCalculatorStorage();
uint256 validatorRewardsPercentage = fcs.validatorRewardsPercentage;
uint256 precision = fcs.precision;

address accountAdmin = LibGovernance.memberAdmin(_account);
address governanceTreasury = LibGovernance.treasury();
address treasury = LibGovernance.treasury();

for (uint256 i = 0; i < LibRouter.nativeTokensCount(); i++) {
address token = LibRouter.nativeTokenAt(i);
uint256 claimableFees = LibFeeCalculator.claimReward(
_account,
token
);
uint256 validatorClaimableAmount = (claimableFees * validatorRewardsPercentage) / precision;
uint256 treasuryClaimableAmount = (claimableFees * fcs.treasuryPercentage) / fcs.precision;

IERC20(token).safeTransfer(accountAdmin, validatorClaimableAmount);
IERC20(token).safeTransfer(governanceTreasury, claimableFees - validatorClaimableAmount);
IERC20(token).safeTransfer(accountAdmin, claimableFees - treasuryClaimableAmount);
IERC20(token).safeTransfer(treasury, treasuryClaimableAmount);
}

for (uint256 i = 0; i < LibPayment.tokensCount(); i++) {
Expand All @@ -65,10 +64,10 @@ contract GovernanceV2Facet is IGovernanceV2 {
_account,
token
);
uint256 validatorClaimableAmount = (claimableFees * validatorRewardsPercentage) / precision;
uint256 treasuryClaimableAmount = (claimableFees * fcs.treasuryPercentage) / fcs.precision;

IERC20(token).safeTransfer(accountAdmin, validatorClaimableAmount);
IERC20(token).safeTransfer(governanceTreasury, claimableFees - validatorClaimableAmount);
IERC20(token).safeTransfer(accountAdmin, claimableFees - treasuryClaimableAmount);
IERC20(token).safeTransfer(treasury, treasuryClaimableAmount);
}

_accountAdmin = address(0);
Expand Down
22 changes: 12 additions & 10 deletions contracts/interfaces/IFeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ interface IFeeCalculator {
uint256 amount
);

/// @notice An event emitted once the validator rewards percentage is modified
event UpdateValidatorRewardsPercentage(address account, uint256 _validatorRewardsPercentage);
/// @notice An event emitted once the treasury rewards percentage is modified
event TreasuryPercentageSet(address account, uint256 _treasuryPercentage);

/// @notice Construct a new FeeCalculator contract
/// @param _precision The precision for every fee calculator
function initFeeCalculator(uint256 _precision, uint256 _validatorRewardsPercentage) external;
function initFeeCalculator(uint256 _precision) external;

/// @return The current precision for service fee calculations of tokens
function serviceFeePrecision() external view returns (uint256);
Expand Down Expand Up @@ -53,17 +53,19 @@ interface IFeeCalculator {
returns (uint256);

/// @notice Sends out the reward accumulated by the member for the specified token
/// to the member admin and treasury
/// to the member admin and a predefined percentage of the rewards is allocated
/// to the treasury.
function claim(address _token, address _member) external;

/// @notice Sends out the reward accumulated by the members for the specified tokens
/// to the members admin and treasury
/// to the members admin and a predefined percentage of the rewards is allocated
/// to the treasury.
function claimMultiple(address[] calldata _tokens, address[] calldata _members) external;

/// @notice updates the validator rewards percentage
/// @param _validatorRewardsPercentage The validator rewards percentage
function updateValidatorRewardsPercentage(uint256 _validatorRewardsPercentage) external;
/// @notice updates the treasury rewards percentage
/// @param _treasuryPercentage The treasury rewards percentage
function setTreasuryPercentage(uint256 _treasuryPercentage) external;

/// @notice The current validator rewards percentage
function validatorRewardsPercentage() external view returns (uint256);
/// @notice The current treasury rewards percentage
function treasuryPercentage() external view returns (uint256);
}
20 changes: 10 additions & 10 deletions contracts/libraries/LibFeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ library LibFeeCalculator {
uint256 precision;
// A mapping consisting of all token fee calculators
mapping(address => FeeCalculator) nativeTokenFeeCalculators;
// Precentage for the validator rewards
uint256 validatorRewardsPercentage;
// Percentage for the treasury rewards
uint256 treasuryPercentage;
}

function feeCalculatorStorage() internal pure returns (Storage storage ds) {
Expand Down Expand Up @@ -107,24 +107,24 @@ library LibFeeCalculator {
ntfc.serviceFeePercentage = _serviceFeePercentage;
}

/// @notice Updates the validator rewards percentage
/// @param _validatorRewardsPercentage The validator rewards precentage
function updateValidatorRewardsPercentage(uint256 _validatorRewardsPercentage)
/// @notice Updates the treasury rewards percentage
/// @param _treasuryPercentage The treasury rewards percentage
function setTreasuryPercentage(uint256 _treasuryPercentage)
internal
{
LibFeeCalculator.Storage storage fcs = feeCalculatorStorage();
require(
_validatorRewardsPercentage < fcs.precision,
_treasuryPercentage < fcs.precision,
"LibFeeCalculator: validator rewards percentage percentage exceeds or equal to precision"
);

fcs.validatorRewardsPercentage = _validatorRewardsPercentage;
fcs.treasuryPercentage = _treasuryPercentage;
}

/// @notice The current validator rewards percentage
function validatorRewardsPercentage() internal view returns (uint256) {
/// @notice The current treasury rewards percentage
function treasuryPercentage() internal view returns (uint256) {
LibFeeCalculator.Storage storage fcs = feeCalculatorStorage();
return fcs.validatorRewardsPercentage;
return fcs.treasuryPercentage;
}

/// @notice Accrues fees to a fee calculator
Expand Down
2 changes: 0 additions & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ task('deploy-router', 'Deploys Router contract will all the necessary facets')
.addParam('members', 'The addresses of the members')
.addParam('membersAdmins', 'The addresses of the members\' admins')
.addParam('treasury', 'The address of the treasury')
.addParam('validatorRewardsPercentage', 'The Validator Reward Precentage Split', 60_000, types.int)
.setAction(async (taskArgs) => {
const deployRouter = require('./scripts/deploy-router');
const membersArray = taskArgs.members.split(',');
Expand All @@ -28,7 +27,6 @@ task('deploy-router', 'Deploys Router contract will all the necessary facets')
taskArgs.owner,
taskArgs.treasury,
taskArgs.governancePercentage,
taskArgs.validatorRewardsPercentage,
taskArgs.governancePrecision,
taskArgs.feeCalculatorPrecision,
membersArray,
Expand Down
6 changes: 3 additions & 3 deletions scripts/deploy-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { getSelectors } = require('../util');

const { performUpgradeErc721Support } = require('./upgrade-erc721-support');

async function deployRouter(owner,treasury, governancePercentage,validatorRewardsPercentage, governancePrecision, feeCalculatorPrecision, members, membersAdmins) {
async function deployRouter(owner, treasury, governancePercentage, governancePrecision, feeCalculatorPrecision, members, membersAdmins) {
await hardhat.run('compile');

const routerFacetFactory = await ethers.getContractFactory('RouterFacet');
Expand Down Expand Up @@ -70,14 +70,14 @@ async function deployRouter(owner,treasury, governancePercentage,validatorReward
membersAdmins [${membersAdmins}],
percentage [${governancePercentage}] and
precision [${governancePrecision}], please wait...`);
const initGovernanceTx = await (await router.initGovernance(members, membersAdmins,treasury, governancePercentage, governancePrecision));
const initGovernanceTx = await (await router.initGovernance(members, membersAdmins, treasury, governancePercentage, governancePrecision));
await initGovernanceTx.wait();

console.log(`Initializing Router, please wait...`);
const initRouterTx = await (await router.initRouter());
await initRouterTx.wait();
console.log(`Initializing Fee Calculator with precision [${feeCalculatorPrecision}], please wait...`);
const initFeeCalculatorTx = await (await router.initFeeCalculator(feeCalculatorPrecision, validatorRewardsPercentage));
const initFeeCalculatorTx = await (await router.initFeeCalculator(feeCalculatorPrecision));
await initFeeCalculatorTx.wait();

console.log('Router address: ', diamond.address);
Expand Down
Loading

0 comments on commit 3e49f89

Please sign in to comment.