Skip to content

Commit

Permalink
fix: update for variations
Browse files Browse the repository at this point in the history
Signed-off-by: Campion Fellin <[email protected]>
  • Loading branch information
campionfellin committed Oct 30, 2023
1 parent 84fafcf commit 1fd76e2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface IPhysicalClaimCore is IERC165, IERC721Receiver {
error InvalidRedeemAmount();
error InvalidPaymentAmount();
error InvalidSignature();
error InvalidVariation();

/**
* @notice the validation type used for a `BurnItem`
Expand Down
41 changes: 32 additions & 9 deletions packages/manifold/contracts/physicalclaim/PhysicalClaimCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ abstract contract PhysicalClaimCore is ERC165, AdminControl, ReentrancyGuard, IP
// { instanceId => { redeemer => Redemption } }
mapping(uint256 => mapping(address => Redemption[])) internal _redemptions;

// { instanceId => { variation => count } }
mapping(uint256 => mapping(uint8 => uint32)) internal _variationRedemptions;

// { instanceId => nonce => t/f }
mapping(uint256 => mapping(bytes32 => bool)) internal _usedMessages;

Expand Down Expand Up @@ -94,6 +97,7 @@ abstract contract PhysicalClaimCore is ERC165, AdminControl, ReentrancyGuard, IP
return _redemptions[instanceId][redeemer];
}


/**
* Helper to get physical claim instance
*/
Expand Down Expand Up @@ -137,7 +141,7 @@ abstract contract PhysicalClaimCore is ERC165, AdminControl, ReentrancyGuard, IP
PhysicalClaim storage physicalClaimInstance = _getPhysicalClaim(submission.instanceId);

// Get the amount that can be burned
uint32 physicalClaimCount = _getAvailablePhysicalClaimCount(physicalClaimInstance.totalSupply, physicalClaimInstance.redeemedCount, submission.physicalClaimCount);
uint32 physicalClaimCount = _getAvailablePhysicalClaimCount(physicalClaimInstance.totalSupply, physicalClaimInstance.redeemedCount, submission.physicalClaimCount, submission.variation, physicalClaimInstance.variations, submission.instanceId);

if (physicalClaimInstance.signer != address(0)) {
// Check that the message value is what was signed...
Expand Down Expand Up @@ -209,7 +213,7 @@ abstract contract PhysicalClaimCore is ERC165, AdminControl, ReentrancyGuard, IP
// 2. The burn only requires one NFT (one burnSet element and one count)
_validateReceivedInput(physicalClaimInstance.burnSet.length, physicalClaimInstance.burnSet[0].requiredCount);

_getAvailablePhysicalClaimCount(physicalClaimInstance.totalSupply, physicalClaimInstance.redeemedCount, 1);
_getAvailablePhysicalClaimCount(physicalClaimInstance.totalSupply, physicalClaimInstance.redeemedCount, 1, variation, physicalClaimInstance.variations, instanceId);

// Check that the burn token is valid
BurnItem memory burnItem = physicalClaimInstance.burnSet[0].items[burnItemIndex];
Expand Down Expand Up @@ -272,15 +276,34 @@ abstract contract PhysicalClaimCore is ERC165, AdminControl, ReentrancyGuard, IP
/**
* Helper to get the number of burn redeems the person can accomplish
*/
function _getAvailablePhysicalClaimCount(uint32 totalSupply, uint32 redeemedCount, uint32 desiredCount) internal pure returns(uint32 burnRedeemCount) {
function _getAvailablePhysicalClaimCount(uint32 totalSupply, uint32 redeemedCount, uint32 desiredCount, uint8 variation, Variation[] memory variations, uint256 instanceId) internal view returns(uint32 burnRedeemCount) {
if (totalSupply == 0) {
burnRedeemCount = desiredCount;
} else {
uint32 remainingCount = (totalSupply - redeemedCount);
if (remainingCount > desiredCount) {
uint32 remainingTotalCount = (totalSupply - redeemedCount);

Variation memory variationSelected;
// Get max redemptions for this variation...
for (uint i = 0; i < variations.length; i++) {
if (variations[i].id == variation) {
variationSelected = variations[i];
break;
}
}

if (variationSelected.id == 0) {
revert InvalidVariation();
}

uint32 variationRemainingCount = (variationSelected.max - _variationRedemptions[instanceId][variation]);

// Use whichever is lesser...
uint32 comparator = remainingTotalCount > variationRemainingCount ? variationRemainingCount : remainingTotalCount;

if (comparator > desiredCount) {
burnRedeemCount = desiredCount;
} else {
burnRedeemCount = remainingCount;
burnRedeemCount = comparator;
}
}

Expand Down Expand Up @@ -328,14 +351,14 @@ abstract contract PhysicalClaimCore is ERC165, AdminControl, ReentrancyGuard, IP
revert InvalidInput();
}
physicalClaimInstance.redeemedCount += uint32(totalCount);
Redemption[] memory redemptions = new Redemption[](1);
redemptions[0] = Redemption({
Redemption memory redemption = Redemption({
timestamp: uint16(block.timestamp),
redeemedCount: count,
variation: variation
});

_redemptions[instanceId][to].push(redemptions[0]);
_redemptions[instanceId][to].push(redemption);
_variationRedemptions[instanceId][variation]++;
emit PhysicalClaimLib.PhysicalClaimRedemption(instanceId, variation, count, data);
}
}
44 changes: 22 additions & 22 deletions packages/manifold/test/physicalclaim/PhysicalClaim.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ contract PhysicalClaimTest is Test {
IPhysicalClaimCore.Variation[] memory variations = new IPhysicalClaimCore.Variation[](1);
variations[0] = IPhysicalClaimCore.Variation({
id: 1,
max: 1
max: 10
});

// Create claim initialization parameters
Expand Down Expand Up @@ -171,7 +171,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -282,7 +282,7 @@ contract PhysicalClaimTest is Test {
IPhysicalClaimCore.Variation[] memory variations = new IPhysicalClaimCore.Variation[](1);
variations[0] = IPhysicalClaimCore.Variation({
id: 1,
max: 1
max: 100
});

// Create claim initialization parameters. Total supply is 1 so they will use the whole supply
Expand Down Expand Up @@ -320,7 +320,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand All @@ -340,7 +340,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 1;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

vm.expectRevert(); // should revert cause none remaining and the setting is to revert it...
Expand Down Expand Up @@ -433,7 +433,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -469,7 +469,7 @@ contract PhysicalClaimTest is Test {
IPhysicalClaimCore.Variation[] memory variations = new IPhysicalClaimCore.Variation[](1);
variations[0] = IPhysicalClaimCore.Variation({
id: 1,
max: 1
max: 10
});

// Create claim initialization parameters. Total supply is 1 so they will use the whole supply
Expand Down Expand Up @@ -508,7 +508,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

IPhysicalClaimCore.BurnToken[] memory burnTokens2 = new IPhysicalClaimCore.BurnToken[](1);
Expand All @@ -523,7 +523,7 @@ contract PhysicalClaimTest is Test {
submissions[1].physicalClaimCount = 1;
submissions[1].currentClaimCount = 1;
submissions[1].burnTokens = burnTokens2;
submissions[1].variation = 0;
submissions[1].variation = 1;
submissions[1].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -596,7 +596,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 2;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -668,7 +668,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 2;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -740,7 +740,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 2;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

vm.expectRevert();
Expand Down Expand Up @@ -813,7 +813,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -885,7 +885,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

example.burnRedeem(submissions);
Expand Down Expand Up @@ -957,7 +957,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

vm.expectRevert();
Expand Down Expand Up @@ -1032,7 +1032,7 @@ contract PhysicalClaimTest is Test {
submissions[0].physicalClaimCount = 1;
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

vm.expectRevert();
Expand Down Expand Up @@ -1188,7 +1188,7 @@ contract PhysicalClaimTest is Test {
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].totalCost = 1;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

vm.expectRevert();
Expand Down Expand Up @@ -1261,7 +1261,7 @@ contract PhysicalClaimTest is Test {
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].totalCost = 1;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";

vm.expectRevert();
Expand Down Expand Up @@ -1334,7 +1334,7 @@ contract PhysicalClaimTest is Test {
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].totalCost = 1;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";
submissions[0].message = "Hello";
submissions[0].nonce = "";
Expand Down Expand Up @@ -1416,7 +1416,7 @@ contract PhysicalClaimTest is Test {
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].totalCost = 1;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";
submissions[0].message = "Hello";
submissions[0].nonce = "";
Expand Down Expand Up @@ -1498,7 +1498,7 @@ contract PhysicalClaimTest is Test {
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].totalCost = 1;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";
submissions[0].nonce = "abcd";

Expand Down Expand Up @@ -1580,7 +1580,7 @@ contract PhysicalClaimTest is Test {
submissions[0].currentClaimCount = 0;
submissions[0].burnTokens = burnTokens;
submissions[0].totalCost = 1;
submissions[0].variation = 0;
submissions[0].variation = 1;
submissions[0].data = "";
submissions[0].nonce = "abcd";

Expand Down

0 comments on commit 1fd76e2

Please sign in to comment.