锁定时, 可选择指定地址

This commit is contained in:
CounterFire2023 2024-07-01 13:14:14 +08:00
parent 6342b7cc76
commit 8e6a76527f
2 changed files with 14 additions and 12 deletions

View File

@ -27,7 +27,7 @@ contract NFTLock is ERC721Holder, HasSignature, TimeChecker, Pausable {
mapping(address nft => bool status) public supportNftList; mapping(address nft => bool status) public supportNftList;
event UnLock(address indexed nft, address indexed user, uint256 nonce, NFTInfo[] nftList); 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); event VerifierUpdated(address indexed verifier);
constructor(uint256 _duration, address _verifier) TimeChecker(_duration) { constructor(uint256 _duration, address _verifier) TimeChecker(_duration) {
@ -36,16 +36,17 @@ contract NFTLock is ERC721Holder, HasSignature, TimeChecker, Pausable {
verifier = _verifier; 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(tokenIds.length <= 100, "tokenIds too many");
require(to != address(0), "not support nft");
require(supportNftList[nft], "not support nft"); require(supportNftList[nft], "not support nft");
address to = _msgSender(); address owner = _msgSender();
for (uint256 i = 0; i < tokenIds.length; i++) { for (uint256 i = 0; i < tokenIds.length; i++) {
lockedOriginal[nft][tokenIds[i]] = to; lockedOriginal[nft][tokenIds[i]] = to;
lockedRecords[nft][to].add(tokenIds[i]); 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 * @dev unlock or mint nft

View File

@ -43,14 +43,14 @@ describe('NFTLock', function() {
describe("Lock", function () { describe("Lock", function () {
it('should lock NFT', async 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" const tokenId = "1001"
// @ts-ignore // @ts-ignore
await nft.connect(otherAccount).approve(nftLock.target, tokenId); await nft.connect(otherAccount).approve(nftLock.target, tokenId);
//@ts-ignore //@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 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); expect(await nft.ownerOf(tokenId)).to.equal(nftLock.target);
}); });
@ -64,15 +64,16 @@ describe('NFTLock', function() {
// @ts-ignore // @ts-ignore
await nft.connect(otherAccount).approve(nftLock.target, tokenId); await nft.connect(otherAccount).approve(nftLock.target, tokenId);
//@ts-ignore //@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 nonce = (Math.random() * 1000) | 0;
const now = Date.now() / 1000 | 0; const now = Date.now() / 1000 | 0;
let localMsgHash = solidityPackedKeccak256(["address", "address", "address", "uint256", "uint256", "uint256", "uint256", "bool"], 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)); const signature = await owner.signMessage(getBytes(localMsgHash));
//@ts-ignore //@ts-ignore
await nftLock.connect(otherAccount).unlockOrMint(nft.target, [[tokenId, false]], now, nonce, signature); await nftLock.unlockOrMint(nft.target, [[tokenId, false]], now, nonce, signature);
expect(await nft.ownerOf(tokenId)).to.equal(otherAccount.address); // 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() { it('should mint NFT from lock', async function() {