function getUserMemorabiliaDetailed(address user)
external
view
returns (uint256[] memory tokenIds, uint256[] memory collectionIds, uint256[] memory itemIds)
{
uint256 count = 0;
for (uint256 cId = 1; cId < nextCollectionId; cId++) {
for (uint256 iId = 1; iId < collections[cId].currentItemId; iId++) {
uint256 tokenId = encodeTokenId(cId, iId);
if (balanceOf(user, tokenId) > 0) {
count++;
}
}
}
tokenIds = new uint256[](count);
collectionIds = new uint256[](count);
itemIds = new uint256[](count);
uint256 index = 0;
@> for (uint256 cId = 1; cId < nextCollectionId; cId++) {
for (uint256 iId = 1; iId < collections[cId].currentItemId; iId++) {
uint256 tokenId = encodeTokenId(cId, iId);
if (balanceOf(user, tokenId) > 0) {
tokenIds[index] = tokenId;
collectionIds[index] = cId;
itemIds[index] = iId;
index++;
}
}
}
return (tokenIds, collectionIds, itemIds);
}
function getUserMemorabiliaDetailed(address user)
external
view
returns (uint256[] memory tokenIds, uint256[] memory collectionIds, uint256[] memory itemIds)
{
- for (uint256 cId = 1; cId < nextCollectionId; cId++) {
- for (uint256 iId = 1; iId < collections[cId].currentItemId; iId++) {
- uint256 tokenId = encodeTokenId(cId, iId);
- if (balanceOf(user, tokenId) > 0) {
- count++;
- }
- }
- }
+ // Calculate max possible using currentItemId (only minted items)
+ uint256 maxPossible = 0;
+ for (uint256 cId = 1; cId < nextCollectionId; cId++) {
+ if (collections[cId].currentItemId > 1) {
+ maxPossible += collections[cId].currentItemId - 1;
+ }
+ }
+ uint256[] memory tempTokenIds = new uint256[](maxPossible);
+ uint256[] memory tempCollectionIds = new uint256[](maxPossible);
+ uint256[] memory tempItemIds = new uint256[](maxPossible);
- tokenIds = new uint256[](count);
- collectionIds = new uint256[](count);
- itemIds = new uint256[](count);
- uint256 index = 0;
- for (uint256 cId = 1; cId < nextCollectionId; cId++) {
- for (uint256 iId = 1; iId < collections[cId].currentItemId; iId++) {
- uint256 tokenId = encodeTokenId(cId, iId);
- if (balanceOf(user, tokenId) > 0) {
- tokenIds[index] = tokenId;
- collectionIds[index] = cId;
- itemIds[index] = iId;
- index++;
- }
- }
- }
uint256 count = 0;
+ for (uint256 cId = 1; cId < nextCollectionId; cId++) {
+ for (uint256 iId = 1; iId < collections[cId].currentItemId; iId++) {
+ uint256 tokenId = encodeTokenId(cId, iId);
+ if (balanceOf(user, tokenId) > 0) {
+ tempTokenIds[count] = tokenId;
+ tempCollectionIds[count] = cId;
+ tempItemIds[count] = iId;
+ count++;
+ }
+ }
+ }
+ // Resize to actual count
+ assembly {
+ mstore(tempTokenIds, count)
+ mstore(tempCollectionIds, count)
+ mstore(tempItemIds, count)
+ }
return (tempTokenIds, tempCollectionIds, tempItemIds);
}