Fix .gitignore: stop tracking ignored files

This commit is contained in:
5t4l1n
2025-07-27 10:39:02 +05:30
parent b42747e9a3
commit 3a87ef0576
625 changed files with 88566 additions and 63 deletions
@@ -0,0 +1,70 @@
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC721Burnable = artifacts.require('$ERC721Burnable');
contract('ERC721Burnable', function (accounts) {
const [owner, approved] = accounts;
const firstTokenId = new BN(1);
const secondTokenId = new BN(2);
const unknownTokenId = new BN(3);
const name = 'Non Fungible Token';
const symbol = 'NFT';
beforeEach(async function () {
this.token = await ERC721Burnable.new(name, symbol);
});
describe('like a burnable ERC721', function () {
beforeEach(async function () {
await this.token.$_mint(owner, firstTokenId);
await this.token.$_mint(owner, secondTokenId);
});
describe('burn', function () {
const tokenId = firstTokenId;
let receipt = null;
describe('when successful', function () {
beforeEach(async function () {
receipt = await this.token.burn(tokenId, { from: owner });
});
it('burns the given token ID and adjusts the balance of the owner', async function () {
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
});
it('emits a burn event', async function () {
expectEvent(receipt, 'Transfer', {
from: owner,
to: constants.ZERO_ADDRESS,
tokenId: tokenId,
});
});
});
describe('when there is a previous approval burned', function () {
beforeEach(async function () {
await this.token.approve(approved, tokenId, { from: owner });
receipt = await this.token.burn(tokenId, { from: owner });
});
context('getApproved', function () {
it('reverts', async function () {
await expectRevert(this.token.getApproved(tokenId), 'ERC721: invalid token ID');
});
});
});
describe('when the given token ID was not tracked by this contract', function () {
it('reverts', async function () {
await expectRevert(this.token.burn(unknownTokenId, { from: owner }), 'ERC721: invalid token ID');
});
});
});
});
});
@@ -0,0 +1,122 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// solhint-disable func-name-mixedcase
import "../../../../contracts/token/ERC721/extensions/ERC721Consecutive.sol";
import "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 {
uint256 public totalMinted = 0;
constructor(address[] memory receivers, uint256[] memory batches) ERC721("", "") {
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);
}
}
contract ERC721ConsecutiveTest is Test {
function test_balance(address receiver, uint256[] calldata batches) public {
vm.assume(receiver != address(0));
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
assertEq(token.balanceOf(receiver), token.totalMinted());
}
function test_ownership(address receiver, uint256[] calldata batches, uint256[2] calldata unboundedTokenId) public {
vm.assume(receiver != address(0));
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
if (token.totalMinted() > 0) {
uint256 validTokenId = bound(unboundedTokenId[0], 0, token.totalMinted() - 1);
assertEq(token.ownerOf(validTokenId), receiver);
}
uint256 invalidTokenId = bound(unboundedTokenId[1], token.totalMinted(), type(uint256).max);
vm.expectRevert();
token.ownerOf(invalidTokenId);
}
function test_burn(address receiver, uint256[] calldata batches, uint256 unboundedTokenId) public {
vm.assume(receiver != address(0));
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
// 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, 0, 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
) public {
vm.assume(accounts[0] != address(0));
vm.assume(accounts[1] != address(0));
vm.assume(accounts[0] != accounts[1]);
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], 1, 5000);
batches[1] = bound(unboundedBatches[1], 1, 5000);
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(receivers, batches);
uint256 tokenId0 = bound(unboundedTokenId[0], 0, batches[0] - 1);
uint256 tokenId1 = bound(unboundedTokenId[1], 0, batches[1] - 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]);
}
}
@@ -0,0 +1,206 @@
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC721ConsecutiveMock = artifacts.require('$ERC721ConsecutiveMock');
const ERC721ConsecutiveEnumerableMock = artifacts.require('$ERC721ConsecutiveEnumerableMock');
const ERC721ConsecutiveNoConstructorMintMock = artifacts.require('$ERC721ConsecutiveNoConstructorMintMock');
contract('ERC721Consecutive', function (accounts) {
const [user1, user2, user3, receiver] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
const batches = [
{ receiver: user1, amount: 0 },
{ receiver: user1, amount: 1 },
{ receiver: user1, amount: 2 },
{ receiver: user2, amount: 5 },
{ receiver: user3, amount: 0 },
{ receiver: user1, amount: 7 },
];
const delegates = [user1, user3];
describe('with valid batches', function () {
beforeEach(async function () {
this.token = await ERC721ConsecutiveMock.new(
name,
symbol,
delegates,
batches.map(({ receiver }) => receiver),
batches.map(({ amount }) => amount),
);
});
describe('minting during construction', function () {
it('events are emitted at construction', async function () {
let first = 0;
for (const batch of batches) {
if (batch.amount > 0) {
await expectEvent.inConstruction(this.token, 'ConsecutiveTransfer', {
fromTokenId: web3.utils.toBN(first),
toTokenId: web3.utils.toBN(first + batch.amount - 1),
fromAddress: constants.ZERO_ADDRESS,
toAddress: batch.receiver,
});
} else {
// expectEvent.notEmitted.inConstruction only looks at event name, and doesn't check the parameters
}
first += batch.amount;
}
});
it('ownership is set', async function () {
const owners = batches.flatMap(({ receiver, amount }) => Array(amount).fill(receiver));
for (const tokenId in owners) {
expect(await this.token.ownerOf(tokenId)).to.be.equal(owners[tokenId]);
}
});
it('balance & voting power are set', async function () {
for (const account of accounts) {
const balance = batches
.filter(({ receiver }) => receiver === account)
.map(({ amount }) => amount)
.reduce((a, b) => a + b, 0);
expect(await this.token.balanceOf(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
// If not delegated at construction, check before + do delegation
if (!delegates.includes(account)) {
expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(0));
await this.token.delegate(account, { from: account });
}
// At this point all accounts should have delegated
expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
}
});
});
describe('minting after construction', function () {
it('consecutive minting is not possible after construction', async function () {
await expectRevert(
this.token.$_mintConsecutive(user1, 10),
'ERC721Consecutive: batch minting restricted to constructor',
);
});
it('simple minting is possible after construction', async function () {
const tokenId = batches.reduce((acc, { amount }) => acc + amount, 0);
expect(await this.token.$_exists(tokenId)).to.be.equal(false);
expectEvent(await this.token.$_mint(user1, tokenId), 'Transfer', {
from: constants.ZERO_ADDRESS,
to: user1,
tokenId: tokenId.toString(),
});
});
it('cannot mint a token that has been batched minted', async function () {
const tokenId = batches.reduce((acc, { amount }) => acc + amount, 0) - 1;
expect(await this.token.$_exists(tokenId)).to.be.equal(true);
await expectRevert(this.token.$_mint(user1, tokenId), 'ERC721: token already minted');
});
});
describe('ERC721 behavior', function () {
it('core takes over ownership on transfer', async function () {
await this.token.transferFrom(user1, receiver, 1, { from: user1 });
expect(await this.token.ownerOf(1)).to.be.equal(receiver);
});
it('tokens can be burned and re-minted #1', async function () {
expectEvent(await this.token.$_burn(1, { from: user1 }), 'Transfer', {
from: user1,
to: constants.ZERO_ADDRESS,
tokenId: '1',
});
await expectRevert(this.token.ownerOf(1), 'ERC721: invalid token ID');
expectEvent(await this.token.$_mint(user2, 1), 'Transfer', {
from: constants.ZERO_ADDRESS,
to: user2,
tokenId: '1',
});
expect(await this.token.ownerOf(1)).to.be.equal(user2);
});
it('tokens can be burned and re-minted #2', async function () {
const tokenId = batches.reduce((acc, { amount }) => acc.addn(amount), web3.utils.toBN(0));
expect(await this.token.$_exists(tokenId)).to.be.equal(false);
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
// mint
await this.token.$_mint(user1, tokenId);
expect(await this.token.$_exists(tokenId)).to.be.equal(true);
expect(await this.token.ownerOf(tokenId), user1);
// burn
expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
from: user1,
to: constants.ZERO_ADDRESS,
tokenId,
});
expect(await this.token.$_exists(tokenId)).to.be.equal(false);
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
// re-mint
expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
from: constants.ZERO_ADDRESS,
to: user2,
tokenId,
});
expect(await this.token.$_exists(tokenId)).to.be.equal(true);
expect(await this.token.ownerOf(tokenId), user2);
});
});
});
describe('invalid use', function () {
it('cannot mint a batch larger than 5000', async function () {
await expectRevert(
ERC721ConsecutiveMock.new(name, symbol, [], [user1], ['5001']),
'ERC721Consecutive: batch too large',
);
});
it('cannot use single minting during construction', async function () {
await expectRevert(
ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
"ERC721Consecutive: can't mint during construction",
);
});
it('cannot use single minting during construction', async function () {
await expectRevert(
ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
"ERC721Consecutive: can't mint during construction",
);
});
it('consecutive mint not compatible with enumerability', async function () {
await expectRevert(
ERC721ConsecutiveEnumerableMock.new(
name,
symbol,
batches.map(({ receiver }) => receiver),
batches.map(({ amount }) => amount),
),
'ERC721Enumerable: consecutive transfers not supported',
);
});
});
});
@@ -0,0 +1,92 @@
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC721Pausable = artifacts.require('$ERC721Pausable');
contract('ERC721Pausable', function (accounts) {
const [owner, receiver, operator] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
beforeEach(async function () {
this.token = await ERC721Pausable.new(name, symbol);
});
context('when token is paused', function () {
const firstTokenId = new BN(1);
const secondTokenId = new BN(1337);
const mockData = '0x42';
beforeEach(async function () {
await this.token.$_mint(owner, firstTokenId, { from: owner });
await this.token.$_pause();
});
it('reverts when trying to transferFrom', async function () {
await expectRevert(
this.token.transferFrom(owner, receiver, firstTokenId, { from: owner }),
'ERC721Pausable: token transfer while paused',
);
});
it('reverts when trying to safeTransferFrom', async function () {
await expectRevert(
this.token.safeTransferFrom(owner, receiver, firstTokenId, { from: owner }),
'ERC721Pausable: token transfer while paused',
);
});
it('reverts when trying to safeTransferFrom with data', async function () {
await expectRevert(
this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](owner, receiver, firstTokenId, mockData, {
from: owner,
}),
'ERC721Pausable: token transfer while paused',
);
});
it('reverts when trying to mint', async function () {
await expectRevert(this.token.$_mint(receiver, secondTokenId), 'ERC721Pausable: token transfer while paused');
});
it('reverts when trying to burn', async function () {
await expectRevert(this.token.$_burn(firstTokenId), 'ERC721Pausable: token transfer while paused');
});
describe('getApproved', function () {
it('returns approved address', async function () {
const approvedAccount = await this.token.getApproved(firstTokenId);
expect(approvedAccount).to.equal(constants.ZERO_ADDRESS);
});
});
describe('balanceOf', function () {
it('returns the amount of tokens owned by the given address', async function () {
const balance = await this.token.balanceOf(owner);
expect(balance).to.be.bignumber.equal('1');
});
});
describe('ownerOf', function () {
it('returns the amount of tokens owned by the given address', async function () {
const ownerOfToken = await this.token.ownerOf(firstTokenId);
expect(ownerOfToken).to.equal(owner);
});
});
describe('exists', function () {
it('returns token existence', async function () {
expect(await this.token.$_exists(firstTokenId)).to.equal(true);
});
});
describe('isApprovedForAll', function () {
it('returns the approval of the operator', async function () {
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
});
});
});
});
@@ -0,0 +1,41 @@
const { BN, constants } = require('@openzeppelin/test-helpers');
const { shouldBehaveLikeERC2981 } = require('../../common/ERC2981.behavior');
const ERC721Royalty = artifacts.require('$ERC721Royalty');
contract('ERC721Royalty', function (accounts) {
const [account1, account2] = accounts;
const tokenId1 = new BN('1');
const tokenId2 = new BN('2');
const royalty = new BN('200');
const salePrice = new BN('1000');
beforeEach(async function () {
this.token = await ERC721Royalty.new('My Token', 'TKN');
await this.token.$_mint(account1, tokenId1);
await this.token.$_mint(account1, tokenId2);
this.account1 = account1;
this.account2 = account2;
this.tokenId1 = tokenId1;
this.tokenId2 = tokenId2;
this.salePrice = salePrice;
});
describe('token specific functions', function () {
beforeEach(async function () {
await this.token.$_setTokenRoyalty(tokenId1, account1, royalty);
});
it('removes royalty information after burn', async function () {
await this.token.$_burn(tokenId1);
const tokenInfo = await this.token.royaltyInfo(tokenId1, salePrice);
expect(tokenInfo[0]).to.be.equal(constants.ZERO_ADDRESS);
expect(tokenInfo[1]).to.be.bignumber.equal(new BN('0'));
});
});
shouldBehaveLikeERC2981();
});
@@ -0,0 +1,100 @@
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
const ERC721URIStorageMock = artifacts.require('$ERC721URIStorageMock');
contract('ERC721URIStorage', function (accounts) {
const [owner] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
const firstTokenId = new BN('5042');
const nonExistentTokenId = new BN('13');
beforeEach(async function () {
this.token = await ERC721URIStorageMock.new(name, symbol);
});
shouldSupportInterfaces(['0x49064906']);
describe('token URI', function () {
beforeEach(async function () {
await this.token.$_mint(owner, firstTokenId);
});
const baseURI = 'https://api.example.com/v1/';
const sampleUri = 'mock://mytoken';
it('it is empty by default', async function () {
expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
});
it('reverts when queried for non existent token id', async function () {
await expectRevert(this.token.tokenURI(nonExistentTokenId), 'ERC721: invalid token ID');
});
it('can be set for a token id', async function () {
await this.token.$_setTokenURI(firstTokenId, sampleUri);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
});
it('setting the uri emits an event', async function () {
expectEvent(await this.token.$_setTokenURI(firstTokenId, sampleUri), 'MetadataUpdate', {
_tokenId: firstTokenId,
});
});
it('reverts when setting for non existent token id', async function () {
await expectRevert(
this.token.$_setTokenURI(nonExistentTokenId, sampleUri),
'ERC721URIStorage: URI set of nonexistent token',
);
});
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(firstTokenId, sampleUri);
expect(await this.token.tokenURI(firstTokenId)).to.be.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(firstTokenId, sampleUri);
const newBaseURI = 'https://api.example.com/v2/';
await this.token.setBaseURI(newBaseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + 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(firstTokenId)).to.be.equal(baseURI + firstTokenId);
});
it('tokens without URI can be burnt ', async function () {
await this.token.$_burn(firstTokenId, { from: owner });
expect(await this.token.$_exists(firstTokenId)).to.equal(false);
await expectRevert(this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID');
});
it('tokens with URI can be burnt ', async function () {
await this.token.$_setTokenURI(firstTokenId, sampleUri);
await this.token.$_burn(firstTokenId, { from: owner });
expect(await this.token.$_exists(firstTokenId)).to.equal(false);
await expectRevert(this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID');
});
});
});
@@ -0,0 +1,184 @@
/* eslint-disable */
const { BN, expectEvent, time } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { getChainId } = require('../../../helpers/chainid');
const { shouldBehaveLikeVotes } = require('../../../governance/utils/Votes.behavior');
const ERC721Votes = artifacts.require('$ERC721Votes');
contract('ERC721Votes', function (accounts) {
const [account1, account2, account1Delegatee, other1, other2] = accounts;
const name = 'My Vote';
const symbol = 'MTKN';
beforeEach(async function () {
this.chainId = await getChainId();
this.votes = await ERC721Votes.new(name, symbol, name, '1');
this.NFT0 = new BN('10000000000000000000000000');
this.NFT1 = new BN('10');
this.NFT2 = new BN('20');
this.NFT3 = new BN('30');
});
describe('balanceOf', function () {
beforeEach(async function () {
await this.votes.$_mint(account1, this.NFT0);
await this.votes.$_mint(account1, this.NFT1);
await this.votes.$_mint(account1, this.NFT2);
await this.votes.$_mint(account1, this.NFT3);
});
it('grants to initial account', async function () {
expect(await this.votes.balanceOf(account1)).to.be.bignumber.equal('4');
});
});
describe('transfers', function () {
beforeEach(async function () {
await this.votes.$_mint(account1, this.NFT0);
});
it('no delegation', async function () {
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
this.account1Votes = '0';
this.account2Votes = '0';
});
it('sender delegation', async function () {
await this.votes.delegate(account1, { from: account1 });
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account1, previousBalance: '1', newBalance: '0' });
const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
expect(
receipt.logs
.filter(({ event }) => event == 'DelegateVotesChanged')
.every(({ logIndex }) => transferLogIndex < logIndex),
).to.be.equal(true);
this.account1Votes = '0';
this.account2Votes = '0';
});
it('receiver delegation', async function () {
await this.votes.delegate(account2, { from: account2 });
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account2, previousBalance: '0', newBalance: '1' });
const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
expect(
receipt.logs
.filter(({ event }) => event == 'DelegateVotesChanged')
.every(({ logIndex }) => transferLogIndex < logIndex),
).to.be.equal(true);
this.account1Votes = '0';
this.account2Votes = '1';
});
it('full delegation', async function () {
await this.votes.delegate(account1, { from: account1 });
await this.votes.delegate(account2, { from: account2 });
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account1, previousBalance: '1', newBalance: '0' });
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account2, previousBalance: '0', newBalance: '1' });
const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
expect(
receipt.logs
.filter(({ event }) => event == 'DelegateVotesChanged')
.every(({ logIndex }) => transferLogIndex < logIndex),
).to.be.equal(true);
this.account1Votes = '0';
this.account2Votes = '1';
});
it('returns the same total supply on transfers', async function () {
await this.votes.delegate(account1, { from: account1 });
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
await time.advanceBlock();
await time.advanceBlock();
expect(await this.votes.getPastTotalSupply(receipt.blockNumber - 1)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(receipt.blockNumber + 1)).to.be.bignumber.equal('1');
this.account1Votes = '0';
this.account2Votes = '0';
});
it('generally returns the voting balance at the appropriate checkpoint', async function () {
await this.votes.$_mint(account1, this.NFT1);
await this.votes.$_mint(account1, this.NFT2);
await this.votes.$_mint(account1, this.NFT3);
const total = await this.votes.balanceOf(account1);
const t1 = await this.votes.delegate(other1, { from: account1 });
await time.advanceBlock();
await time.advanceBlock();
const t2 = await this.votes.transferFrom(account1, other2, this.NFT0, { from: account1 });
await time.advanceBlock();
await time.advanceBlock();
const t3 = await this.votes.transferFrom(account1, other2, this.NFT2, { from: account1 });
await time.advanceBlock();
await time.advanceBlock();
const t4 = await this.votes.transferFrom(other2, account1, this.NFT2, { from: other2 });
await time.advanceBlock();
await time.advanceBlock();
expect(await this.votes.getPastVotes(other1, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(other1, t1.receipt.blockNumber)).to.be.bignumber.equal(total);
expect(await this.votes.getPastVotes(other1, t1.receipt.blockNumber + 1)).to.be.bignumber.equal(total);
expect(await this.votes.getPastVotes(other1, t2.receipt.blockNumber)).to.be.bignumber.equal('3');
expect(await this.votes.getPastVotes(other1, t2.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
expect(await this.votes.getPastVotes(other1, t3.receipt.blockNumber)).to.be.bignumber.equal('2');
expect(await this.votes.getPastVotes(other1, t3.receipt.blockNumber + 1)).to.be.bignumber.equal('2');
expect(await this.votes.getPastVotes(other1, t4.receipt.blockNumber)).to.be.bignumber.equal('3');
expect(await this.votes.getPastVotes(other1, t4.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
this.account1Votes = '0';
this.account2Votes = '0';
});
afterEach(async function () {
expect(await this.votes.getVotes(account1)).to.be.bignumber.equal(this.account1Votes);
expect(await this.votes.getVotes(account2)).to.be.bignumber.equal(this.account2Votes);
// need to advance 2 blocks to see the effect of a transfer on "getPastVotes"
const blockNumber = await time.latestBlock();
await time.advanceBlock();
expect(await this.votes.getPastVotes(account1, blockNumber)).to.be.bignumber.equal(this.account1Votes);
expect(await this.votes.getPastVotes(account2, blockNumber)).to.be.bignumber.equal(this.account2Votes);
});
});
describe('Voting workflow', function () {
beforeEach(async function () {
this.account1 = account1;
this.account1Delegatee = account1Delegatee;
this.account2 = account2;
this.name = 'My Vote';
});
// includes EIP6372 behavior check
shouldBehaveLikeVotes();
});
});
@@ -0,0 +1,283 @@
const { BN, expectEvent, constants, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
const ERC721 = artifacts.require('$ERC721');
const ERC721Wrapper = artifacts.require('$ERC721Wrapper');
contract('ERC721Wrapper', function (accounts) {
const [initialHolder, anotherAccount, approvedAccount] = accounts;
const name = 'My Token';
const symbol = 'MTKN';
const firstTokenId = new BN(1);
const secondTokenId = new BN(2);
beforeEach(async function () {
this.underlying = await ERC721.new(name, symbol);
this.token = await ERC721Wrapper.new(`Wrapped ${name}`, `W${symbol}`, this.underlying.address);
await this.underlying.$_safeMint(initialHolder, firstTokenId);
await this.underlying.$_safeMint(initialHolder, secondTokenId);
});
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.be.bignumber.equal(this.underlying.address);
});
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,
});
});
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,
});
});
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,
});
});
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,
});
});
it('reverts with missing approval', async function () {
await expectRevert(
this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder }),
'ERC721: caller is not token owner or approved',
);
});
});
describe('withdrawTo', function () {
beforeEach(async function () {
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
});
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,
});
});
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,
});
});
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,
});
});
it("doesn't work for a non-owner nor approved", async function () {
await expectRevert(
this.token.withdrawTo(initialHolder, [firstTokenId], { from: anotherAccount }),
'ERC721Wrapper: caller is not token owner or approved',
);
});
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,
});
});
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,
});
});
});
describe('onERC721Received', function () {
it('only allows calls from underlying', async function () {
await expectRevert(
this.token.onERC721Received(
initialHolder,
this.token.address,
firstTokenId,
anotherAccount, // Correct data
{ from: anotherAccount },
),
'ERC721Wrapper: caller is not underlying',
);
});
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,
});
});
});
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 });
const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: anotherAccount,
tokenId: firstTokenId,
});
});
it('reverts if there is nothing to recover', async function () {
await expectRevert(
this.token.$_recover(initialHolder, firstTokenId),
'ERC721Wrapper: wrapper is not token owner',
);
});
});
describe('ERC712 behavior', function () {
shouldBehaveLikeERC721('ERC721', ...accounts);
});
});