Skip to content

Commit

Permalink
Fixed distribution setter (#69)
Browse files Browse the repository at this point in the history
* chore: adding variable in initalizer and logic for custom fixed distribution

* chore: adding setter for fixed yield distribution

* chore: adding new test and fixing missing curly brace

* chore: addressing #45

* fix: renaming yieldFixedSplit to yieldFixedSplitDivisor

* fix: moving variable down for upgrade safety

* fix: removing extreneous check
  • Loading branch information
RonTuretzky authored Jul 25, 2024
1 parent d9e912e commit 471c443
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
2 changes: 2 additions & 0 deletions script/deploy/DeployYieldDistributor.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ contract DeployYieldDistributor is Script {
uint256 _maxPoints = stdJson.readUint(config_data, "._maxPoints");
uint256 _precision = stdJson.readUint(config_data, "._precision");
uint256 _lastClaimedBlockNumber = stdJson.readUint(config_data, "._lastClaimedBlockNumber");
uint256 _yieldFixedSplitDivisor = stdJson.readUint(config_data, "._yieldFixedSplitDivisor");
address _owner = stdJson.readAddress(config_data, "._owner");
bytes projectsRaw = stdJson.parseRaw(config_data, "._projects");
address[] projects = abi.decode(projectsRaw, (address[]));
Expand All @@ -28,6 +29,7 @@ contract DeployYieldDistributor is Script {
_minRequiredVotingPower,
_maxPoints,
_cycleLength,
_yieldFixedSplitDivisor,
_lastClaimedBlockNumber,
projects
);
Expand Down
5 changes: 3 additions & 2 deletions script/deploy/config/deploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"_blocktime": 5,
"_maxPoints": 10000,
"_cycleLength": 518400,
"_minRequiredVotingPower":1728000000000000000000000,
"_minRequiredVotingPower": 1728000000000000000000000,
"_lastClaimedBlockNumber": 0,
"_precision": 1000000000000000000
"_precision": 1000000000000000000,
"_yieldFixedSplitDivisor": 2
}
28 changes: 22 additions & 6 deletions src/YieldDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ contract YieldDistributor is OwnableUpgradeable {
mapping(address => uint256) public accountLastVoted;
// @notice The voting power allocated to projects by voters in the current cycle
mapping(address => uint256[]) voterDistributions;
// @notice How much of the yield is divided equally among projects
uint256 public yieldFixedSplitDivisor;

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
Expand All @@ -87,6 +89,7 @@ contract YieldDistributor is OwnableUpgradeable {
uint256 _minRequiredVotingPower,
uint256 _maxPoints,
uint256 _cycleLength,
uint256 _yieldFixedSplitDivisor,
uint256 _lastClaimedBlockNumber,
address[] memory _projects
) public initializer {
Expand All @@ -97,6 +100,7 @@ contract YieldDistributor is OwnableUpgradeable {
minRequiredVotingPower = _minRequiredVotingPower;
maxPoints = _maxPoints;
cycleLength = _cycleLength;
yieldFixedSplitDivisor = _yieldFixedSplitDivisor;
lastClaimedBlockNumber = _lastClaimedBlockNumber;

projectDistributions = new uint256[](_projects.length);
Expand Down Expand Up @@ -182,10 +186,11 @@ contract YieldDistributor is OwnableUpgradeable {
* @return bytes Calldata used by the resolver to distribute the yield
*/
function resolveYieldDistribution() public view returns (bool, bytes memory) {
uint256 _available_yield = BREAD.balanceOf(address(this)) + BREAD.yieldAccrued();
if (
currentVotes == 0 // No votes were cast
|| block.number < lastClaimedBlockNumber + cycleLength // Already claimed this cycle
|| BREAD.balanceOf(address(this)) + BREAD.yieldAccrued() < projects.length // Yield is insufficient
|| _available_yield / yieldFixedSplitDivisor < projects.length // Yield is insufficient
) {
return (false, new bytes(0));
} else {
Expand All @@ -202,18 +207,19 @@ contract YieldDistributor is OwnableUpgradeable {

BREAD.claimYield(BREAD.yieldAccrued(), address(this));
lastClaimedBlockNumber = block.number;

uint256 _halfYield = BREAD.balanceOf(address(this)) / 2;
uint256 _baseSplit = _halfYield / projects.length;
uint256 balance = BREAD.balanceOf(address(this));
uint256 _fixedYield = balance / yieldFixedSplitDivisor;
uint256 _baseSplit = _fixedYield / projects.length;
uint256 _votedYield = balance - _fixedYield;

for (uint256 i; i < projects.length; ++i) {
uint256 _votedSplit = ((projectDistributions[i] * _halfYield * PRECISION) / currentVotes) / PRECISION;
uint256 _votedSplit = ((projectDistributions[i] * _votedYield * PRECISION) / currentVotes) / PRECISION;
BREAD.transfer(projects[i], _votedSplit + _baseSplit);
}

_updateBreadchainProjects();

emit YieldDistributed(_halfYield * 2, currentVotes, projectDistributions);
emit YieldDistributed(balance, currentVotes, projectDistributions);

delete currentVotes;
projectDistributions = new uint256[](projects.length);
Expand Down Expand Up @@ -376,4 +382,14 @@ contract YieldDistributor is OwnableUpgradeable {

cycleLength = _cycleLength;
}

/**
* @notice Set a new fixed split for the yield distribution
* @param _yieldFixedSplitDivisor New fixed split for the yield distribution
*/
function setyieldFixedSplitDivisor(uint256 _yieldFixedSplitDivisor) public onlyOwner {
if (_yieldFixedSplitDivisor == 0) revert MustBeGreaterThanZero();

yieldFixedSplitDivisor = _yieldFixedSplitDivisor;
}
}
38 changes: 38 additions & 0 deletions test/YieldDistributor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ contract YieldDistributorTest is Test {
uint256 _cycleLength = stdJson.readUint(config_data, "._cycleLength");
uint256 _minHoldingDuration = stdJson.readUint(config_data, "._minHoldingDuration");
uint256 _lastClaimedBlockNumber = stdJson.readUint(config_data, "._lastClaimedBlockNumber");
uint256 _yieldFixedSplitDivisor = stdJson.readUint(config_data, "._yieldFixedSplitDivisor");
Bread public bread = Bread(address(_bread));
uint256 minHoldingDurationInBlocks = _minHoldingDuration / _blocktime;

Expand All @@ -62,6 +63,7 @@ contract YieldDistributorTest is Test {
_minRequiredVotingPower,
_maxPoints,
_cycleLength,
_yieldFixedSplitDivisor,
_lastClaimedBlockNumber,
projects1
);
Expand All @@ -79,6 +81,7 @@ contract YieldDistributorTest is Test {
_minRequiredVotingPower,
_maxPoints,
_cycleLength,
_yieldFixedSplitDivisor,
_lastClaimedBlockNumber,
projects2
);
Expand Down Expand Up @@ -136,6 +139,41 @@ contract YieldDistributorTest is Test {
assertGt(bread_bal_after, yieldAccrued - marginOfError);
}

function test_fixed_yield_split() public {
// Getting the balance of the project before the distribution
uint256 bread_bal_before = bread.balanceOf(address(this));
assertEq(bread_bal_before, 0);
// Getting the amount of yield to be distributed
uint256 yieldAccrued = bread.yieldAccrued();

// Setting up a voter
address account = address(0x1234567890123456789012345678901234567890);
address[] memory accounts = new address[](1);
accounts[0] = account;
setUpAccountsForVoting(accounts);

// Setting up for a cycle
setUpForCycle(yieldDistributor2);
address owner = yieldDistributor2.owner();
vm.prank(owner);
yieldDistributor2.setyieldFixedSplitDivisor(3);

// Casting vote and distributing yield
uint256 vote = 50;
uint256 vote2 = 50;
percentages.push(vote);
percentages.push(vote2);
vm.prank(account);
yieldDistributor2.castVote(percentages);
yieldDistributor2.distributeYield();
uint256 fixedSplit = yieldAccrued / _yieldFixedSplitDivisor;
uint256 votedSplit = yieldAccrued - fixedSplit;
uint256 projectsLength = yieldDistributor2.getProjectsLength();
// Getting the balance of the project after the distribution and checking if it similiar to the yield accrued (there may be rounding issues)
uint256 bread_bal_after = bread.balanceOf(address(secondProject));
assertGt(bread_bal_after, ((fixedSplit + votedSplit) / projectsLength) - marginOfError);
}

function test_simple_recast_vote() public {
// Getting the balance of the project before the distribution
uint256 bread_bal_before = bread.balanceOf(address(this));
Expand Down
3 changes: 2 additions & 1 deletion test/test_deploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"_minRequiredVotingPower": 10000000000000000000,
"_minVotingAmount": 10000000000000000000,
"_minHoldingDuration": 864000,
"_precision": 1000000000000000000
"_precision": 1000000000000000000,
"_yieldFixedSplitDivisor": 2
}

0 comments on commit 471c443

Please sign in to comment.