111 lines
5.1 KiB
TypeScript
111 lines
5.1 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 } = 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, [tokenId]);
|
|
expect(await nft.balanceOf(nftLock.target)).to.equal(1);
|
|
expect(await nftLock.lockedOriginal(nft.target, tokenId)).to.equal(otherAccount.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, [tokenId]);
|
|
const nonce = (Math.random() * 1000) | 0;
|
|
const now = Date.now() / 1000 | 0;
|
|
let localMsgHash = solidityPackedKeccak256(["address", "address", "address", "uint256", "uint256", "uint256", "uint256", "bool"],
|
|
[otherAccount.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, false]);
|
|
const signature = await owner.signMessage(getBytes(localMsgHash));
|
|
//@ts-ignore
|
|
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", "bool"],
|
|
[otherAccount.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, isMint]);
|
|
const signature = await owner.signMessage(getBytes(localMsgHash));
|
|
//@ts-ignore
|
|
await nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, 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", "bool"],
|
|
[otherAccount.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, isMint]);
|
|
const signature = await owner.signMessage(getBytes(localMsgHash));
|
|
//@ts-ignore
|
|
await expect(nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, isMint]], now, nonce, signature)).to.be.revertedWith(
|
|
"ERC721: token already minted"
|
|
);
|
|
});
|
|
});
|
|
|
|
|
|
}) |