Skip to content

Commit 3ab91c9

Browse files
committed
fix(cachable-nft): rm from key & add cache getter
1 parent b316e5c commit 3ab91c9

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

contracts/story-nft/CachableNFT.sol

+21-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ abstract contract CachableNFT is OwnableUpgradeable {
4949
return $.cache.length();
5050
}
5151

52+
/// @notice Returns the cache mode.
53+
/// @return The cache mode, true for cache mode, false for passthrough mode.
54+
function getCacheMode() external view returns (bool) {
55+
return _getCacheableNFTStorage().cacheMode;
56+
}
57+
58+
/// @notice Returns the NFT at the given index in the cache.
59+
/// @param index The index of the NFT in the cache.
60+
/// @return tokenId The token ID of the NFT.
61+
/// @return ipId The IP ID of the NFT.
62+
function getCacheAtIndex(uint256 index) external view returns (uint256 tokenId, address ipId) {
63+
return _getCacheableNFTStorage().cache.at(index);
64+
}
65+
66+
/// @notice Returns the number of NFTs in the cache.
67+
/// @return The number of NFTs in the cache.
68+
function getCacheLength() external view returns (uint256) {
69+
return _getCacheableNFTStorage().cache.length();
70+
}
71+
5272
/// @notice Transfers the first NFT from the cache to the recipient.
5373
/// @param recipient The recipient of the NFT.
5474
/// @return tokenId The token ID of the transferred NFT.
@@ -59,7 +79,7 @@ abstract contract CachableNFT is OwnableUpgradeable {
5979
return (0, address(0));
6080
}
6181
(tokenId, ipId) = $.cache.at(0);
62-
$.cache.remove(0);
82+
$.cache.remove(tokenId);
6383

6484
_transferFrom(address(this), recipient, tokenId);
6585
}

test/story-nft/StoryBadgeNFT.t.sol

+8-2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ contract StoryBadgeNFTTest is BaseTest {
213213
}
214214

215215
function test_StoryBadgeNFT_cachedMint() public {
216+
bytes memory signature = _signAddress(rootOrgStoryNftSignerSk, u.alice);
217+
vm.startPrank(u.alice);
218+
(uint256 tokenId, ) = rootOrgStoryNft.mint(u.alice, signature);
219+
assertEq(rootOrgStoryNft.ownerOf(tokenId), u.alice); // minted directly
220+
vm.stopPrank();
221+
216222
vm.startPrank(rootOrgStoryNftOwner);
217223
rootOrgStoryNft.mintToCache(1);
218224
assertEq(rootOrgStoryNft.cacheSize(), 1); // 1 cached
@@ -221,9 +227,9 @@ contract StoryBadgeNFTTest is BaseTest {
221227
rootOrgStoryNft.setCacheMode(true); // enable cache mode
222228
vm.stopPrank();
223229

224-
bytes memory signature = _signAddress(rootOrgStoryNftSignerSk, u.carl);
230+
signature = _signAddress(rootOrgStoryNftSignerSk, u.carl);
225231
vm.startPrank(u.carl);
226-
(uint256 tokenId, ) = rootOrgStoryNft.mint(u.carl, signature);
232+
(tokenId, ) = rootOrgStoryNft.mint(u.carl, signature);
227233
assertEq(rootOrgStoryNft.ownerOf(tokenId), u.carl); // minted from cache
228234
vm.stopPrank();
229235
assertEq(rootOrgStoryNft.cacheSize(), 100); // cache size is reduced by 1

0 commit comments

Comments
 (0)