import{loadFixture}from"@nomicfoundation/hardhat-toolbox/network-helpers";import{expect}from"chai";import{ethers}from"hardhat";constNAME="NAME";constSYMBOL="SYMBOL";constTOKEN_ID=1234;constPARENT_1_COLLECTION="0xDEAdBEEf00000000000000000123456789ABCdeF";constPARENT_1_ID=8888;constPARENT_1_TOKEN={collection:PARENT_1_COLLECTION,id:PARENT_1_ID};constPARENT_2_COLLECTION="0xBaDc0ffEe0000000000000000123456789aBCDef";constPARENT_2_ID=9999;constPARENT_2_TOKEN={collection:PARENT_2_COLLECTION,id:PARENT_2_ID};describe("ERC7510",function(){asyncfunctiondeployContractFixture(){const[deployer,owner]=awaitethers.getSigners();constcontract=awaitethers.deployContract("ERC7510",[NAME,SYMBOL],deployer);awaitcontract.mint(owner,TOKEN_ID);return{contract,owner};}describe("Functions",function(){it("Should not set parent tokens if not owner or approved",asyncfunction(){const{contract}=awaitloadFixture(deployContractFixture);awaitexpect(contract.setParentTokens(TOKEN_ID,[PARENT_1_TOKEN])).to.be.revertedWith("ERC7510: caller is not owner or approved");});it("Should correctly query token without parents",asyncfunction(){const{contract}=awaitloadFixture(deployContractFixture);expect(awaitcontract.parentTokensOf(TOKEN_ID)).to.have.lengthOf(0);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_1_TOKEN)).to.equal(false);});it("Should set parent tokens and then update",asyncfunction(){const{contract,owner}=awaitloadFixture(deployContractFixture);awaitcontract.connect(owner).setParentTokens(TOKEN_ID,[PARENT_1_TOKEN]);letparentTokens=awaitcontract.parentTokensOf(TOKEN_ID);expect(parentTokens).to.have.lengthOf(1);expect(parentTokens[0].collection).to.equal(PARENT_1_COLLECTION);expect(parentTokens[0].id).to.equal(PARENT_1_ID);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_1_TOKEN)).to.equal(true);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_2_TOKEN)).to.equal(false);awaitcontract.connect(owner).setParentTokens(TOKEN_ID,[PARENT_2_TOKEN]);parentTokens=awaitcontract.parentTokensOf(TOKEN_ID);expect(parentTokens).to.have.lengthOf(1);expect(parentTokens[0].collection).to.equal(PARENT_2_COLLECTION);expect(parentTokens[0].id).to.equal(PARENT_2_ID);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_1_TOKEN)).to.equal(false);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_2_TOKEN)).to.equal(true);});it("Should burn and clear parent tokens",asyncfunction(){const{contract,owner}=awaitloadFixture(deployContractFixture);awaitcontract.connect(owner).setParentTokens(TOKEN_ID,[PARENT_1_TOKEN,PARENT_2_TOKEN]);awaitcontract.burn(TOKEN_ID);awaitexpect(contract.parentTokensOf(TOKEN_ID)).to.be.revertedWith("ERC7510: query for nonexistent token");awaitexpect(contract.isParentToken(TOKEN_ID,PARENT_1_TOKEN)).to.be.revertedWith("ERC7510: query for nonexistent token");awaitexpect(contract.isParentToken(TOKEN_ID,PARENT_2_TOKEN)).to.be.revertedWith("ERC7510: query for nonexistent token");awaitcontract.mint(owner,TOKEN_ID);expect(awaitcontract.parentTokensOf(TOKEN_ID)).to.have.lengthOf(0);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_1_TOKEN)).to.equal(false);expect(awaitcontract.isParentToken(TOKEN_ID,PARENT_2_TOKEN)).to.equal(false);});});describe("Events",function(){it("Should emit event when set parent tokens",asyncfunction(){const{contract,owner}=awaitloadFixture(deployContractFixture);awaitexpect(contract.connect(owner).setParentTokens(TOKEN_ID,[PARENT_1_TOKEN,PARENT_2_TOKEN])).to.emit(contract,"UpdateParentTokens").withArgs(TOKEN_ID);});});});
Ming Jiang (@minkyn), Zheng Han (@hanbsd), Fan Yang (@fayang), "ERC-7510: 跨合约分层 NFT [DRAFT]," Ethereum Improvement Proposals, no. 7510, August 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7510.