From 8e6a76527fd41c5f907608050c8f8fe9129082ce Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:14:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=81=E5=AE=9A=E6=97=B6,=20=E5=8F=AF?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E6=8C=87=E5=AE=9A=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/game/NFTLock.sol | 11 ++++++----- test/testNFTLocker.ts | 15 ++++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/contracts/game/NFTLock.sol b/contracts/game/NFTLock.sol index 7469497..bbb623d 100644 --- a/contracts/game/NFTLock.sol +++ b/contracts/game/NFTLock.sol @@ -27,7 +27,7 @@ contract NFTLock is ERC721Holder, HasSignature, TimeChecker, Pausable { mapping(address nft => bool status) public supportNftList; event UnLock(address indexed nft, address indexed user, uint256 nonce, NFTInfo[] nftList); - event Lock(address indexed nft, address indexed user, uint256[] tokenIds); + event Lock(address indexed nft, address indexed owner, address indexed to, uint256[] tokenIds); event VerifierUpdated(address indexed verifier); constructor(uint256 _duration, address _verifier) TimeChecker(_duration) { @@ -36,16 +36,17 @@ contract NFTLock is ERC721Holder, HasSignature, TimeChecker, Pausable { verifier = _verifier; } - function lock(address nft, uint256[] calldata tokenIds) external whenNotPaused{ + function lock(address nft, address to, uint256[] calldata tokenIds) external whenNotPaused{ require(tokenIds.length <= 100, "tokenIds too many"); + require(to != address(0), "not support nft"); require(supportNftList[nft], "not support nft"); - address to = _msgSender(); + address owner = _msgSender(); for (uint256 i = 0; i < tokenIds.length; i++) { lockedOriginal[nft][tokenIds[i]] = to; lockedRecords[nft][to].add(tokenIds[i]); - INFT(nft).transferFrom(to, address(this), tokenIds[i]); + INFT(nft).transferFrom(owner, address(this), tokenIds[i]); } - emit Lock(nft, to, tokenIds); + emit Lock(nft, owner, to, tokenIds); } /** * @dev unlock or mint nft diff --git a/test/testNFTLocker.ts b/test/testNFTLocker.ts index b634f58..7dc2c88 100644 --- a/test/testNFTLocker.ts +++ b/test/testNFTLocker.ts @@ -43,14 +43,14 @@ describe('NFTLock', function() { describe("Lock", function () { it('should lock NFT', async function() { - const { nftLock, nft, otherAccount } = await loadFixture(deployOneContract); + 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, [tokenId]); + await nftLock.connect(otherAccount).lock(nft.target, owner.address, [tokenId]); expect(await nft.balanceOf(nftLock.target)).to.equal(1); - expect(await nftLock.lockedOriginal(nft.target, tokenId)).to.equal(otherAccount.address); + expect(await nftLock.lockedOriginal(nft.target, tokenId)).to.equal(owner.address); expect(await nft.ownerOf(tokenId)).to.equal(nftLock.target); }); @@ -64,15 +64,16 @@ describe('NFTLock', function() { // @ts-ignore await nft.connect(otherAccount).approve(nftLock.target, tokenId); //@ts-ignore - await nftLock.connect(otherAccount).lock(nft.target, [tokenId]); + 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", "bool"], - [otherAccount.address, nft.target, nftLock.target, chainId, now, nonce, tokenId, false]); + [owner.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); + await nftLock.unlockOrMint(nft.target, [[tokenId, false]], now, nonce, signature); + // await nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, false]], now, nonce, signature); + expect(await nft.ownerOf(tokenId)).to.equal(owner.address); }); it('should mint NFT from lock', async function() {