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

feat: add tag logic #168

Merged
merged 1 commit into from
Aug 19, 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
6 changes: 6 additions & 0 deletions packages/zevm-app-contracts/contracts/xp-nft/xpNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract ZetaXP is ERC721Upgradeable, OwnableUpgradeable {
mapping(uint256 => uint256) public lastUpdateTimestampByTokenId;
mapping(uint256 => uint256) public signedUpByTokenId;
mapping(uint256 => bytes32) public tagByTokenId;
mapping(address => mapping(bytes32 => uint256)) public tokenByUserTag;

// Base URL for NFT images
string public baseTokenURI;
Expand All @@ -40,6 +41,7 @@ contract ZetaXP is ERC721Upgradeable, OwnableUpgradeable {
error LengthMismatch();
error TransferNotAllowed();
error OutdatedSignature();
error TagAlreadyHoldByUser();

function initialize(
string memory name,
Expand Down Expand Up @@ -122,13 +124,16 @@ contract ZetaXP is ERC721Upgradeable, OwnableUpgradeable {
_verify(tokenId, updateData);
lastUpdateTimestampByTokenId[tokenId] = updateData.sigTimestamp;
signedUpByTokenId[tokenId] = updateData.signedUp;
tagByTokenId[tokenId] = updateData.tag;
tokenByUserTag[updateData.to][updateData.tag] = tokenId;
}

// External mint function with auto-incremented token ID
function mintNFT(UpdateData memory mintData) external {
uint256 newTokenId = _currentTokenId;
_mint(mintData.to, newTokenId);

if (tokenByUserTag[mintData.to][mintData.tag] != 0) revert TagAlreadyHoldByUser();
_updateNFT(newTokenId, mintData);

emit NFTMinted(mintData.to, newTokenId, mintData.tag);
Expand All @@ -140,6 +145,7 @@ contract ZetaXP is ERC721Upgradeable, OwnableUpgradeable {
function updateNFT(uint256 tokenId, UpdateData memory updateData) external {
address owner = ownerOf(tokenId);
updateData.to = owner;
if (tokenByUserTag[owner][updateData.tag] != tokenId) revert TagAlreadyHoldByUser();
_updateNFT(tokenId, updateData);

emit NFTUpdated(owner, tokenId, updateData.tag);
Expand Down
66 changes: 65 additions & 1 deletion packages/zevm-app-contracts/test/xp-nft/xp-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ describe("XP NFT Contract test", () => {
await expect(url).to.be.eq(`${ZETA_BASE_URL}v2/`);

{
const sampleNFT2 = { ...sampleNFT, tokenId: 2 };
const sampleNFT2 = {
...sampleNFT,
tag: ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["string"], ["XP_NFT2"])),
tokenId: 2,
};
const currentBlock = await ethers.provider.getBlock("latest");
const sigTimestamp = currentBlock.timestamp;

Expand Down Expand Up @@ -250,4 +254,64 @@ describe("XP NFT Contract test", () => {
const version2 = await zetaXPV2.version();
await expect(version2).to.be.eq("2.0.0");
});

it("Should revert if user already have the tag", async () => {
{
const currentBlock = await ethers.provider.getBlock("latest");
const sigTimestamp = currentBlock.timestamp;

const signature = await getSignature(signer, sigTimestamp, sampleNFT.to, sampleNFT);

const nftParams: UpdateParam = {
...sampleNFT,
sigTimestamp,
signature,
} as UpdateParam;

await zetaXP.mintNFT(nftParams);
}

{
const sampleNFT2 = {
...sampleNFT,
tokenId: 2,
};
const currentBlock = await ethers.provider.getBlock("latest");
const sigTimestamp = currentBlock.timestamp;

const signature = await getSignature(signer, sigTimestamp, user.address, sampleNFT2);

const nftParams: UpdateParam = {
...sampleNFT2,
sigTimestamp,
signature,
} as UpdateParam;

const tx = zetaXP.mintNFT(nftParams);
await expect(tx).to.be.revertedWith("TagAlreadyHoldByUser");
}
});

it("Should query by tag and by user", async () => {
const currentBlock = await ethers.provider.getBlock("latest");
const sigTimestamp = currentBlock.timestamp;

const signature = await getSignature(signer, sigTimestamp, sampleNFT.to, sampleNFT);

const nftParams: UpdateParam = {
...sampleNFT,
sigTimestamp,
signature,
} as UpdateParam;

const tx = await zetaXP.mintNFT(nftParams);
const receipt = await tx.wait();
const tokenId = getTokenIdFromRecipient(receipt);

const queriedTokenId = await zetaXP.tokenByUserTag(sampleNFT.to, sampleNFT.tag);
await expect(queriedTokenId).to.be.eq(tokenId);

const queriedTag = await zetaXP.tagByTokenId(tokenId);
await expect(queriedTag).to.be.eq(sampleNFT.tag);
});
});
Loading