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(cachable-nft): fix removing NFT from cache & add cache state getter #116

Merged
merged 2 commits into from
Nov 6, 2024
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
16 changes: 15 additions & 1 deletion contracts/story-nft/CachableNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ abstract contract CachableNFT is OwnableUpgradeable {
return $.cache.length();
}

/// @notice Returns the cache mode.
/// @return The cache mode, true for cache mode, false for passthrough mode.
function getCacheMode() external view returns (bool) {
return _getCacheableNFTStorage().cacheMode;
}

/// @notice Returns the NFT at the given index in the cache.
/// @param index The index of the NFT in the cache.
/// @return tokenId The token ID of the NFT.
/// @return ipId The IP ID of the NFT.
function getCacheAtIndex(uint256 index) external view returns (uint256 tokenId, address ipId) {
return _getCacheableNFTStorage().cache.at(index);
}

/// @notice Transfers the first NFT from the cache to the recipient.
/// @param recipient The recipient of the NFT.
/// @return tokenId The token ID of the transferred NFT.
Expand All @@ -59,7 +73,7 @@ abstract contract CachableNFT is OwnableUpgradeable {
return (0, address(0));
}
(tokenId, ipId) = $.cache.at(0);
$.cache.remove(0);
$.cache.remove(tokenId);

_transferFrom(address(this), recipient, tokenId);
}
Expand Down
10 changes: 8 additions & 2 deletions test/story-nft/StoryBadgeNFT.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ contract StoryBadgeNFTTest is BaseTest {
}

function test_StoryBadgeNFT_cachedMint() public {
bytes memory signature = _signAddress(rootOrgStoryNftSignerSk, u.alice);
vm.startPrank(u.alice);
(uint256 tokenId, ) = rootOrgStoryNft.mint(u.alice, signature);
assertEq(rootOrgStoryNft.ownerOf(tokenId), u.alice); // minted directly
vm.stopPrank();

vm.startPrank(rootOrgStoryNftOwner);
rootOrgStoryNft.mintToCache(1);
assertEq(rootOrgStoryNft.cacheSize(), 1); // 1 cached
Expand All @@ -221,9 +227,9 @@ contract StoryBadgeNFTTest is BaseTest {
rootOrgStoryNft.setCacheMode(true); // enable cache mode
vm.stopPrank();

bytes memory signature = _signAddress(rootOrgStoryNftSignerSk, u.carl);
signature = _signAddress(rootOrgStoryNftSignerSk, u.carl);
vm.startPrank(u.carl);
(uint256 tokenId, ) = rootOrgStoryNft.mint(u.carl, signature);
(tokenId, ) = rootOrgStoryNft.mint(u.carl, signature);
assertEq(rootOrgStoryNft.ownerOf(tokenId), u.carl); // minted from cache
vm.stopPrank();
assertEq(rootOrgStoryNft.cacheSize(), 100); // cache size is reduced by 1
Expand Down
Loading