Skip to content

Commit

Permalink
Break out collection plus parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
neokry committed Sep 12, 2023
1 parent 4805ee0 commit b77830d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 99 deletions.
60 changes: 25 additions & 35 deletions src/minters/CollectionPlusMinter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,74 +82,64 @@ contract CollectionPlusMinter {
}

/// @notice mints a token from reserve using the collection plus strategy and sets delegations
/// @param params Mint parameters
/// @param signature Signature for the ERC1271 delegation
/// @param deadline Deadline for the ERC1271 delegation
function mintFromReserveAndDelegate(
MintParams calldata params,
address tokenContract,
address redeemFor,
uint256[] calldata tokenIds,
bytes calldata initData,
bytes calldata signature,
uint256 deadline
) public payable {
CollectionPlusSettings memory settings = allowedCollections[params.tokenContract];
uint256 tokenCount = params.tokenIds.length;
CollectionPlusSettings memory settings = allowedCollections[tokenContract];
uint256 tokenCount = tokenIds.length;

_validateParams(settings, tokenCount);

address[] memory fromAddresses = new address[](tokenCount);

unchecked {
for (uint256 i = 0; i < tokenCount; ++i) {
fromAddresses[i] = erc6551Registry.createAccount(
erc6551Impl,
block.chainid,
settings.redeemToken,
params.tokenIds[i],
0,
params.initData
);
IPartialSoulboundToken(params.tokenContract).mintFromReserveAndLockTo(fromAddresses[i], params.tokenIds[i]);

if (IERC721(settings.redeemToken).ownerOf(params.tokenIds[i]) != params.redeemFor) {
fromAddresses[i] = erc6551Registry.createAccount(erc6551Impl, block.chainid, settings.redeemToken, tokenIds[i], 0, initData);
IPartialSoulboundToken(tokenContract).mintFromReserveAndLockTo(fromAddresses[i], tokenIds[i]);

if (IERC721(settings.redeemToken).ownerOf(tokenIds[i]) != redeemFor) {
revert INVALID_OWNER();
}
}
}

IPartialSoulboundToken(params.tokenContract).batchDelegateBySigERC1271(fromAddresses, params.redeemFor, deadline, signature);
IPartialSoulboundToken(tokenContract).batchDelegateBySigERC1271(fromAddresses, redeemFor, deadline, signature);

if (settings.pricePerToken > 0) {
_distributeFees(params.tokenContract, tokenCount);
_distributeFees(tokenContract, tokenCount);
}
}

/// @notice mints a token from reserve using the collection plus strategy
/// @param params Mint parameters
function mintFromReserve(MintParams calldata params) public payable {
CollectionPlusSettings memory settings = allowedCollections[params.tokenContract];
uint256 tokenCount = params.tokenIds.length;
function mintFromReserve(
address tokenContract,
address redeemFor,
uint256[] calldata tokenIds,
bytes calldata initData
) public payable {
CollectionPlusSettings memory settings = allowedCollections[tokenContract];
uint256 tokenCount = tokenIds.length;

_validateParams(settings, tokenCount);

unchecked {
for (uint256 i = 0; i < tokenCount; ++i) {
address account = erc6551Registry.createAccount(
erc6551Impl,
block.chainid,
settings.redeemToken,
params.tokenIds[i],
0,
params.initData
);
IPartialSoulboundToken(params.tokenContract).mintFromReserveAndLockTo(account, params.tokenIds[i]);

if (IERC721(settings.redeemToken).ownerOf(params.tokenIds[i]) != params.redeemFor) {
address account = erc6551Registry.createAccount(erc6551Impl, block.chainid, settings.redeemToken, tokenIds[i], 0, initData);
IPartialSoulboundToken(tokenContract).mintFromReserveAndLockTo(account, tokenIds[i]);

if (IERC721(settings.redeemToken).ownerOf(tokenIds[i]) != redeemFor) {
revert INVALID_OWNER();
}
}
}

if (settings.pricePerToken > 0) {
_distributeFees(params.tokenContract, tokenCount);
_distributeFees(tokenContract, tokenCount);
}
}

Expand Down
72 changes: 8 additions & 64 deletions test/CollectionPlusMinter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,7 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
uint256[] memory tokenIds = new uint256[](1);
tokenIds[0] = 6;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

minter.mintFromReserve(mintParams);
minter.mintFromReserve(address(token), claimer, tokenIds, "");

address tokenBoundAccount = erc6551Registry.account(erc6551Impl, block.chainid, address(redeemToken), 6, 0);

Expand Down Expand Up @@ -152,13 +145,6 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
uint256[] memory tokenIds = new uint256[](1);
tokenIds[0] = 6;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

address[] memory fromAddresses = new address[](1);
fromAddresses[0] = erc6551Registry.account(erc6551Impl, block.chainid, address(redeemToken), 6, 0);

Expand All @@ -171,7 +157,7 @@ contract MerkleReserveMinterTest is NounsBuilderTest {

assertEq(token.getVotes(claimer), 0);

minter.mintFromReserveAndDelegate(mintParams, signature, deadline);
minter.mintFromReserveAndDelegate(address(token), claimer, tokenIds, "", signature, deadline);

assertEq(soulboundToken.ownerOf(6), fromAddresses[0]);
assertEq(soulboundToken.locked(6), true);
Expand Down Expand Up @@ -204,14 +190,7 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
tokenIds[0] = 6;
tokenIds[1] = 7;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

minter.mintFromReserve(mintParams);
minter.mintFromReserve(address(token), claimer, tokenIds, "");

address tokenBoundAccount1 = erc6551Registry.account(erc6551Impl, block.chainid, address(redeemToken), 6, 0);
address tokenBoundAccount2 = erc6551Registry.account(erc6551Impl, block.chainid, address(redeemToken), 7, 0);
Expand Down Expand Up @@ -252,13 +231,6 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
tokenIds[0] = 6;
tokenIds[1] = 7;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

address[] memory fromAddresses = new address[](2);
fromAddresses[0] = erc6551Registry.account(erc6551Impl, block.chainid, address(redeemToken), 6, 0);
fromAddresses[1] = erc6551Registry.account(erc6551Impl, block.chainid, address(redeemToken), 7, 0);
Expand All @@ -272,7 +244,7 @@ contract MerkleReserveMinterTest is NounsBuilderTest {

assertEq(token.getVotes(claimer), 0);

minter.mintFromReserveAndDelegate(mintParams, signature, deadline);
minter.mintFromReserveAndDelegate(address(token), claimer, tokenIds, "", signature, deadline);

assertEq(soulboundToken.ownerOf(6), fromAddresses[0]);
assertEq(soulboundToken.locked(6), true);
Expand Down Expand Up @@ -309,20 +281,13 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
tokenIds[0] = 6;
tokenIds[1] = 7;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

uint256 fees = minter.getTotalFeesForMint(address(token), tokenIds.length);

uint256 prevTreasuryBalance = address(treasury).balance;
uint256 prevBuilderBalance = address(builderDAO).balance;
uint256 builderFee = minter.BUILDER_DAO_FEE() * tokenIds.length;

minter.mintFromReserve{ value: fees }(mintParams);
minter.mintFromReserve{ value: fees }(address(token), claimer, tokenIds, "");

assertEq(address(builderDAO).balance, prevBuilderBalance + builderFee);
assertEq(address(treasury).balance, prevTreasuryBalance + (fees - builderFee));
Expand Down Expand Up @@ -354,17 +319,10 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
tokenIds[0] = 6;
tokenIds[1] = 7;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

uint256 fees = minter.getTotalFeesForMint(address(token), tokenIds.length);

vm.expectRevert(abi.encodeWithSignature("MINT_NOT_STARTED()"));
minter.mintFromReserve{ value: fees }(mintParams);
minter.mintFromReserve{ value: fees }(address(token), claimer, tokenIds, "");
}

function testRevert_MintEnded() public {
Expand Down Expand Up @@ -395,17 +353,10 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
tokenIds[0] = 6;
tokenIds[1] = 7;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

uint256 fees = minter.getTotalFeesForMint(address(token), tokenIds.length);

vm.expectRevert(abi.encodeWithSignature("MINT_ENDED()"));
minter.mintFromReserve{ value: fees }(mintParams);
minter.mintFromReserve{ value: fees }(address(token), claimer, tokenIds, "");
}

function testRevert_InvalidValue() public {
Expand Down Expand Up @@ -436,14 +387,7 @@ contract MerkleReserveMinterTest is NounsBuilderTest {
tokenIds[0] = 6;
tokenIds[1] = 7;

CollectionPlusMinter.MintParams memory mintParams = CollectionPlusMinter.MintParams({
tokenContract: address(token),
tokenIds: tokenIds,
redeemFor: claimer,
initData: ""
});

vm.expectRevert(abi.encodeWithSignature("INVALID_VALUE()"));
minter.mintFromReserve{ value: 0.001 ether }(mintParams);
minter.mintFromReserve{ value: 0.0001 ether }(address(token), claimer, tokenIds, "");
}
}

0 comments on commit b77830d

Please sign in to comment.