Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix twin inefficiencies #656

Merged
merged 15 commits into from
Jul 7, 2023
34 changes: 34 additions & 0 deletions contracts/protocol/facets/ExchangeHandlerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,40 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
twinReceipt.amount = twin.amount;
twinReceipt.tokenType = twin.tokenType;

if (twin.tokenType == TokenType.NonFungibleToken) {
// Get all ranges of twins that belong to the seller and to the same token address of the new twin to validate if range is available
TokenRange[] storage twinRanges = lookups.twinRangesBySeller[seller.id][twin.tokenAddress];

uint256 lastIndex = twinRanges.length - 1;

// Find the range that contains the twin
zajck marked this conversation as resolved.
Show resolved Hide resolved
for (uint256 j = 0; j <= lastIndex; j++) {
if (twinRanges[j].start <= tokenId && twinRanges[j].end >= tokenId) {
zajck marked this conversation as resolved.
Show resolved Hide resolved
bool unlimitedSupply = twin.supplyAvailable == type(uint256).max;

// Limited: Order is descending first tokenId is the last one to be transferred
// Unlimited: Order is ascending first tokenId is the first one to be transferred
if ((unlimitedSupply ? twinRanges[j].end : twinRanges[j].start) == tokenId) {
// Remove from ranges mapping
if (j != lastIndex) {
twinRanges[j] = twinRanges[lastIndex];
}

twinRanges.pop();
break;
}

// If twin has unlimited supply
if (unlimitedSupply) {
// Increase start property
twinRanges[j].start++;
} else {
// Otherwise reduce end property
twinRanges[j].end--;
}
}
}
}
emit TwinTransferred(twin.id, twin.tokenAddress, exchangeId, tokenId, twin.amount, sender);
}
}
Expand Down
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading