Skip to content

Commit

Permalink
feat: implement member removal split rewards transaction, adapt facet…
Browse files Browse the repository at this point in the history
… script
  • Loading branch information
valeriivanchev committed Jun 27, 2024
1 parent faf5d3d commit 386b7d8
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 41 deletions.
23 changes: 17 additions & 6 deletions contracts/facets/FeeCalculatorFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ contract FeeCalculatorFacet is IFeeCalculator {

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

/// @return The current precision for service fee calculations of tokens
Expand All @@ -47,6 +47,17 @@ 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)
external
override
{
LibDiamond.enforceIsContractOwner();
LibFeeCalculator.updateValidatorRewardsPercentage(_validatorRewardsPercentage);
emit UpdateValidatorRewardsPercentage(msg.sender, _validatorRewardsPercentage);
}

/// @param _account The address of a validator
/// @param _token The token address
/// @return The total amount of claimed tokens by the provided validator address
Expand Down Expand Up @@ -109,14 +120,14 @@ contract FeeCalculatorFacet is IFeeCalculator {
.feeCalculatorStorage();

uint256 validatorRewardsPercentage = fcs.validatorRewardsPercentage;
uint256 treasuryRewardsPercentage = fcs.treasuryRewardsPercentage;
uint256 precision = fcs.precision;
uint256 claimableAmount = LibFeeCalculator.claimReward(_member, _token);
address memberAdmin = LibGovernance.memberAdmin(_member);
address treasury = LibGovernance.treasury();
uint256 validatorClaimableAmount = (claimableAmount * validatorRewardsPercentage) / precision;

IERC20(_token).safeTransfer(memberAdmin, (claimableAmount * validatorRewardsPercentage) / precision);
IERC20(_token).safeTransfer(treasury, (claimableAmount * treasuryRewardsPercentage) / precision);
IERC20(_token).safeTransfer(memberAdmin, validatorClaimableAmount);
IERC20(_token).safeTransfer(treasury, claimableAmount - validatorClaimableAmount);

emit Claim(_member, memberAdmin, _token, claimableAmount);
}
Expand Down
15 changes: 13 additions & 2 deletions contracts/facets/GovernanceV2Facet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ contract GovernanceV2Facet is IGovernanceV2 {
LibFeeCalculator.addNewMember(_account, LibPayment.tokenAt(i));
}
} else {
LibFeeCalculator.Storage storage fcs = LibFeeCalculator
.feeCalculatorStorage();
uint256 validatorRewardsPercentage = fcs.validatorRewardsPercentage;
uint256 precision = fcs.precision;
address accountAdmin = LibGovernance.memberAdmin(_account);
address governanceTreasury = LibGovernance.treasury();

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

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

for (uint256 i = 0; i < LibPayment.tokensCount(); i++) {
Expand All @@ -57,7 +65,10 @@ contract GovernanceV2Facet is IGovernanceV2 {
_account,
token
);
IERC20(token).safeTransfer(accountAdmin, claimableFees);
uint256 validatorClaimableAmount = (claimableFees * validatorRewardsPercentage) / precision;

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

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

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

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

/// @return The current precision for service fee calculations of tokens
function serviceFeePrecision() external view returns (uint256);
Expand Down Expand Up @@ -56,4 +59,8 @@ interface IFeeCalculator {
/// @notice Sends out the reward accumulated by the members for the specified tokens
/// to the members admin and 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;
}
20 changes: 16 additions & 4 deletions contracts/libraries/LibFeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ library LibFeeCalculator {
bool initialized;
// Precision for every calculator's fee percentage.
uint256 precision;
// Precentage for the validator rewards
uint256 validatorRewardsPercentage;
// Precentage for the treasury rewards
uint256 treasuryRewardsPercentage;
// A mapping consisting of all token fee calculators
mapping(address => FeeCalculator) nativeTokenFeeCalculators;
// Precentage for the validator rewards
uint256 validatorRewardsPercentage;
}

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

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

fcs.validatorRewardsPercentage = _validatorRewardsPercentage;
}

/// @notice Accrues fees to a fee calculator
/// @param _fc The fee calculator
/// @return The updated accumulator
Expand Down
4 changes: 2 additions & 2 deletions contracts/libraries/LibGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ library LibGovernance {
uint256 percentage;
// Admin of the contract
address admin;
// Treasury of the contract
address treasury;
// used to restrict certain functionality in case of an emergency stop
bool paused;
// Treasury of the contract
address treasury;
}

function governanceStorage() internal pure returns (Storage storage gs) {
Expand Down
4 changes: 2 additions & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ task('deploy-router', 'Deploys Router contract will all the necessary facets')
.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)
.addParam('treasuryRewardsPercentage', 'The Treasury Reward Precentage Split', 40_000, types.int)
.setAction(async (taskArgs) => {
const deployRouter = require('./scripts/deploy-router');
const membersArray = taskArgs.members.split(',');
Expand All @@ -30,7 +29,6 @@ task('deploy-router', 'Deploys Router contract will all the necessary facets')
taskArgs.treasury,
taskArgs.governancePercentage,
taskArgs.validatorRewardsPercentage,
taskArgs.treasuryRewardsPercentage,
taskArgs.governancePrecision,
taskArgs.feeCalculatorPrecision,
membersArray,
Expand Down Expand Up @@ -281,6 +279,8 @@ task('updateFacet', 'Deploys ERC721PortalFacet')
.addParam("facetName", "The addres of the router")
.addParam("facetAddress", "The addres of the router")
.addParam("routerAddress", "The addres of the router")
.addParam("actions", "The diamond cut action(Add=0, Replace=1, Remove=2)", 0, types.int)
.addParam("functionSignatures", "The function signatures divided by coma")
.setAction(async (taskArgs) => {
console.log(taskArgs);
const updateFacet = require('./scripts/update-facet');
Expand Down
4 changes: 2 additions & 2 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,treasuryRewardsPercentage, governancePrecision, feeCalculatorPrecision, members, membersAdmins) {
async function deployRouter(owner,treasury, governancePercentage,validatorRewardsPercentage, governancePrecision, feeCalculatorPrecision, members, membersAdmins) {
await hardhat.run('compile');

const routerFacetFactory = await ethers.getContractFactory('RouterFacet');
Expand Down Expand Up @@ -77,7 +77,7 @@ async function deployRouter(owner,treasury, governancePercentage,validatorReward
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, treasuryRewardsPercentage));
const initFeeCalculatorTx = await (await router.initFeeCalculator(feeCalculatorPrecision, validatorRewardsPercentage));
await initFeeCalculatorTx.wait();

console.log('Router address: ', diamond.address);
Expand Down
10 changes: 6 additions & 4 deletions scripts/update-facet.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const hardhat = require("hardhat");
const ethers = hardhat.ethers;
const { getSelectors } = require("../util");
const { getSelector } = require("../util");

async function updateFacet(facetName, facetAddress, routerAddress) {
async function updateFacet(facetName, facetAddress, routerAddress, action, functionSignatures) {
const facetContract = await ethers.getContractAt(facetName, facetAddress);
let signatures = functionSignatures.split(',');


const diamondAddCutReplace = [
{
facetAddress: facetContract.address,
action: 1, // Replace
functionSelectors: getSelectors(facetContract),
action, // Replace
functionSelectors: signatures.map(signature => getSelector(signature)),
},
];

Expand Down
Loading

0 comments on commit 386b7d8

Please sign in to comment.