|
@@ -1,26 +1,29 @@
|
|
|
-const { BN, expectEvent, constants } = require('@openzeppelin/test-helpers');
|
|
|
+const { ethers } = require('hardhat');
|
|
|
const { expect } = require('chai');
|
|
|
+const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
|
|
|
|
|
const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
|
|
|
-const { expectRevertCustomError } = require('../../../helpers/customError');
|
|
|
|
|
|
-const ERC721 = artifacts.require('$ERC721');
|
|
|
-const ERC721Wrapper = artifacts.require('$ERC721Wrapper');
|
|
|
+const name = 'Non Fungible Token';
|
|
|
+const symbol = 'NFT';
|
|
|
+const tokenId = 1n;
|
|
|
+const otherTokenId = 2n;
|
|
|
|
|
|
-contract('ERC721Wrapper', function (accounts) {
|
|
|
- const [initialHolder, anotherAccount, approvedAccount] = accounts;
|
|
|
+async function fixture() {
|
|
|
+ const accounts = await ethers.getSigners();
|
|
|
+ const [owner, approved, other] = accounts;
|
|
|
|
|
|
- const name = 'My Token';
|
|
|
- const symbol = 'MTKN';
|
|
|
- const firstTokenId = new BN(1);
|
|
|
- const secondTokenId = new BN(2);
|
|
|
+ const underlying = await ethers.deployContract('$ERC721', [name, symbol]);
|
|
|
+ await underlying.$_safeMint(owner, tokenId);
|
|
|
+ await underlying.$_safeMint(owner, otherTokenId);
|
|
|
+ const token = await ethers.deployContract('$ERC721Wrapper', [`Wrapped ${name}`, `W${symbol}`, underlying]);
|
|
|
|
|
|
- beforeEach(async function () {
|
|
|
- this.underlying = await ERC721.new(name, symbol);
|
|
|
- this.token = await ERC721Wrapper.new(`Wrapped ${name}`, `W${symbol}`, this.underlying.address);
|
|
|
+ return { accounts, owner, approved, other, underlying, token };
|
|
|
+}
|
|
|
|
|
|
- await this.underlying.$_safeMint(initialHolder, firstTokenId);
|
|
|
- await this.underlying.$_safeMint(initialHolder, secondTokenId);
|
|
|
+describe('ERC721Wrapper', function () {
|
|
|
+ beforeEach(async function () {
|
|
|
+ Object.assign(this, await loadFixture(fixture));
|
|
|
});
|
|
|
|
|
|
it('has a name', async function () {
|
|
@@ -32,258 +35,167 @@ contract('ERC721Wrapper', function (accounts) {
|
|
|
});
|
|
|
|
|
|
it('has underlying', async function () {
|
|
|
- expect(await this.token.underlying()).to.be.bignumber.equal(this.underlying.address);
|
|
|
+ expect(await this.token.underlying()).to.equal(this.underlying.target);
|
|
|
});
|
|
|
|
|
|
describe('depositFor', function () {
|
|
|
it('works with token approval', async function () {
|
|
|
- await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: this.token.address,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await this.underlying.connect(this.owner).approve(this.token, tokenId);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, this.token.target, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works with approval for all', async function () {
|
|
|
- await this.underlying.setApprovalForAll(this.token.address, true, { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: this.token.address,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await this.underlying.connect(this.owner).setApprovalForAll(this.token, true);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, this.token.target, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works sending to another account', async function () {
|
|
|
- await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.depositFor(anotherAccount, [firstTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: this.token.address,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: anotherAccount,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await this.underlying.connect(this.owner).approve(this.token, tokenId);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.owner).depositFor(this.other, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, this.token.target, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.other.address, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works with multiple tokens', async function () {
|
|
|
- await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
- await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.depositFor(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: this.token.address,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: this.token.address,
|
|
|
- tokenId: secondTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: secondTokenId,
|
|
|
- });
|
|
|
+ await this.underlying.connect(this.owner).approve(this.token, tokenId);
|
|
|
+ await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId, otherTokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, this.token.target, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.owner.address, tokenId)
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, this.token.target, otherTokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.owner.address, otherTokenId);
|
|
|
});
|
|
|
|
|
|
it('reverts with missing approval', async function () {
|
|
|
- await expectRevertCustomError(
|
|
|
- this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder }),
|
|
|
- 'ERC721InsufficientApproval',
|
|
|
- [this.token.address, firstTokenId],
|
|
|
- );
|
|
|
+ await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
|
|
|
+ .to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
|
|
|
+ .withArgs(this.token.target, tokenId);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('withdrawTo', function () {
|
|
|
beforeEach(async function () {
|
|
|
- await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
- await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
+ await this.underlying.connect(this.owner).approve(this.token, tokenId);
|
|
|
+ await this.token.connect(this.owner).depositFor(this.owner, [tokenId]);
|
|
|
});
|
|
|
|
|
|
it('works for an owner', async function () {
|
|
|
- const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: this.token.address,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: constants.ZERO_ADDRESS,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.token.target, this.owner.address, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works for an approved', async function () {
|
|
|
- await this.token.approve(approvedAccount, firstTokenId, { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: this.token.address,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: constants.ZERO_ADDRESS,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await this.token.connect(this.owner).approve(this.approved, tokenId);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.token.target, this.owner.address, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works for an approved for all', async function () {
|
|
|
- await this.token.setApprovalForAll(approvedAccount, true, { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: this.token.address,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: constants.ZERO_ADDRESS,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await this.token.connect(this.owner).setApprovalForAll(this.approved, true);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.token.target, this.owner.address, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
|
|
|
});
|
|
|
|
|
|
it("doesn't work for a non-owner nor approved", async function () {
|
|
|
- await expectRevertCustomError(
|
|
|
- this.token.withdrawTo(initialHolder, [firstTokenId], { from: anotherAccount }),
|
|
|
- 'ERC721InsufficientApproval',
|
|
|
- [anotherAccount, firstTokenId],
|
|
|
- );
|
|
|
+ await expect(this.token.connect(this.other).withdrawTo(this.owner, [tokenId]))
|
|
|
+ .to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
|
|
|
+ .withArgs(this.other.address, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works with multiple tokens', async function () {
|
|
|
- await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
|
|
|
- await this.token.depositFor(initialHolder, [secondTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: this.token.address,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: this.token.address,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: secondTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: constants.ZERO_ADDRESS,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: constants.ZERO_ADDRESS,
|
|
|
- tokenId: secondTokenId,
|
|
|
- });
|
|
|
+ await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
|
|
|
+ await this.token.connect(this.owner).depositFor(this.owner, [otherTokenId]);
|
|
|
+
|
|
|
+ await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId, otherTokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.token.target, this.owner.address, tokenId)
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.token.target, this.owner.address, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, ethers.ZeroAddress, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
|
|
|
});
|
|
|
|
|
|
it('works to another account', async function () {
|
|
|
- const { tx } = await this.token.withdrawTo(anotherAccount, [firstTokenId], { from: initialHolder });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
- from: this.token.address,
|
|
|
- to: anotherAccount,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: initialHolder,
|
|
|
- to: constants.ZERO_ADDRESS,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await expect(this.token.connect(this.owner).withdrawTo(this.other, [tokenId]))
|
|
|
+ .to.emit(this.underlying, 'Transfer')
|
|
|
+ .withArgs(this.token.target, this.other.address, tokenId)
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('onERC721Received', function () {
|
|
|
it('only allows calls from underlying', async function () {
|
|
|
- await expectRevertCustomError(
|
|
|
- this.token.onERC721Received(
|
|
|
- initialHolder,
|
|
|
- this.token.address,
|
|
|
- firstTokenId,
|
|
|
- anotherAccount, // Correct data
|
|
|
- { from: anotherAccount },
|
|
|
+ await expect(
|
|
|
+ this.token.connect(this.other).onERC721Received(
|
|
|
+ this.owner,
|
|
|
+ this.token,
|
|
|
+ tokenId,
|
|
|
+ this.other.address, // Correct data
|
|
|
),
|
|
|
- 'ERC721UnsupportedToken',
|
|
|
- [anotherAccount],
|
|
|
- );
|
|
|
+ )
|
|
|
+ .to.be.revertedWithCustomError(this.token, 'ERC721UnsupportedToken')
|
|
|
+ .withArgs(this.other.address);
|
|
|
});
|
|
|
|
|
|
it('mints a token to from', async function () {
|
|
|
- const { tx } = await this.underlying.safeTransferFrom(initialHolder, this.token.address, firstTokenId, {
|
|
|
- from: initialHolder,
|
|
|
- });
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: initialHolder,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await expect(this.underlying.connect(this.owner).safeTransferFrom(this.owner, this.token, tokenId))
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('_recover', function () {
|
|
|
it('works if there is something to recover', async function () {
|
|
|
// Should use `transferFrom` to avoid `onERC721Received` minting
|
|
|
- await this.underlying.transferFrom(initialHolder, this.token.address, firstTokenId, { from: initialHolder });
|
|
|
+ await this.underlying.connect(this.owner).transferFrom(this.owner, this.token, tokenId);
|
|
|
|
|
|
- const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
|
|
|
-
|
|
|
- await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
- from: constants.ZERO_ADDRESS,
|
|
|
- to: anotherAccount,
|
|
|
- tokenId: firstTokenId,
|
|
|
- });
|
|
|
+ await expect(this.token.$_recover(this.other, tokenId))
|
|
|
+ .to.emit(this.token, 'Transfer')
|
|
|
+ .withArgs(ethers.ZeroAddress, this.other.address, tokenId);
|
|
|
});
|
|
|
|
|
|
it('reverts if there is nothing to recover', async function () {
|
|
|
- const owner = await this.underlying.ownerOf(firstTokenId);
|
|
|
- await expectRevertCustomError(this.token.$_recover(initialHolder, firstTokenId), 'ERC721IncorrectOwner', [
|
|
|
- this.token.address,
|
|
|
- firstTokenId,
|
|
|
- owner,
|
|
|
- ]);
|
|
|
+ const holder = await this.underlying.ownerOf(tokenId);
|
|
|
+
|
|
|
+ await expect(this.token.$_recover(holder, tokenId))
|
|
|
+ .to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
|
|
|
+ .withArgs(this.token.target, tokenId, holder);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('ERC712 behavior', function () {
|
|
|
- shouldBehaveLikeERC721(...accounts);
|
|
|
+ shouldBehaveLikeERC721();
|
|
|
});
|
|
|
});
|