dexorder
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
const tokenId = 1n;
|
||||
const otherTokenId = 2n;
|
||||
const unknownTokenId = 3n;
|
||||
|
||||
async function fixture() {
|
||||
const [owner, approved, another] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC721Burnable', [name, symbol]);
|
||||
return { owner, approved, another, token };
|
||||
}
|
||||
|
||||
describe('ERC721Burnable', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('like a burnable ERC721', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, tokenId);
|
||||
await this.token.$_mint(this.owner, otherTokenId);
|
||||
});
|
||||
|
||||
describe('burn', function () {
|
||||
describe('when successful', function () {
|
||||
it('emits a burn event, burns the given token ID and adjusts the balance of the owner', async function () {
|
||||
const balanceBefore = await this.token.balanceOf(this.owner);
|
||||
|
||||
await expect(this.token.connect(this.owner).burn(tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
|
||||
|
||||
await expect(this.token.ownerOf(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore - 1n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there is a previous approval burned', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
await this.token.connect(this.owner).burn(tokenId);
|
||||
});
|
||||
|
||||
describe('getApproved', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.getApproved(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there is no previous approval burned', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.connect(this.another).burn(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
|
||||
.withArgs(this.another, tokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the given token ID was not tracked by this contract', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.connect(this.owner).burn(unknownTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(unknownTokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,181 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
// solhint-disable func-name-mixedcase
|
||||
|
||||
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
|
||||
import {ERC721Consecutive} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Consecutive.sol";
|
||||
import {Test, StdUtils} from "@forge-std/Test.sol";
|
||||
|
||||
function toSingleton(address account) pure returns (address[] memory) {
|
||||
address[] memory accounts = new address[](1);
|
||||
accounts[0] = account;
|
||||
return accounts;
|
||||
}
|
||||
|
||||
contract ERC721ConsecutiveTarget is StdUtils, ERC721Consecutive {
|
||||
uint96 private immutable _offset;
|
||||
uint256 public totalMinted = 0;
|
||||
|
||||
constructor(address[] memory receivers, uint256[] memory batches, uint256 startingId) ERC721("", "") {
|
||||
_offset = uint96(startingId);
|
||||
for (uint256 i = 0; i < batches.length; i++) {
|
||||
address receiver = receivers[i % receivers.length];
|
||||
uint96 batchSize = uint96(bound(batches[i], 0, _maxBatchSize()));
|
||||
_mintConsecutive(receiver, batchSize);
|
||||
totalMinted += batchSize;
|
||||
}
|
||||
}
|
||||
|
||||
function burn(uint256 tokenId) public {
|
||||
_burn(tokenId);
|
||||
}
|
||||
|
||||
function _firstConsecutiveId() internal view virtual override returns (uint96) {
|
||||
return _offset;
|
||||
}
|
||||
}
|
||||
|
||||
contract ERC721ConsecutiveTest is Test {
|
||||
function test_balance(address receiver, uint256[] calldata batches, uint96 startingId) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
uint256 startingTokenId = bound(startingId, 0, 5000);
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
|
||||
|
||||
assertEq(token.balanceOf(receiver), token.totalMinted());
|
||||
}
|
||||
|
||||
function test_ownership(
|
||||
address receiver,
|
||||
uint256[] calldata batches,
|
||||
uint256[2] calldata unboundedTokenId,
|
||||
uint96 startingId
|
||||
) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
uint256 startingTokenId = bound(startingId, 0, 5000);
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
|
||||
|
||||
if (token.totalMinted() > 0) {
|
||||
uint256 validTokenId = bound(
|
||||
unboundedTokenId[0],
|
||||
startingTokenId,
|
||||
startingTokenId + token.totalMinted() - 1
|
||||
);
|
||||
assertEq(token.ownerOf(validTokenId), receiver);
|
||||
}
|
||||
|
||||
uint256 invalidTokenId = bound(
|
||||
unboundedTokenId[1],
|
||||
startingTokenId + token.totalMinted(),
|
||||
startingTokenId + token.totalMinted() + 1
|
||||
);
|
||||
vm.expectRevert();
|
||||
token.ownerOf(invalidTokenId);
|
||||
}
|
||||
|
||||
function test_burn(
|
||||
address receiver,
|
||||
uint256[] calldata batches,
|
||||
uint256 unboundedTokenId,
|
||||
uint96 startingId
|
||||
) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
uint256 startingTokenId = bound(startingId, 0, 5000);
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
|
||||
|
||||
// only test if we minted at least one token
|
||||
uint256 supply = token.totalMinted();
|
||||
vm.assume(supply > 0);
|
||||
|
||||
// burn a token in [0; supply[
|
||||
uint256 tokenId = bound(unboundedTokenId, startingTokenId, startingTokenId + supply - 1);
|
||||
token.burn(tokenId);
|
||||
|
||||
// balance should have decreased
|
||||
assertEq(token.balanceOf(receiver), supply - 1);
|
||||
|
||||
// token should be burnt
|
||||
vm.expectRevert();
|
||||
token.ownerOf(tokenId);
|
||||
}
|
||||
|
||||
function test_transfer(
|
||||
address[2] calldata accounts,
|
||||
uint256[2] calldata unboundedBatches,
|
||||
uint256[2] calldata unboundedTokenId,
|
||||
uint96 startingId
|
||||
) public {
|
||||
vm.assume(accounts[0] != address(0));
|
||||
vm.assume(accounts[1] != address(0));
|
||||
vm.assume(accounts[0] != accounts[1]);
|
||||
|
||||
uint256 startingTokenId = bound(startingId, 1, 5000);
|
||||
|
||||
address[] memory receivers = new address[](2);
|
||||
receivers[0] = accounts[0];
|
||||
receivers[1] = accounts[1];
|
||||
|
||||
// We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
|
||||
uint256[] memory batches = new uint256[](2);
|
||||
batches[0] = bound(unboundedBatches[0], startingTokenId, 5000);
|
||||
batches[1] = bound(unboundedBatches[1], startingTokenId, 5000);
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(receivers, batches, startingTokenId);
|
||||
|
||||
uint256 tokenId0 = bound(unboundedTokenId[0], startingTokenId, batches[0]);
|
||||
uint256 tokenId1 = bound(unboundedTokenId[1], startingTokenId, batches[1]) + batches[0];
|
||||
|
||||
assertEq(token.ownerOf(tokenId0), accounts[0]);
|
||||
assertEq(token.ownerOf(tokenId1), accounts[1]);
|
||||
assertEq(token.balanceOf(accounts[0]), batches[0]);
|
||||
assertEq(token.balanceOf(accounts[1]), batches[1]);
|
||||
|
||||
vm.prank(accounts[0]);
|
||||
token.transferFrom(accounts[0], accounts[1], tokenId0);
|
||||
|
||||
assertEq(token.ownerOf(tokenId0), accounts[1]);
|
||||
assertEq(token.ownerOf(tokenId1), accounts[1]);
|
||||
assertEq(token.balanceOf(accounts[0]), batches[0] - 1);
|
||||
assertEq(token.balanceOf(accounts[1]), batches[1] + 1);
|
||||
|
||||
vm.prank(accounts[1]);
|
||||
token.transferFrom(accounts[1], accounts[0], tokenId1);
|
||||
|
||||
assertEq(token.ownerOf(tokenId0), accounts[1]);
|
||||
assertEq(token.ownerOf(tokenId1), accounts[0]);
|
||||
assertEq(token.balanceOf(accounts[0]), batches[0]);
|
||||
assertEq(token.balanceOf(accounts[1]), batches[1]);
|
||||
}
|
||||
|
||||
function test_start_consecutive_id(
|
||||
address receiver,
|
||||
uint256[2] calldata unboundedBatches,
|
||||
uint256[2] calldata unboundedTokenId,
|
||||
uint96 startingId
|
||||
) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
uint256 startingTokenId = bound(startingId, 1, 5000);
|
||||
|
||||
// We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
|
||||
uint256[] memory batches = new uint256[](2);
|
||||
batches[0] = bound(unboundedBatches[0], startingTokenId, 5000);
|
||||
batches[1] = bound(unboundedBatches[1], startingTokenId, 5000);
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
|
||||
|
||||
uint256 tokenId0 = bound(unboundedTokenId[0], startingTokenId, batches[0]);
|
||||
uint256 tokenId1 = bound(unboundedTokenId[1], startingTokenId, batches[1]);
|
||||
|
||||
assertEq(token.ownerOf(tokenId0), receiver);
|
||||
assertEq(token.ownerOf(tokenId1), receiver);
|
||||
assertEq(token.balanceOf(receiver), batches[0] + batches[1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { sum } = require('../../../helpers/math');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
describe('ERC721Consecutive', function () {
|
||||
for (const offset of [0n, 1n, 42n]) {
|
||||
describe(`with offset ${offset}`, function () {
|
||||
async function fixture() {
|
||||
const accounts = await ethers.getSigners();
|
||||
const [alice, bruce, chris, receiver] = accounts;
|
||||
|
||||
const batches = [
|
||||
{ receiver: alice, amount: 0n },
|
||||
{ receiver: alice, amount: 1n },
|
||||
{ receiver: alice, amount: 2n },
|
||||
{ receiver: bruce, amount: 5n },
|
||||
{ receiver: chris, amount: 0n },
|
||||
{ receiver: alice, amount: 7n },
|
||||
];
|
||||
const delegates = [alice, chris];
|
||||
|
||||
const token = await ethers.deployContract('$ERC721ConsecutiveMock', [
|
||||
name,
|
||||
symbol,
|
||||
offset,
|
||||
delegates,
|
||||
batches.map(({ receiver }) => receiver),
|
||||
batches.map(({ amount }) => amount),
|
||||
]);
|
||||
|
||||
return { accounts, alice, bruce, chris, receiver, batches, delegates, token };
|
||||
}
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('minting during construction', function () {
|
||||
it('events are emitted at construction', async function () {
|
||||
let first = offset;
|
||||
for (const batch of this.batches) {
|
||||
if (batch.amount > 0) {
|
||||
await expect(this.token.deploymentTransaction())
|
||||
.to.emit(this.token, 'ConsecutiveTransfer')
|
||||
.withArgs(
|
||||
first /* fromTokenId */,
|
||||
first + batch.amount - 1n /* toTokenId */,
|
||||
ethers.ZeroAddress /* fromAddress */,
|
||||
batch.receiver /* toAddress */,
|
||||
);
|
||||
} else {
|
||||
// ".to.not.emit" only looks at event name, and doesn't check the parameters
|
||||
}
|
||||
first += batch.amount;
|
||||
}
|
||||
});
|
||||
|
||||
it('ownership is set', async function () {
|
||||
const owners = [
|
||||
...Array(Number(offset)).fill(ethers.ZeroAddress),
|
||||
...this.batches.flatMap(({ receiver, amount }) => Array(Number(amount)).fill(receiver.address)),
|
||||
];
|
||||
|
||||
for (const tokenId in owners) {
|
||||
if (owners[tokenId] != ethers.ZeroAddress) {
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(owners[tokenId]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('balance & voting power are set', async function () {
|
||||
for (const account of this.accounts) {
|
||||
const balance =
|
||||
sum(...this.batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount)) ?? 0n;
|
||||
|
||||
expect(await this.token.balanceOf(account)).to.equal(balance);
|
||||
|
||||
// If not delegated at construction, check before + do delegation
|
||||
if (!this.delegates.includes(account)) {
|
||||
expect(await this.token.getVotes(account)).to.equal(0n);
|
||||
|
||||
await this.token.connect(account).delegate(account);
|
||||
}
|
||||
|
||||
// At this point all accounts should have delegated
|
||||
expect(await this.token.getVotes(account)).to.equal(balance);
|
||||
}
|
||||
});
|
||||
|
||||
it('reverts on consecutive minting to the zero address', async function () {
|
||||
await expect(
|
||||
ethers.deployContract('$ERC721ConsecutiveMock', [
|
||||
name,
|
||||
symbol,
|
||||
offset,
|
||||
this.delegates,
|
||||
[ethers.ZeroAddress],
|
||||
[10],
|
||||
]),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('minting after construction', function () {
|
||||
it('consecutive minting is not possible after construction', async function () {
|
||||
await expect(this.token.$_mintConsecutive(this.alice, 10)).to.be.revertedWithCustomError(
|
||||
this.token,
|
||||
'ERC721ForbiddenBatchMint',
|
||||
);
|
||||
});
|
||||
|
||||
it('simple minting is possible after construction', async function () {
|
||||
const tokenId = sum(...this.batches.map(b => b.amount)) + offset;
|
||||
|
||||
await expect(this.token.ownerOf(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
|
||||
await expect(this.token.$_mint(this.alice, tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.alice, tokenId);
|
||||
});
|
||||
|
||||
it('cannot mint a token that has been batched minted', async function () {
|
||||
const tokenId = sum(...this.batches.map(b => b.amount)) + offset - 1n;
|
||||
|
||||
expect(await this.token.ownerOf(tokenId)).to.not.equal(ethers.ZeroAddress);
|
||||
|
||||
await expect(this.token.$_mint(this.alice, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ERC721 behavior', function () {
|
||||
const tokenId = offset + 1n;
|
||||
|
||||
it('core takes over ownership on transfer', async function () {
|
||||
await this.token.connect(this.alice).transferFrom(this.alice, this.receiver, tokenId);
|
||||
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.receiver);
|
||||
});
|
||||
|
||||
it('tokens can be burned and re-minted #1', async function () {
|
||||
await expect(this.token.connect(this.alice).$_burn(tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.alice, ethers.ZeroAddress, tokenId);
|
||||
|
||||
await expect(this.token.ownerOf(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
|
||||
await expect(this.token.$_mint(this.bruce, tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.bruce, tokenId);
|
||||
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.bruce);
|
||||
});
|
||||
|
||||
it('tokens can be burned and re-minted #2', async function () {
|
||||
const tokenId = sum(...this.batches.map(({ amount }) => amount)) + offset;
|
||||
|
||||
await expect(this.token.ownerOf(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
|
||||
// mint
|
||||
await expect(this.token.$_mint(this.alice, tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.alice, tokenId);
|
||||
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.alice);
|
||||
|
||||
// burn
|
||||
await expect(await this.token.$_burn(tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.alice, ethers.ZeroAddress, tokenId);
|
||||
|
||||
await expect(this.token.ownerOf(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
|
||||
// re-mint
|
||||
await expect(this.token.$_mint(this.bruce, tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.bruce, tokenId);
|
||||
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.bruce);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('invalid use', function () {
|
||||
const receiver = ethers.Wallet.createRandom();
|
||||
|
||||
it('cannot mint a batch larger than 5000', async function () {
|
||||
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveMock');
|
||||
|
||||
await expect(ethers.deployContract('$ERC721ConsecutiveMock', [name, symbol, 0, [], [receiver], [5001n]]))
|
||||
.to.be.revertedWithCustomError({ interface }, 'ERC721ExceededMaxBatchMint')
|
||||
.withArgs(5001n, 5000n);
|
||||
});
|
||||
|
||||
it('cannot use single minting during construction', async function () {
|
||||
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveNoConstructorMintMock');
|
||||
|
||||
await expect(
|
||||
ethers.deployContract('$ERC721ConsecutiveNoConstructorMintMock', [name, symbol]),
|
||||
).to.be.revertedWithCustomError({ interface }, 'ERC721ForbiddenMint');
|
||||
});
|
||||
|
||||
it('cannot use single minting during construction', async function () {
|
||||
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveNoConstructorMintMock');
|
||||
|
||||
await expect(
|
||||
ethers.deployContract('$ERC721ConsecutiveNoConstructorMintMock', [name, symbol]),
|
||||
).to.be.revertedWithCustomError({ interface }, 'ERC721ForbiddenMint');
|
||||
});
|
||||
|
||||
it('consecutive mint not compatible with enumerability', async function () {
|
||||
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveEnumerableMock');
|
||||
|
||||
await expect(
|
||||
ethers.deployContract('$ERC721ConsecutiveEnumerableMock', [name, symbol, [receiver], [100n]]),
|
||||
).to.be.revertedWithCustomError({ interface }, 'ERC721EnumerableForbiddenBatchMint');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
const tokenId = 1n;
|
||||
const otherTokenId = 2n;
|
||||
const data = ethers.Typed.bytes('0x42');
|
||||
|
||||
async function fixture() {
|
||||
const [owner, receiver, operator] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC721Pausable', [name, symbol]);
|
||||
return { owner, receiver, operator, token };
|
||||
}
|
||||
|
||||
describe('ERC721Pausable', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('when token is paused', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, tokenId);
|
||||
await this.token.$_pause();
|
||||
});
|
||||
|
||||
it('reverts when trying to transferFrom', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.owner).transferFrom(this.owner, this.receiver, tokenId),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to safeTransferFrom', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.owner).safeTransferFrom(this.owner, this.receiver, tokenId),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to safeTransferFrom with data', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.owner).safeTransferFrom(this.owner, this.receiver, tokenId, data),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to mint', async function () {
|
||||
await expect(this.token.$_mint(this.receiver, otherTokenId)).to.be.revertedWithCustomError(
|
||||
this.token,
|
||||
'EnforcedPause',
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to burn', async function () {
|
||||
await expect(this.token.$_burn(tokenId)).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
describe('getApproved', function () {
|
||||
it('returns approved address', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
it('returns the amount of tokens owned by the given address', async function () {
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(1n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ownerOf', function () {
|
||||
it('returns the amount of tokens owned by the given address', async function () {
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.owner);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isApprovedForAll', function () {
|
||||
it('returns the approval of the operator', async function () {
|
||||
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.false;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC2981 } = require('../../common/ERC2981.behavior');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
const tokenId1 = 1n;
|
||||
const tokenId2 = 2n;
|
||||
const royalty = 200n;
|
||||
const salePrice = 1000n;
|
||||
|
||||
async function fixture() {
|
||||
const [account1, account2, recipient] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC721Royalty', [name, symbol]);
|
||||
await token.$_mint(account1, tokenId1);
|
||||
await token.$_mint(account1, tokenId2);
|
||||
|
||||
return { account1, account2, recipient, token };
|
||||
}
|
||||
|
||||
describe('ERC721Royalty', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(
|
||||
this,
|
||||
await loadFixture(fixture),
|
||||
{ tokenId1, tokenId2, royalty, salePrice }, // set for behavior tests
|
||||
);
|
||||
});
|
||||
|
||||
describe('token specific functions', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_setTokenRoyalty(tokenId1, this.recipient, royalty);
|
||||
});
|
||||
|
||||
it('royalty information are kept during burn and re-mint', async function () {
|
||||
await this.token.$_burn(tokenId1);
|
||||
|
||||
expect(await this.token.royaltyInfo(tokenId1, salePrice)).to.deep.equal([
|
||||
this.recipient.address,
|
||||
(salePrice * royalty) / 10000n,
|
||||
]);
|
||||
|
||||
await this.token.$_mint(this.account2, tokenId1);
|
||||
|
||||
expect(await this.token.royaltyInfo(tokenId1, salePrice)).to.deep.equal([
|
||||
this.recipient.address,
|
||||
(salePrice * royalty) / 10000n,
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC2981();
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
const baseURI = 'https://api.example.com/v1/';
|
||||
const otherBaseURI = 'https://api.example.com/v2/';
|
||||
const sampleUri = 'mock://mytoken';
|
||||
const tokenId = 1n;
|
||||
const nonExistentTokenId = 2n;
|
||||
|
||||
async function fixture() {
|
||||
const [owner] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC721URIStorageMock', [name, symbol]);
|
||||
return { owner, token };
|
||||
}
|
||||
|
||||
describe('ERC721URIStorage', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['0x49064906']);
|
||||
|
||||
describe('token URI', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, tokenId);
|
||||
});
|
||||
|
||||
it('it is empty by default', async function () {
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal('');
|
||||
});
|
||||
|
||||
it('reverts when queried for non existent token id', async function () {
|
||||
await expect(this.token.tokenURI(nonExistentTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(nonExistentTokenId);
|
||||
});
|
||||
|
||||
it('can be set for a token id', async function () {
|
||||
await this.token.$_setTokenURI(tokenId, sampleUri);
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
|
||||
});
|
||||
|
||||
it('setting the uri emits an event', async function () {
|
||||
await expect(this.token.$_setTokenURI(tokenId, sampleUri))
|
||||
.to.emit(this.token, 'MetadataUpdate')
|
||||
.withArgs(tokenId);
|
||||
});
|
||||
|
||||
it('setting the uri for non existent token id is allowed', async function () {
|
||||
await expect(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri))
|
||||
.to.emit(this.token, 'MetadataUpdate')
|
||||
.withArgs(nonExistentTokenId);
|
||||
|
||||
// value will be accessible after mint
|
||||
await this.token.$_mint(this.owner, nonExistentTokenId);
|
||||
expect(await this.token.tokenURI(nonExistentTokenId)).to.equal(sampleUri);
|
||||
});
|
||||
|
||||
it('base URI can be set', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
expect(await this.token.$_baseURI()).to.equal(baseURI);
|
||||
});
|
||||
|
||||
it('base URI is added as a prefix to the token URI', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
await this.token.$_setTokenURI(tokenId, sampleUri);
|
||||
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + sampleUri);
|
||||
});
|
||||
|
||||
it('token URI can be changed by changing the base URI', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
await this.token.$_setTokenURI(tokenId, sampleUri);
|
||||
|
||||
await this.token.setBaseURI(otherBaseURI);
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal(otherBaseURI + sampleUri);
|
||||
});
|
||||
|
||||
it('tokenId is appended to base URI for tokens with no URI', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + tokenId);
|
||||
});
|
||||
|
||||
it('tokens without URI can be burnt ', async function () {
|
||||
await this.token.$_burn(tokenId);
|
||||
|
||||
await expect(this.token.tokenURI(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
});
|
||||
|
||||
it('tokens with URI can be burnt ', async function () {
|
||||
await this.token.$_setTokenURI(tokenId, sampleUri);
|
||||
|
||||
await this.token.$_burn(tokenId);
|
||||
|
||||
await expect(this.token.tokenURI(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
});
|
||||
|
||||
it('tokens URI is kept if token is burnt and reminted ', async function () {
|
||||
await this.token.$_setTokenURI(tokenId, sampleUri);
|
||||
|
||||
await this.token.$_burn(tokenId);
|
||||
|
||||
await expect(this.token.tokenURI(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
|
||||
await this.token.$_mint(this.owner, tokenId);
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,194 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture, mine } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const time = require('../../../helpers/time');
|
||||
|
||||
const { shouldBehaveLikeVotes } = require('../../../governance/utils/Votes.behavior');
|
||||
|
||||
const TOKENS = [
|
||||
{ Token: '$ERC721Votes', mode: 'blocknumber' },
|
||||
// no timestamp mode for ERC721Votes yet
|
||||
];
|
||||
|
||||
const name = 'My Vote';
|
||||
const symbol = 'MTKN';
|
||||
const version = '1';
|
||||
const tokens = [ethers.parseEther('10000000'), 10n, 20n, 30n];
|
||||
|
||||
describe('ERC721Votes', function () {
|
||||
for (const { Token, mode } of TOKENS) {
|
||||
const fixture = async () => {
|
||||
// accounts is required by shouldBehaveLikeVotes
|
||||
const accounts = await ethers.getSigners();
|
||||
const [holder, recipient, other1, other2] = accounts;
|
||||
|
||||
const token = await ethers.deployContract(Token, [name, symbol, name, version]);
|
||||
|
||||
return { accounts, holder, recipient, other1, other2, token };
|
||||
};
|
||||
|
||||
describe(`vote with ${mode}`, function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
this.votes = this.token;
|
||||
});
|
||||
|
||||
// includes ERC6372 behavior check
|
||||
shouldBehaveLikeVotes(tokens, { mode, fungible: false });
|
||||
|
||||
describe('balanceOf', function () {
|
||||
beforeEach(async function () {
|
||||
await this.votes.$_mint(this.holder, tokens[0]);
|
||||
await this.votes.$_mint(this.holder, tokens[1]);
|
||||
await this.votes.$_mint(this.holder, tokens[2]);
|
||||
await this.votes.$_mint(this.holder, tokens[3]);
|
||||
});
|
||||
|
||||
it('grants to initial account', async function () {
|
||||
expect(await this.votes.balanceOf(this.holder)).to.equal(4n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfers', function () {
|
||||
beforeEach(async function () {
|
||||
await this.votes.$_mint(this.holder, tokens[0]);
|
||||
});
|
||||
|
||||
it('no delegation', async function () {
|
||||
await expect(this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, tokens[0])
|
||||
.to.not.emit(this.token, 'DelegateVotesChanged');
|
||||
|
||||
this.holderVotes = 0n;
|
||||
this.recipientVotes = 0n;
|
||||
});
|
||||
|
||||
it('sender delegation', async function () {
|
||||
await this.votes.connect(this.holder).delegate(this.holder);
|
||||
|
||||
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, tokens[0])
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, 1n, 0n);
|
||||
|
||||
const { logs } = await tx.wait();
|
||||
const { index } = logs.find(event => event.fragment.name == 'DelegateVotesChanged');
|
||||
for (const event of logs.filter(event => event.fragment.name == 'Transfer')) {
|
||||
expect(event.index).to.lt(index);
|
||||
}
|
||||
|
||||
this.holderVotes = 0n;
|
||||
this.recipientVotes = 0n;
|
||||
});
|
||||
|
||||
it('receiver delegation', async function () {
|
||||
await this.votes.connect(this.recipient).delegate(this.recipient);
|
||||
|
||||
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, tokens[0])
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.recipient, 0n, 1n);
|
||||
|
||||
const { logs } = await tx.wait();
|
||||
const { index } = logs.find(event => event.fragment.name == 'DelegateVotesChanged');
|
||||
for (const event of logs.filter(event => event.fragment.name == 'Transfer')) {
|
||||
expect(event.index).to.lt(index);
|
||||
}
|
||||
|
||||
this.holderVotes = 0n;
|
||||
this.recipientVotes = 1n;
|
||||
});
|
||||
|
||||
it('full delegation', async function () {
|
||||
await this.votes.connect(this.holder).delegate(this.holder);
|
||||
await this.votes.connect(this.recipient).delegate(this.recipient);
|
||||
|
||||
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, tokens[0])
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, 1n, 0n)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.recipient, 0n, 1n);
|
||||
|
||||
const { logs } = await tx.wait();
|
||||
const { index } = logs.find(event => event.fragment.name == 'DelegateVotesChanged');
|
||||
for (const event of logs.filter(event => event.fragment.name == 'Transfer')) {
|
||||
expect(event.index).to.lt(index);
|
||||
}
|
||||
|
||||
this.holderVotes = 0;
|
||||
this.recipientVotes = 1n;
|
||||
});
|
||||
|
||||
it('returns the same total supply on transfers', async function () {
|
||||
await this.votes.connect(this.holder).delegate(this.holder);
|
||||
|
||||
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
|
||||
await mine(2);
|
||||
|
||||
expect(await this.votes.getPastTotalSupply(timepoint - 1n)).to.equal(1n);
|
||||
expect(await this.votes.getPastTotalSupply(timepoint + 1n)).to.equal(1n);
|
||||
|
||||
this.holderVotes = 0n;
|
||||
this.recipientVotes = 0n;
|
||||
});
|
||||
|
||||
it('generally returns the voting balance at the appropriate checkpoint', async function () {
|
||||
await this.votes.$_mint(this.holder, tokens[1]);
|
||||
await this.votes.$_mint(this.holder, tokens[2]);
|
||||
await this.votes.$_mint(this.holder, tokens[3]);
|
||||
|
||||
const total = await this.votes.balanceOf(this.holder);
|
||||
|
||||
const t1 = await this.votes.connect(this.holder).delegate(this.other1);
|
||||
await mine(2);
|
||||
const t2 = await this.votes.connect(this.holder).transferFrom(this.holder, this.other2, tokens[0]);
|
||||
await mine(2);
|
||||
const t3 = await this.votes.connect(this.holder).transferFrom(this.holder, this.other2, tokens[2]);
|
||||
await mine(2);
|
||||
const t4 = await this.votes.connect(this.other2).transferFrom(this.other2, this.holder, tokens[2]);
|
||||
await mine(2);
|
||||
|
||||
t1.timepoint = await time.clockFromReceipt[mode](t1);
|
||||
t2.timepoint = await time.clockFromReceipt[mode](t2);
|
||||
t3.timepoint = await time.clockFromReceipt[mode](t3);
|
||||
t4.timepoint = await time.clockFromReceipt[mode](t4);
|
||||
|
||||
expect(await this.votes.getPastVotes(this.other1, t1.timepoint - 1n)).to.equal(0n);
|
||||
expect(await this.votes.getPastVotes(this.other1, t1.timepoint)).to.equal(total);
|
||||
expect(await this.votes.getPastVotes(this.other1, t1.timepoint + 1n)).to.equal(total);
|
||||
expect(await this.votes.getPastVotes(this.other1, t2.timepoint)).to.equal(3n);
|
||||
expect(await this.votes.getPastVotes(this.other1, t2.timepoint + 1n)).to.equal(3n);
|
||||
expect(await this.votes.getPastVotes(this.other1, t3.timepoint)).to.equal(2n);
|
||||
expect(await this.votes.getPastVotes(this.other1, t3.timepoint + 1n)).to.equal(2n);
|
||||
expect(await this.votes.getPastVotes(this.other1, t4.timepoint)).to.equal('3');
|
||||
expect(await this.votes.getPastVotes(this.other1, t4.timepoint + 1n)).to.equal(3n);
|
||||
|
||||
this.holderVotes = 0n;
|
||||
this.recipientVotes = 0n;
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
expect(await this.votes.getVotes(this.holder)).to.equal(this.holderVotes);
|
||||
expect(await this.votes.getVotes(this.recipient)).to.equal(this.recipientVotes);
|
||||
|
||||
// need to advance 2 blocks to see the effect of a transfer on "getPastVotes"
|
||||
const timepoint = await time.clock[mode]();
|
||||
await mine();
|
||||
expect(await this.votes.getPastVotes(this.holder, timepoint)).to.equal(this.holderVotes);
|
||||
expect(await this.votes.getPastVotes(this.recipient, timepoint)).to.equal(this.recipientVotes);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
const tokenId = 1n;
|
||||
const otherTokenId = 2n;
|
||||
|
||||
async function fixture() {
|
||||
const accounts = await ethers.getSigners();
|
||||
const [owner, approved, other] = accounts;
|
||||
|
||||
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]);
|
||||
|
||||
return { accounts, owner, approved, other, underlying, token };
|
||||
}
|
||||
|
||||
describe('ERC721Wrapper', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('has a name', async function () {
|
||||
expect(await this.token.name()).to.equal(`Wrapped ${name}`);
|
||||
});
|
||||
|
||||
it('has a symbol', async function () {
|
||||
expect(await this.token.symbol()).to.equal(`W${symbol}`);
|
||||
});
|
||||
|
||||
it('has underlying', async function () {
|
||||
expect(await this.token.underlying()).to.equal(this.underlying);
|
||||
});
|
||||
|
||||
describe('depositFor', function () {
|
||||
it('works with token approval', async function () {
|
||||
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, this.token, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.owner, tokenId);
|
||||
});
|
||||
|
||||
it('works with approval for all', async function () {
|
||||
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, this.token, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.owner, tokenId);
|
||||
});
|
||||
|
||||
it('works sending to another account', async function () {
|
||||
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, this.token, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.other, tokenId);
|
||||
});
|
||||
|
||||
it('works with multiple tokens', async function () {
|
||||
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, this.token, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.owner, tokenId)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.owner, this.token, otherTokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.owner, otherTokenId);
|
||||
});
|
||||
|
||||
it('reverts with missing approval', async function () {
|
||||
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
|
||||
.withArgs(this.token, tokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('withdrawTo', function () {
|
||||
beforeEach(async function () {
|
||||
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 () {
|
||||
await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId]))
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.token, this.owner, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
|
||||
});
|
||||
|
||||
it('works for an approved', async function () {
|
||||
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, this.owner, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
|
||||
});
|
||||
|
||||
it('works for an approved for all', async function () {
|
||||
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, this.owner, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
|
||||
});
|
||||
|
||||
it("doesn't work for a non-owner nor approved", async function () {
|
||||
await expect(this.token.connect(this.other).withdrawTo(this.owner, [tokenId]))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
|
||||
.withArgs(this.other, tokenId);
|
||||
});
|
||||
|
||||
it('works with multiple tokens', async function () {
|
||||
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, this.owner, tokenId)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.token, this.owner, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
|
||||
});
|
||||
|
||||
it('works to another account', async function () {
|
||||
await expect(this.token.connect(this.owner).withdrawTo(this.other, [tokenId]))
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.token, this.other, tokenId)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('onERC721Received', function () {
|
||||
it('only allows calls from underlying', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.other).onERC721Received(
|
||||
this.owner,
|
||||
this.token,
|
||||
tokenId,
|
||||
this.other.address, // Correct data
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721UnsupportedToken')
|
||||
.withArgs(this.other);
|
||||
});
|
||||
|
||||
it('mints a token to from', async function () {
|
||||
await expect(this.underlying.connect(this.owner).safeTransferFrom(this.owner, this.token, tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.owner, tokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_recover', function () {
|
||||
it('works if there is something to recover', async function () {
|
||||
// Should use `transferFrom` to avoid `onERC721Received` minting
|
||||
await this.underlying.connect(this.owner).transferFrom(this.owner, this.token, tokenId);
|
||||
|
||||
await expect(this.token.$_recover(this.other, tokenId))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.other, tokenId);
|
||||
});
|
||||
|
||||
it('reverts if there is nothing to recover', async function () {
|
||||
const holder = await this.underlying.ownerOf(tokenId);
|
||||
|
||||
await expect(this.token.$_recover(holder, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
|
||||
.withArgs(this.token, tokenId, holder);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ERC712 behavior', function () {
|
||||
shouldBehaveLikeERC721();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user