Skip to content

Commit

Permalink
chore: lock & unlock should update totalLockedNFT
Browse files Browse the repository at this point in the history
  • Loading branch information
pyk committed Jan 27, 2024
1 parent 449138c commit 1ae3d6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/LlamaLocker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract LlamaLocker is ERC721Holder, Ownable2Step {
IERC721 public nft;
mapping(uint256 tokenId => address owner) private nftOwners;
uint256 public constant REWARD_DURATION = 7 days;
uint256 public totalLockedNft;
uint256 public totalLockedNFT;

error RenounceInvalid();
error RewardTokenExists();
Expand Down Expand Up @@ -89,7 +89,7 @@ contract LlamaLocker is ERC721Holder, Ownable2Step {
function distribute(IERC20 _token, uint256 _amount) external onlyOwner {
if (rewardStates[_token].updatedAt == 0) revert RewardTokenNotExists();
if (_amount == 0) revert RewardAmountInvalid();
if (totalLockedNft == 0) revert NoLockers();
if (totalLockedNFT == 0) revert NoLockers();
//
}

Expand All @@ -100,11 +100,18 @@ contract LlamaLocker is ERC721Holder, Ownable2Step {
nft.safeTransferFrom(msg.sender, address(this), tokenIds_[i]);
nftOwners[tokenIds_[i]] = msg.sender;
}

// Increase total locked NFT
totalLockedNFT += tokenCount;
}

function unlock(uint256[] calldata tokenIds_) external {
uint256 tokenCount = tokenIds_.length;
if (tokenCount == 0) revert LockZeroToken();

// Decrease total locked NFT
totalLockedNFT -= tokenCount;

for (uint256 i = 0; i < tokenCount; ++i) {
if (nftOwners[tokenIds_[i]] != msg.sender) revert UnlockOwnerInvalid();
nft.safeTransferFrom(address(this), msg.sender, tokenIds_[i]);
Expand Down
20 changes: 18 additions & 2 deletions test/LlamaLocker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ contract LlamaLockerTest is Test {
// assertEq(data.lastUpdatedAt, block.timestamp);
// }

//************************************************************//
// Lock NFT //
//************************************************************//

function testLockNFTZeroToken() public {
uint256[] memory tokenIds = new uint256[](0);

vm.expectRevert(abi.encodeWithSelector(LlamaLocker.LockZeroToken.selector));
locker.lock(tokenIds);
}

function testLockNFTTransferred() public {
function testLockNFT() public {
uint256 tokenId1 = nft.mint(alice);
uint256 tokenId2 = nft.mint(alice);
uint256[] memory tokenIds = new uint256[](2);
Expand All @@ -158,10 +162,18 @@ contract LlamaLockerTest is Test {
locker.lock(tokenIds);
vm.stopPrank();

// NFT should be transfered to locker
assertEq(nft.ownerOf(tokenId1), address(locker));
assertEq(nft.ownerOf(tokenId2), address(locker));

// total locked NFT should be increased
assertEq(locker.totalLockedNFT(), 2);
}

//************************************************************//
// Unlock NFT //
//************************************************************//

function testUnlockNFTInvalidOwner() public {
uint256 tokenId1 = nft.mint(alice);
uint256 tokenId2 = nft.mint(alice);
Expand All @@ -179,7 +191,7 @@ contract LlamaLockerTest is Test {
locker.unlock(tokenIds);
}

function testUnlockNFTTransfered() public {
function testUnlockNFT() public {
uint256 tokenId1 = nft.mint(alice);
uint256 tokenId2 = nft.mint(alice);
uint256[] memory tokenIds = new uint256[](2);
Expand All @@ -192,7 +204,11 @@ contract LlamaLockerTest is Test {
locker.unlock(tokenIds);
vm.stopPrank();

// Unlocked NFT should be transferred
assertEq(nft.ownerOf(tokenId1), alice);
assertEq(nft.ownerOf(tokenId2), alice);

// totalLockedNFT should decrease
assertEq(locker.totalLockedNFT(), 0);
}
}

0 comments on commit 1ae3d6f

Please sign in to comment.