Skip to content

Commit

Permalink
Fix rounding error in rewards calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
neokry committed Dec 7, 2023
1 parent e81cfce commit de83eca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/auction/Auction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ contract Auction is IAuction, VersionedContract, UUPS, Ownable, ReentrancyGuard,
/// @param _manager The contract upgrade manager address
/// @param _rewardsManager The protocol rewards manager address
/// @param _weth The address of WETH
constructor(
address _manager,
address _rewardsManager,
address _weth,
uint16 _builderRewardsBPS,
uint16 _referralRewardsBPS
) payable initializer {
constructor(address _manager, address _rewardsManager, address _weth, uint16 _builderRewardsBPS, uint16 _referralRewardsBPS) payable initializer {
manager = Manager(_manager);
rewardsManager = IProtocolRewards(_rewardsManager);
WETH = _weth;
Expand Down Expand Up @@ -478,9 +472,6 @@ contract Auction is IAuction, VersionedContract, UUPS, Ownable, ReentrancyGuard,
revert INVALID_REWARD_TOTAL();
}

// Calulate total rewards
split.totalRewards = (_finalBidAmount * totalBPS) / BPS_PER_100_PERCENT;

// Check if founder reward is enabled
bool hasFounderReward = _founderRewardBps > 0 && founderReward.recipient != address(0);

Expand All @@ -493,17 +484,23 @@ contract Auction is IAuction, VersionedContract, UUPS, Ownable, ReentrancyGuard,
split.reasons = new bytes4[](arraySize);

// Set builder reward
uint256 builderAmount = (_finalBidAmount * builderRewardsBPS) / BPS_PER_100_PERCENT;
split.recipients[0] = builderRecipient;
split.amounts[0] = (_finalBidAmount * builderRewardsBPS) / BPS_PER_100_PERCENT;
split.amounts[0] = builderAmount;
split.totalRewards += builderAmount;

// Set referral reward
uint256 referralAmount = (_finalBidAmount * referralRewardsBPS) / BPS_PER_100_PERCENT;
split.recipients[1] = _currentBidRefferal != address(0) ? _currentBidRefferal : builderRecipient;
split.amounts[1] = (_finalBidAmount * referralRewardsBPS) / BPS_PER_100_PERCENT;
split.amounts[1] = referralAmount;
split.totalRewards += referralAmount;

// Set founder reward if enabled
if (hasFounderReward) {
uint256 founderAmount = (_finalBidAmount * _founderRewardBps) / BPS_PER_100_PERCENT;
split.recipients[2] = founderReward.recipient;
split.amounts[2] = (_finalBidAmount * _founderRewardBps) / BPS_PER_100_PERCENT;
split.amounts[2] = founderAmount;
split.totalRewards += founderAmount;
}
}

Expand Down
33 changes: 33 additions & 0 deletions test/Auction.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,37 @@ contract AuctionTest is NounsBuilderTest {
assertEq(MockProtocolRewards(rewards).balanceOf(referral), 0.04 ether);
assertEq(MockProtocolRewards(rewards).balanceOf(founder), 0.05 ether);
}

function test_FounderBuilderAndReferralRewardWithSmallValues() external {
// Setup
deployAltMock(founder, 500);

vm.prank(manager.owner());
manager.registerUpgrade(auctionImpl, address(rewardImpl));

vm.prank(auction.owner());
auction.upgradeTo(address(rewardImpl));

vm.prank(founder);
auction.unpause();

// Check reward values

vm.prank(bidder1);
auction.createBidWithReferral{ value: 1 ether + 19 }(2, referral);

vm.warp(10 minutes + 1 seconds);

auction.settleCurrentAndCreateNewAuction();

assertEq(token.ownerOf(2), bidder1);
assertEq(token.getVotes(bidder1), 1);

assertEq(address(treasury).balance, 0.88 ether + 19);
assertEq(address(rewards).balance, 0.03 ether + 0.04 ether + 0.05 ether);

assertEq(MockProtocolRewards(rewards).balanceOf(zoraDAO), 0.03 ether);
assertEq(MockProtocolRewards(rewards).balanceOf(referral), 0.04 ether);
assertEq(MockProtocolRewards(rewards).balanceOf(founder), 0.05 ether);
}
}

0 comments on commit de83eca

Please sign in to comment.