contracts-imtbl/test/testNFTLocker.ts
2024-07-01 16:13:26 +08:00

126 lines
6.0 KiB
TypeScript

import { expect } from 'chai'
import hre from "hardhat";
import {
getBytes,
solidityPackedKeccak256,
} from 'ethers'
import {
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
describe('NFTLock', function() {
async function deployOneContract() {
const [owner, otherAccount] = await hre.ethers.getSigners();
const verifier = owner.address;
const OperatorAllowlist = await hre.ethers.getContractFactory("OperatorAllowlist");
const operatorAllowlist = await OperatorAllowlist.deploy(owner.address);
const CFNFTGame = await hre.ethers.getContractFactory("CFNFTGame");
const nft = await CFNFTGame.deploy(owner.address, 'name', 'symbol', 'baseURI', 'contractURI', operatorAllowlist.target, owner.address, 5);
const nftAddress = nft.target;
const NFTLock = await hre.ethers.getContractFactory("NFTLock");
const nftLock = await NFTLock.deploy( 3600, verifier );
await nft.grantMinterRole(nftLock.target)
await nft.grantMinterRole(owner.address)
await nftLock.addSupportNftList([nftAddress]);
await nft.mint(otherAccount.address, "1001");
const chainId = hre.network.config.chainId
await operatorAllowlist.grantRegistrarRole(owner.address)
await operatorAllowlist.addAddressToAllowlist([nftLock.target])
return { nftLock, owner, otherAccount, verifier, nftAddress, nft, chainId };
}
describe("Deployment", function () {
it('should deploy NFTLock with the correct verifier', async function() {
const { nftLock, verifier } = await loadFixture(deployOneContract);
expect(await nftLock.verifier()).to.equal(verifier);
});
it('should deploy NFTLock with the correct NFT address', async function() {
const { nftLock, nftAddress } = await loadFixture(deployOneContract);
expect(await nftLock.supportNftList(nftAddress)).to.equal(true);
});
})
describe("Lock", function () {
it('should lock NFT', async function() {
const { nftLock, nft, otherAccount, owner } = await loadFixture(deployOneContract);
const tokenId = "1001"
// @ts-ignore
await nft.connect(otherAccount).approve(nftLock.target, tokenId);
//@ts-ignore
await nftLock.connect(otherAccount).lock(nft.target, owner.address, [tokenId]);
expect(await nft.balanceOf(nftLock.target)).to.equal(1);
expect(await nftLock.passportOriginal(nft.target, tokenId)).to.equal(owner.address);
expect(await nft.ownerOf(tokenId)).to.equal(nftLock.target);
});
})
describe("UnLock", function () {
it('should unlock NFT from lock', async function() {
const { nftLock, nft, otherAccount, chainId, owner } = await loadFixture(deployOneContract);
const tokenId = '1001'
// @ts-ignore
await nft.connect(otherAccount).approve(nftLock.target, tokenId);
//@ts-ignore
await nftLock.connect(otherAccount).lock(nft.target, owner.address, [tokenId]);
const nonce = (Math.random() * 1000) | 0;
const now = Date.now() / 1000 | 0;
let localMsgHash = solidityPackedKeccak256(["address", "address", "address", "uint256", "uint256", "uint256", "uint256", "address", "bool"],
[owner.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, otherAccount.address, false]);
const signature = await owner.signMessage(getBytes(localMsgHash));
//@ts-ignore
await nftLock.unlockOrMint(nft.target, [[tokenId, otherAccount.address, false]], now, nonce, signature);
// await nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, false]], now, nonce, signature);
expect(await nft.ownerOf(tokenId)).to.equal(otherAccount.address);
});
it('should mint NFT from lock', async function() {
const { nftLock, nft, otherAccount, chainId, owner } = await loadFixture(deployOneContract);
const tokenId = '1002'
const isMint = true
const nonce = (Math.random() * 1000) | 0;
const now = Date.now() / 1000 | 0;
let localMsgHash = solidityPackedKeccak256(["address", "address", "address", "uint256", "uint256", "uint256", "uint256", "address", "bool"],
[otherAccount.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, otherAccount.address, isMint]);
const signature = await owner.signMessage(getBytes(localMsgHash));
//@ts-ignore
await nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, otherAccount.address, isMint]], now, nonce, signature);
expect(await nft.ownerOf(tokenId)).to.equal(otherAccount.address);
});
it('should revert NFT mint for nft already minted', async function() {
const { nftLock, nft, otherAccount, chainId, owner } = await loadFixture(deployOneContract);
const tokenId = '1001'
const isMint = true
const nonce = (Math.random() * 1000) | 0;
const now = Date.now() / 1000 | 0;
let localMsgHash = solidityPackedKeccak256(["address", "address", "address", "uint256", "uint256", "uint256", "uint256", "address", "bool"],
[otherAccount.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, otherAccount.address, isMint]);
const signature = await owner.signMessage(getBytes(localMsgHash));
//@ts-ignore
await expect(nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, otherAccount.address, isMint]], now, nonce, signature)).to.be.revertedWith(
"ERC721: token already minted"
);
});
it('should unlock NFT with svr', async function() {
const { nftLock, nft, otherAccount, chainId, owner } = await loadFixture(deployOneContract);
const tokenId = '1001'
// @ts-ignore
await nft.connect(otherAccount).approve(nftLock.target, tokenId);
//@ts-ignore
await nftLock.connect(otherAccount).lock(nft.target, owner.address, [tokenId]);
//@ts-ignore
await nftLock.unlockWithSvr(nft.target, [tokenId]);
// await nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, false]], now, nonce, signature);
expect(await nft.ownerOf(tokenId)).to.equal(otherAccount.address);
});
});
})