Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
try switching to structs
Browse files Browse the repository at this point in the history
  • Loading branch information
iainnash committed Aug 2, 2023
1 parent 56580fd commit 987ff40
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 64 deletions.
66 changes: 38 additions & 28 deletions src/abstract/RewardSplits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ pragma solidity 0.8.17;

import {IProtocolRewards} from "../interfaces/IProtocolRewards.sol";

struct RewardsSettings {
uint256 creatorReward;
uint256 mintReferralReward;
uint256 createReferralReward;
uint256 firstMinterReward;
uint256 zoraReward;
}

abstract contract RewardSplits {
error CREATOR_FUNDS_RECIPIENT_NOT_SET();
error INVALID_ADDRESS_ZERO();
error INVALID_ETH_AMOUNT();
error ONLY_CREATE_REFERRAL();



uint256 internal constant TOTAL_REWARD_PER_MINT = 0.000777 ether;

uint256 internal constant CREATOR_REWARD = 0.000333 ether;
Expand Down Expand Up @@ -40,31 +50,31 @@ abstract contract RewardSplits {

function computeFreeMintRewards(
uint256 numTokens
) public pure returns (uint256 creatorReward, uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) {
creatorReward = numTokens * CREATOR_REWARD;
mintReferralReward = numTokens * MINT_REFERRAL_FREE_MINT_REWARD;
createReferralReward = numTokens * CREATE_REFERRAL_FREE_MINT_REWARD;
firstMinterReward = numTokens * FIRST_MINTER_REWARD;
zoraReward = numTokens * ZORA_FREE_MINT_REWARD;
) public pure returns (RewardsSettings memory) {
return RewardsSettings({
creatorReward: numTokens * CREATOR_REWARD,
mintReferralReward: numTokens * MINT_REFERRAL_FREE_MINT_REWARD,
createReferralReward: numTokens * CREATE_REFERRAL_FREE_MINT_REWARD,
firstMinterReward: numTokens * FIRST_MINTER_REWARD,
zoraReward: numTokens * ZORA_FREE_MINT_REWARD
});
}

function computePaidMintRewards(
uint256 numTokens
) public pure returns (uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) {
mintReferralReward = numTokens * MINT_REFERRAL_PAID_MINT_REWARD;
createReferralReward = numTokens * CREATE_REFERRAL_PAID_MINT_REWARD;
firstMinterReward = numTokens * FIRST_MINTER_REWARD;
zoraReward = numTokens * ZORA_PAID_MINT_REWARD;
) public pure returns (RewardsSettings memory) {
return RewardsSettings({
creatorReward: 0,
mintReferralReward: numTokens * MINT_REFERRAL_PAID_MINT_REWARD,
createReferralReward: numTokens * CREATE_REFERRAL_PAID_MINT_REWARD,
firstMinterReward: numTokens * FIRST_MINTER_REWARD,
zoraReward: numTokens * ZORA_PAID_MINT_REWARD
});
}

function _depositFreeMintRewards(uint256 totalReward, uint256 numTokens, address creator, address mintReferral, address createReferral) internal {
(
uint256 creatorReward,
uint256 mintReferralReward,
uint256 createReferralReward,
uint256 firstMinterReward,
uint256 zoraReward
) = computeFreeMintRewards(numTokens);

RewardsSettings memory settings = computeFreeMintRewards(numTokens);

if (mintReferral == address(0)) {
mintReferral = zoraRewardRecipient;
Expand All @@ -76,20 +86,20 @@ abstract contract RewardSplits {

protocolRewards.depositRewards{value: totalReward}(
creator,
creatorReward,
settings.creatorReward,
mintReferral,
mintReferralReward,
settings.mintReferralReward,
createReferral,
createReferralReward,
settings.createReferralReward,
creator,
firstMinterReward,
settings.firstMinterReward,
zoraRewardRecipient,
zoraReward
settings.zoraReward
);
}

function _depositPaidMintRewards(uint256 totalReward, uint256 numTokens, address creator, address mintReferral, address createReferral) internal {
(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = computePaidMintRewards(numTokens);
RewardsSettings memory settings = computePaidMintRewards(numTokens);

if (mintReferral == address(0)) {
mintReferral = zoraRewardRecipient;
Expand All @@ -103,13 +113,13 @@ abstract contract RewardSplits {
address(0),
0,
mintReferral,
mintReferralReward,
settings.mintReferralReward,
createReferral,
createReferralReward,
settings.createReferralReward,
creator,
firstMinterReward,
settings.firstMinterReward,
zoraRewardRecipient,
zoraReward
settings.zoraReward
);
}
}
33 changes: 17 additions & 16 deletions test/unit/ERC1155Rewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.17;

import "../ProtocolRewardsTest.sol";
import {RewardsSettings} from "../../src/abstract/RewardSplits.sol";

contract ERC1155RewardsTest is ProtocolRewardsTest {
MockERC1155 internal mockERC1155;
Expand All @@ -24,14 +25,14 @@ contract ERC1155RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC1155.mintWithRewards{value: totalReward}(collector, 0, numTokens, mintReferral);

(uint256 creatorReward, uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC1155
RewardsSettings memory settings = mockERC1155
.computeFreeMintRewards(numTokens);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), creatorReward + firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), createReferralReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward);
assertEq(protocolRewards.balanceOf(creator), settings.creatorReward + settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), settings.mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), settings.createReferralReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward);
}

function test1155PaidMintDeposit(uint16 numTokens, uint256 pricePerToken) public {
Expand All @@ -49,15 +50,15 @@ contract ERC1155RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC1155.mintWithRewards{value: totalValue}(collector, 0, numTokens, mintReferral);

(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC1155.computePaidMintRewards(
RewardsSettings memory settings = mockERC1155.computePaidMintRewards(
numTokens
);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), createReferralReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward);
assertEq(protocolRewards.balanceOf(creator), settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), settings.mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), settings.createReferralReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward);
}

