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

try switching to structs #6

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/ProtocolRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ contract ProtocolRewards is IProtocolRewards, EIP712 {
unchecked {
withdrawHash = keccak256(abi.encode(WITHDRAW_TYPEHASH, owner, amount, nonces[owner]++, deadline));
}


bytes32 digest = _hashTypedDataV4(withdrawHash);

Expand Down
2 changes: 1 addition & 1 deletion src/abstract/ERC1155/ERC1155RewardsStorageV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pragma solidity 0.8.17;

contract ERC1155RewardsStorageV1 {
mapping(uint256 => address) public createReferrals;
}
}
69 changes: 37 additions & 32 deletions src/abstract/RewardSplits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ 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();
Expand Down Expand Up @@ -38,33 +46,30 @@ abstract contract RewardSplits {
return numTokens * TOTAL_REWARD_PER_MINT;
}

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;
function computeFreeMintRewards(uint256 numTokens) 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;
function computePaidMintRewards(uint256 numTokens) 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 +81,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 +108,13 @@ abstract contract RewardSplits {
address(0),
0,
mintReferral,
mintReferralReward,
settings.mintReferralReward,
createReferral,
createReferralReward,
settings.createReferralReward,
creator,
firstMinterReward,
settings.firstMinterReward,
zoraRewardRecipient,
zoraReward
settings.zoraReward
);
}
}
39 changes: 17 additions & 22 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,13 @@ 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
.computeFreeMintRewards(numTokens);
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 +49,13 @@ 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(
numTokens
);
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 +70,11 @@ 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
.computeFreeMintRewards(numTokens);
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 +94,11 @@ 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(
numTokens
);
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
54 changes: 25 additions & 29 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,23 @@ 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
.computeFreeMintRewards(numTokens);
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(
numTokens
);
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 +49,13 @@ 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
.computeFreeMintRewards(numTokens);
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,13 @@ contract ERC721RewardsTest is ProtocolRewardsTest {
vm.prank(collector);
mockERC721.mintWithRewards{value: totalValue}(collector, numTokens, mintReferral);

(uint256 mintReferralReward, uint256 createReferralReward, uint256 firstMinterReward, uint256 zoraReward) = mockERC721.computePaidMintRewards(
numTokens
);
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 +94,11 @@ 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
.computeFreeMintRewards(numTokens);
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 +118,11 @@ 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(
numTokens
);
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