From 76381344674f407050df31309c247eee8dbf1c37 Mon Sep 17 00:00:00 2001 From: dwong <0xdwong@gmail.com> Date: Wed, 16 Oct 2024 10:27:35 +0800 Subject: [PATCH] Update 852f5836-a03d-4483-a7e0-b0f6f8bda01c.json --- translation/en/852f5836-a03d-4483-a7e0-b0f6f8bda01c.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translation/en/852f5836-a03d-4483-a7e0-b0f6f8bda01c.json b/translation/en/852f5836-a03d-4483-a7e0-b0f6f8bda01c.json index a80c796..63cd685 100644 --- a/translation/en/852f5836-a03d-4483-a7e0-b0f6f8bda01c.json +++ b/translation/en/852f5836-a03d-4483-a7e0-b0f6f8bda01c.json @@ -20,7 +20,7 @@ "spj_code": [ { "frame": "Hardhat", - "code": "const { expect } = require('chai');\nconst { ethers } = require('hardhat');\n\n\ndescribe(\"BaseERC721\", async () => {\n let contract, contractAddr;\n let receivercontract, receivercontractAddr;\n let accounts, owner;\n const name = 'BaseERC721';\n const symbol = 'BERC721';\n const baseURI = 'https://images.example.com/';\n\n const randomAccount = ethers.Wallet.createRandom();\n const randomAddr = randomAccount.address;\n const ZeroAddress = ethers.constants.AddressZero;\n\n\n async function init() {\n // 部署 BaseERC721\n accounts = await ethers.getSigners();\n owner = accounts[0];\n\n {\n const factory = await ethers.getContractFactory('BaseERC721');\n contract = await factory.deploy(...[name, symbol, baseURI]);\n await contract.deployed();\n }\n {\n const factory = await ethers.getContractFactory('BaseERC721Receiver');\n receivercontract = await factory.deploy();\n await receivercontract.deployed();\n }\n\n contractAddr = contract.address;\n receivercontractAddr = receivercontract.address;\n }\n\n beforeEach(async () => {\n await init();\n })\n\n describe(\"IERC721Metadata\", async () => {\n it(\"name\", async () => {\n expect(await contract.name()).to.equal(name);\n });\n\n it(\"symbol\", async () => {\n expect(await contract.symbol()).to.equal(symbol);\n });\n\n describe(\"tokenURI\", async () => {\n it(\"URI query for nonexistent token should revert\", async () => {\n const NONE_EXISTENT_TOKEN_ID = 1234\n await expect(\n contract.tokenURI(NONE_EXISTENT_TOKEN_ID)\n ).to.be.revertedWith(\"ERC721Metadata: URI query for nonexistent token\");\n });\n\n it('Should return baseURI when tokenId exists', async function () {\n const tokenId = 1\n await contract.connect(owner).mint(randomAddr, tokenId);\n\n // const expectURI = baseURI + String(tokenId);\n const expectURI = baseURI;\n expect(await contract.tokenURI(tokenId)).to.equal(expectURI);\n });\n })\n })\n\n describe(\"IERC721\", async () => {\n describe(\"balanceOf \", async () => {\n it(\"balanceOf\", async () => {\n // before should be 0\n const beforeBalance = await contract.balanceOf(randomAddr);\n expect(beforeBalance).to.equal(0);\n\n // mint\n await contract.connect(owner).mint(randomAddr, 1);\n\n // after should be 1\n const afterBalance = await contract.balanceOf(randomAddr);\n expect(afterBalance).to.equal(1);\n });\n });\n\n describe(\"ownerOf \", async () => {\n it(\"ownerOf\", async () => {\n const tokenId = 1;\n const receiver = randomAddr;\n // before should be 0 address\n const beforeBalance = await contract.ownerOf(tokenId);\n expect(beforeBalance).to.equal(ZeroAddress);\n\n // mint\n await contract.connect(owner).mint(receiver, 1);\n\n // after should be receiver\n const holder = await contract.ownerOf(tokenId);\n expect(holder).to.equal(receiver);\n });\n });\n\n describe('approve', function () {\n it('owner should approve successfully', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); //mint to self\n\n const to = randomAddr;\n await expect(\n contract.connect(owner).approve(to, tokenId)\n ).to.emit(contract, \"Approval\")\n .withArgs(owner.address, to, tokenId);\n\n // getApproved\n expect(await contract.getApproved(tokenId)).to.equal(to);\n });\n\n it('approved account should approve successfully', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); //mint to self\n\n // setApprovalForAll accounts1\n const caller = accounts[1];\n await contract.connect(owner).setApprovalForAll(caller.address, true)\n\n // accounts1 arpprove owner's tokenId[1] to randomAddr\n const to = randomAddr;\n expect(\n await contract.connect(caller).approve(to, tokenId)\n ).to.be.ok;\n\n // getApproved\n expect(await contract.getApproved(tokenId)).to.equal(to);\n });\n\n it('Approve to current owner should revert', async function () {\n // mint token first\n const tokenId = 1;\n const receiver = owner.address; //self\n await contract.connect(owner).mint(receiver, tokenId);\n\n await expect(\n contract.connect(owner).approve(receiver, tokenId)\n ).to.be.revertedWith(\"ERC721: approval to current owner\");\n });\n\n it('Not owner nor approved token approveal should revert', async function () {\n // mint token first\n const tokenId = 1;\n const receiver = owner.address; //self\n await contract.connect(owner).mint(receiver, tokenId);\n\n const otherAccount = accounts[1]; //not owner or approved\n await expect(\n contract.connect(otherAccount).approve(randomAddr, tokenId)\n ).to.be.revertedWith(\"ERC721: approve caller is not owner nor approved for all\");\n });\n });\n\n describe('getApproved', function () {\n it('should return approval address', async function () {\n // mint token first\n const tokenId = 1;\n const receiver = owner.address; //self\n await contract.connect(owner).mint(receiver, tokenId);\n\n // approve\n const approvedAddr = randomAddr;\n await contract.connect(owner).approve(randomAddr, tokenId);\n\n expect(await contract.getApproved(tokenId)).to.equal(approvedAddr);\n });\n\n it('Approved query for nonexistent token should revert', async function () {\n const tokenId = 1; // not exists\n\n await expect(\n contract.getApproved(tokenId)\n ).to.be.revertedWith('ERC721: approved query for nonexistent token');\n });\n });\n\n describe('setApprovalForAll', function () {\n it('setApprovalForAll true/flase', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const spender = randomAddr;\n\n // set true\n await contract.connect(owner).setApprovalForAll(spender, true);\n expect(await contract.isApprovedForAll(owner.address, spender)).to.equal(true);\n\n // set false\n await contract.connect(owner).setApprovalForAll(spender, false);\n expect(await contract.isApprovedForAll(owner.address, spender)).to.equal(false);\n });\n\n it('Approve to self should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n await expect(\n contract.connect(owner).setApprovalForAll(owner.address, true) // approve to self\n ).to.be.revertedWith(\"ERC721: approve to caller\");\n });\n });\n\n describe('transferFrom', function () {\n it('owner account should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // balance change\n await expect(\n contract.connect(owner).transferFrom(owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n it('approved account should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // approve\n const spenderAccout = accounts[1];\n await contract.connect(owner).approve(spenderAccout.address, tokenId)\n\n // transfer and balance should change\n await expect(\n contract.connect(spenderAccout).transferFrom(owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n it('approvedForAll account should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // setApprovalForAll\n const spenderAccout = accounts[1];\n await contract.connect(owner).setApprovalForAll(spenderAccout.address, true)\n\n // transfer and balance should change\n await expect(\n contract.connect(spenderAccout).transferFrom(owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n it('not owner nor approved should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n const otherAccount = accounts[1]; //not owner or approved\n await expect(\n contract.connect(otherAccount).transferFrom(owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer caller is not owner nor approved\");\n });\n\n it('none exists tokenId should revert', async function () {\n const NONE_EXISTENT_TOKEN_ID = Math.ceil(Math.random() * 1000000);\n const to = randomAddr;\n await expect(\n contract.connect(owner).transferFrom(owner.address, to, NONE_EXISTENT_TOKEN_ID)\n ).to.revertedWith(\"ERC721: operator query for nonexistent token\");\n });\n\n it('to zero address should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = ZeroAddress;\n await expect(\n contract.connect(owner).transferFrom(owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer to the zero address\");\n });\n\n it('from != caller.address should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n const from = accounts[1].address;\n await expect(\n contract.connect(owner).transferFrom(from, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer from incorrect owner\");\n });\n\n it('should revoke old approval when token transfered', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // approve\n const spender = accounts[1].address;\n await contract.connect(owner).approve(spender, tokenId);\n expect(await contract.getApproved(tokenId)).to.equal(spender); //before\n\n // transfer\n await contract.connect(owner).transferFrom(owner.address, to, tokenId);\n\n // should revoke approval\n expect(await contract.getApproved(tokenId)).to.equal(ZeroAddress); // after\n });\n });\n\n describe('safeTransferFrom', function () {\n // same as transferFrom\n it('owner should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // balance change\n await expect(\n contract.connect(owner)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n // same as transferFrom\n it('approved should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // approve\n const spenderAccout = accounts[1];\n await contract.connect(owner).approve(spenderAccout.address, tokenId)\n\n // transfer and balance should change\n await expect(\n contract.connect(spenderAccout)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n // same as transferFrom\n it('not owner nor approved should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n const otherAccount = accounts[1]; //not owner or approved\n await expect(\n contract.connect(otherAccount)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer caller is not owner nor approved\");\n });\n\n it('transfer to none ERC721Receiver implementer should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = contractAddr; // not support ERC721Receiver\n await expect(\n contract.connect(owner)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer to non ERC721Receiver implementer\");\n });\n\n it('transfer to ERC721Receiver implementer should succeed', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = receivercontractAddr; // support ERC721Receiver\n expect(\n await contract.connect(owner)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.be.ok;\n });\n });\n })\n\n describe(\"mint\", async () => {\n it('mint succeed should update balance', async function () {\n const tokenId = 1;\n\n await expect(\n contract.connect(owner).mint(randomAddr, tokenId)\n ).to.changeTokenBalance(contract, randomAddr, 1);\n });\n\n it(\"mint to the zero address should revert\", async () => {\n const tokenId = 1;\n\n await expect(\n contract.connect(owner).mint(ZeroAddress, tokenId)\n ).to.be.revertedWith(\"ERC721: mint to the zero address\");\n });\n\n it(\"mint repeated tokenId should revert\", async () => {\n const tokenId = 1;\n\n // first mint\n await contract.connect(owner).mint(randomAddr, tokenId)\n\n // sencond\n await expect(\n contract.connect(owner).mint(randomAddr, tokenId)\n ).to.be.revertedWith(\"ERC721: token already minted\");\n });\n })\n});" + "code": "const { expect } = require('chai');\nconst { ethers } = require('hardhat');\n\n\ndescribe(\"BaseERC721\", async () => {\n let contract, contractAddr;\n let receivercontract, receivercontractAddr;\n let accounts, owner;\n const name = 'BaseERC721';\n const symbol = 'BERC721';\n const baseURI = 'https://images.example.com/';\n\n const randomAccount = ethers.Wallet.createRandom();\n const randomAddr = randomAccount.address;\n const ZeroAddress = ethers.constants.AddressZero;\n\n\n async function init() {\n // 部署 BaseERC721\n accounts = await ethers.getSigners();\n owner = accounts[0];\n\n {\n const factory = await ethers.getContractFactory('BaseERC721');\n contract = await factory.deploy(...[name, symbol, baseURI]);\n await contract.deployed();\n }\n {\n const factory = await ethers.getContractFactory('BaseERC721Receiver');\n receivercontract = await factory.deploy();\n await receivercontract.deployed();\n }\n\n contractAddr = contract.address;\n receivercontractAddr = receivercontract.address;\n }\n\n beforeEach(async () => {\n await init();\n })\n\n describe(\"IERC721Metadata\", async () => {\n it(\"name\", async () => {\n expect(await contract.name()).to.equal(name);\n });\n\n it(\"symbol\", async () => {\n expect(await contract.symbol()).to.equal(symbol);\n });\n\n describe(\"tokenURI\", async () => {\n it(\"URI query for nonexistent token should revert\", async () => {\n const NONE_EXISTENT_TOKEN_ID = 1234\n await expect(\n contract.tokenURI(NONE_EXISTENT_TOKEN_ID)\n ).to.be.revertedWith(\"ERC721Metadata: URI query for nonexistent token\");\n });\n\n it('Should return baseURI when tokenId exists', async function () {\n const tokenId = 1\n await contract.connect(owner).mint(randomAddr, tokenId);\n\n const expectURI = baseURI + String(tokenId);\n expect(await contract.tokenURI(tokenId)).to.equal(expectURI);\n });\n })\n })\n\n describe(\"IERC721\", async () => {\n describe(\"balanceOf \", async () => {\n it(\"balanceOf\", async () => {\n // before should be 0\n const beforeBalance = await contract.balanceOf(randomAddr);\n expect(beforeBalance).to.equal(0);\n\n // mint\n await contract.connect(owner).mint(randomAddr, 1);\n\n // after should be 1\n const afterBalance = await contract.balanceOf(randomAddr);\n expect(afterBalance).to.equal(1);\n });\n });\n\n describe(\"ownerOf \", async () => {\n it(\"ownerOf\", async () => {\n const tokenId = 1;\n const receiver = randomAddr;\n // before should be 0 address\n const beforeBalance = await contract.ownerOf(tokenId);\n expect(beforeBalance).to.equal(ZeroAddress);\n\n // mint\n await contract.connect(owner).mint(receiver, 1);\n\n // after should be receiver\n const holder = await contract.ownerOf(tokenId);\n expect(holder).to.equal(receiver);\n });\n });\n\n describe('approve', function () {\n it('owner should approve successfully', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); //mint to self\n\n const to = randomAddr;\n await expect(\n contract.connect(owner).approve(to, tokenId)\n ).to.emit(contract, \"Approval\")\n .withArgs(owner.address, to, tokenId);\n\n // getApproved\n expect(await contract.getApproved(tokenId)).to.equal(to);\n });\n\n it('approved account should approve successfully', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); //mint to self\n\n // setApprovalForAll accounts1\n const caller = accounts[1];\n await contract.connect(owner).setApprovalForAll(caller.address, true)\n\n // accounts1 arpprove owner's tokenId[1] to randomAddr\n const to = randomAddr;\n expect(\n await contract.connect(caller).approve(to, tokenId)\n ).to.be.ok;\n\n // getApproved\n expect(await contract.getApproved(tokenId)).to.equal(to);\n });\n\n it('Approve to current owner should revert', async function () {\n // mint token first\n const tokenId = 1;\n const receiver = owner.address; //self\n await contract.connect(owner).mint(receiver, tokenId);\n\n await expect(\n contract.connect(owner).approve(receiver, tokenId)\n ).to.be.revertedWith(\"ERC721: approval to current owner\");\n });\n\n it('Not owner nor approved token approveal should revert', async function () {\n // mint token first\n const tokenId = 1;\n const receiver = owner.address; //self\n await contract.connect(owner).mint(receiver, tokenId);\n\n const otherAccount = accounts[1]; //not owner or approved\n await expect(\n contract.connect(otherAccount).approve(randomAddr, tokenId)\n ).to.be.revertedWith(\"ERC721: approve caller is not owner nor approved for all\");\n });\n });\n\n describe('getApproved', function () {\n it('should return approval address', async function () {\n // mint token first\n const tokenId = 1;\n const receiver = owner.address; //self\n await contract.connect(owner).mint(receiver, tokenId);\n\n // approve\n const approvedAddr = randomAddr;\n await contract.connect(owner).approve(randomAddr, tokenId);\n\n expect(await contract.getApproved(tokenId)).to.equal(approvedAddr);\n });\n\n it('Approved query for nonexistent token should revert', async function () {\n const tokenId = 1; // not exists\n\n await expect(\n contract.getApproved(tokenId)\n ).to.be.revertedWith('ERC721: approved query for nonexistent token');\n });\n });\n\n describe('setApprovalForAll', function () {\n it('setApprovalForAll true/flase', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const spender = randomAddr;\n\n // set true\n await contract.connect(owner).setApprovalForAll(spender, true);\n expect(await contract.isApprovedForAll(owner.address, spender)).to.equal(true);\n\n // set false\n await contract.connect(owner).setApprovalForAll(spender, false);\n expect(await contract.isApprovedForAll(owner.address, spender)).to.equal(false);\n });\n\n it('Approve to self should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n await expect(\n contract.connect(owner).setApprovalForAll(owner.address, true) // approve to self\n ).to.be.revertedWith(\"ERC721: approve to caller\");\n });\n });\n\n describe('transferFrom', function () {\n it('owner account should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // balance change\n await expect(\n contract.connect(owner).transferFrom(owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n it('approved account should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // approve\n const spenderAccout = accounts[1];\n await contract.connect(owner).approve(spenderAccout.address, tokenId)\n\n // transfer and balance should change\n await expect(\n contract.connect(spenderAccout).transferFrom(owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n it('approvedForAll account should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // setApprovalForAll\n const spenderAccout = accounts[1];\n await contract.connect(owner).setApprovalForAll(spenderAccout.address, true)\n\n // transfer and balance should change\n await expect(\n contract.connect(spenderAccout).transferFrom(owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n it('not owner nor approved should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n const otherAccount = accounts[1]; //not owner or approved\n await expect(\n contract.connect(otherAccount).transferFrom(owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer caller is not owner nor approved\");\n });\n\n it('none exists tokenId should revert', async function () {\n const NONE_EXISTENT_TOKEN_ID = Math.ceil(Math.random() * 1000000);\n const to = randomAddr;\n await expect(\n contract.connect(owner).transferFrom(owner.address, to, NONE_EXISTENT_TOKEN_ID)\n ).to.revertedWith(\"ERC721: operator query for nonexistent token\");\n });\n\n it('to zero address should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = ZeroAddress;\n await expect(\n contract.connect(owner).transferFrom(owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer to the zero address\");\n });\n\n it('from != caller.address should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n const from = accounts[1].address;\n await expect(\n contract.connect(owner).transferFrom(from, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer from incorrect owner\");\n });\n\n it('should revoke old approval when token transfered', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // approve\n const spender = accounts[1].address;\n await contract.connect(owner).approve(spender, tokenId);\n expect(await contract.getApproved(tokenId)).to.equal(spender); //before\n\n // transfer\n await contract.connect(owner).transferFrom(owner.address, to, tokenId);\n\n // should revoke approval\n expect(await contract.getApproved(tokenId)).to.equal(ZeroAddress); // after\n });\n });\n\n describe('safeTransferFrom', function () {\n // same as transferFrom\n it('owner should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // balance change\n await expect(\n contract.connect(owner)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n // same as transferFrom\n it('approved should succeed and balance should change', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n\n // approve\n const spenderAccout = accounts[1];\n await contract.connect(owner).approve(spenderAccout.address, tokenId)\n\n // transfer and balance should change\n await expect(\n contract.connect(spenderAccout)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.changeTokenBalances(contract, [owner.address, to], [-1, 1]);\n });\n\n // same as transferFrom\n it('not owner nor approved should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = randomAddr;\n const otherAccount = accounts[1]; //not owner or approved\n await expect(\n contract.connect(otherAccount)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer caller is not owner nor approved\");\n });\n\n it('transfer to none ERC721Receiver implementer should revert', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = contractAddr; // not support ERC721Receiver\n await expect(\n contract.connect(owner)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.revertedWith(\"ERC721: transfer to non ERC721Receiver implementer\");\n });\n\n it('transfer to ERC721Receiver implementer should succeed', async function () {\n // mint token first\n const tokenId = 1;\n await contract.connect(owner).mint(owner.address, tokenId); // mint to self\n\n const to = receivercontractAddr; // support ERC721Receiver\n expect(\n await contract.connect(owner)[\"safeTransferFrom(address,address,uint256)\"](owner.address, to, tokenId)\n ).to.be.ok;\n });\n });\n })\n\n describe(\"mint\", async () => {\n it('mint succeed should update balance', async function () {\n const tokenId = 1;\n\n await expect(\n contract.connect(owner).mint(randomAddr, tokenId)\n ).to.changeTokenBalance(contract, randomAddr, 1);\n });\n\n it(\"mint to the zero address should revert\", async () => {\n const tokenId = 1;\n\n await expect(\n contract.connect(owner).mint(ZeroAddress, tokenId)\n ).to.be.revertedWith(\"ERC721: mint to the zero address\");\n });\n\n it(\"mint repeated tokenId should revert\", async () => {\n const tokenId = 1;\n\n // first mint\n await contract.connect(owner).mint(randomAddr, tokenId)\n\n // sencond\n await expect(\n contract.connect(owner).mint(randomAddr, tokenId)\n ).to.be.revertedWith(\"ERC721: token already minted\");\n });\n })\n});" } ] }