function test1155FreeMintNullReferralRecipients(uint16 numTokens) public {
Expand All @@ -72,12 +73,12 @@ contract ERC1155RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC1155.mintWithRewards{value: totalReward}(collector, 0, numTokens, address(0));

(uint256 creatorReward, uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC1155
RewardsSettings memory settings = mockERC1155
.computeFreeMintRewards(numTokens);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), creatorReward + firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward + mintReferralReward + createReferralReward);
assertEq(protocolRewards.balanceOf(creator), settings.creatorReward + settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward + settings.mintReferralReward + settings.createReferralReward);
}

function test1155PaidMintNullReferralRecipient(uint16 numTokens, uint256 pricePerToken) public {
Expand All @@ -97,13 +98,13 @@ contract ERC1155RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC1155.mintWithRewards{value: totalValue}(collector, 0, numTokens, address(0));

(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC1155.computePaidMintRewards(
RewardsSettings memory settings = mockERC1155.computePaidMintRewards(
numTokens
);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward + mintReferralReward + createReferralReward);
assertEq(protocolRewards.balanceOf(creator), settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward + settings.mintReferralReward + settings.createReferralReward);
}

function testRevert1155FreeMintInvalidEth(uint16 numTokens) public {
Expand Down
41 changes: 21 additions & 20 deletions test/unit/ERC721Rewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.17;

import "../ProtocolRewardsTest.sol";
import {RewardsSettings} from "../../src/abstract/RewardSplits.sol";

contract ERC721RewardsTest is ProtocolRewardsTest {
MockERC721 internal mockERC721;
Expand All @@ -17,22 +18,22 @@ contract ERC721RewardsTest is ProtocolRewardsTest {
function testValidateFreeMintTotalComputation(uint16 numTokens) public {
uint256 expectedTotal = mockERC721.computeTotalReward(numTokens);

(uint256 creatorReward, uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721
RewardsSettings memory settings = mockERC721
.computeFreeMintRewards(numTokens);

uint256 actualTotal = creatorReward + mintReferralReward + createReferralReward + firstMinterReward + zoraReward;
uint256 actualTotal = settings.creatorReward + settings.mintReferralReward + settings.createReferralReward + settings.firstMinterReward + settings.zoraReward;

assertEq(expectedTotal, actualTotal);
}

function testValidatePaidMintTotalComputation(uint32 numTokens) public {
uint256 expectedTotal = mockERC721.computeTotalReward(numTokens);

(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721.computePaidMintRewards(
RewardsSettings memory settings = mockERC721.computePaidMintRewards(
numTokens
);

uint256 actualTotal = mintReferralReward + createReferralReward + firstMinterReward + zoraReward;
uint256 actualTotal = settings.mintReferralReward + settings.createReferralReward + settings.firstMinterReward + settings.zoraReward;

assertEq(expectedTotal, actualTotal);
}
Expand All @@ -47,14 +48,14 @@ contract ERC721RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC721.mintWithRewards{value: totalReward}(collector, numTokens, mintReferral);

(uint256 creatorReward, uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721
RewardsSettings memory settings = mockERC721
.computeFreeMintRewards(numTokens);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), creatorReward + firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), createReferralReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward);
assertEq(protocolRewards.balanceOf(creator), settings.creatorReward + settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), settings.mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), settings.createReferralReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward);
}

function test721PaidMintDeposit(uint16 numTokens, uint256 pricePerToken) public {
Expand All @@ -72,15 +73,15 @@ contract ERC721RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC721.mintWithRewards{value: totalValue}(collector, numTokens, mintReferral);

(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721.computePaidMintRewards(
RewardsSettings memory settings = mockERC721.computePaidMintRewards(
numTokens
);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), createReferralReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward);
assertEq(protocolRewards.balanceOf(creator), settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(mintReferral), settings.mintReferralReward);
assertEq(protocolRewards.balanceOf(createReferral), settings.createReferralReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward);
}

function test721FreeMintNullReferralRecipients(uint16 numTokens) public {
Expand All @@ -95,12 +96,12 @@ contract ERC721RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC721.mintWithRewards{value: totalReward}(collector, numTokens, address(0));

(uint256 creatorReward, uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721
RewardsSettings memory settings = mockERC721
.computeFreeMintRewards(numTokens);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), creatorReward + firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward + mintReferralReward + createReferralReward);
assertEq(protocolRewards.balanceOf(creator), settings.creatorReward + settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward + settings.mintReferralReward + settings.createReferralReward);
}

function test721PaidMintNullReferralRecipient(uint16 numTokens, uint256 pricePerToken) public {
Expand All @@ -120,13 +121,13 @@ contract ERC721RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC721.mintWithRewards{value: totalValue}(collector, numTokens, address(0));

(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721.computePaidMintRewards(
RewardsSettings memory settings = mockERC721.computePaidMintRewards(
numTokens
);

assertEq(protocolRewards.totalSupply(), totalReward);
assertEq(protocolRewards.balanceOf(creator), firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), zoraReward + mintReferralReward + createReferralReward);
assertEq(protocolRewards.balanceOf(creator), settings.firstMinterReward);
assertEq(protocolRewards.balanceOf(zora), settings.zoraReward + settings.mintReferralReward + settings.createReferralReward);
}

function testRevert721CreatorFundsRecipientNotSet(uint16 numTokens) public {
Expand Down

0 comments on commit 987ff40

Please sign in to comment.