dexorder
This commit is contained in:
@@ -0,0 +1,763 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
|
||||
|
||||
const { RevertType } = require('../../helpers/enums');
|
||||
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
function shouldBehaveLikeERC1155() {
|
||||
const firstTokenId = 1n;
|
||||
const secondTokenId = 2n;
|
||||
const unknownTokenId = 3n;
|
||||
|
||||
const firstTokenValue = 1000n;
|
||||
const secondTokenValue = 2000n;
|
||||
|
||||
const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
|
||||
const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
|
||||
|
||||
beforeEach(async function () {
|
||||
[this.recipient, this.proxy, this.alice, this.bruce] = this.otherAccounts;
|
||||
});
|
||||
|
||||
describe('like an ERC1155', function () {
|
||||
describe('balanceOf', function () {
|
||||
it('should return 0 when queried about the zero address', async function () {
|
||||
expect(await this.token.balanceOf(ethers.ZeroAddress, firstTokenId)).to.equal(0n);
|
||||
});
|
||||
|
||||
describe("when accounts don't own tokens", function () {
|
||||
it('returns zero for given addresses', async function () {
|
||||
expect(await this.token.balanceOf(this.alice, firstTokenId)).to.equal(0n);
|
||||
expect(await this.token.balanceOf(this.bruce, secondTokenId)).to.equal(0n);
|
||||
expect(await this.token.balanceOf(this.alice, unknownTokenId)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when accounts own some tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.alice, firstTokenId, firstTokenValue, '0x');
|
||||
await this.token.$_mint(this.bruce, secondTokenId, secondTokenValue, '0x');
|
||||
});
|
||||
|
||||
it('returns the amount of tokens owned by the given addresses', async function () {
|
||||
expect(await this.token.balanceOf(this.alice, firstTokenId)).to.equal(firstTokenValue);
|
||||
expect(await this.token.balanceOf(this.bruce, secondTokenId)).to.equal(secondTokenValue);
|
||||
expect(await this.token.balanceOf(this.alice, unknownTokenId)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('balanceOfBatch', function () {
|
||||
it("reverts when input arrays don't match up", async function () {
|
||||
const accounts1 = [this.alice, this.bruce, this.alice, this.bruce];
|
||||
const ids1 = [firstTokenId, secondTokenId, unknownTokenId];
|
||||
|
||||
await expect(this.token.balanceOfBatch(accounts1, ids1))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(ids1.length, accounts1.length);
|
||||
|
||||
const accounts2 = [this.alice, this.bruce];
|
||||
const ids2 = [firstTokenId, secondTokenId, unknownTokenId];
|
||||
await expect(this.token.balanceOfBatch(accounts2, ids2))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(ids2.length, accounts2.length);
|
||||
});
|
||||
|
||||
it('should return 0 as the balance when one of the addresses is the zero address', async function () {
|
||||
const result = await this.token.balanceOfBatch(
|
||||
[this.alice, this.bruce, ethers.ZeroAddress],
|
||||
[firstTokenId, secondTokenId, unknownTokenId],
|
||||
);
|
||||
expect(result).to.deep.equal([0n, 0n, 0n]);
|
||||
});
|
||||
|
||||
describe("when accounts don't own tokens", function () {
|
||||
it('returns zeros for each account', async function () {
|
||||
const result = await this.token.balanceOfBatch(
|
||||
[this.alice, this.bruce, this.alice],
|
||||
[firstTokenId, secondTokenId, unknownTokenId],
|
||||
);
|
||||
expect(result).to.deep.equal([0n, 0n, 0n]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when accounts own some tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.alice, firstTokenId, firstTokenValue, '0x');
|
||||
await this.token.$_mint(this.bruce, secondTokenId, secondTokenValue, '0x');
|
||||
});
|
||||
|
||||
it('returns amounts owned by each account in order passed', async function () {
|
||||
const result = await this.token.balanceOfBatch(
|
||||
[this.bruce, this.alice, this.alice],
|
||||
[secondTokenId, firstTokenId, unknownTokenId],
|
||||
);
|
||||
expect(result).to.deep.equal([secondTokenValue, firstTokenValue, 0n]);
|
||||
});
|
||||
|
||||
it('returns multiple times the balance of the same address when asked', async function () {
|
||||
const result = await this.token.balanceOfBatch(
|
||||
[this.alice, this.bruce, this.alice],
|
||||
[firstTokenId, secondTokenId, firstTokenId],
|
||||
);
|
||||
expect(result).to.deep.equal([firstTokenValue, secondTokenValue, firstTokenValue]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setApprovalForAll', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.holder).setApprovalForAll(this.proxy, true);
|
||||
});
|
||||
|
||||
it('sets approval status which can be queried via isApprovedForAll', async function () {
|
||||
expect(await this.token.isApprovedForAll(this.holder, this.proxy)).to.be.true;
|
||||
});
|
||||
|
||||
it('emits an ApprovalForAll log', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'ApprovalForAll').withArgs(this.holder, this.proxy, true);
|
||||
});
|
||||
|
||||
it('can unset approval for an operator', async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.proxy, false);
|
||||
expect(await this.token.isApprovedForAll(this.holder, this.proxy)).to.be.false;
|
||||
});
|
||||
|
||||
it('reverts if attempting to approve zero address as an operator', async function () {
|
||||
await expect(this.token.connect(this.holder).setApprovalForAll(ethers.ZeroAddress, true))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidOperator')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('safeTransferFrom', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
|
||||
await this.token.$_mint(this.holder, secondTokenId, secondTokenValue, '0x');
|
||||
});
|
||||
|
||||
it('reverts when transferring more than balance', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, this.recipient, firstTokenId, firstTokenValue + 1n, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
|
||||
.withArgs(this.holder, firstTokenValue, firstTokenValue + 1n, firstTokenId);
|
||||
});
|
||||
|
||||
it('reverts when transferring to zero address', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, ethers.ZeroAddress, firstTokenId, firstTokenValue, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
function transferWasSuccessful() {
|
||||
it('debits transferred balance from sender', async function () {
|
||||
expect(await this.token.balanceOf(this.args.from, this.args.id)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('credits transferred balance to receiver', async function () {
|
||||
expect(await this.token.balanceOf(this.args.to, this.args.id)).to.equal(this.args.value);
|
||||
});
|
||||
|
||||
it('emits a TransferSingle log', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'TransferSingle')
|
||||
.withArgs(this.args.operator, this.args.from, this.args.to, this.args.id, this.args.value);
|
||||
});
|
||||
}
|
||||
|
||||
describe('when called by the holder', function () {
|
||||
beforeEach(async function () {
|
||||
this.args = {
|
||||
operator: this.holder,
|
||||
from: this.holder,
|
||||
to: this.recipient,
|
||||
id: firstTokenId,
|
||||
value: firstTokenValue,
|
||||
data: '0x',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeTransferFrom(this.args.from, this.args.to, this.args.id, this.args.value, this.args.data);
|
||||
});
|
||||
|
||||
transferWasSuccessful();
|
||||
|
||||
it('preserves existing balances which are not transferred by holder', async function () {
|
||||
expect(await this.token.balanceOf(this.holder, secondTokenId)).to.equal(secondTokenValue);
|
||||
expect(await this.token.balanceOf(this.recipient, secondTokenId)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when called by an operator on behalf of the holder', function () {
|
||||
describe('when operator is not approved by holder', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.proxy, false);
|
||||
});
|
||||
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.proxy)
|
||||
.safeTransferFrom(this.holder, this.recipient, firstTokenId, firstTokenValue, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
|
||||
.withArgs(this.proxy, this.holder);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when operator is approved by holder', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.proxy, true);
|
||||
|
||||
this.args = {
|
||||
operator: this.proxy,
|
||||
from: this.holder,
|
||||
to: this.recipient,
|
||||
id: firstTokenId,
|
||||
value: firstTokenValue,
|
||||
data: '0x',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeTransferFrom(this.args.from, this.args.to, this.args.id, this.args.value, this.args.data);
|
||||
});
|
||||
|
||||
transferWasSuccessful();
|
||||
|
||||
it("preserves operator's balances not involved in the transfer", async function () {
|
||||
expect(await this.token.balanceOf(this.proxy, firstTokenId)).to.equal(0n);
|
||||
expect(await this.token.balanceOf(this.proxy, secondTokenId)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when sending to a valid receiver', function () {
|
||||
beforeEach(async function () {
|
||||
this.receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.None,
|
||||
]);
|
||||
});
|
||||
|
||||
describe('without data', function () {
|
||||
beforeEach(async function () {
|
||||
this.args = {
|
||||
operator: this.holder,
|
||||
from: this.holder,
|
||||
to: this.receiver,
|
||||
id: firstTokenId,
|
||||
value: firstTokenValue,
|
||||
data: '0x',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeTransferFrom(this.args.from, this.args.to, this.args.id, this.args.value, this.args.data);
|
||||
});
|
||||
|
||||
transferWasSuccessful();
|
||||
|
||||
it('calls onERC1155Received', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.receiver, 'Received')
|
||||
.withArgs(this.args.operator, this.args.from, this.args.id, this.args.value, this.args.data, anyValue);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with data', function () {
|
||||
beforeEach(async function () {
|
||||
this.args = {
|
||||
operator: this.holder,
|
||||
from: this.holder,
|
||||
to: this.receiver,
|
||||
id: firstTokenId,
|
||||
value: firstTokenValue,
|
||||
data: '0xf00dd00d',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeTransferFrom(this.args.from, this.args.to, this.args.id, this.args.value, this.args.data);
|
||||
});
|
||||
|
||||
transferWasSuccessful();
|
||||
|
||||
it('calls onERC1155Received', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.receiver, 'Received')
|
||||
.withArgs(this.args.operator, this.args.from, this.args.id, this.args.value, this.args.data, anyValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract returning unexpected value', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
'0x00c0ffee',
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.None,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(receiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts', function () {
|
||||
describe('with a revert string', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.RevertWithMessage,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
).to.be.revertedWith('ERC1155ReceiverMock: reverting on receive');
|
||||
});
|
||||
});
|
||||
|
||||
describe('without a revert string', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.RevertWithoutMessage,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(receiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a custom error', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.RevertWithCustomError,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(receiver, 'CustomError')
|
||||
.withArgs(RECEIVER_SINGLE_MAGIC_VALUE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a panic', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.Panic,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
).to.be.revertedWithPanic();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a contract that does not implement the required function', function () {
|
||||
it('reverts', async function () {
|
||||
const invalidReceiver = this.token;
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, invalidReceiver, firstTokenId, firstTokenValue, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(invalidReceiver);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('safeBatchTransferFrom', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
|
||||
await this.token.$_mint(this.holder, secondTokenId, secondTokenValue, '0x');
|
||||
});
|
||||
|
||||
it('reverts when transferring value more than any of balances', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
this.recipient,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue + 1n],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
|
||||
.withArgs(this.holder, secondTokenValue, secondTokenValue + 1n, secondTokenId);
|
||||
});
|
||||
|
||||
it("reverts when ids array length doesn't match values array length", async function () {
|
||||
const ids1 = [firstTokenId];
|
||||
const tokenValues1 = [firstTokenValue, secondTokenValue];
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).safeBatchTransferFrom(this.holder, this.recipient, ids1, tokenValues1, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(ids1.length, tokenValues1.length);
|
||||
|
||||
const ids2 = [firstTokenId, secondTokenId];
|
||||
const tokenValues2 = [firstTokenValue];
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).safeBatchTransferFrom(this.holder, this.recipient, ids2, tokenValues2, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(ids2.length, tokenValues2.length);
|
||||
});
|
||||
|
||||
it('reverts when transferring to zero address', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
ethers.ZeroAddress,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('reverts when transferring from zero address', async function () {
|
||||
await expect(
|
||||
this.token.$_safeBatchTransferFrom(ethers.ZeroAddress, this.holder, [firstTokenId], [firstTokenValue], '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
function batchTransferWasSuccessful() {
|
||||
it('debits transferred balances from sender', async function () {
|
||||
const newBalances = await this.token.balanceOfBatch(
|
||||
this.args.ids.map(() => this.args.from),
|
||||
this.args.ids,
|
||||
);
|
||||
expect(newBalances).to.deep.equal(this.args.ids.map(() => 0n));
|
||||
});
|
||||
|
||||
it('credits transferred balances to receiver', async function () {
|
||||
const newBalances = await this.token.balanceOfBatch(
|
||||
this.args.ids.map(() => this.args.to),
|
||||
this.args.ids,
|
||||
);
|
||||
expect(newBalances).to.deep.equal(this.args.values);
|
||||
});
|
||||
|
||||
it('emits a TransferBatch log', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'TransferBatch')
|
||||
.withArgs(this.args.operator, this.args.from, this.args.to, this.args.ids, this.args.values);
|
||||
});
|
||||
}
|
||||
|
||||
describe('when called by the holder', function () {
|
||||
beforeEach(async function () {
|
||||
this.args = {
|
||||
operator: this.holder,
|
||||
from: this.holder,
|
||||
to: this.recipient,
|
||||
ids: [firstTokenId, secondTokenId],
|
||||
values: [firstTokenValue, secondTokenValue],
|
||||
data: '0x',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeBatchTransferFrom(this.args.from, this.args.to, this.args.ids, this.args.values, this.args.data);
|
||||
});
|
||||
|
||||
batchTransferWasSuccessful();
|
||||
});
|
||||
|
||||
describe('when called by an operator on behalf of the holder', function () {
|
||||
describe('when operator is not approved by holder', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.proxy, false);
|
||||
});
|
||||
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.proxy)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
this.recipient,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
|
||||
.withArgs(this.proxy, this.holder);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when operator is approved by holder', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.proxy, true);
|
||||
|
||||
this.args = {
|
||||
operator: this.proxy,
|
||||
from: this.holder,
|
||||
to: this.recipient,
|
||||
ids: [firstTokenId, secondTokenId],
|
||||
values: [firstTokenValue, secondTokenValue],
|
||||
data: '0x',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeBatchTransferFrom(this.args.from, this.args.to, this.args.ids, this.args.values, this.args.data);
|
||||
});
|
||||
|
||||
batchTransferWasSuccessful();
|
||||
|
||||
it("preserves operator's balances not involved in the transfer", async function () {
|
||||
expect(await this.token.balanceOf(this.proxy, firstTokenId)).to.equal(0n);
|
||||
expect(await this.token.balanceOf(this.proxy, secondTokenId)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when sending to a valid receiver', function () {
|
||||
beforeEach(async function () {
|
||||
this.receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.None,
|
||||
]);
|
||||
});
|
||||
|
||||
describe('without data', function () {
|
||||
beforeEach(async function () {
|
||||
this.args = {
|
||||
operator: this.holder,
|
||||
from: this.holder,
|
||||
to: this.receiver,
|
||||
ids: [firstTokenId, secondTokenId],
|
||||
values: [firstTokenValue, secondTokenValue],
|
||||
data: '0x',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeBatchTransferFrom(this.args.from, this.args.to, this.args.ids, this.args.values, this.args.data);
|
||||
});
|
||||
|
||||
batchTransferWasSuccessful();
|
||||
|
||||
it('calls onERC1155BatchReceived', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.receiver, 'BatchReceived')
|
||||
.withArgs(this.holder, this.holder, this.args.ids, this.args.values, this.args.data, anyValue);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with data', function () {
|
||||
beforeEach(async function () {
|
||||
this.args = {
|
||||
operator: this.holder,
|
||||
from: this.holder,
|
||||
to: this.receiver,
|
||||
ids: [firstTokenId, secondTokenId],
|
||||
values: [firstTokenValue, secondTokenValue],
|
||||
data: '0xf00dd00d',
|
||||
};
|
||||
this.tx = await this.token
|
||||
.connect(this.args.operator)
|
||||
.safeBatchTransferFrom(this.args.from, this.args.to, this.args.ids, this.args.values, this.args.data);
|
||||
});
|
||||
|
||||
batchTransferWasSuccessful();
|
||||
|
||||
it('calls onERC1155Received', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.receiver, 'BatchReceived')
|
||||
.withArgs(this.holder, this.holder, this.args.ids, this.args.values, this.args.data, anyValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract returning unexpected value', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RevertType.None,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
receiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(receiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts', function () {
|
||||
describe('with a revert string', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.RevertWithMessage,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
receiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
).to.be.revertedWith('ERC1155ReceiverMock: reverting on batch receive');
|
||||
});
|
||||
});
|
||||
|
||||
describe('without a revert string', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.RevertWithoutMessage,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
receiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(receiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a custom error', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.RevertWithCustomError,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
receiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(receiver, 'CustomError')
|
||||
.withArgs(RECEIVER_SINGLE_MAGIC_VALUE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with a panic', function () {
|
||||
it('reverts', async function () {
|
||||
const receiver = await ethers.deployContract('$ERC1155ReceiverMock', [
|
||||
RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
RECEIVER_BATCH_MAGIC_VALUE,
|
||||
RevertType.Panic,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
receiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
).to.be.revertedWithPanic();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a contract that does not implement the required function', function () {
|
||||
it('reverts', async function () {
|
||||
const invalidReceiver = this.token;
|
||||
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(
|
||||
this.holder,
|
||||
invalidReceiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(invalidReceiver);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC1155', 'ERC1155MetadataURI']);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeERC1155,
|
||||
};
|
||||
213
lib_openzeppelin_contracts/test/token/ERC1155/ERC1155.test.js
Normal file
213
lib_openzeppelin_contracts/test/token/ERC1155/ERC1155.test.js
Normal file
@@ -0,0 +1,213 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { zip } = require('../../helpers/iterate');
|
||||
const { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior');
|
||||
|
||||
const initialURI = 'https://token-cdn-domain/{id}.json';
|
||||
|
||||
async function fixture() {
|
||||
const [operator, holder, ...otherAccounts] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC1155', [initialURI]);
|
||||
return { token, operator, holder, otherAccounts };
|
||||
}
|
||||
|
||||
describe('ERC1155', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC1155();
|
||||
|
||||
describe('internal functions', function () {
|
||||
const tokenId = 1990n;
|
||||
const mintValue = 9001n;
|
||||
const burnValue = 3000n;
|
||||
|
||||
const tokenBatchIds = [2000n, 2010n, 2020n];
|
||||
const mintValues = [5000n, 10000n, 42195n];
|
||||
const burnValues = [5000n, 9001n, 195n];
|
||||
|
||||
const data = '0x12345678';
|
||||
|
||||
describe('_mint', function () {
|
||||
it('reverts with a zero destination address', async function () {
|
||||
await expect(this.token.$_mint(ethers.ZeroAddress, tokenId, mintValue, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue, data);
|
||||
});
|
||||
|
||||
it('emits a TransferSingle event', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'TransferSingle')
|
||||
.withArgs(this.operator, ethers.ZeroAddress, this.holder, tokenId, mintValue);
|
||||
});
|
||||
|
||||
it('credits the minted token value', async function () {
|
||||
expect(await this.token.balanceOf(this.holder, tokenId)).to.equal(mintValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_mintBatch', function () {
|
||||
it('reverts with a zero destination address', async function () {
|
||||
await expect(this.token.$_mintBatch(ethers.ZeroAddress, tokenBatchIds, mintValues, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('reverts if length of inputs do not match', async function () {
|
||||
await expect(this.token.$_mintBatch(this.holder, tokenBatchIds, mintValues.slice(1), data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(tokenBatchIds.length, mintValues.length - 1);
|
||||
|
||||
await expect(this.token.$_mintBatch(this.holder, tokenBatchIds.slice(1), mintValues, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(tokenBatchIds.length - 1, mintValues.length);
|
||||
});
|
||||
|
||||
describe('with minted batch of tokens', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.operator).$_mintBatch(this.holder, tokenBatchIds, mintValues, data);
|
||||
});
|
||||
|
||||
it('emits a TransferBatch event', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'TransferBatch')
|
||||
.withArgs(this.operator, ethers.ZeroAddress, this.holder, tokenBatchIds, mintValues);
|
||||
});
|
||||
|
||||
it('credits the minted batch of tokens', async function () {
|
||||
const holderBatchBalances = await this.token.balanceOfBatch(
|
||||
tokenBatchIds.map(() => this.holder),
|
||||
tokenBatchIds,
|
||||
);
|
||||
|
||||
expect(holderBatchBalances).to.deep.equal(mintValues);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it("reverts when burning the zero account's tokens", async function () {
|
||||
await expect(this.token.$_burn(ethers.ZeroAddress, tokenId, mintValue))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('reverts when burning a non-existent token id', async function () {
|
||||
await expect(this.token.$_burn(this.holder, tokenId, mintValue))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
|
||||
.withArgs(this.holder, 0, mintValue, tokenId);
|
||||
});
|
||||
|
||||
it('reverts when burning more than available tokens', async function () {
|
||||
await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue, data);
|
||||
|
||||
await expect(this.token.$_burn(this.holder, tokenId, mintValue + 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
|
||||
.withArgs(this.holder, mintValue, mintValue + 1n, tokenId);
|
||||
});
|
||||
|
||||
describe('with minted-then-burnt tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, tokenId, mintValue, data);
|
||||
this.tx = await this.token.connect(this.operator).$_burn(this.holder, tokenId, burnValue);
|
||||
});
|
||||
|
||||
it('emits a TransferSingle event', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'TransferSingle')
|
||||
.withArgs(this.operator, this.holder, ethers.ZeroAddress, tokenId, burnValue);
|
||||
});
|
||||
|
||||
it('accounts for both minting and burning', async function () {
|
||||
expect(await this.token.balanceOf(this.holder, tokenId)).to.equal(mintValue - burnValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burnBatch', function () {
|
||||
it("reverts when burning the zero account's tokens", async function () {
|
||||
await expect(this.token.$_burnBatch(ethers.ZeroAddress, tokenBatchIds, burnValues))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('reverts if length of inputs do not match', async function () {
|
||||
await expect(this.token.$_burnBatch(this.holder, tokenBatchIds, burnValues.slice(1)))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(tokenBatchIds.length, burnValues.length - 1);
|
||||
|
||||
await expect(this.token.$_burnBatch(this.holder, tokenBatchIds.slice(1), burnValues))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
|
||||
.withArgs(tokenBatchIds.length - 1, burnValues.length);
|
||||
});
|
||||
|
||||
it('reverts when burning a non-existent token id', async function () {
|
||||
await expect(this.token.$_burnBatch(this.holder, tokenBatchIds, burnValues))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
|
||||
.withArgs(this.holder, 0, burnValues[0], tokenBatchIds[0]);
|
||||
});
|
||||
|
||||
describe('with minted-then-burnt tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mintBatch(this.holder, tokenBatchIds, mintValues, data);
|
||||
this.tx = await this.token.connect(this.operator).$_burnBatch(this.holder, tokenBatchIds, burnValues);
|
||||
});
|
||||
|
||||
it('emits a TransferBatch event', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'TransferBatch')
|
||||
.withArgs(this.operator, this.holder, ethers.ZeroAddress, tokenBatchIds, burnValues);
|
||||
});
|
||||
|
||||
it('accounts for both minting and burning', async function () {
|
||||
const holderBatchBalances = await this.token.balanceOfBatch(
|
||||
tokenBatchIds.map(() => this.holder),
|
||||
tokenBatchIds,
|
||||
);
|
||||
|
||||
expect(holderBatchBalances).to.deep.equal(
|
||||
zip(mintValues, burnValues).map(([mintValue, burnValue]) => mintValue - burnValue),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ERC1155MetadataURI', function () {
|
||||
const firstTokenID = 42n;
|
||||
const secondTokenID = 1337n;
|
||||
|
||||
it('emits no URI event in constructor', async function () {
|
||||
await expect(this.token.deploymentTransaction()).to.not.emit(this.token, 'URI');
|
||||
});
|
||||
|
||||
it('sets the initial URI for all token types', async function () {
|
||||
expect(await this.token.uri(firstTokenID)).to.equal(initialURI);
|
||||
expect(await this.token.uri(secondTokenID)).to.equal(initialURI);
|
||||
});
|
||||
|
||||
describe('_setURI', function () {
|
||||
const newURI = 'https://token-cdn-domain/{locale}/{id}.json';
|
||||
|
||||
it('emits no URI event', async function () {
|
||||
await expect(this.token.$_setURI(newURI)).to.not.emit(this.token, 'URI');
|
||||
});
|
||||
|
||||
it('sets the new URI for all token types', async function () {
|
||||
await this.token.$_setURI(newURI);
|
||||
|
||||
expect(await this.token.uri(firstTokenID)).to.equal(newURI);
|
||||
expect(await this.token.uri(secondTokenID)).to.equal(newURI);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const ids = [42n, 1137n];
|
||||
const values = [3000n, 9902n];
|
||||
|
||||
async function fixture() {
|
||||
const [holder, operator, other] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC1155Burnable', ['https://token-cdn-domain/{id}.json']);
|
||||
await token.$_mint(holder, ids[0], values[0], '0x');
|
||||
await token.$_mint(holder, ids[1], values[1], '0x');
|
||||
|
||||
return { token, holder, operator, other };
|
||||
}
|
||||
|
||||
describe('ERC1155Burnable', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('burn', function () {
|
||||
it('holder can burn their tokens', async function () {
|
||||
await this.token.connect(this.holder).burn(this.holder, ids[0], values[0] - 1n);
|
||||
|
||||
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
|
||||
});
|
||||
|
||||
it("approved operators can burn the holder's tokens", async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
|
||||
await this.token.connect(this.operator).burn(this.holder, ids[0], values[0] - 1n);
|
||||
|
||||
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
|
||||
});
|
||||
|
||||
it("unapproved accounts cannot burn the holder's tokens", async function () {
|
||||
await expect(this.token.connect(this.other).burn(this.holder, ids[0], values[0] - 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
|
||||
.withArgs(this.other, this.holder);
|
||||
});
|
||||
});
|
||||
|
||||
describe('burnBatch', function () {
|
||||
it('holder can burn their tokens', async function () {
|
||||
await this.token.connect(this.holder).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]);
|
||||
|
||||
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
|
||||
expect(await this.token.balanceOf(this.holder, ids[1])).to.equal(2n);
|
||||
});
|
||||
|
||||
it("approved operators can burn the holder's tokens", async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
|
||||
await this.token.connect(this.operator).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]);
|
||||
|
||||
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
|
||||
expect(await this.token.balanceOf(this.holder, ids[1])).to.equal(2n);
|
||||
});
|
||||
|
||||
it("unapproved accounts cannot burn the holder's tokens", async function () {
|
||||
await expect(this.token.connect(this.other).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
|
||||
.withArgs(this.other, this.holder);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,105 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
async function fixture() {
|
||||
const [holder, operator, receiver, other] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC1155Pausable', ['https://token-cdn-domain/{id}.json']);
|
||||
return { token, holder, operator, receiver, other };
|
||||
}
|
||||
|
||||
describe('ERC1155Pausable', function () {
|
||||
const firstTokenId = 37n;
|
||||
const firstTokenValue = 42n;
|
||||
const secondTokenId = 19842n;
|
||||
const secondTokenValue = 23n;
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('when token is paused', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
|
||||
await this.token.$_pause();
|
||||
});
|
||||
|
||||
it('reverts when trying to safeTransferFrom from holder', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeTransferFrom(this.holder, this.receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to safeTransferFrom from operator', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.operator)
|
||||
.safeTransferFrom(this.holder, this.receiver, firstTokenId, firstTokenValue, '0x'),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to safeBatchTransferFrom from holder', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.holder)
|
||||
.safeBatchTransferFrom(this.holder, this.receiver, [firstTokenId], [firstTokenValue], '0x'),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to safeBatchTransferFrom from operator', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.operator)
|
||||
.safeBatchTransferFrom(this.holder, this.receiver, [firstTokenId], [firstTokenValue], '0x'),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to mint', async function () {
|
||||
await expect(this.token.$_mint(this.holder, secondTokenId, secondTokenValue, '0x')).to.be.revertedWithCustomError(
|
||||
this.token,
|
||||
'EnforcedPause',
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to mintBatch', async function () {
|
||||
await expect(
|
||||
this.token.$_mintBatch(this.holder, [secondTokenId], [secondTokenValue], '0x'),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
it('reverts when trying to burn', async function () {
|
||||
await expect(this.token.$_burn(this.holder, firstTokenId, firstTokenValue)).to.be.revertedWithCustomError(
|
||||
this.token,
|
||||
'EnforcedPause',
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to burnBatch', async function () {
|
||||
await expect(
|
||||
this.token.$_burnBatch(this.holder, [firstTokenId], [firstTokenValue]),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
|
||||
describe('setApprovalForAll', function () {
|
||||
it('approves an operator', async function () {
|
||||
await this.token.connect(this.holder).setApprovalForAll(this.other, true);
|
||||
expect(await this.token.isApprovedForAll(this.holder, this.other)).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
it('returns the token value owned by the given address', async function () {
|
||||
expect(await this.token.balanceOf(this.holder, firstTokenId)).to.equal(firstTokenValue);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isApprovedForAll', function () {
|
||||
it('returns the approval of the operator', async function () {
|
||||
expect(await this.token.isApprovedForAll(this.holder, this.operator)).to.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,119 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
async function fixture() {
|
||||
const [holder] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC1155Supply', ['https://token-cdn-domain/{id}.json']);
|
||||
return { token, holder };
|
||||
}
|
||||
|
||||
describe('ERC1155Supply', function () {
|
||||
const firstTokenId = 37n;
|
||||
const firstTokenValue = 42n;
|
||||
const secondTokenId = 19842n;
|
||||
const secondTokenValue = 23n;
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('before mint', function () {
|
||||
it('exist', async function () {
|
||||
expect(await this.token.exists(firstTokenId)).to.be.false;
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
|
||||
expect(await this.token.totalSupply()).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('after mint', function () {
|
||||
describe('single', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
|
||||
});
|
||||
|
||||
it('exist', async function () {
|
||||
expect(await this.token.exists(firstTokenId)).to.be.true;
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(firstTokenValue);
|
||||
expect(await this.token.totalSupply()).to.equal(firstTokenValue);
|
||||
});
|
||||
});
|
||||
|
||||
describe('batch', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mintBatch(
|
||||
this.holder,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
);
|
||||
});
|
||||
|
||||
it('exist', async function () {
|
||||
expect(await this.token.exists(firstTokenId)).to.be.true;
|
||||
expect(await this.token.exists(secondTokenId)).to.be.true;
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(firstTokenValue);
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(secondTokenId))).to.equal(secondTokenValue);
|
||||
expect(await this.token.totalSupply()).to.equal(firstTokenValue + secondTokenValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('after burn', function () {
|
||||
describe('single', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
|
||||
await this.token.$_burn(this.holder, firstTokenId, firstTokenValue);
|
||||
});
|
||||
|
||||
it('exist', async function () {
|
||||
expect(await this.token.exists(firstTokenId)).to.be.false;
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
|
||||
expect(await this.token.totalSupply()).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('batch', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mintBatch(
|
||||
this.holder,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
);
|
||||
await this.token.$_burnBatch(this.holder, [firstTokenId, secondTokenId], [firstTokenValue, secondTokenValue]);
|
||||
});
|
||||
|
||||
it('exist', async function () {
|
||||
expect(await this.token.exists(firstTokenId)).to.be.false;
|
||||
expect(await this.token.exists(secondTokenId)).to.be.false;
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(secondTokenId))).to.equal(0n);
|
||||
expect(await this.token.totalSupply()).to.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('other', function () {
|
||||
it('supply unaffected by no-op', async function () {
|
||||
await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, [firstTokenId], [firstTokenValue]);
|
||||
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
|
||||
expect(await this.token.totalSupply()).to.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const erc1155Uri = 'https://token.com/nfts/';
|
||||
const baseUri = 'https://token.com/';
|
||||
const tokenId = 1n;
|
||||
const value = 3000n;
|
||||
|
||||
describe('ERC1155URIStorage', function () {
|
||||
describe('with base uri set', function () {
|
||||
async function fixture() {
|
||||
const [holder] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC1155URIStorage', [erc1155Uri]);
|
||||
await token.$_setBaseURI(baseUri);
|
||||
await token.$_mint(holder, tokenId, value, '0x');
|
||||
|
||||
return { token, holder };
|
||||
}
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('can request the token uri, returning the erc1155 uri if no token uri was set', async function () {
|
||||
expect(await this.token.uri(tokenId)).to.equal(erc1155Uri);
|
||||
});
|
||||
|
||||
it('can request the token uri, returning the concatenated uri if a token uri was set', async function () {
|
||||
const tokenUri = '1234/';
|
||||
const expectedUri = `${baseUri}${tokenUri}`;
|
||||
|
||||
await expect(this.token.$_setURI(ethers.Typed.uint256(tokenId), tokenUri))
|
||||
.to.emit(this.token, 'URI')
|
||||
.withArgs(expectedUri, tokenId);
|
||||
|
||||
expect(await this.token.uri(tokenId)).to.equal(expectedUri);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with base uri set to the empty string', function () {
|
||||
async function fixture() {
|
||||
const [holder] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC1155URIStorage', ['']);
|
||||
await token.$_mint(holder, tokenId, value, '0x');
|
||||
|
||||
return { token, holder };
|
||||
}
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('can request the token uri, returning an empty string if no token uri was set', async function () {
|
||||
expect(await this.token.uri(tokenId)).to.equal('');
|
||||
});
|
||||
|
||||
it('can request the token uri, returning the token uri if a token uri was set', async function () {
|
||||
const tokenUri = 'ipfs://1234/';
|
||||
|
||||
await expect(this.token.$_setURI(ethers.Typed.uint256(tokenId), tokenUri))
|
||||
.to.emit(this.token, 'URI')
|
||||
.withArgs(tokenUri, tokenId);
|
||||
|
||||
expect(await this.token.uri(tokenId)).to.equal(tokenUri);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
const ids = [1n, 2n, 3n];
|
||||
const values = [1000n, 2000n, 3000n];
|
||||
const data = '0x12345678';
|
||||
|
||||
async function fixture() {
|
||||
const [owner] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC1155', ['https://token-cdn-domain/{id}.json']);
|
||||
const mock = await ethers.deployContract('$ERC1155Holder');
|
||||
|
||||
await token.$_mintBatch(owner, ids, values, '0x');
|
||||
|
||||
return { owner, token, mock };
|
||||
}
|
||||
|
||||
describe('ERC1155Holder', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC1155Receiver']);
|
||||
|
||||
it('receives ERC1155 tokens from a single ID', async function () {
|
||||
await this.token.connect(this.owner).safeTransferFrom(this.owner, this.mock, ids[0], values[0], data);
|
||||
|
||||
expect(await this.token.balanceOf(this.mock, ids[0])).to.equal(values[0]);
|
||||
|
||||
for (let i = 1; i < ids.length; i++) {
|
||||
expect(await this.token.balanceOf(this.mock, ids[i])).to.equal(0n);
|
||||
}
|
||||
});
|
||||
|
||||
it('receives ERC1155 tokens from a multiple IDs', async function () {
|
||||
expect(
|
||||
await this.token.balanceOfBatch(
|
||||
ids.map(() => this.mock),
|
||||
ids,
|
||||
),
|
||||
).to.deep.equal(ids.map(() => 0n));
|
||||
|
||||
await this.token.connect(this.owner).safeBatchTransferFrom(this.owner, this.mock, ids, values, data);
|
||||
|
||||
expect(
|
||||
await this.token.balanceOfBatch(
|
||||
ids.map(() => this.mock),
|
||||
ids,
|
||||
),
|
||||
).to.deep.equal(values);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,299 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { RevertType } = require('../../../helpers/enums');
|
||||
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||
|
||||
const firstTokenId = 1n;
|
||||
const secondTokenId = 2n;
|
||||
const firstTokenValue = 1000n;
|
||||
const secondTokenValue = 1000n;
|
||||
|
||||
const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
|
||||
const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
|
||||
|
||||
const deployReceiver = (
|
||||
revertType,
|
||||
returnValueSingle = RECEIVER_SINGLE_MAGIC_VALUE,
|
||||
returnValueBatched = RECEIVER_BATCH_MAGIC_VALUE,
|
||||
) => ethers.deployContract('$ERC1155ReceiverMock', [returnValueSingle, returnValueBatched, revertType]);
|
||||
|
||||
const fixture = async () => {
|
||||
const [eoa, operator, owner] = await ethers.getSigners();
|
||||
const utils = await ethers.deployContract('$ERC1155Utils');
|
||||
|
||||
const receivers = {
|
||||
correct: await deployReceiver(RevertType.None),
|
||||
invalid: await deployReceiver(RevertType.None, '0xdeadbeef', '0xdeadbeef'),
|
||||
message: await deployReceiver(RevertType.RevertWithMessage),
|
||||
empty: await deployReceiver(RevertType.RevertWithoutMessage),
|
||||
customError: await deployReceiver(RevertType.RevertWithCustomError),
|
||||
panic: await deployReceiver(RevertType.Panic),
|
||||
nonReceiver: await ethers.deployContract('CallReceiverMock'),
|
||||
eoa,
|
||||
};
|
||||
|
||||
return { operator, owner, utils, receivers };
|
||||
};
|
||||
|
||||
describe('ERC1155Utils', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('onERC1155Received', function () {
|
||||
it('succeeds when called by an EOA', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.eoa,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
).to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('succeeds when data is passed', async function () {
|
||||
const data = '0x12345678';
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.correct,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
data,
|
||||
),
|
||||
).to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('succeeds when data is empty', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.correct,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
).to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('reverts when receiver returns invalid value', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.invalid,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
|
||||
.withArgs(this.receivers.invalid);
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts with message', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.message,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
).to.be.revertedWith('ERC1155ReceiverMock: reverting on receive');
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts without message', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.empty,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
|
||||
.withArgs(this.receivers.empty);
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts with custom error', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.customError,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
|
||||
.withArgs(RECEIVER_SINGLE_MAGIC_VALUE);
|
||||
});
|
||||
|
||||
it('reverts when receiver panics', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.panic,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
|
||||
});
|
||||
|
||||
it('reverts when receiver does not implement onERC1155Received', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155Received(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.nonReceiver,
|
||||
firstTokenId,
|
||||
firstTokenValue,
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
|
||||
.withArgs(this.receivers.nonReceiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('onERC1155BatchReceived', function () {
|
||||
it('succeeds when called by an EOA', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.eoa,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
).to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('succeeds when data is passed', async function () {
|
||||
const data = '0x12345678';
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.correct,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
data,
|
||||
),
|
||||
).to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('succeeds when data is empty', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.correct,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
).to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('reverts when receiver returns invalid value', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.invalid,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
|
||||
.withArgs(this.receivers.invalid);
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts with message', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.message,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
).to.be.revertedWith('ERC1155ReceiverMock: reverting on batch receive');
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts without message', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.empty,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
|
||||
.withArgs(this.receivers.empty);
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts with custom error', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.customError,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
|
||||
.withArgs(RECEIVER_SINGLE_MAGIC_VALUE);
|
||||
});
|
||||
|
||||
it('reverts when receiver panics', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.panic,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
|
||||
});
|
||||
|
||||
it('reverts when receiver does not implement onERC1155BatchReceived', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC1155BatchReceived(
|
||||
this.operator,
|
||||
this.owner,
|
||||
this.receivers.nonReceiver,
|
||||
[firstTokenId, secondTokenId],
|
||||
[firstTokenValue, secondTokenValue],
|
||||
'0x',
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
|
||||
.withArgs(this.receivers.nonReceiver);
|
||||
});
|
||||
});
|
||||
});
|
||||
260
lib_openzeppelin_contracts/test/token/ERC20/ERC20.behavior.js
Normal file
260
lib_openzeppelin_contracts/test/token/ERC20/ERC20.behavior.js
Normal file
@@ -0,0 +1,260 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function shouldBehaveLikeERC20(initialSupply, opts = {}) {
|
||||
const { forcedApproval } = opts;
|
||||
|
||||
beforeEach(async function () {
|
||||
[this.holder, this.recipient, this.other] = this.accounts;
|
||||
});
|
||||
|
||||
it('total supply: returns the total token value', async function () {
|
||||
expect(await this.token.totalSupply()).to.equal(initialSupply);
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
it('returns zero when the requested account has no tokens', async function () {
|
||||
expect(await this.token.balanceOf(this.other)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('returns the total token value when the requested account has some tokens', async function () {
|
||||
expect(await this.token.balanceOf(this.holder)).to.equal(initialSupply);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfer', function () {
|
||||
beforeEach(function () {
|
||||
this.transfer = (from, to, value) => this.token.connect(from).transfer(to, value);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Transfer(initialSupply);
|
||||
});
|
||||
|
||||
describe('transfer from', function () {
|
||||
describe('when the token owner is not the zero address', function () {
|
||||
describe('when the recipient is not the zero address', function () {
|
||||
describe('when the spender has enough allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).approve(this.recipient, initialSupply);
|
||||
});
|
||||
|
||||
describe('when the token owner has enough balance', function () {
|
||||
const value = initialSupply;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.recipient).transferFrom(this.holder, this.other, value);
|
||||
});
|
||||
|
||||
it('transfers the requested value', async function () {
|
||||
await expect(this.tx).to.changeTokenBalances(this.token, [this.holder, this.other], [-value, value]);
|
||||
});
|
||||
|
||||
it('decreases the spender allowance', async function () {
|
||||
expect(await this.token.allowance(this.holder, this.recipient)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('emits a transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.other, value);
|
||||
});
|
||||
|
||||
if (forcedApproval) {
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(
|
||||
this.holder.address,
|
||||
this.recipient.address,
|
||||
await this.token.allowance(this.holder, this.recipient),
|
||||
);
|
||||
});
|
||||
} else {
|
||||
it('does not emit an approval event', async function () {
|
||||
await expect(this.tx).to.not.emit(this.token, 'Approval');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('reverts when the token owner does not have enough balance', async function () {
|
||||
const value = initialSupply;
|
||||
await this.token.connect(this.holder).transfer(this.other, 1n);
|
||||
await expect(this.token.connect(this.recipient).transferFrom(this.holder, this.other, value))
|
||||
.to.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.holder, value - 1n, value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender does not have enough allowance', function () {
|
||||
const allowance = initialSupply - 1n;
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).approve(this.recipient, allowance);
|
||||
});
|
||||
|
||||
it('reverts when the token owner has enough balance', async function () {
|
||||
const value = initialSupply;
|
||||
await expect(this.token.connect(this.recipient).transferFrom(this.holder, this.other, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientAllowance')
|
||||
.withArgs(this.recipient, allowance, value);
|
||||
});
|
||||
|
||||
it('reverts when the token owner does not have enough balance', async function () {
|
||||
const value = allowance;
|
||||
await this.token.connect(this.holder).transfer(this.other, 2);
|
||||
await expect(this.token.connect(this.recipient).transferFrom(this.holder, this.other, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.holder, value - 1n, value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender has unlimited allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).approve(this.recipient, ethers.MaxUint256);
|
||||
this.tx = await this.token.connect(this.recipient).transferFrom(this.holder, this.other, 1n);
|
||||
});
|
||||
|
||||
it('does not decrease the spender allowance', async function () {
|
||||
expect(await this.token.allowance(this.holder, this.recipient)).to.equal(ethers.MaxUint256);
|
||||
});
|
||||
|
||||
it('does not emit an approval event', async function () {
|
||||
await expect(this.tx).to.not.emit(this.token, 'Approval');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts when the recipient is the zero address', async function () {
|
||||
const value = initialSupply;
|
||||
await this.token.connect(this.holder).approve(this.recipient, value);
|
||||
await expect(this.token.connect(this.recipient).transferFrom(this.holder, ethers.ZeroAddress, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts when the token owner is the zero address', async function () {
|
||||
const value = 0n;
|
||||
await expect(this.token.connect(this.recipient).transferFrom(ethers.ZeroAddress, this.recipient, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('approve', function () {
|
||||
beforeEach(function () {
|
||||
this.approve = (owner, spender, value) => this.token.connect(owner).approve(spender, value);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Approve(initialSupply);
|
||||
});
|
||||
}
|
||||
|
||||
function shouldBehaveLikeERC20Transfer(balance) {
|
||||
describe('when the recipient is not the zero address', function () {
|
||||
it('reverts when the sender does not have enough balance', async function () {
|
||||
const value = balance + 1n;
|
||||
await expect(this.transfer(this.holder, this.recipient, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.holder, balance, value);
|
||||
});
|
||||
|
||||
describe('when the sender transfers all balance', function () {
|
||||
const value = balance;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.transfer(this.holder, this.recipient, value);
|
||||
});
|
||||
|
||||
it('transfers the requested value', async function () {
|
||||
await expect(this.tx).to.changeTokenBalances(this.token, [this.holder, this.recipient], [-value, value]);
|
||||
});
|
||||
|
||||
it('emits a transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.recipient, value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender transfers zero tokens', function () {
|
||||
const value = 0n;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.transfer(this.holder, this.recipient, value);
|
||||
});
|
||||
|
||||
it('transfers the requested value', async function () {
|
||||
await expect(this.tx).to.changeTokenBalances(this.token, [this.holder, this.recipient], [0n, 0n]);
|
||||
});
|
||||
|
||||
it('emits a transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.recipient, value);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts when the recipient is the zero address', async function () {
|
||||
await expect(this.transfer(this.holder, ethers.ZeroAddress, balance))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
}
|
||||
|
||||
function shouldBehaveLikeERC20Approve(supply) {
|
||||
describe('when the spender is not the zero address', function () {
|
||||
describe('when the sender has enough balance', function () {
|
||||
const value = supply;
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.approve(this.holder, this.recipient, value))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.holder, this.recipient, value);
|
||||
});
|
||||
|
||||
it('approves the requested value when there was no approved value before', async function () {
|
||||
await this.approve(this.holder, this.recipient, value);
|
||||
|
||||
expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
|
||||
});
|
||||
|
||||
it('approves the requested value and replaces the previous one when the spender had an approved value', async function () {
|
||||
await this.approve(this.holder, this.recipient, 1n);
|
||||
await this.approve(this.holder, this.recipient, value);
|
||||
|
||||
expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender does not have enough balance', function () {
|
||||
const value = supply + 1n;
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.approve(this.holder, this.recipient, value))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.holder, this.recipient, value);
|
||||
});
|
||||
|
||||
it('approves the requested value when there was no approved value before', async function () {
|
||||
await this.approve(this.holder, this.recipient, value);
|
||||
|
||||
expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
|
||||
});
|
||||
|
||||
it('approves the requested value and replaces the previous one when the spender had an approved value', async function () {
|
||||
await this.approve(this.holder, this.recipient, 1n);
|
||||
await this.approve(this.holder, this.recipient, value);
|
||||
|
||||
expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts when the spender is the zero address', async function () {
|
||||
await expect(this.approve(this.holder, ethers.ZeroAddress, supply))
|
||||
.to.be.revertedWithCustomError(this.token, `ERC20InvalidSpender`)
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeERC20,
|
||||
shouldBehaveLikeERC20Transfer,
|
||||
shouldBehaveLikeERC20Approve,
|
||||
};
|
||||
199
lib_openzeppelin_contracts/test/token/ERC20/ERC20.test.js
Normal file
199
lib_openzeppelin_contracts/test/token/ERC20/ERC20.test.js
Normal file
@@ -0,0 +1,199 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||
|
||||
const {
|
||||
shouldBehaveLikeERC20,
|
||||
shouldBehaveLikeERC20Transfer,
|
||||
shouldBehaveLikeERC20Approve,
|
||||
} = require('./ERC20.behavior');
|
||||
|
||||
const TOKENS = [{ Token: '$ERC20' }, { Token: '$ERC20ApprovalMock', forcedApproval: true }];
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const initialSupply = 100n;
|
||||
|
||||
describe('ERC20', function () {
|
||||
for (const { Token, forcedApproval } of TOKENS) {
|
||||
describe(Token, function () {
|
||||
const fixture = async () => {
|
||||
// this.accounts is used by shouldBehaveLikeERC20
|
||||
const accounts = await ethers.getSigners();
|
||||
const [holder, recipient] = accounts;
|
||||
|
||||
const token = await ethers.deployContract(Token, [name, symbol]);
|
||||
await token.$_mint(holder, initialSupply);
|
||||
|
||||
return { accounts, holder, recipient, token };
|
||||
};
|
||||
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20(initialSupply, { forcedApproval });
|
||||
|
||||
it('has a name', async function () {
|
||||
expect(await this.token.name()).to.equal(name);
|
||||
});
|
||||
|
||||
it('has a symbol', async function () {
|
||||
expect(await this.token.symbol()).to.equal(symbol);
|
||||
});
|
||||
|
||||
it('has 18 decimals', async function () {
|
||||
expect(await this.token.decimals()).to.equal(18n);
|
||||
});
|
||||
|
||||
describe('_mint', function () {
|
||||
const value = 50n;
|
||||
it('rejects a null account', async function () {
|
||||
await expect(this.token.$_mint(ethers.ZeroAddress, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('rejects overflow', async function () {
|
||||
await expect(this.token.$_mint(this.recipient, ethers.MaxUint256)).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
|
||||
);
|
||||
});
|
||||
|
||||
describe('for a non zero account', function () {
|
||||
beforeEach('minting', async function () {
|
||||
this.tx = await this.token.$_mint(this.recipient, value);
|
||||
});
|
||||
|
||||
it('increments totalSupply', async function () {
|
||||
await expect(await this.token.totalSupply()).to.equal(initialSupply + value);
|
||||
});
|
||||
|
||||
it('increments recipient balance', async function () {
|
||||
await expect(this.tx).to.changeTokenBalance(this.token, this.recipient, value);
|
||||
});
|
||||
|
||||
it('emits Transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient, value);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it('rejects a null account', async function () {
|
||||
await expect(this.token.$_burn(ethers.ZeroAddress, 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('for a non zero account', function () {
|
||||
it('rejects burning more than balance', async function () {
|
||||
await expect(this.token.$_burn(this.holder, initialSupply + 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.holder, initialSupply, initialSupply + 1n);
|
||||
});
|
||||
|
||||
const describeBurn = function (description, value) {
|
||||
describe(description, function () {
|
||||
beforeEach('burning', async function () {
|
||||
this.tx = await this.token.$_burn(this.holder, value);
|
||||
});
|
||||
|
||||
it('decrements totalSupply', async function () {
|
||||
expect(await this.token.totalSupply()).to.equal(initialSupply - value);
|
||||
});
|
||||
|
||||
it('decrements holder balance', async function () {
|
||||
await expect(this.tx).to.changeTokenBalance(this.token, this.holder, -value);
|
||||
});
|
||||
|
||||
it('emits Transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, ethers.ZeroAddress, value);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
describeBurn('for entire balance', initialSupply);
|
||||
describeBurn('for less value than balance', initialSupply - 1n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_update', function () {
|
||||
const value = 1n;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.totalSupply = await this.token.totalSupply();
|
||||
});
|
||||
|
||||
it('from is the zero address', async function () {
|
||||
const tx = await this.token.$_update(ethers.ZeroAddress, this.holder, value);
|
||||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.holder, value);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(this.totalSupply + value);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, value);
|
||||
});
|
||||
|
||||
it('to is the zero address', async function () {
|
||||
const tx = await this.token.$_update(this.holder, ethers.ZeroAddress, value);
|
||||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.holder, ethers.ZeroAddress, value);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(this.totalSupply - value);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, -value);
|
||||
});
|
||||
|
||||
describe('from and to are the same address', function () {
|
||||
it('zero address', async function () {
|
||||
const tx = await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, value);
|
||||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, ethers.ZeroAddress, value);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(this.totalSupply);
|
||||
await expect(tx).to.changeTokenBalance(this.token, ethers.ZeroAddress, 0n);
|
||||
});
|
||||
|
||||
describe('non zero address', function () {
|
||||
it('reverts without balance', async function () {
|
||||
await expect(this.token.$_update(this.recipient, this.recipient, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.recipient, 0n, value);
|
||||
});
|
||||
|
||||
it('executes with balance', async function () {
|
||||
const tx = await this.token.$_update(this.holder, this.holder, value);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, 0n);
|
||||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.holder, value);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_transfer', function () {
|
||||
beforeEach(function () {
|
||||
this.transfer = this.token.$_transfer;
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Transfer(initialSupply);
|
||||
|
||||
it('reverts when the sender is the zero address', async function () {
|
||||
await expect(this.token.$_transfer(ethers.ZeroAddress, this.recipient, initialSupply))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_approve', function () {
|
||||
beforeEach(function () {
|
||||
this.approve = this.token.$_approve;
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Approve(initialSupply);
|
||||
|
||||
it('reverts when the owner is the zero address', async function () {
|
||||
await expect(this.token.$_approve(ethers.ZeroAddress, this.recipient, initialSupply))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,370 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const {
|
||||
shouldBehaveLikeERC20,
|
||||
shouldBehaveLikeERC20Transfer,
|
||||
shouldBehaveLikeERC20Approve,
|
||||
} = require('../ERC20.behavior.js');
|
||||
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
|
||||
const { RevertType } = require('../../../helpers/enums.js');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const value = 1000n;
|
||||
const data = '0x123456';
|
||||
|
||||
async function fixture() {
|
||||
// this.accounts is used by shouldBehaveLikeERC20
|
||||
const accounts = await ethers.getSigners();
|
||||
const [holder, other] = accounts;
|
||||
|
||||
const receiver = await ethers.deployContract('ERC1363ReceiverMock');
|
||||
const spender = await ethers.deployContract('ERC1363SpenderMock');
|
||||
const token = await ethers.deployContract('$ERC1363', [name, symbol]);
|
||||
|
||||
await token.$_mint(holder, value);
|
||||
|
||||
return {
|
||||
accounts,
|
||||
holder,
|
||||
other,
|
||||
token,
|
||||
receiver,
|
||||
spender,
|
||||
selectors: {
|
||||
onTransferReceived: receiver.interface.getFunction('onTransferReceived(address,address,uint256,bytes)').selector,
|
||||
onApprovalReceived: spender.interface.getFunction('onApprovalReceived(address,uint256,bytes)').selector,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('ERC1363', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC165', 'ERC1363']);
|
||||
shouldBehaveLikeERC20(value);
|
||||
|
||||
describe('transferAndCall', function () {
|
||||
describe('as a transfer', function () {
|
||||
beforeEach(async function () {
|
||||
this.recipient = this.receiver;
|
||||
this.transfer = (holder, ...rest) =>
|
||||
this.token.connect(holder).getFunction('transferAndCall(address,uint256)')(...rest);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Transfer(value);
|
||||
});
|
||||
|
||||
it('reverts transferring to an EOA', async function () {
|
||||
await expect(this.token.connect(this.holder).getFunction('transferAndCall(address,uint256)')(this.other, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.other.address);
|
||||
});
|
||||
|
||||
it('succeeds without data', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256)')(this.receiver, value),
|
||||
)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder.address, this.receiver.target, value)
|
||||
.to.emit(this.receiver, 'Received')
|
||||
.withArgs(this.holder.address, this.holder.address, value, '0x');
|
||||
});
|
||||
|
||||
it('succeeds with data', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256,bytes)')(
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder.address, this.receiver.target, value)
|
||||
.to.emit(this.receiver, 'Received')
|
||||
.withArgs(this.holder.address, this.holder.address, value, data);
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (without reason)', async function () {
|
||||
await this.receiver.setUp(this.selectors.onTransferReceived, RevertType.RevertWithoutMessage);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256,bytes)')(
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.receiver.target);
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (with reason)', async function () {
|
||||
await this.receiver.setUp(this.selectors.onTransferReceived, RevertType.RevertWithMessage);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256,bytes)')(
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
).to.be.revertedWith('ERC1363ReceiverMock: reverting');
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (with custom error)', async function () {
|
||||
const reason = '0x12345678';
|
||||
await this.receiver.setUp(reason, RevertType.RevertWithCustomError);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256,bytes)')(
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.receiver, 'CustomError')
|
||||
.withArgs(reason);
|
||||
});
|
||||
|
||||
it('panics with reverting hook (with panic)', async function () {
|
||||
await this.receiver.setUp(this.selectors.onTransferReceived, RevertType.Panic);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256,bytes)')(
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
).to.be.revertedWithPanic();
|
||||
});
|
||||
|
||||
it('reverts with bad return value', async function () {
|
||||
await this.receiver.setUp('0x12345678', RevertType.None);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('transferAndCall(address,uint256,bytes)')(
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.receiver.target);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transferFromAndCall', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).approve(this.other, ethers.MaxUint256);
|
||||
});
|
||||
|
||||
describe('as a transfer', function () {
|
||||
beforeEach(async function () {
|
||||
this.recipient = this.receiver;
|
||||
this.transfer = this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256)');
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Transfer(value);
|
||||
});
|
||||
|
||||
it('reverts transferring to an EOA', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256)')(
|
||||
this.holder,
|
||||
this.other,
|
||||
value,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.other.address);
|
||||
});
|
||||
|
||||
it('succeeds without data', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
),
|
||||
)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder.address, this.receiver.target, value)
|
||||
.to.emit(this.receiver, 'Received')
|
||||
.withArgs(this.other.address, this.holder.address, value, '0x');
|
||||
});
|
||||
|
||||
it('succeeds with data', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256,bytes)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder.address, this.receiver.target, value)
|
||||
.to.emit(this.receiver, 'Received')
|
||||
.withArgs(this.other.address, this.holder.address, value, data);
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (without reason)', async function () {
|
||||
await this.receiver.setUp(this.selectors.onTransferReceived, RevertType.RevertWithoutMessage);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256,bytes)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.receiver.target);
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (with reason)', async function () {
|
||||
await this.receiver.setUp(this.selectors.onTransferReceived, RevertType.RevertWithMessage);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256,bytes)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
).to.be.revertedWith('ERC1363ReceiverMock: reverting');
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (with custom error)', async function () {
|
||||
const reason = '0x12345678';
|
||||
await this.receiver.setUp(reason, RevertType.RevertWithCustomError);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256,bytes)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.receiver, 'CustomError')
|
||||
.withArgs(reason);
|
||||
});
|
||||
|
||||
it('panics with reverting hook (with panic)', async function () {
|
||||
await this.receiver.setUp(this.selectors.onTransferReceived, RevertType.Panic);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256,bytes)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
).to.be.revertedWithPanic();
|
||||
});
|
||||
|
||||
it('reverts with bad return value', async function () {
|
||||
await this.receiver.setUp('0x12345678', RevertType.None);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.other).getFunction('transferFromAndCall(address,address,uint256,bytes)')(
|
||||
this.holder,
|
||||
this.receiver,
|
||||
value,
|
||||
data,
|
||||
),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.receiver.target);
|
||||
});
|
||||
});
|
||||
|
||||
describe('approveAndCall', function () {
|
||||
describe('as an approval', function () {
|
||||
beforeEach(async function () {
|
||||
this.recipient = this.spender;
|
||||
this.approve = (holder, ...rest) =>
|
||||
this.token.connect(holder).getFunction('approveAndCall(address,uint256)')(...rest);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20Approve(value);
|
||||
});
|
||||
|
||||
it('reverts approving an EOA', async function () {
|
||||
await expect(this.token.connect(this.holder).getFunction('approveAndCall(address,uint256)')(this.other, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidSpender')
|
||||
.withArgs(this.other.address);
|
||||
});
|
||||
|
||||
it('succeeds without data', async function () {
|
||||
await expect(this.token.connect(this.holder).getFunction('approveAndCall(address,uint256)')(this.spender, value))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.holder.address, this.spender.target, value)
|
||||
.to.emit(this.spender, 'Approved')
|
||||
.withArgs(this.holder.address, value, '0x');
|
||||
});
|
||||
|
||||
it('succeeds with data', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('approveAndCall(address,uint256,bytes)')(this.spender, value, data),
|
||||
)
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.holder.address, this.spender.target, value)
|
||||
.to.emit(this.spender, 'Approved')
|
||||
.withArgs(this.holder.address, value, data);
|
||||
});
|
||||
|
||||
it('with reverting hook (without reason)', async function () {
|
||||
await this.spender.setUp(this.selectors.onApprovalReceived, RevertType.RevertWithoutMessage);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('approveAndCall(address,uint256,bytes)')(this.spender, value, data),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidSpender')
|
||||
.withArgs(this.spender.target);
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (with reason)', async function () {
|
||||
await this.spender.setUp(this.selectors.onApprovalReceived, RevertType.RevertWithMessage);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('approveAndCall(address,uint256,bytes)')(this.spender, value, data),
|
||||
).to.be.revertedWith('ERC1363SpenderMock: reverting');
|
||||
});
|
||||
|
||||
it('reverts with reverting hook (with custom error)', async function () {
|
||||
const reason = '0x12345678';
|
||||
await this.spender.setUp(reason, RevertType.RevertWithCustomError);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('approveAndCall(address,uint256,bytes)')(this.spender, value, data),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.spender, 'CustomError')
|
||||
.withArgs(reason);
|
||||
});
|
||||
|
||||
it('panics with reverting hook (with panic)', async function () {
|
||||
await this.spender.setUp(this.selectors.onApprovalReceived, RevertType.Panic);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('approveAndCall(address,uint256,bytes)')(this.spender, value, data),
|
||||
).to.be.revertedWithPanic();
|
||||
});
|
||||
|
||||
it('reverts with bad return value', async function () {
|
||||
await this.spender.setUp('0x12345678', RevertType.None);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).getFunction('approveAndCall(address,uint256,bytes)')(this.spender, value, data),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidSpender')
|
||||
.withArgs(this.spender.target);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,105 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const initialBalance = 1000n;
|
||||
|
||||
async function fixture() {
|
||||
const [owner, burner] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC20Burnable', [name, symbol], owner);
|
||||
await token.$_mint(owner, initialBalance);
|
||||
|
||||
return { owner, burner, token, initialBalance };
|
||||
}
|
||||
|
||||
describe('ERC20Burnable', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('burn', function () {
|
||||
it('reverts if not enough balance', async function () {
|
||||
const value = this.initialBalance + 1n;
|
||||
|
||||
await expect(this.token.connect(this.owner).burn(value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.owner, this.initialBalance, value);
|
||||
});
|
||||
|
||||
describe('on success', function () {
|
||||
for (const { title, value } of [
|
||||
{ title: 'for a zero value', value: 0n },
|
||||
{ title: 'for a non-zero value', value: 100n },
|
||||
]) {
|
||||
describe(title, function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.owner).burn(value);
|
||||
});
|
||||
|
||||
it('burns the requested value', async function () {
|
||||
await expect(this.tx).to.changeTokenBalance(this.token, this.owner, -value);
|
||||
});
|
||||
|
||||
it('emits a transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.owner, ethers.ZeroAddress, value);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('burnFrom', function () {
|
||||
describe('reverts', function () {
|
||||
it('if not enough balance', async function () {
|
||||
const value = this.initialBalance + 1n;
|
||||
|
||||
await this.token.connect(this.owner).approve(this.burner, value);
|
||||
|
||||
await expect(this.token.connect(this.burner).burnFrom(this.owner, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.owner, this.initialBalance, value);
|
||||
});
|
||||
|
||||
it('if not enough allowance', async function () {
|
||||
const allowance = 100n;
|
||||
|
||||
await this.token.connect(this.owner).approve(this.burner, allowance);
|
||||
|
||||
await expect(this.token.connect(this.burner).burnFrom(this.owner, allowance + 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientAllowance')
|
||||
.withArgs(this.burner, allowance, allowance + 1n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('on success', function () {
|
||||
for (const { title, value } of [
|
||||
{ title: 'for a zero value', value: 0n },
|
||||
{ title: 'for a non-zero value', value: 100n },
|
||||
]) {
|
||||
describe(title, function () {
|
||||
const originalAllowance = value * 3n;
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.burner, originalAllowance);
|
||||
this.tx = await this.token.connect(this.burner).burnFrom(this.owner, value);
|
||||
});
|
||||
|
||||
it('burns the requested value', async function () {
|
||||
await expect(this.tx).to.changeTokenBalance(this.token, this.owner, -value);
|
||||
});
|
||||
|
||||
it('decrements allowance', async function () {
|
||||
expect(await this.token.allowance(this.owner, this.burner)).to.equal(originalAllowance - value);
|
||||
});
|
||||
|
||||
it('emits a transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.owner, ethers.ZeroAddress, value);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const cap = 1000n;
|
||||
|
||||
async function fixture() {
|
||||
const [user] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC20Capped', [name, symbol, cap]);
|
||||
|
||||
return { user, token, cap };
|
||||
}
|
||||
|
||||
describe('ERC20Capped', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('requires a non-zero cap', async function () {
|
||||
const ERC20Capped = await ethers.getContractFactory('$ERC20Capped');
|
||||
|
||||
await expect(ERC20Capped.deploy(name, symbol, 0))
|
||||
.to.be.revertedWithCustomError(ERC20Capped, 'ERC20InvalidCap')
|
||||
.withArgs(0);
|
||||
});
|
||||
|
||||
describe('capped token', function () {
|
||||
it('starts with the correct cap', async function () {
|
||||
expect(await this.token.cap()).to.equal(this.cap);
|
||||
});
|
||||
|
||||
it('mints when value is less than cap', async function () {
|
||||
const value = this.cap - 1n;
|
||||
await this.token.$_mint(this.user, value);
|
||||
expect(await this.token.totalSupply()).to.equal(value);
|
||||
});
|
||||
|
||||
it('fails to mint if the value exceeds the cap', async function () {
|
||||
await this.token.$_mint(this.user, this.cap - 1n);
|
||||
await expect(this.token.$_mint(this.user, 2))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20ExceededCap')
|
||||
.withArgs(this.cap + 1n, this.cap);
|
||||
});
|
||||
|
||||
it('fails to mint after cap is reached', async function () {
|
||||
await this.token.$_mint(this.user, this.cap);
|
||||
await expect(this.token.$_mint(this.user, 1))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20ExceededCap')
|
||||
.withArgs(this.cap + 1n, this.cap);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const initialSupply = 100n;
|
||||
const loanValue = 10_000_000_000_000n;
|
||||
|
||||
async function fixture() {
|
||||
const [holder, other] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC20FlashMintMock', [name, symbol]);
|
||||
await token.$_mint(holder, initialSupply);
|
||||
|
||||
return { holder, other, token };
|
||||
}
|
||||
|
||||
describe('ERC20FlashMint', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('maxFlashLoan', function () {
|
||||
it('token match', async function () {
|
||||
expect(await this.token.maxFlashLoan(this.token)).to.equal(ethers.MaxUint256 - initialSupply);
|
||||
});
|
||||
|
||||
it('token mismatch', async function () {
|
||||
expect(await this.token.maxFlashLoan(ethers.ZeroAddress)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flashFee', function () {
|
||||
it('token match', async function () {
|
||||
expect(await this.token.flashFee(this.token, loanValue)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('token mismatch', async function () {
|
||||
await expect(this.token.flashFee(ethers.ZeroAddress, loanValue))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC3156UnsupportedToken')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flashFeeReceiver', function () {
|
||||
it('default receiver', async function () {
|
||||
expect(await this.token.$_flashFeeReceiver()).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flashLoan', function () {
|
||||
it('success', async function () {
|
||||
const receiver = await ethers.deployContract('ERC3156FlashBorrowerMock', [true, true]);
|
||||
|
||||
const tx = await this.token.flashLoan(receiver, this.token, loanValue, '0x');
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, receiver, loanValue)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(receiver, ethers.ZeroAddress, loanValue)
|
||||
.to.emit(receiver, 'BalanceOf')
|
||||
.withArgs(this.token, receiver, loanValue)
|
||||
.to.emit(receiver, 'TotalSupply')
|
||||
.withArgs(this.token, initialSupply + loanValue);
|
||||
await expect(tx).to.changeTokenBalance(this.token, receiver, 0);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(initialSupply);
|
||||
expect(await this.token.allowance(receiver, this.token)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('missing return value', async function () {
|
||||
const receiver = await ethers.deployContract('ERC3156FlashBorrowerMock', [false, true]);
|
||||
await expect(this.token.flashLoan(receiver, this.token, loanValue, '0x'))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC3156InvalidReceiver')
|
||||
.withArgs(receiver);
|
||||
});
|
||||
|
||||
it('missing approval', async function () {
|
||||
const receiver = await ethers.deployContract('ERC3156FlashBorrowerMock', [true, false]);
|
||||
await expect(this.token.flashLoan(receiver, this.token, loanValue, '0x'))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientAllowance')
|
||||
.withArgs(this.token, 0, loanValue);
|
||||
});
|
||||
|
||||
it('unavailable funds', async function () {
|
||||
const receiver = await ethers.deployContract('ERC3156FlashBorrowerMock', [true, true]);
|
||||
const data = this.token.interface.encodeFunctionData('transfer', [this.other.address, 10]);
|
||||
await expect(this.token.flashLoan(receiver, this.token, loanValue, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(receiver, loanValue - 10n, loanValue);
|
||||
});
|
||||
|
||||
it('more than maxFlashLoan', async function () {
|
||||
const receiver = await ethers.deployContract('ERC3156FlashBorrowerMock', [true, true]);
|
||||
const data = this.token.interface.encodeFunctionData('transfer', [this.other.address, 10]);
|
||||
await expect(this.token.flashLoan(receiver, this.token, ethers.MaxUint256, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC3156ExceededMaxLoan')
|
||||
.withArgs(ethers.MaxUint256 - initialSupply);
|
||||
});
|
||||
|
||||
describe('custom flash fee & custom fee receiver', function () {
|
||||
const receiverInitialBalance = 200_000n;
|
||||
const flashFee = 5_000n;
|
||||
|
||||
beforeEach('init receiver balance & set flash fee', async function () {
|
||||
this.receiver = await ethers.deployContract('ERC3156FlashBorrowerMock', [true, true]);
|
||||
|
||||
const tx = await this.token.$_mint(this.receiver, receiverInitialBalance);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.receiver, receiverInitialBalance);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.receiver, receiverInitialBalance);
|
||||
|
||||
await this.token.setFlashFee(flashFee);
|
||||
expect(await this.token.flashFee(this.token, loanValue)).to.equal(flashFee);
|
||||
});
|
||||
|
||||
it('default flash fee receiver', async function () {
|
||||
const tx = await this.token.flashLoan(this.receiver, this.token, loanValue, '0x');
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.receiver, loanValue)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.receiver, ethers.ZeroAddress, loanValue + flashFee)
|
||||
.to.emit(this.receiver, 'BalanceOf')
|
||||
.withArgs(this.token, this.receiver, receiverInitialBalance + loanValue)
|
||||
.to.emit(this.receiver, 'TotalSupply')
|
||||
.withArgs(this.token, initialSupply + receiverInitialBalance + loanValue);
|
||||
await expect(tx).to.changeTokenBalances(this.token, [this.receiver, ethers.ZeroAddress], [-flashFee, 0]);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(initialSupply + receiverInitialBalance - flashFee);
|
||||
expect(await this.token.allowance(this.receiver, this.token)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('custom flash fee receiver', async function () {
|
||||
const flashFeeReceiverAddress = this.other;
|
||||
await this.token.setFlashFeeReceiver(flashFeeReceiverAddress);
|
||||
expect(await this.token.$_flashFeeReceiver()).to.equal(flashFeeReceiverAddress);
|
||||
|
||||
const tx = await this.token.flashLoan(this.receiver, this.token, loanValue, '0x');
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.receiver, loanValue)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.receiver, ethers.ZeroAddress, loanValue)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.receiver, flashFeeReceiverAddress, flashFee)
|
||||
.to.emit(this.receiver, 'BalanceOf')
|
||||
.withArgs(this.token, this.receiver, receiverInitialBalance + loanValue)
|
||||
.to.emit(this.receiver, 'TotalSupply')
|
||||
.withArgs(this.token, initialSupply + receiverInitialBalance + loanValue);
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.receiver, flashFeeReceiverAddress],
|
||||
[-flashFee, flashFee],
|
||||
);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(initialSupply + receiverInitialBalance);
|
||||
expect(await this.token.allowance(this.receiver, flashFeeReceiverAddress)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,129 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const initialSupply = 100n;
|
||||
|
||||
async function fixture() {
|
||||
const [holder, recipient, approved] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC20Pausable', [name, symbol]);
|
||||
await token.$_mint(holder, initialSupply);
|
||||
|
||||
return { holder, recipient, approved, token };
|
||||
}
|
||||
|
||||
describe('ERC20Pausable', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('pausable token', function () {
|
||||
describe('transfer', function () {
|
||||
it('allows to transfer when unpaused', async function () {
|
||||
await expect(this.token.connect(this.holder).transfer(this.recipient, initialSupply)).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.recipient],
|
||||
[-initialSupply, initialSupply],
|
||||
);
|
||||
});
|
||||
|
||||
it('allows to transfer when paused and then unpaused', async function () {
|
||||
await this.token.$_pause();
|
||||
await this.token.$_unpause();
|
||||
|
||||
await expect(this.token.connect(this.holder).transfer(this.recipient, initialSupply)).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.recipient],
|
||||
[-initialSupply, initialSupply],
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to transfer when paused', async function () {
|
||||
await this.token.$_pause();
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.holder).transfer(this.recipient, initialSupply),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfer from', function () {
|
||||
const allowance = 40n;
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).approve(this.approved, allowance);
|
||||
});
|
||||
|
||||
it('allows to transfer from when unpaused', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.approved).transferFrom(this.holder, this.recipient, allowance),
|
||||
).to.changeTokenBalances(this.token, [this.holder, this.recipient], [-allowance, allowance]);
|
||||
});
|
||||
|
||||
it('allows to transfer when paused and then unpaused', async function () {
|
||||
await this.token.$_pause();
|
||||
await this.token.$_unpause();
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.approved).transferFrom(this.holder, this.recipient, allowance),
|
||||
).to.changeTokenBalances(this.token, [this.holder, this.recipient], [-allowance, allowance]);
|
||||
});
|
||||
|
||||
it('reverts when trying to transfer from when paused', async function () {
|
||||
await this.token.$_pause();
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.approved).transferFrom(this.holder, this.recipient, allowance),
|
||||
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
});
|
||||
|
||||
describe('mint', function () {
|
||||
const value = 42n;
|
||||
|
||||
it('allows to mint when unpaused', async function () {
|
||||
await expect(this.token.$_mint(this.recipient, value)).to.changeTokenBalance(this.token, this.recipient, value);
|
||||
});
|
||||
|
||||
it('allows to mint when paused and then unpaused', async function () {
|
||||
await this.token.$_pause();
|
||||
await this.token.$_unpause();
|
||||
|
||||
await expect(this.token.$_mint(this.recipient, value)).to.changeTokenBalance(this.token, this.recipient, value);
|
||||
});
|
||||
|
||||
it('reverts when trying to mint when paused', async function () {
|
||||
await this.token.$_pause();
|
||||
|
||||
await expect(this.token.$_mint(this.recipient, value)).to.be.revertedWithCustomError(
|
||||
this.token,
|
||||
'EnforcedPause',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('burn', function () {
|
||||
const value = 42n;
|
||||
|
||||
it('allows to burn when unpaused', async function () {
|
||||
await expect(this.token.$_burn(this.holder, value)).to.changeTokenBalance(this.token, this.holder, -value);
|
||||
});
|
||||
|
||||
it('allows to burn when paused and then unpaused', async function () {
|
||||
await this.token.$_pause();
|
||||
await this.token.$_unpause();
|
||||
|
||||
await expect(this.token.$_burn(this.holder, value)).to.changeTokenBalance(this.token, this.holder, -value);
|
||||
});
|
||||
|
||||
it('reverts when trying to burn when paused', async function () {
|
||||
await this.token.$_pause();
|
||||
|
||||
await expect(this.token.$_burn(this.holder, value)).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,109 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { getDomain, domainSeparator, Permit } = require('../../../helpers/eip712');
|
||||
const time = require('../../../helpers/time');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const initialSupply = 100n;
|
||||
|
||||
async function fixture() {
|
||||
const [holder, spender, owner, other] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC20Permit', [name, symbol, name]);
|
||||
await token.$_mint(holder, initialSupply);
|
||||
|
||||
return {
|
||||
holder,
|
||||
spender,
|
||||
owner,
|
||||
other,
|
||||
token,
|
||||
};
|
||||
}
|
||||
|
||||
describe('ERC20Permit', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('initial nonce is 0', async function () {
|
||||
expect(await this.token.nonces(this.holder)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('domain separator', async function () {
|
||||
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(await getDomain(this.token).then(domainSeparator));
|
||||
});
|
||||
|
||||
describe('permit', function () {
|
||||
const value = 42n;
|
||||
const nonce = 0n;
|
||||
const maxDeadline = ethers.MaxUint256;
|
||||
|
||||
beforeEach(function () {
|
||||
this.buildData = (contract, deadline = maxDeadline) =>
|
||||
getDomain(contract).then(domain => ({
|
||||
domain,
|
||||
types: { Permit },
|
||||
message: {
|
||||
owner: this.owner.address,
|
||||
spender: this.spender.address,
|
||||
value,
|
||||
nonce,
|
||||
deadline,
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
it('accepts owner signature', async function () {
|
||||
const { v, r, s } = await this.buildData(this.token)
|
||||
.then(({ domain, types, message }) => this.owner.signTypedData(domain, types, message))
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
await this.token.permit(this.owner, this.spender, value, maxDeadline, v, r, s);
|
||||
|
||||
expect(await this.token.nonces(this.owner)).to.equal(1n);
|
||||
expect(await this.token.allowance(this.owner, this.spender)).to.equal(value);
|
||||
});
|
||||
|
||||
it('rejects reused signature', async function () {
|
||||
const { v, r, s, serialized } = await this.buildData(this.token)
|
||||
.then(({ domain, types, message }) => this.owner.signTypedData(domain, types, message))
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
await this.token.permit(this.owner, this.spender, value, maxDeadline, v, r, s);
|
||||
|
||||
const recovered = await this.buildData(this.token).then(({ domain, types, message }) =>
|
||||
ethers.verifyTypedData(domain, types, { ...message, nonce: nonce + 1n, deadline: maxDeadline }, serialized),
|
||||
);
|
||||
|
||||
await expect(this.token.permit(this.owner, this.spender, value, maxDeadline, v, r, s))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2612InvalidSigner')
|
||||
.withArgs(recovered, this.owner);
|
||||
});
|
||||
|
||||
it('rejects other signature', async function () {
|
||||
const { v, r, s } = await this.buildData(this.token)
|
||||
.then(({ domain, types, message }) => this.other.signTypedData(domain, types, message))
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
await expect(this.token.permit(this.owner, this.spender, value, maxDeadline, v, r, s))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2612InvalidSigner')
|
||||
.withArgs(this.other, this.owner);
|
||||
});
|
||||
|
||||
it('rejects expired permit', async function () {
|
||||
const deadline = (await time.clock.timestamp()) - time.duration.weeks(1);
|
||||
|
||||
const { v, r, s } = await this.buildData(this.token, deadline)
|
||||
.then(({ domain, types, message }) => this.owner.signTypedData(domain, types, message))
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
await expect(this.token.permit(this.owner, this.spender, value, deadline, v, r, s))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2612ExpiredSignature')
|
||||
.withArgs(deadline);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,546 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture, mine } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { getDomain, Delegation } = require('../../../helpers/eip712');
|
||||
const { batchInBlock } = require('../../../helpers/txpool');
|
||||
const time = require('../../../helpers/time');
|
||||
|
||||
const { shouldBehaveLikeVotes } = require('../../../governance/utils/Votes.behavior');
|
||||
|
||||
const TOKENS = [
|
||||
{ Token: '$ERC20Votes', mode: 'blocknumber' },
|
||||
{ Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
|
||||
];
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const version = '1';
|
||||
const supply = ethers.parseEther('10000000');
|
||||
|
||||
describe('ERC20Votes', function () {
|
||||
for (const { Token, mode } of TOKENS) {
|
||||
const fixture = async () => {
|
||||
// accounts is required by shouldBehaveLikeVotes
|
||||
const accounts = await ethers.getSigners();
|
||||
const [holder, recipient, delegatee, other1, other2] = accounts;
|
||||
|
||||
const token = await ethers.deployContract(Token, [name, symbol, name, version]);
|
||||
const domain = await getDomain(token);
|
||||
|
||||
return { accounts, holder, recipient, delegatee, other1, other2, token, domain };
|
||||
};
|
||||
|
||||
describe(`vote with ${mode}`, function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
this.votes = this.token;
|
||||
});
|
||||
|
||||
// includes ERC6372 behavior check
|
||||
shouldBehaveLikeVotes([1, 17, 42], { mode, fungible: true });
|
||||
|
||||
it('initial nonce is 0', async function () {
|
||||
expect(await this.token.nonces(this.holder)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('minting restriction', async function () {
|
||||
const value = 2n ** 208n;
|
||||
await expect(this.token.$_mint(this.holder, value))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20ExceededSafeSupply')
|
||||
.withArgs(value, value - 1n);
|
||||
});
|
||||
|
||||
it('recent checkpoints', async function () {
|
||||
await this.token.connect(this.holder).delegate(this.holder);
|
||||
for (let i = 0; i < 6; i++) {
|
||||
await this.token.$_mint(this.holder, 1n);
|
||||
}
|
||||
const timepoint = await time.clock[mode]();
|
||||
expect(await this.token.numCheckpoints(this.holder)).to.equal(6n);
|
||||
// recent
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint - 1n)).to.equal(5n);
|
||||
// non-recent
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint - 6n)).to.equal(0n);
|
||||
});
|
||||
|
||||
describe('set delegation', function () {
|
||||
describe('call', function () {
|
||||
it('delegation with balance', async function () {
|
||||
await this.token.$_mint(this.holder, supply);
|
||||
expect(await this.token.delegates(this.holder)).to.equal(ethers.ZeroAddress);
|
||||
|
||||
const tx = await this.token.connect(this.holder).delegate(this.holder);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'DelegateChanged')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, this.holder)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, 0n, supply);
|
||||
|
||||
expect(await this.token.delegates(this.holder)).to.equal(this.holder);
|
||||
expect(await this.token.getVotes(this.holder)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint - 1n)).to.equal(0n);
|
||||
await mine();
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint)).to.equal(supply);
|
||||
});
|
||||
|
||||
it('delegation without balance', async function () {
|
||||
expect(await this.token.delegates(this.holder)).to.equal(ethers.ZeroAddress);
|
||||
|
||||
await expect(this.token.connect(this.holder).delegate(this.holder))
|
||||
.to.emit(this.token, 'DelegateChanged')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, this.holder)
|
||||
.to.not.emit(this.token, 'DelegateVotesChanged');
|
||||
|
||||
expect(await this.token.delegates(this.holder)).to.equal(this.holder);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with signature', function () {
|
||||
const nonce = 0n;
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, supply);
|
||||
});
|
||||
|
||||
it('accept signed delegation', async function () {
|
||||
const { r, s, v } = await this.holder
|
||||
.signTypedData(
|
||||
this.domain,
|
||||
{ Delegation },
|
||||
{
|
||||
delegatee: this.holder.address,
|
||||
nonce,
|
||||
expiry: ethers.MaxUint256,
|
||||
},
|
||||
)
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
expect(await this.token.delegates(this.holder)).to.equal(ethers.ZeroAddress);
|
||||
|
||||
const tx = await this.token.delegateBySig(this.holder, nonce, ethers.MaxUint256, v, r, s);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'DelegateChanged')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, this.holder)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, 0n, supply);
|
||||
|
||||
expect(await this.token.delegates(this.holder)).to.equal(this.holder);
|
||||
|
||||
expect(await this.token.getVotes(this.holder)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint - 1n)).to.equal(0n);
|
||||
await mine();
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint)).to.equal(supply);
|
||||
});
|
||||
|
||||
it('rejects reused signature', async function () {
|
||||
const { r, s, v } = await this.holder
|
||||
.signTypedData(
|
||||
this.domain,
|
||||
{ Delegation },
|
||||
{
|
||||
delegatee: this.holder.address,
|
||||
nonce,
|
||||
expiry: ethers.MaxUint256,
|
||||
},
|
||||
)
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
await this.token.delegateBySig(this.holder, nonce, ethers.MaxUint256, v, r, s);
|
||||
|
||||
await expect(this.token.delegateBySig(this.holder, nonce, ethers.MaxUint256, v, r, s))
|
||||
.to.be.revertedWithCustomError(this.token, 'InvalidAccountNonce')
|
||||
.withArgs(this.holder, nonce + 1n);
|
||||
});
|
||||
|
||||
it('rejects bad delegatee', async function () {
|
||||
const { r, s, v } = await this.holder
|
||||
.signTypedData(
|
||||
this.domain,
|
||||
{ Delegation },
|
||||
{
|
||||
delegatee: this.holder.address,
|
||||
nonce,
|
||||
expiry: ethers.MaxUint256,
|
||||
},
|
||||
)
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
const tx = await this.token.delegateBySig(this.delegatee, nonce, ethers.MaxUint256, v, r, s);
|
||||
|
||||
const { args } = await tx
|
||||
.wait()
|
||||
.then(receipt => receipt.logs.find(event => event.fragment.name == 'DelegateChanged'));
|
||||
expect(args[0]).to.not.equal(this.holder);
|
||||
expect(args[1]).to.equal(ethers.ZeroAddress);
|
||||
expect(args[2]).to.equal(this.delegatee);
|
||||
});
|
||||
|
||||
it('rejects bad nonce', async function () {
|
||||
const { r, s, v, serialized } = await this.holder
|
||||
.signTypedData(
|
||||
this.domain,
|
||||
{ Delegation },
|
||||
{
|
||||
delegatee: this.holder.address,
|
||||
nonce,
|
||||
expiry: ethers.MaxUint256,
|
||||
},
|
||||
)
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
const recovered = ethers.verifyTypedData(
|
||||
this.domain,
|
||||
{ Delegation },
|
||||
{
|
||||
delegatee: this.holder.address,
|
||||
nonce: nonce + 1n,
|
||||
expiry: ethers.MaxUint256,
|
||||
},
|
||||
serialized,
|
||||
);
|
||||
|
||||
await expect(this.token.delegateBySig(this.holder, nonce + 1n, ethers.MaxUint256, v, r, s))
|
||||
.to.be.revertedWithCustomError(this.token, 'InvalidAccountNonce')
|
||||
.withArgs(recovered, nonce);
|
||||
});
|
||||
|
||||
it('rejects expired permit', async function () {
|
||||
const expiry = (await time.clock.timestamp()) - time.duration.weeks(1);
|
||||
|
||||
const { r, s, v } = await this.holder
|
||||
.signTypedData(
|
||||
this.domain,
|
||||
{ Delegation },
|
||||
{
|
||||
delegatee: this.holder.address,
|
||||
nonce,
|
||||
expiry,
|
||||
},
|
||||
)
|
||||
.then(ethers.Signature.from);
|
||||
|
||||
await expect(this.token.delegateBySig(this.holder, nonce, expiry, v, r, s))
|
||||
.to.be.revertedWithCustomError(this.token, 'VotesExpiredSignature')
|
||||
.withArgs(expiry);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('change delegation', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, supply);
|
||||
await this.token.connect(this.holder).delegate(this.holder);
|
||||
});
|
||||
|
||||
it('call', async function () {
|
||||
expect(await this.token.delegates(this.holder)).to.equal(this.holder);
|
||||
|
||||
const tx = await this.token.connect(this.holder).delegate(this.delegatee);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'DelegateChanged')
|
||||
.withArgs(this.holder, this.holder, this.delegatee)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, supply, 0n)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.delegatee, 0n, supply);
|
||||
|
||||
expect(await this.token.delegates(this.holder)).to.equal(this.delegatee);
|
||||
|
||||
expect(await this.token.getVotes(this.holder)).to.equal(0n);
|
||||
expect(await this.token.getVotes(this.delegatee)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint - 1n)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.delegatee, timepoint - 1n)).to.equal(0n);
|
||||
await mine();
|
||||
expect(await this.token.getPastVotes(this.holder, timepoint)).to.equal(0n);
|
||||
expect(await this.token.getPastVotes(this.delegatee, timepoint)).to.equal(supply);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfers', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, supply);
|
||||
});
|
||||
|
||||
it('no delegation', async function () {
|
||||
await expect(this.token.connect(this.holder).transfer(this.recipient, 1n))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, 1n)
|
||||
.to.not.emit(this.token, 'DelegateVotesChanged');
|
||||
|
||||
this.holderVotes = 0n;
|
||||
this.recipientVotes = 0n;
|
||||
});
|
||||
|
||||
it('sender delegation', async function () {
|
||||
await this.token.connect(this.holder).delegate(this.holder);
|
||||
|
||||
const tx = await this.token.connect(this.holder).transfer(this.recipient, 1n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, 1n)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, supply, supply - 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 = supply - 1n;
|
||||
this.recipientVotes = 0n;
|
||||
});
|
||||
|
||||
it('receiver delegation', async function () {
|
||||
await this.token.connect(this.recipient).delegate(this.recipient);
|
||||
|
||||
const tx = await this.token.connect(this.holder).transfer(this.recipient, 1n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, 1n)
|
||||
.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.token.connect(this.holder).delegate(this.holder);
|
||||
await this.token.connect(this.recipient).delegate(this.recipient);
|
||||
|
||||
const tx = await this.token.connect(this.holder).transfer(this.recipient, 1n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.recipient, 1n)
|
||||
.to.emit(this.token, 'DelegateVotesChanged')
|
||||
.withArgs(this.holder, supply, supply - 1n)
|
||||
.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 = supply - 1n;
|
||||
this.recipientVotes = 1n;
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
expect(await this.token.getVotes(this.holder)).to.equal(this.holderVotes);
|
||||
expect(await this.token.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.token.getPastVotes(this.holder, timepoint)).to.equal(this.holderVotes);
|
||||
expect(await this.token.getPastVotes(this.recipient, timepoint)).to.equal(this.recipientVotes);
|
||||
});
|
||||
});
|
||||
|
||||
// The following tests are a adaptation of https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
|
||||
describe('Compound test suite', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, supply);
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
it('grants to initial account', async function () {
|
||||
expect(await this.token.balanceOf(this.holder)).to.equal(supply);
|
||||
});
|
||||
});
|
||||
|
||||
describe('numCheckpoints', function () {
|
||||
it('returns the number of checkpoints for a delegate', async function () {
|
||||
await this.token.connect(this.holder).transfer(this.recipient, 100n); //give an account a few tokens for readability
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(0n);
|
||||
|
||||
const t1 = await this.token.connect(this.recipient).delegate(this.other1);
|
||||
t1.timepoint = await time.clockFromReceipt[mode](t1);
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(1n);
|
||||
|
||||
const t2 = await this.token.connect(this.recipient).transfer(this.other2, 10);
|
||||
t2.timepoint = await time.clockFromReceipt[mode](t2);
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(2n);
|
||||
|
||||
const t3 = await this.token.connect(this.recipient).transfer(this.other2, 10);
|
||||
t3.timepoint = await time.clockFromReceipt[mode](t3);
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(3n);
|
||||
|
||||
const t4 = await this.token.connect(this.holder).transfer(this.recipient, 20);
|
||||
t4.timepoint = await time.clockFromReceipt[mode](t4);
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(4n);
|
||||
|
||||
expect(await this.token.checkpoints(this.other1, 0n)).to.deep.equal([t1.timepoint, 100n]);
|
||||
expect(await this.token.checkpoints(this.other1, 1n)).to.deep.equal([t2.timepoint, 90n]);
|
||||
expect(await this.token.checkpoints(this.other1, 2n)).to.deep.equal([t3.timepoint, 80n]);
|
||||
expect(await this.token.checkpoints(this.other1, 3n)).to.deep.equal([t4.timepoint, 100n]);
|
||||
await mine();
|
||||
expect(await this.token.getPastVotes(this.other1, t1.timepoint)).to.equal(100n);
|
||||
expect(await this.token.getPastVotes(this.other1, t2.timepoint)).to.equal(90n);
|
||||
expect(await this.token.getPastVotes(this.other1, t3.timepoint)).to.equal(80n);
|
||||
expect(await this.token.getPastVotes(this.other1, t4.timepoint)).to.equal(100n);
|
||||
});
|
||||
|
||||
it('does not add more than one checkpoint in a block', async function () {
|
||||
await this.token.connect(this.holder).transfer(this.recipient, 100n);
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(0n);
|
||||
|
||||
const [t1, t2, t3] = await batchInBlock([
|
||||
() => this.token.connect(this.recipient).delegate(this.other1, { gasLimit: 200000 }),
|
||||
() => this.token.connect(this.recipient).transfer(this.other2, 10n, { gasLimit: 200000 }),
|
||||
() => this.token.connect(this.recipient).transfer(this.other2, 10n, { gasLimit: 200000 }),
|
||||
]);
|
||||
t1.timepoint = await time.clockFromReceipt[mode](t1);
|
||||
t2.timepoint = await time.clockFromReceipt[mode](t2);
|
||||
t3.timepoint = await time.clockFromReceipt[mode](t3);
|
||||
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(1);
|
||||
expect(await this.token.checkpoints(this.other1, 0n)).to.be.deep.equal([t1.timepoint, 80n]);
|
||||
|
||||
const t4 = await this.token.connect(this.holder).transfer(this.recipient, 20n);
|
||||
t4.timepoint = await time.clockFromReceipt[mode](t4);
|
||||
|
||||
expect(await this.token.numCheckpoints(this.other1)).to.equal(2n);
|
||||
expect(await this.token.checkpoints(this.other1, 1n)).to.be.deep.equal([t4.timepoint, 100n]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPastVotes', function () {
|
||||
it('reverts if block number >= current block', async function () {
|
||||
const clock = await this.token.clock();
|
||||
await expect(this.token.getPastVotes(this.other1, 50_000_000_000n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC5805FutureLookup')
|
||||
.withArgs(50_000_000_000n, clock);
|
||||
});
|
||||
|
||||
it('returns 0 if there are no checkpoints', async function () {
|
||||
expect(await this.token.getPastVotes(this.other1, 0n)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('returns the latest block if >= last checkpoint block', async function () {
|
||||
const tx = await this.token.connect(this.holder).delegate(this.other1);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
await mine(2);
|
||||
|
||||
expect(await this.token.getPastVotes(this.other1, timepoint)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.other1, timepoint + 1n)).to.equal(supply);
|
||||
});
|
||||
|
||||
it('returns zero if < first checkpoint block', async function () {
|
||||
await mine();
|
||||
const tx = await this.token.connect(this.holder).delegate(this.other1);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
await mine(2);
|
||||
|
||||
expect(await this.token.getPastVotes(this.other1, timepoint - 1n)).to.equal(0n);
|
||||
expect(await this.token.getPastVotes(this.other1, timepoint + 1n)).to.equal(supply);
|
||||
});
|
||||
|
||||
it('generally returns the voting balance at the appropriate checkpoint', async function () {
|
||||
const t1 = await this.token.connect(this.holder).delegate(this.other1);
|
||||
await mine(2);
|
||||
const t2 = await this.token.connect(this.holder).transfer(this.other2, 10);
|
||||
await mine(2);
|
||||
const t3 = await this.token.connect(this.holder).transfer(this.other2, 10);
|
||||
await mine(2);
|
||||
const t4 = await this.token.connect(this.other2).transfer(this.holder, 20);
|
||||
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.token.getPastVotes(this.other1, t1.timepoint - 1n)).to.equal(0n);
|
||||
expect(await this.token.getPastVotes(this.other1, t1.timepoint)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.other1, t1.timepoint + 1n)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.other1, t2.timepoint)).to.equal(supply - 10n);
|
||||
expect(await this.token.getPastVotes(this.other1, t2.timepoint + 1n)).to.equal(supply - 10n);
|
||||
expect(await this.token.getPastVotes(this.other1, t3.timepoint)).to.equal(supply - 20n);
|
||||
expect(await this.token.getPastVotes(this.other1, t3.timepoint + 1n)).to.equal(supply - 20n);
|
||||
expect(await this.token.getPastVotes(this.other1, t4.timepoint)).to.equal(supply);
|
||||
expect(await this.token.getPastVotes(this.other1, t4.timepoint + 1n)).to.equal(supply);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPastTotalSupply', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).delegate(this.holder);
|
||||
});
|
||||
|
||||
it('reverts if block number >= current block', async function () {
|
||||
const clock = await this.token.clock();
|
||||
await expect(this.token.getPastTotalSupply(50_000_000_000n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC5805FutureLookup')
|
||||
.withArgs(50_000_000_000n, clock);
|
||||
});
|
||||
|
||||
it('returns 0 if there are no checkpoints', async function () {
|
||||
expect(await this.token.getPastTotalSupply(0n)).to.equal(0n);
|
||||
});
|
||||
|
||||
it('returns the latest block if >= last checkpoint block', async function () {
|
||||
const tx = await this.token.$_mint(this.holder, supply);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
await mine(2);
|
||||
|
||||
expect(await this.token.getPastTotalSupply(timepoint)).to.equal(supply);
|
||||
expect(await this.token.getPastTotalSupply(timepoint + 1n)).to.equal(supply);
|
||||
});
|
||||
|
||||
it('returns zero if < first checkpoint block', async function () {
|
||||
await mine();
|
||||
const tx = await this.token.$_mint(this.holder, supply);
|
||||
const timepoint = await time.clockFromReceipt[mode](tx);
|
||||
await mine(2);
|
||||
|
||||
expect(await this.token.getPastTotalSupply(timepoint - 1n)).to.equal(0n);
|
||||
expect(await this.token.getPastTotalSupply(timepoint + 1n)).to.equal(supply);
|
||||
});
|
||||
|
||||
it('generally returns the voting balance at the appropriate checkpoint', async function () {
|
||||
const t1 = await this.token.$_mint(this.holder, supply);
|
||||
await mine(2);
|
||||
const t2 = await this.token.$_burn(this.holder, 10n);
|
||||
await mine(2);
|
||||
const t3 = await this.token.$_burn(this.holder, 10n);
|
||||
await mine(2);
|
||||
const t4 = await this.token.$_mint(this.holder, 20n);
|
||||
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.token.getPastTotalSupply(t1.timepoint - 1n)).to.equal(0n);
|
||||
expect(await this.token.getPastTotalSupply(t1.timepoint)).to.equal(supply);
|
||||
expect(await this.token.getPastTotalSupply(t1.timepoint + 1n)).to.equal(supply);
|
||||
expect(await this.token.getPastTotalSupply(t2.timepoint)).to.equal(supply - 10n);
|
||||
expect(await this.token.getPastTotalSupply(t2.timepoint + 1n)).to.equal(supply - 10n);
|
||||
expect(await this.token.getPastTotalSupply(t3.timepoint)).to.equal(supply - 20n);
|
||||
expect(await this.token.getPastTotalSupply(t3.timepoint + 1n)).to.equal(supply - 20n);
|
||||
expect(await this.token.getPastTotalSupply(t4.timepoint)).to.equal(supply);
|
||||
expect(await this.token.getPastTotalSupply(t4.timepoint + 1n)).to.equal(supply);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,203 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC20 } = require('../ERC20.behavior');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const decimals = 9n;
|
||||
const initialSupply = 100n;
|
||||
|
||||
async function fixture() {
|
||||
// this.accounts is used by shouldBehaveLikeERC20
|
||||
const accounts = await ethers.getSigners();
|
||||
const [holder, recipient, other] = accounts;
|
||||
|
||||
const underlying = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, decimals]);
|
||||
await underlying.$_mint(holder, initialSupply);
|
||||
|
||||
const token = await ethers.deployContract('$ERC20Wrapper', [`Wrapped ${name}`, `W${symbol}`, underlying]);
|
||||
|
||||
return { accounts, holder, recipient, other, underlying, token };
|
||||
}
|
||||
|
||||
describe('ERC20Wrapper', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
afterEach('Underlying balance', async function () {
|
||||
expect(await this.underlying.balanceOf(this.token)).to.equal(await this.token.totalSupply());
|
||||
});
|
||||
|
||||
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 the same decimals as the underlying token', async function () {
|
||||
expect(await this.token.decimals()).to.equal(decimals);
|
||||
});
|
||||
|
||||
it('decimals default back to 18 if token has no metadata', async function () {
|
||||
const noDecimals = await ethers.deployContract('CallReceiverMock');
|
||||
const token = await ethers.deployContract('$ERC20Wrapper', [`Wrapped ${name}`, `W${symbol}`, noDecimals]);
|
||||
expect(await token.decimals()).to.equal(18n);
|
||||
});
|
||||
|
||||
it('has underlying', async function () {
|
||||
expect(await this.token.underlying()).to.equal(this.underlying);
|
||||
});
|
||||
|
||||
describe('deposit', function () {
|
||||
it('executes with approval', async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, initialSupply);
|
||||
|
||||
const tx = await this.token.connect(this.holder).depositFor(this.holder, initialSupply);
|
||||
await expect(tx)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.holder, this.token, initialSupply)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.holder, initialSupply);
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.underlying,
|
||||
[this.holder, this.token],
|
||||
[-initialSupply, initialSupply],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, initialSupply);
|
||||
});
|
||||
|
||||
it('reverts when missing approval', async function () {
|
||||
await expect(this.token.connect(this.holder).depositFor(this.holder, initialSupply))
|
||||
.to.be.revertedWithCustomError(this.underlying, 'ERC20InsufficientAllowance')
|
||||
.withArgs(this.token, 0, initialSupply);
|
||||
});
|
||||
|
||||
it('reverts when inssuficient balance', async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, ethers.MaxUint256);
|
||||
|
||||
await expect(this.token.connect(this.holder).depositFor(this.holder, ethers.MaxUint256))
|
||||
.to.be.revertedWithCustomError(this.underlying, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.holder, initialSupply, ethers.MaxUint256);
|
||||
});
|
||||
|
||||
it('deposits to other account', async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, initialSupply);
|
||||
|
||||
const tx = await this.token.connect(this.holder).depositFor(this.recipient, initialSupply);
|
||||
await expect(tx)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.holder, this.token.target, initialSupply)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, initialSupply);
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.underlying,
|
||||
[this.holder, this.token],
|
||||
[-initialSupply, initialSupply],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalances(this.token, [this.holder, this.recipient], [0, initialSupply]);
|
||||
});
|
||||
|
||||
it('reverts minting to the wrapper contract', async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, ethers.MaxUint256);
|
||||
|
||||
await expect(this.token.connect(this.holder).depositFor(this.token, ethers.MaxUint256))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
});
|
||||
|
||||
describe('withdraw', function () {
|
||||
beforeEach(async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, initialSupply);
|
||||
await this.token.connect(this.holder).depositFor(this.holder, initialSupply);
|
||||
});
|
||||
|
||||
it('reverts when inssuficient balance', async function () {
|
||||
await expect(this.token.connect(this.holder).withdrawTo(this.holder, ethers.MaxInt256))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
||||
.withArgs(this.holder, initialSupply, ethers.MaxInt256);
|
||||
});
|
||||
|
||||
it('executes when operation is valid', async function () {
|
||||
const value = 42n;
|
||||
|
||||
const tx = await this.token.connect(this.holder).withdrawTo(this.holder, value);
|
||||
await expect(tx)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.token.target, this.holder, value)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, value);
|
||||
await expect(tx).to.changeTokenBalances(this.underlying, [this.token, this.holder], [-value, value]);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, -value);
|
||||
});
|
||||
|
||||
it('entire balance', async function () {
|
||||
const tx = await this.token.connect(this.holder).withdrawTo(this.holder, initialSupply);
|
||||
await expect(tx)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.token.target, this.holder, initialSupply)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, initialSupply);
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.underlying,
|
||||
[this.token, this.holder],
|
||||
[-initialSupply, initialSupply],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, -initialSupply);
|
||||
});
|
||||
|
||||
it('to other account', async function () {
|
||||
const tx = await this.token.connect(this.holder).withdrawTo(this.recipient, initialSupply);
|
||||
await expect(tx)
|
||||
.to.emit(this.underlying, 'Transfer')
|
||||
.withArgs(this.token, this.recipient, initialSupply)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, initialSupply);
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.underlying,
|
||||
[this.token, this.holder, this.recipient],
|
||||
[-initialSupply, 0, initialSupply],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.holder, -initialSupply);
|
||||
});
|
||||
|
||||
it('reverts withdrawing to the wrapper contract', async function () {
|
||||
await expect(this.token.connect(this.holder).withdrawTo(this.token, initialSupply))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
});
|
||||
|
||||
describe('recover', function () {
|
||||
it('nothing to recover', async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, initialSupply);
|
||||
await this.token.connect(this.holder).depositFor(this.holder, initialSupply);
|
||||
|
||||
const tx = await this.token.$_recover(this.recipient);
|
||||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient, 0n);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.recipient, 0);
|
||||
});
|
||||
|
||||
it('something to recover', async function () {
|
||||
await this.underlying.connect(this.holder).transfer(this.token, initialSupply);
|
||||
|
||||
const tx = await this.token.$_recover(this.recipient);
|
||||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient, initialSupply);
|
||||
await expect(tx).to.changeTokenBalance(this.token, this.recipient, initialSupply);
|
||||
});
|
||||
});
|
||||
|
||||
describe('erc20 behaviour', function () {
|
||||
beforeEach(async function () {
|
||||
await this.underlying.connect(this.holder).approve(this.token, initialSupply);
|
||||
await this.token.connect(this.holder).depositFor(this.holder, initialSupply);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC20(initialSupply);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC4626Test} from "erc4626-tests/ERC4626.test.sol";
|
||||
|
||||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||
import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
|
||||
|
||||
import {ERC20Mock} from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol";
|
||||
import {ERC4626Mock} from "@openzeppelin/contracts/mocks/token/ERC4626Mock.sol";
|
||||
import {ERC4626OffsetMock} from "@openzeppelin/contracts/mocks/token/ERC4626OffsetMock.sol";
|
||||
|
||||
contract ERC4626VaultOffsetMock is ERC4626OffsetMock {
|
||||
constructor(
|
||||
ERC20 underlying_,
|
||||
uint8 offset_
|
||||
) ERC20("My Token Vault", "MTKNV") ERC4626(underlying_) ERC4626OffsetMock(offset_) {}
|
||||
}
|
||||
|
||||
contract ERC4626StdTest is ERC4626Test {
|
||||
ERC20 private _underlying = new ERC20Mock();
|
||||
|
||||
function setUp() public override {
|
||||
_underlying_ = address(_underlying);
|
||||
_vault_ = address(new ERC4626Mock(_underlying_));
|
||||
_delta_ = 0;
|
||||
_vaultMayBeEmpty = true;
|
||||
_unlimitedAmount = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Check the case where calculated `decimals` value overflows the `uint8` type.
|
||||
*/
|
||||
function testFuzzDecimalsOverflow(uint8 offset) public {
|
||||
/// @dev Remember that the `_underlying` exhibits a `decimals` value of 18.
|
||||
offset = uint8(bound(uint256(offset), 238, uint256(type(uint8).max)));
|
||||
ERC4626VaultOffsetMock erc4626VaultOffsetMock = new ERC4626VaultOffsetMock(_underlying, offset);
|
||||
vm.expectRevert();
|
||||
erc4626VaultOffsetMock.decimals();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,888 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||
|
||||
const { Enum } = require('../../../helpers/enums');
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const decimals = 18n;
|
||||
|
||||
async function fixture() {
|
||||
const [holder, recipient, spender, other, ...accounts] = await ethers.getSigners();
|
||||
return { holder, recipient, spender, other, accounts };
|
||||
}
|
||||
|
||||
describe('ERC4626', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('inherit decimals if from asset', async function () {
|
||||
for (const decimals of [0n, 9n, 12n, 18n, 36n]) {
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', ['', '', decimals]);
|
||||
const vault = await ethers.deployContract('$ERC4626', ['', '', token]);
|
||||
expect(await vault.decimals()).to.equal(decimals);
|
||||
}
|
||||
});
|
||||
|
||||
it('asset has not yet been created', async function () {
|
||||
const vault = await ethers.deployContract('$ERC4626', ['', '', this.other.address]);
|
||||
expect(await vault.decimals()).to.equal(decimals);
|
||||
});
|
||||
|
||||
it('underlying excess decimals', async function () {
|
||||
const token = await ethers.deployContract('$ERC20ExcessDecimalsMock');
|
||||
const vault = await ethers.deployContract('$ERC4626', ['', '', token]);
|
||||
expect(await vault.decimals()).to.equal(decimals);
|
||||
});
|
||||
|
||||
it('decimals overflow', async function () {
|
||||
for (const offset of [243n, 250n, 255n]) {
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', ['', '', decimals]);
|
||||
const vault = await ethers.deployContract('$ERC4626OffsetMock', ['', '', token, offset]);
|
||||
await expect(vault.decimals()).to.be.revertedWithPanic(PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW);
|
||||
}
|
||||
});
|
||||
|
||||
describe('reentrancy', function () {
|
||||
const reenterType = Enum('No', 'Before', 'After');
|
||||
|
||||
const value = 1_000_000_000_000_000_000n;
|
||||
const reenterValue = 1_000_000_000n;
|
||||
|
||||
beforeEach(async function () {
|
||||
// Use offset 1 so the rate is not 1:1 and we can't possibly confuse assets and shares
|
||||
const token = await ethers.deployContract('$ERC20Reentrant');
|
||||
const vault = await ethers.deployContract('$ERC4626OffsetMock', ['', '', token, 1n]);
|
||||
// Funds and approval for tests
|
||||
await token.$_mint(this.holder, value);
|
||||
await token.$_mint(this.other, value);
|
||||
await token.$_approve(this.holder, vault, ethers.MaxUint256);
|
||||
await token.$_approve(this.other, vault, ethers.MaxUint256);
|
||||
await token.$_approve(token, vault, ethers.MaxUint256);
|
||||
|
||||
Object.assign(this, { token, vault });
|
||||
});
|
||||
|
||||
// During a `_deposit`, the vault does `transferFrom(depositor, vault, assets)` -> `_mint(receiver, shares)`
|
||||
// such that a reentrancy BEFORE the transfer guarantees the price is kept the same.
|
||||
// If the order of transfer -> mint is changed to mint -> transfer, the reentrancy could be triggered on an
|
||||
// intermediate state in which the ratio of assets/shares has been decreased (more shares than assets).
|
||||
it('correct share price is observed during reentrancy before deposit', async function () {
|
||||
// mint token for deposit
|
||||
await this.token.$_mint(this.token, reenterValue);
|
||||
|
||||
// Schedules a reentrancy from the token contract
|
||||
await this.token.scheduleReenter(
|
||||
reenterType.Before,
|
||||
this.vault,
|
||||
this.vault.interface.encodeFunctionData('deposit', [reenterValue, this.holder.address]),
|
||||
);
|
||||
|
||||
// Initial share price
|
||||
const sharesForDeposit = await this.vault.previewDeposit(value);
|
||||
const sharesForReenter = await this.vault.previewDeposit(reenterValue);
|
||||
|
||||
await expect(this.vault.connect(this.holder).deposit(value, this.holder))
|
||||
// Deposit normally, reentering before the internal `_update`
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.holder, value, sharesForDeposit)
|
||||
// Reentrant deposit event → uses the same price
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.token, this.holder, reenterValue, sharesForReenter);
|
||||
|
||||
// Assert prices is kept
|
||||
expect(await this.vault.previewDeposit(value)).to.equal(sharesForDeposit);
|
||||
});
|
||||
|
||||
// During a `_withdraw`, the vault does `_burn(owner, shares)` -> `transfer(receiver, assets)`
|
||||
// such that a reentrancy AFTER the transfer guarantees the price is kept the same.
|
||||
// If the order of burn -> transfer is changed to transfer -> burn, the reentrancy could be triggered on an
|
||||
// intermediate state in which the ratio of shares/assets has been decreased (more assets than shares).
|
||||
it('correct share price is observed during reentrancy after withdraw', async function () {
|
||||
// Deposit into the vault: holder gets `value` share, token.address gets `reenterValue` shares
|
||||
await this.vault.connect(this.holder).deposit(value, this.holder);
|
||||
await this.vault.connect(this.other).deposit(reenterValue, this.token);
|
||||
|
||||
// Schedules a reentrancy from the token contract
|
||||
await this.token.scheduleReenter(
|
||||
reenterType.After,
|
||||
this.vault,
|
||||
this.vault.interface.encodeFunctionData('withdraw', [reenterValue, this.holder.address, this.token.target]),
|
||||
);
|
||||
|
||||
// Initial share price
|
||||
const sharesForWithdraw = await this.vault.previewWithdraw(value);
|
||||
const sharesForReenter = await this.vault.previewWithdraw(reenterValue);
|
||||
|
||||
// Do withdraw normally, triggering the _afterTokenTransfer hook
|
||||
await expect(this.vault.connect(this.holder).withdraw(value, this.holder, this.holder))
|
||||
// Main withdraw event
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.holder, this.holder, value, sharesForWithdraw)
|
||||
// Reentrant withdraw event → uses the same price
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.token, this.holder, this.token, reenterValue, sharesForReenter);
|
||||
|
||||
// Assert price is kept
|
||||
expect(await this.vault.previewWithdraw(value)).to.equal(sharesForWithdraw);
|
||||
});
|
||||
|
||||
// Donate newly minted tokens to the vault during the reentracy causes the share price to increase.
|
||||
// Still, the deposit that trigger the reentracy is not affected and get the previewed price.
|
||||
// Further deposits will get a different price (getting fewer shares for the same value of assets)
|
||||
it('share price change during reentracy does not affect deposit', async function () {
|
||||
// Schedules a reentrancy from the token contract that mess up the share price
|
||||
await this.token.scheduleReenter(
|
||||
reenterType.Before,
|
||||
this.token,
|
||||
this.token.interface.encodeFunctionData('$_mint', [this.vault.target, reenterValue]),
|
||||
);
|
||||
|
||||
// Price before
|
||||
const sharesBefore = await this.vault.previewDeposit(value);
|
||||
|
||||
// Deposit, reentering before the internal `_update`
|
||||
await expect(this.vault.connect(this.holder).deposit(value, this.holder))
|
||||
// Price is as previewed
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.holder, value, sharesBefore);
|
||||
|
||||
// Price was modified during reentrancy
|
||||
expect(await this.vault.previewDeposit(value)).to.lt(sharesBefore);
|
||||
});
|
||||
|
||||
// Burn some tokens from the vault during the reentracy causes the share price to drop.
|
||||
// Still, the withdraw that trigger the reentracy is not affected and get the previewed price.
|
||||
// Further withdraw will get a different price (needing more shares for the same value of assets)
|
||||
it('share price change during reentracy does not affect withdraw', async function () {
|
||||
await this.vault.connect(this.holder).deposit(value, this.holder);
|
||||
await this.vault.connect(this.other).deposit(value, this.other);
|
||||
|
||||
// Schedules a reentrancy from the token contract that mess up the share price
|
||||
await this.token.scheduleReenter(
|
||||
reenterType.After,
|
||||
this.token,
|
||||
this.token.interface.encodeFunctionData('$_burn', [this.vault.target, reenterValue]),
|
||||
);
|
||||
|
||||
// Price before
|
||||
const sharesBefore = await this.vault.previewWithdraw(value);
|
||||
|
||||
// Withdraw, triggering the _afterTokenTransfer hook
|
||||
await expect(this.vault.connect(this.holder).withdraw(value, this.holder, this.holder))
|
||||
// Price is as previewed
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.holder, this.holder, value, sharesBefore);
|
||||
|
||||
// Price was modified during reentrancy
|
||||
expect(await this.vault.previewWithdraw(value)).to.gt(sharesBefore);
|
||||
});
|
||||
});
|
||||
|
||||
describe('limits', function () {
|
||||
beforeEach(async function () {
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, decimals]);
|
||||
const vault = await ethers.deployContract('$ERC4626LimitsMock', ['', '', token]);
|
||||
|
||||
Object.assign(this, { token, vault });
|
||||
});
|
||||
|
||||
it('reverts on deposit() above max deposit', async function () {
|
||||
const maxDeposit = await this.vault.maxDeposit(this.holder);
|
||||
await expect(this.vault.connect(this.holder).deposit(maxDeposit + 1n, this.recipient))
|
||||
.to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxDeposit')
|
||||
.withArgs(this.recipient, maxDeposit + 1n, maxDeposit);
|
||||
});
|
||||
|
||||
it('reverts on mint() above max mint', async function () {
|
||||
const maxMint = await this.vault.maxMint(this.holder);
|
||||
|
||||
await expect(this.vault.connect(this.holder).mint(maxMint + 1n, this.recipient))
|
||||
.to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxMint')
|
||||
.withArgs(this.recipient, maxMint + 1n, maxMint);
|
||||
});
|
||||
|
||||
it('reverts on withdraw() above max withdraw', async function () {
|
||||
const maxWithdraw = await this.vault.maxWithdraw(this.holder);
|
||||
|
||||
await expect(this.vault.connect(this.holder).withdraw(maxWithdraw + 1n, this.recipient, this.holder))
|
||||
.to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxWithdraw')
|
||||
.withArgs(this.holder, maxWithdraw + 1n, maxWithdraw);
|
||||
});
|
||||
|
||||
it('reverts on redeem() above max redeem', async function () {
|
||||
const maxRedeem = await this.vault.maxRedeem(this.holder);
|
||||
|
||||
await expect(this.vault.connect(this.holder).redeem(maxRedeem + 1n, this.recipient, this.holder))
|
||||
.to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxRedeem')
|
||||
.withArgs(this.holder, maxRedeem + 1n, maxRedeem);
|
||||
});
|
||||
});
|
||||
|
||||
for (const offset of [0n, 6n, 18n]) {
|
||||
const parseToken = token => token * 10n ** decimals;
|
||||
const parseShare = share => share * 10n ** (decimals + offset);
|
||||
|
||||
const virtualAssets = 1n;
|
||||
const virtualShares = 10n ** offset;
|
||||
|
||||
describe(`offset: ${offset}`, function () {
|
||||
beforeEach(async function () {
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, decimals]);
|
||||
const vault = await ethers.deployContract('$ERC4626OffsetMock', [name + ' Vault', symbol + 'V', token, offset]);
|
||||
|
||||
await token.$_mint(this.holder, ethers.MaxUint256 / 2n); // 50% of maximum
|
||||
await token.$_approve(this.holder, vault, ethers.MaxUint256);
|
||||
await vault.$_approve(this.holder, this.spender, ethers.MaxUint256);
|
||||
|
||||
Object.assign(this, { token, vault });
|
||||
});
|
||||
|
||||
it('metadata', async function () {
|
||||
expect(await this.vault.name()).to.equal(name + ' Vault');
|
||||
expect(await this.vault.symbol()).to.equal(symbol + 'V');
|
||||
expect(await this.vault.decimals()).to.equal(decimals + offset);
|
||||
expect(await this.vault.asset()).to.equal(this.token);
|
||||
});
|
||||
|
||||
describe('empty vault: no assets & no shares', function () {
|
||||
it('status', async function () {
|
||||
expect(await this.vault.totalAssets()).to.equal(0n);
|
||||
});
|
||||
|
||||
it('deposit', async function () {
|
||||
expect(await this.vault.maxDeposit(this.holder)).to.equal(ethers.MaxUint256);
|
||||
expect(await this.vault.previewDeposit(parseToken(1n))).to.equal(parseShare(1n));
|
||||
|
||||
const tx = this.vault.connect(this.holder).deposit(parseToken(1n), this.recipient);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault],
|
||||
[-parseToken(1n), parseToken(1n)],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.recipient, parseShare(1n));
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, parseToken(1n))
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, parseShare(1n))
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, parseToken(1n), parseShare(1n));
|
||||
});
|
||||
|
||||
it('mint', async function () {
|
||||
expect(await this.vault.maxMint(this.holder)).to.equal(ethers.MaxUint256);
|
||||
expect(await this.vault.previewMint(parseShare(1n))).to.equal(parseToken(1n));
|
||||
|
||||
const tx = this.vault.connect(this.holder).mint(parseShare(1n), this.recipient);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault],
|
||||
[-parseToken(1n), parseToken(1n)],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.recipient, parseShare(1n));
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, parseToken(1n))
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, parseShare(1n))
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, parseToken(1n), parseShare(1n));
|
||||
});
|
||||
|
||||
it('withdraw', async function () {
|
||||
expect(await this.vault.maxWithdraw(this.holder)).to.equal(0n);
|
||||
expect(await this.vault.previewWithdraw(0n)).to.equal(0n);
|
||||
|
||||
const tx = this.vault.connect(this.holder).withdraw(0n, this.recipient, this.holder);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, 0n)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, 0n)
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, 0n, 0n);
|
||||
});
|
||||
|
||||
it('redeem', async function () {
|
||||
expect(await this.vault.maxRedeem(this.holder)).to.equal(0n);
|
||||
expect(await this.vault.previewRedeem(0n)).to.equal(0n);
|
||||
|
||||
const tx = this.vault.connect(this.holder).redeem(0n, this.recipient, this.holder);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, 0n)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, 0n)
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, 0n, 0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('inflation attack: offset price by direct deposit of assets', function () {
|
||||
beforeEach(async function () {
|
||||
// Donate 1 token to the vault to offset the price
|
||||
await this.token.$_mint(this.vault, parseToken(1n));
|
||||
});
|
||||
|
||||
it('status', async function () {
|
||||
expect(await this.vault.totalSupply()).to.equal(0n);
|
||||
expect(await this.vault.totalAssets()).to.equal(parseToken(1n));
|
||||
});
|
||||
|
||||
/**
|
||||
* | offset | deposited assets | redeemable assets |
|
||||
* |--------|----------------------|----------------------|
|
||||
* | 0 | 1.000000000000000000 | 0. |
|
||||
* | 6 | 1.000000000000000000 | 0.999999000000000000 |
|
||||
* | 18 | 1.000000000000000000 | 0.999999999999999999 |
|
||||
*
|
||||
* Attack is possible, but made difficult by the offset. For the attack to be successful
|
||||
* the attacker needs to frontrun a deposit 10**offset times bigger than what the victim
|
||||
* was trying to deposit
|
||||
*/
|
||||
it('deposit', async function () {
|
||||
const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
|
||||
const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
|
||||
|
||||
const depositAssets = parseToken(1n);
|
||||
const expectedShares = (depositAssets * effectiveShares) / effectiveAssets;
|
||||
|
||||
expect(await this.vault.maxDeposit(this.holder)).to.equal(ethers.MaxUint256);
|
||||
expect(await this.vault.previewDeposit(depositAssets)).to.equal(expectedShares);
|
||||
|
||||
const tx = this.vault.connect(this.holder).deposit(depositAssets, this.recipient);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault],
|
||||
[-depositAssets, depositAssets],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.recipient, expectedShares);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, depositAssets)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, expectedShares)
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, depositAssets, expectedShares);
|
||||
});
|
||||
|
||||
/**
|
||||
* | offset | deposited assets | redeemable assets |
|
||||
* |--------|----------------------|----------------------|
|
||||
* | 0 | 1000000000000000001. | 1000000000000000001. |
|
||||
* | 6 | 1000000000000000001. | 1000000000000000001. |
|
||||
* | 18 | 1000000000000000001. | 1000000000000000001. |
|
||||
*
|
||||
* Using mint protects against inflation attack, but makes minting shares very expensive.
|
||||
* The ER20 allowance for the underlying asset is needed to protect the user from (too)
|
||||
* large deposits.
|
||||
*/
|
||||
it('mint', async function () {
|
||||
const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
|
||||
const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
|
||||
|
||||
const mintShares = parseShare(1n);
|
||||
const expectedAssets = (mintShares * effectiveAssets) / effectiveShares;
|
||||
|
||||
expect(await this.vault.maxMint(this.holder)).to.equal(ethers.MaxUint256);
|
||||
expect(await this.vault.previewMint(mintShares)).to.equal(expectedAssets);
|
||||
|
||||
const tx = this.vault.connect(this.holder).mint(mintShares, this.recipient);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault],
|
||||
[-expectedAssets, expectedAssets],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.recipient, mintShares);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, expectedAssets)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, mintShares)
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, expectedAssets, mintShares);
|
||||
});
|
||||
|
||||
it('withdraw', async function () {
|
||||
expect(await this.vault.maxWithdraw(this.holder)).to.equal(0n);
|
||||
expect(await this.vault.previewWithdraw(0n)).to.equal(0n);
|
||||
|
||||
const tx = this.vault.connect(this.holder).withdraw(0n, this.recipient, this.holder);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, 0n)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, 0n)
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, 0n, 0n);
|
||||
});
|
||||
|
||||
it('redeem', async function () {
|
||||
expect(await this.vault.maxRedeem(this.holder)).to.equal(0n);
|
||||
expect(await this.vault.previewRedeem(0n)).to.equal(0n);
|
||||
|
||||
const tx = this.vault.connect(this.holder).redeem(0n, this.recipient, this.holder);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, 0n)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, 0n)
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, 0n, 0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('full vault: assets & shares', function () {
|
||||
beforeEach(async function () {
|
||||
// Add 1 token of underlying asset and 100 shares to the vault
|
||||
await this.token.$_mint(this.vault, parseToken(1n));
|
||||
await this.vault.$_mint(this.holder, parseShare(100n));
|
||||
});
|
||||
|
||||
it('status', async function () {
|
||||
expect(await this.vault.totalSupply()).to.equal(parseShare(100n));
|
||||
expect(await this.vault.totalAssets()).to.equal(parseToken(1n));
|
||||
});
|
||||
|
||||
/**
|
||||
* | offset | deposited assets | redeemable assets |
|
||||
* |--------|--------------------- |----------------------|
|
||||
* | 0 | 1.000000000000000000 | 0.999999999999999999 |
|
||||
* | 6 | 1.000000000000000000 | 0.999999999999999999 |
|
||||
* | 18 | 1.000000000000000000 | 0.999999999999999999 |
|
||||
*
|
||||
* Virtual shares & assets captures part of the value
|
||||
*/
|
||||
it('deposit', async function () {
|
||||
const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
|
||||
const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
|
||||
|
||||
const depositAssets = parseToken(1n);
|
||||
const expectedShares = (depositAssets * effectiveShares) / effectiveAssets;
|
||||
|
||||
expect(await this.vault.maxDeposit(this.holder)).to.equal(ethers.MaxUint256);
|
||||
expect(await this.vault.previewDeposit(depositAssets)).to.equal(expectedShares);
|
||||
|
||||
const tx = this.vault.connect(this.holder).deposit(depositAssets, this.recipient);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault],
|
||||
[-depositAssets, depositAssets],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.recipient, expectedShares);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, depositAssets)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, expectedShares)
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, depositAssets, expectedShares);
|
||||
});
|
||||
|
||||
/**
|
||||
* | offset | deposited assets | redeemable assets |
|
||||
* |--------|--------------------- |----------------------|
|
||||
* | 0 | 0.010000000000000001 | 0.010000000000000000 |
|
||||
* | 6 | 0.010000000000000001 | 0.010000000000000000 |
|
||||
* | 18 | 0.010000000000000001 | 0.010000000000000000 |
|
||||
*
|
||||
* Virtual shares & assets captures part of the value
|
||||
*/
|
||||
it('mint', async function () {
|
||||
const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
|
||||
const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
|
||||
|
||||
const mintShares = parseShare(1n);
|
||||
const expectedAssets = (mintShares * effectiveAssets) / effectiveShares + 1n; // add for the rounding
|
||||
|
||||
expect(await this.vault.maxMint(this.holder)).to.equal(ethers.MaxUint256);
|
||||
expect(await this.vault.previewMint(mintShares)).to.equal(expectedAssets);
|
||||
|
||||
const tx = this.vault.connect(this.holder).mint(mintShares, this.recipient);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault],
|
||||
[-expectedAssets, expectedAssets],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.recipient, mintShares);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, expectedAssets)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, mintShares)
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, expectedAssets, mintShares);
|
||||
});
|
||||
|
||||
it('withdraw', async function () {
|
||||
const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
|
||||
const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
|
||||
|
||||
const withdrawAssets = parseToken(1n);
|
||||
const expectedShares = (withdrawAssets * effectiveShares) / effectiveAssets + 1n; // add for the rounding
|
||||
|
||||
expect(await this.vault.maxWithdraw(this.holder)).to.equal(withdrawAssets);
|
||||
expect(await this.vault.previewWithdraw(withdrawAssets)).to.equal(expectedShares);
|
||||
|
||||
const tx = this.vault.connect(this.holder).withdraw(withdrawAssets, this.recipient, this.holder);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.vault, this.recipient],
|
||||
[-withdrawAssets, withdrawAssets],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.holder, -expectedShares);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, withdrawAssets)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, expectedShares)
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, withdrawAssets, expectedShares);
|
||||
});
|
||||
|
||||
it('withdraw with approval', async function () {
|
||||
const assets = await this.vault.previewWithdraw(parseToken(1n));
|
||||
|
||||
await expect(this.vault.connect(this.other).withdraw(parseToken(1n), this.recipient, this.holder))
|
||||
.to.be.revertedWithCustomError(this.vault, 'ERC20InsufficientAllowance')
|
||||
.withArgs(this.other, 0n, assets);
|
||||
|
||||
await expect(this.vault.connect(this.spender).withdraw(parseToken(1n), this.recipient, this.holder)).to.not.be
|
||||
.reverted;
|
||||
});
|
||||
|
||||
it('redeem', async function () {
|
||||
const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
|
||||
const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
|
||||
|
||||
const redeemShares = parseShare(100n);
|
||||
const expectedAssets = (redeemShares * effectiveAssets) / effectiveShares;
|
||||
|
||||
expect(await this.vault.maxRedeem(this.holder)).to.equal(redeemShares);
|
||||
expect(await this.vault.previewRedeem(redeemShares)).to.equal(expectedAssets);
|
||||
|
||||
const tx = this.vault.connect(this.holder).redeem(redeemShares, this.recipient, this.holder);
|
||||
|
||||
await expect(tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.vault, this.recipient],
|
||||
[-expectedAssets, expectedAssets],
|
||||
);
|
||||
await expect(tx).to.changeTokenBalance(this.vault, this.holder, -redeemShares);
|
||||
await expect(tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, expectedAssets)
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, redeemShares)
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, expectedAssets, redeemShares);
|
||||
});
|
||||
|
||||
it('redeem with approval', async function () {
|
||||
await expect(this.vault.connect(this.other).redeem(parseShare(100n), this.recipient, this.holder))
|
||||
.to.be.revertedWithCustomError(this.vault, 'ERC20InsufficientAllowance')
|
||||
.withArgs(this.other, 0n, parseShare(100n));
|
||||
|
||||
await expect(this.vault.connect(this.spender).redeem(parseShare(100n), this.recipient, this.holder)).to.not.be
|
||||
.reverted;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('ERC4626Fees', function () {
|
||||
const feeBasisPoints = 500n; // 5%
|
||||
const valueWithoutFees = 10_000n;
|
||||
const fees = (valueWithoutFees * feeBasisPoints) / 10_000n;
|
||||
const valueWithFees = valueWithoutFees + fees;
|
||||
|
||||
describe('input fees', function () {
|
||||
beforeEach(async function () {
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 18n]);
|
||||
const vault = await ethers.deployContract('$ERC4626FeesMock', [
|
||||
'',
|
||||
'',
|
||||
token,
|
||||
feeBasisPoints,
|
||||
this.other,
|
||||
0n,
|
||||
ethers.ZeroAddress,
|
||||
]);
|
||||
|
||||
await token.$_mint(this.holder, ethers.MaxUint256 / 2n);
|
||||
await token.$_approve(this.holder, vault, ethers.MaxUint256 / 2n);
|
||||
|
||||
Object.assign(this, { token, vault });
|
||||
});
|
||||
|
||||
it('deposit', async function () {
|
||||
expect(await this.vault.previewDeposit(valueWithFees)).to.equal(valueWithoutFees);
|
||||
this.tx = this.vault.connect(this.holder).deposit(valueWithFees, this.recipient);
|
||||
});
|
||||
|
||||
it('mint', async function () {
|
||||
expect(await this.vault.previewMint(valueWithoutFees)).to.equal(valueWithFees);
|
||||
this.tx = this.vault.connect(this.holder).mint(valueWithoutFees, this.recipient);
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
await expect(this.tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.holder, this.vault, this.other],
|
||||
[-valueWithFees, valueWithoutFees, fees],
|
||||
);
|
||||
await expect(this.tx).to.changeTokenBalance(this.vault, this.recipient, valueWithoutFees);
|
||||
await expect(this.tx)
|
||||
// get total
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.vault, valueWithFees)
|
||||
// redirect fees
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.other, fees)
|
||||
// mint shares
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, this.recipient, valueWithoutFees)
|
||||
// deposit event
|
||||
.to.emit(this.vault, 'Deposit')
|
||||
.withArgs(this.holder, this.recipient, valueWithFees, valueWithoutFees);
|
||||
});
|
||||
});
|
||||
|
||||
describe('output fees', function () {
|
||||
beforeEach(async function () {
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 18n]);
|
||||
const vault = await ethers.deployContract('$ERC4626FeesMock', [
|
||||
'',
|
||||
'',
|
||||
token,
|
||||
0n,
|
||||
ethers.ZeroAddress,
|
||||
feeBasisPoints,
|
||||
this.other,
|
||||
]);
|
||||
|
||||
await token.$_mint(vault, ethers.MaxUint256 / 2n);
|
||||
await vault.$_mint(this.holder, ethers.MaxUint256 / 2n);
|
||||
|
||||
Object.assign(this, { token, vault });
|
||||
});
|
||||
|
||||
it('redeem', async function () {
|
||||
expect(await this.vault.previewRedeem(valueWithFees)).to.equal(valueWithoutFees);
|
||||
this.tx = this.vault.connect(this.holder).redeem(valueWithFees, this.recipient, this.holder);
|
||||
});
|
||||
|
||||
it('withdraw', async function () {
|
||||
expect(await this.vault.previewWithdraw(valueWithoutFees)).to.equal(valueWithFees);
|
||||
this.tx = this.vault.connect(this.holder).withdraw(valueWithoutFees, this.recipient, this.holder);
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
await expect(this.tx).to.changeTokenBalances(
|
||||
this.token,
|
||||
[this.vault, this.recipient, this.other],
|
||||
[-valueWithFees, valueWithoutFees, fees],
|
||||
);
|
||||
await expect(this.tx).to.changeTokenBalance(this.vault, this.holder, -valueWithFees);
|
||||
await expect(this.tx)
|
||||
// withdraw principal
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.recipient, valueWithoutFees)
|
||||
// redirect fees
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.vault, this.other, fees)
|
||||
// mint shares
|
||||
.to.emit(this.vault, 'Transfer')
|
||||
.withArgs(this.holder, ethers.ZeroAddress, valueWithFees)
|
||||
// withdraw event
|
||||
.to.emit(this.vault, 'Withdraw')
|
||||
.withArgs(this.holder, this.recipient, this.holder, valueWithoutFees, valueWithFees);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/// Scenario inspired by solmate ERC4626 tests:
|
||||
/// https://github.com/transmissions11/solmate/blob/main/src/test/ERC4626.t.sol
|
||||
it('multiple mint, deposit, redeem & withdrawal', async function () {
|
||||
// test designed with both asset using similar decimals
|
||||
const [alice, bruce] = this.accounts;
|
||||
const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 18n]);
|
||||
const vault = await ethers.deployContract('$ERC4626', ['', '', token]);
|
||||
|
||||
await token.$_mint(alice, 4000n);
|
||||
await token.$_mint(bruce, 7001n);
|
||||
await token.connect(alice).approve(vault, 4000n);
|
||||
await token.connect(bruce).approve(vault, 7001n);
|
||||
|
||||
// 1. Alice mints 2000 shares (costs 2000 tokens)
|
||||
await expect(vault.connect(alice).mint(2000n, alice))
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(alice, vault, 2000n)
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, alice, 2000n);
|
||||
|
||||
expect(await vault.previewDeposit(2000n)).to.equal(2000n);
|
||||
expect(await vault.balanceOf(alice)).to.equal(2000n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(0n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(2000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(0n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(2000n);
|
||||
expect(await vault.totalSupply()).to.equal(2000n);
|
||||
expect(await vault.totalAssets()).to.equal(2000n);
|
||||
|
||||
// 2. Bruce deposits 4000 tokens (mints 4000 shares)
|
||||
await expect(vault.connect(bruce).mint(4000n, bruce))
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(bruce, vault, 4000n)
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, bruce, 4000n);
|
||||
|
||||
expect(await vault.previewDeposit(4000n)).to.equal(4000n);
|
||||
expect(await vault.balanceOf(alice)).to.equal(2000n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(4000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(2000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(4000n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(6000n);
|
||||
expect(await vault.totalSupply()).to.equal(6000n);
|
||||
expect(await vault.totalAssets()).to.equal(6000n);
|
||||
|
||||
// 3. Vault mutates by +3000 tokens (simulated yield returned from strategy)
|
||||
await token.$_mint(vault, 3000n);
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(2000n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(4000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(2999n); // used to be 3000, but virtual assets/shares captures part of the yield
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(5999n); // used to be 6000, but virtual assets/shares captures part of the yield
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(6000n);
|
||||
expect(await vault.totalSupply()).to.equal(6000n);
|
||||
expect(await vault.totalAssets()).to.equal(9000n);
|
||||
|
||||
// 4. Alice deposits 2000 tokens (mints 1333 shares)
|
||||
await expect(vault.connect(alice).deposit(2000n, alice))
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(alice, vault, 2000n)
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, alice, 1333n);
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(3333n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(4000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(4999n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(6000n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(7333n);
|
||||
expect(await vault.totalSupply()).to.equal(7333n);
|
||||
expect(await vault.totalAssets()).to.equal(11000n);
|
||||
|
||||
// 5. Bruce mints 2000 shares (costs 3001 assets)
|
||||
// NOTE: Bruce's assets spent got rounded towards infinity
|
||||
// NOTE: Alices's vault assets got rounded towards infinity
|
||||
await expect(vault.connect(bruce).mint(2000n, bruce))
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(bruce, vault, 3000n)
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(ethers.ZeroAddress, bruce, 2000n);
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(3333n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(6000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(4999n); // used to be 5000
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(9000n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(9333n);
|
||||
expect(await vault.totalSupply()).to.equal(9333n);
|
||||
expect(await vault.totalAssets()).to.equal(14000n); // used to be 14001
|
||||
|
||||
// 6. Vault mutates by +3000 tokens
|
||||
// NOTE: Vault holds 17001 tokens, but sum of assetsOf() is 17000.
|
||||
await token.$_mint(vault, 3000n);
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(3333n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(6000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(6070n); // used to be 6071
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(10928n); // used to be 10929
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(9333n);
|
||||
expect(await vault.totalSupply()).to.equal(9333n);
|
||||
expect(await vault.totalAssets()).to.equal(17000n); // used to be 17001
|
||||
|
||||
// 7. Alice redeem 1333 shares (2428 assets)
|
||||
await expect(vault.connect(alice).redeem(1333n, alice, alice))
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(alice, ethers.ZeroAddress, 1333n)
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(vault, alice, 2427n); // used to be 2428
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(2000n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(6000n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(3643n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(10929n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(8000n);
|
||||
expect(await vault.totalSupply()).to.equal(8000n);
|
||||
expect(await vault.totalAssets()).to.equal(14573n);
|
||||
|
||||
// 8. Bruce withdraws 2929 assets (1608 shares)
|
||||
await expect(vault.connect(bruce).withdraw(2929n, bruce, bruce))
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(bruce, ethers.ZeroAddress, 1608n)
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(vault, bruce, 2929n);
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(2000n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(4392n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(3643n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(8000n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(6392n);
|
||||
expect(await vault.totalSupply()).to.equal(6392n);
|
||||
expect(await vault.totalAssets()).to.equal(11644n);
|
||||
|
||||
// 9. Alice withdraws 3643 assets (2000 shares)
|
||||
// NOTE: Bruce's assets have been rounded back towards infinity
|
||||
await expect(vault.connect(alice).withdraw(3643n, alice, alice))
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(alice, ethers.ZeroAddress, 2000n)
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(vault, alice, 3643n);
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(0n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(4392n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(0n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(8000n); // used to be 8001
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(4392n);
|
||||
expect(await vault.totalSupply()).to.equal(4392n);
|
||||
expect(await vault.totalAssets()).to.equal(8001n);
|
||||
|
||||
// 10. Bruce redeem 4392 shares (8001 tokens)
|
||||
await expect(vault.connect(bruce).redeem(4392n, bruce, bruce))
|
||||
.to.emit(vault, 'Transfer')
|
||||
.withArgs(bruce, ethers.ZeroAddress, 4392n)
|
||||
.to.emit(token, 'Transfer')
|
||||
.withArgs(vault, bruce, 8000n); // used to be 8001
|
||||
|
||||
expect(await vault.balanceOf(alice)).to.equal(0n);
|
||||
expect(await vault.balanceOf(bruce)).to.equal(0n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(0n);
|
||||
expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(0n);
|
||||
expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(0n);
|
||||
expect(await vault.totalSupply()).to.equal(0n);
|
||||
expect(await vault.totalAssets()).to.equal(1n); // used to be 0
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,427 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const name = 'ERC20Mock';
|
||||
const symbol = 'ERC20Mock';
|
||||
const value = 100n;
|
||||
const data = '0x12345678';
|
||||
|
||||
async function fixture() {
|
||||
const [hasNoCode, owner, receiver, spender, other] = await ethers.getSigners();
|
||||
|
||||
const mock = await ethers.deployContract('$SafeERC20');
|
||||
const erc20ReturnFalseMock = await ethers.deployContract('$ERC20ReturnFalseMock', [name, symbol]);
|
||||
const erc20ReturnTrueMock = await ethers.deployContract('$ERC20', [name, symbol]); // default implementation returns true
|
||||
const erc20NoReturnMock = await ethers.deployContract('$ERC20NoReturnMock', [name, symbol]);
|
||||
const erc20ForceApproveMock = await ethers.deployContract('$ERC20ForceApproveMock', [name, symbol]);
|
||||
const erc1363Mock = await ethers.deployContract('$ERC1363', [name, symbol]);
|
||||
const erc1363ReturnFalseOnErc20Mock = await ethers.deployContract('$ERC1363ReturnFalseOnERC20Mock', [name, symbol]);
|
||||
const erc1363ReturnFalseMock = await ethers.deployContract('$ERC1363ReturnFalseMock', [name, symbol]);
|
||||
const erc1363NoReturnMock = await ethers.deployContract('$ERC1363NoReturnMock', [name, symbol]);
|
||||
const erc1363ForceApproveMock = await ethers.deployContract('$ERC1363ForceApproveMock', [name, symbol]);
|
||||
const erc1363Receiver = await ethers.deployContract('$ERC1363ReceiverMock');
|
||||
const erc1363Spender = await ethers.deployContract('$ERC1363SpenderMock');
|
||||
|
||||
return {
|
||||
hasNoCode,
|
||||
owner,
|
||||
receiver,
|
||||
spender,
|
||||
other,
|
||||
mock,
|
||||
erc20ReturnFalseMock,
|
||||
erc20ReturnTrueMock,
|
||||
erc20NoReturnMock,
|
||||
erc20ForceApproveMock,
|
||||
erc1363Mock,
|
||||
erc1363ReturnFalseOnErc20Mock,
|
||||
erc1363ReturnFalseMock,
|
||||
erc1363NoReturnMock,
|
||||
erc1363ForceApproveMock,
|
||||
erc1363Receiver,
|
||||
erc1363Spender,
|
||||
};
|
||||
}
|
||||
|
||||
describe('SafeERC20', function () {
|
||||
before(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('with address that has no contract code', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.hasNoCode;
|
||||
});
|
||||
|
||||
it('reverts on transfer', async function () {
|
||||
await expect(this.mock.$safeTransfer(this.token, this.receiver, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on transferFrom', async function () {
|
||||
await expect(this.mock.$safeTransferFrom(this.token, this.mock, this.receiver, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on increaseAllowance', async function () {
|
||||
// Call to 'token.allowance' does not return any data, resulting in a decoding error (revert without reason)
|
||||
await expect(this.mock.$safeIncreaseAllowance(this.token, this.spender, 0n)).to.be.revertedWithoutReason();
|
||||
});
|
||||
|
||||
it('reverts on decreaseAllowance', async function () {
|
||||
// Call to 'token.allowance' does not return any data, resulting in a decoding error (revert without reason)
|
||||
await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 0n)).to.be.revertedWithoutReason();
|
||||
});
|
||||
|
||||
it('reverts on forceApprove', async function () {
|
||||
await expect(this.mock.$forceApprove(this.token, this.spender, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with token that returns false on all calls', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc20ReturnFalseMock;
|
||||
});
|
||||
|
||||
it('reverts on transfer', async function () {
|
||||
await expect(this.mock.$safeTransfer(this.token, this.receiver, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on transferFrom', async function () {
|
||||
await expect(this.mock.$safeTransferFrom(this.token, this.mock, this.receiver, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on increaseAllowance', async function () {
|
||||
await expect(this.mock.$safeIncreaseAllowance(this.token, this.spender, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on decreaseAllowance', async function () {
|
||||
await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on forceApprove', async function () {
|
||||
await expect(this.mock.$forceApprove(this.token, this.spender, 0n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with token that returns true on all calls', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc20ReturnTrueMock;
|
||||
});
|
||||
|
||||
shouldOnlyRevertOnErrors();
|
||||
});
|
||||
|
||||
describe('with token that returns no boolean values', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc20NoReturnMock;
|
||||
});
|
||||
|
||||
shouldOnlyRevertOnErrors();
|
||||
});
|
||||
|
||||
describe('with usdt approval behaviour', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc20ForceApproveMock;
|
||||
});
|
||||
|
||||
describe('with initial approval', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_approve(this.mock, this.spender, 100n);
|
||||
});
|
||||
|
||||
it('safeIncreaseAllowance works', async function () {
|
||||
await this.mock.$safeIncreaseAllowance(this.token, this.spender, 10n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(110n);
|
||||
});
|
||||
|
||||
it('safeDecreaseAllowance works', async function () {
|
||||
await this.mock.$safeDecreaseAllowance(this.token, this.spender, 10n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(90n);
|
||||
});
|
||||
|
||||
it('forceApprove works', async function () {
|
||||
await this.mock.$forceApprove(this.token, this.spender, 200n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(200n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with standard ERC1363', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc1363Mock;
|
||||
});
|
||||
|
||||
shouldOnlyRevertOnErrors();
|
||||
|
||||
describe('transferAndCall', function () {
|
||||
it('cannot transferAndCall to an EOA directly', async function () {
|
||||
await this.token.$_mint(this.owner, 100n);
|
||||
|
||||
await expect(this.token.connect(this.owner).transferAndCall(this.receiver, value, ethers.Typed.bytes(data)))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363InvalidReceiver')
|
||||
.withArgs(this.receiver);
|
||||
});
|
||||
|
||||
it('can transferAndCall to an EOA using helper', async function () {
|
||||
await this.token.$_mint(this.mock, value);
|
||||
|
||||
await expect(this.mock.$transferAndCallRelaxed(this.token, this.receiver, value, data))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.mock, this.receiver, value);
|
||||
});
|
||||
|
||||
it('can transferAndCall to an ERC1363Receiver using helper', async function () {
|
||||
await this.token.$_mint(this.mock, value);
|
||||
|
||||
await expect(this.mock.$transferAndCallRelaxed(this.token, this.erc1363Receiver, value, data))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.mock, this.erc1363Receiver, value)
|
||||
.to.emit(this.erc1363Receiver, 'Received')
|
||||
.withArgs(this.mock, this.mock, value, data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transferFromAndCall', function () {
|
||||
it('can transferFromAndCall to an EOA using helper', async function () {
|
||||
await this.token.$_mint(this.owner, value);
|
||||
await this.token.$_approve(this.owner, this.mock, ethers.MaxUint256);
|
||||
|
||||
await expect(this.mock.$transferFromAndCallRelaxed(this.token, this.owner, this.receiver, value, data))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, this.receiver, value);
|
||||
});
|
||||
|
||||
it('can transferFromAndCall to an ERC1363Receiver using helper', async function () {
|
||||
await this.token.$_mint(this.owner, value);
|
||||
await this.token.$_approve(this.owner, this.mock, ethers.MaxUint256);
|
||||
|
||||
await expect(this.mock.$transferFromAndCallRelaxed(this.token, this.owner, this.erc1363Receiver, value, data))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, this.erc1363Receiver, value)
|
||||
.to.emit(this.erc1363Receiver, 'Received')
|
||||
.withArgs(this.mock, this.owner, value, data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('approveAndCall', function () {
|
||||
it('can approveAndCall to an EOA using helper', async function () {
|
||||
await expect(this.mock.$approveAndCallRelaxed(this.token, this.receiver, value, data))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.mock, this.receiver, value);
|
||||
});
|
||||
|
||||
it('can approveAndCall to an ERC1363Spender using helper', async function () {
|
||||
await expect(this.mock.$approveAndCallRelaxed(this.token, this.erc1363Spender, value, data))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.mock, this.erc1363Spender, value)
|
||||
.to.emit(this.erc1363Spender, 'Approved')
|
||||
.withArgs(this.mock, value, data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with ERC1363 that returns false on all ERC20 calls', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc1363ReturnFalseOnErc20Mock;
|
||||
});
|
||||
|
||||
it('reverts on transferAndCallRelaxed', async function () {
|
||||
await expect(this.mock.$transferAndCallRelaxed(this.token, this.erc1363Receiver, 0n, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363TransferFailed')
|
||||
.withArgs(this.erc1363Receiver, 0n);
|
||||
});
|
||||
|
||||
it('reverts on transferFromAndCallRelaxed', async function () {
|
||||
await expect(this.mock.$transferFromAndCallRelaxed(this.token, this.mock, this.erc1363Receiver, 0n, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363TransferFromFailed')
|
||||
.withArgs(this.mock, this.erc1363Receiver, 0n);
|
||||
});
|
||||
|
||||
it('reverts on approveAndCallRelaxed', async function () {
|
||||
await expect(this.mock.$approveAndCallRelaxed(this.token, this.erc1363Spender, 0n, data))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC1363ApproveFailed')
|
||||
.withArgs(this.erc1363Spender, 0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with ERC1363 that returns false on all ERC1363 calls', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc1363ReturnFalseMock;
|
||||
});
|
||||
|
||||
it('reverts on transferAndCallRelaxed', async function () {
|
||||
await expect(this.mock.$transferAndCallRelaxed(this.token, this.erc1363Receiver, 0n, data))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on transferFromAndCallRelaxed', async function () {
|
||||
await expect(this.mock.$transferFromAndCallRelaxed(this.token, this.mock, this.erc1363Receiver, 0n, data))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
|
||||
it('reverts on approveAndCallRelaxed', async function () {
|
||||
await expect(this.mock.$approveAndCallRelaxed(this.token, this.erc1363Spender, 0n, data))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
|
||||
.withArgs(this.token);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with ERC1363 that returns no boolean values', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc1363NoReturnMock;
|
||||
});
|
||||
|
||||
it('reverts on transferAndCallRelaxed', async function () {
|
||||
await expect(
|
||||
this.mock.$transferAndCallRelaxed(this.token, this.erc1363Receiver, 0n, data),
|
||||
).to.be.revertedWithoutReason();
|
||||
});
|
||||
|
||||
it('reverts on transferFromAndCallRelaxed', async function () {
|
||||
await expect(
|
||||
this.mock.$transferFromAndCallRelaxed(this.token, this.mock, this.erc1363Receiver, 0n, data),
|
||||
).to.be.revertedWithoutReason();
|
||||
});
|
||||
|
||||
it('reverts on approveAndCallRelaxed', async function () {
|
||||
await expect(
|
||||
this.mock.$approveAndCallRelaxed(this.token, this.erc1363Spender, 0n, data),
|
||||
).to.be.revertedWithoutReason();
|
||||
});
|
||||
});
|
||||
|
||||
describe('with ERC1363 with usdt approval behaviour', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = this.erc1363ForceApproveMock;
|
||||
});
|
||||
|
||||
describe('without initial approval', function () {
|
||||
it('approveAndCallRelaxed works when recipient is an EOA', async function () {
|
||||
await this.mock.$approveAndCallRelaxed(this.token, this.spender, 10n, data);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(10n);
|
||||
});
|
||||
|
||||
it('approveAndCallRelaxed works when recipient is a contract', async function () {
|
||||
await this.mock.$approveAndCallRelaxed(this.token, this.erc1363Spender, 10n, data);
|
||||
expect(await this.token.allowance(this.mock, this.erc1363Spender)).to.equal(10n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with initial approval', function () {
|
||||
it('approveAndCallRelaxed works when recipient is an EOA', async function () {
|
||||
await this.token.$_approve(this.mock, this.spender, 100n);
|
||||
|
||||
await this.mock.$approveAndCallRelaxed(this.token, this.spender, 10n, data);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(10n);
|
||||
});
|
||||
|
||||
it('approveAndCallRelaxed reverts when recipient is a contract', async function () {
|
||||
await this.token.$_approve(this.mock, this.erc1363Spender, 100n);
|
||||
await expect(this.mock.$approveAndCallRelaxed(this.token, this.erc1363Spender, 10n, data)).to.be.revertedWith(
|
||||
'USDT approval failure',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function shouldOnlyRevertOnErrors() {
|
||||
describe('transfers', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, 100n);
|
||||
await this.token.$_mint(this.mock, 100n);
|
||||
await this.token.$_approve(this.owner, this.mock, ethers.MaxUint256);
|
||||
});
|
||||
|
||||
it("doesn't revert on transfer", async function () {
|
||||
await expect(this.mock.$safeTransfer(this.token, this.receiver, 10n))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.mock, this.receiver, 10n);
|
||||
});
|
||||
|
||||
it("doesn't revert on transferFrom", async function () {
|
||||
await expect(this.mock.$safeTransferFrom(this.token, this.owner, this.receiver, 10n))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.owner, this.receiver, 10n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('approvals', function () {
|
||||
describe('with zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_approve(this.mock, this.spender, 0n);
|
||||
});
|
||||
|
||||
it("doesn't revert when force approving a non-zero allowance", async function () {
|
||||
await this.mock.$forceApprove(this.token, this.spender, 100n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(100n);
|
||||
});
|
||||
|
||||
it("doesn't revert when force approving a zero allowance", async function () {
|
||||
await this.mock.$forceApprove(this.token, this.spender, 0n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(0n);
|
||||
});
|
||||
|
||||
it("doesn't revert when increasing the allowance", async function () {
|
||||
await this.mock.$safeIncreaseAllowance(this.token, this.spender, 10n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(10n);
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance', async function () {
|
||||
await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 10n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedDecreaseAllowance')
|
||||
.withArgs(this.spender, 0n, 10n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with non-zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_approve(this.mock, this.spender, 100n);
|
||||
});
|
||||
|
||||
it("doesn't revert when force approving a non-zero allowance", async function () {
|
||||
await this.mock.$forceApprove(this.token, this.spender, 20n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(20n);
|
||||
});
|
||||
|
||||
it("doesn't revert when force approving a zero allowance", async function () {
|
||||
await this.mock.$forceApprove(this.token, this.spender, 0n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(0n);
|
||||
});
|
||||
|
||||
it("doesn't revert when increasing the allowance", async function () {
|
||||
await this.mock.$safeIncreaseAllowance(this.token, this.spender, 10n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(110n);
|
||||
});
|
||||
|
||||
it("doesn't revert when decreasing the allowance to a positive value", async function () {
|
||||
await this.mock.$safeDecreaseAllowance(this.token, this.spender, 50n);
|
||||
expect(await this.token.allowance(this.mock, this.spender)).to.equal(50n);
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance to a negative value', async function () {
|
||||
await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 200n))
|
||||
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedDecreaseAllowance')
|
||||
.withArgs(this.spender, 100n, 200n);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
972
lib_openzeppelin_contracts/test/token/ERC721/ERC721.behavior.js
Normal file
972
lib_openzeppelin_contracts/test/token/ERC721/ERC721.behavior.js
Normal file
@@ -0,0 +1,972 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||
const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
|
||||
const { RevertType } = require('../../helpers/enums');
|
||||
|
||||
const firstTokenId = 5042n;
|
||||
const secondTokenId = 79217n;
|
||||
const nonExistentTokenId = 13n;
|
||||
const fourthTokenId = 4n;
|
||||
const baseURI = 'https://api.example.com/v1/';
|
||||
|
||||
const RECEIVER_MAGIC_VALUE = '0x150b7a02';
|
||||
|
||||
function shouldBehaveLikeERC721() {
|
||||
beforeEach(async function () {
|
||||
const [owner, newOwner, approved, operator, other] = this.accounts;
|
||||
Object.assign(this, { owner, newOwner, approved, operator, other });
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC721']);
|
||||
|
||||
describe('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, firstTokenId);
|
||||
await this.token.$_mint(this.owner, secondTokenId);
|
||||
this.to = this.other;
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
describe('when the given address owns some tokens', function () {
|
||||
it('returns the amount of tokens owned by the given address', async function () {
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(2n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the given address does not own any tokens', function () {
|
||||
it('returns 0', async function () {
|
||||
expect(await this.token.balanceOf(this.other)).to.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when querying the zero address', function () {
|
||||
it('throws', async function () {
|
||||
await expect(this.token.balanceOf(ethers.ZeroAddress))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidOwner')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ownerOf', function () {
|
||||
describe('when the given token ID was tracked by this token', function () {
|
||||
const tokenId = firstTokenId;
|
||||
|
||||
it('returns the owner of the given token ID', async function () {
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.owner);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the given token ID was not tracked by this token', function () {
|
||||
const tokenId = nonExistentTokenId;
|
||||
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.ownerOf(tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(tokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfers', function () {
|
||||
const tokenId = firstTokenId;
|
||||
const data = '0x42';
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
|
||||
});
|
||||
|
||||
const transferWasSuccessful = () => {
|
||||
it('transfers the ownership of the given token ID to the given address', async function () {
|
||||
await this.tx();
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.to);
|
||||
});
|
||||
|
||||
it('emits a Transfer event', async function () {
|
||||
await expect(this.tx()).to.emit(this.token, 'Transfer').withArgs(this.owner, this.to, tokenId);
|
||||
});
|
||||
|
||||
it('clears the approval for the token ID with no event', async function () {
|
||||
await expect(this.tx()).to.not.emit(this.token, 'Approval');
|
||||
|
||||
expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('adjusts owners balances', async function () {
|
||||
const balanceBefore = await this.token.balanceOf(this.owner);
|
||||
await this.tx();
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore - 1n);
|
||||
});
|
||||
|
||||
it('adjusts owners tokens by index', async function () {
|
||||
if (!this.token.tokenOfOwnerByIndex) return;
|
||||
|
||||
await this.tx();
|
||||
expect(await this.token.tokenOfOwnerByIndex(this.to, 0n)).to.equal(tokenId);
|
||||
expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.not.equal(tokenId);
|
||||
});
|
||||
};
|
||||
|
||||
const shouldTransferTokensByUsers = function (fragment, opts = {}) {
|
||||
describe('when called by the owner', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = () =>
|
||||
this.token.connect(this.owner)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
|
||||
});
|
||||
transferWasSuccessful();
|
||||
});
|
||||
|
||||
describe('when called by the approved individual', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = () =>
|
||||
this.token.connect(this.approved)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
|
||||
});
|
||||
transferWasSuccessful();
|
||||
});
|
||||
|
||||
describe('when called by the operator', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = () =>
|
||||
this.token.connect(this.operator)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
|
||||
});
|
||||
transferWasSuccessful();
|
||||
});
|
||||
|
||||
describe('when called by the owner without an approved user', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(ethers.ZeroAddress, tokenId);
|
||||
this.tx = () =>
|
||||
this.token.connect(this.operator)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
|
||||
});
|
||||
transferWasSuccessful();
|
||||
});
|
||||
|
||||
describe('when sent to the owner', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = () =>
|
||||
this.token.connect(this.owner)[fragment](this.owner, this.owner, tokenId, ...(opts.extra ?? []));
|
||||
});
|
||||
|
||||
it('keeps ownership of the token', async function () {
|
||||
await this.tx();
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(this.owner);
|
||||
});
|
||||
|
||||
it('clears the approval for the token ID', async function () {
|
||||
await this.tx();
|
||||
expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('emits only a transfer event', async function () {
|
||||
await expect(this.tx()).to.emit(this.token, 'Transfer').withArgs(this.owner, this.owner, tokenId);
|
||||
});
|
||||
|
||||
it('keeps the owner balance', async function () {
|
||||
const balanceBefore = await this.token.balanceOf(this.owner);
|
||||
await this.tx();
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore);
|
||||
});
|
||||
|
||||
it('keeps same tokens by index', async function () {
|
||||
if (!this.token.tokenOfOwnerByIndex) return;
|
||||
|
||||
expect(await Promise.all([0n, 1n].map(i => this.token.tokenOfOwnerByIndex(this.owner, i)))).to.have.members(
|
||||
[firstTokenId, secondTokenId],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the address of the previous owner is incorrect', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.owner)[fragment](this.other, this.other, tokenId, ...(opts.extra ?? [])),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
|
||||
.withArgs(this.other, tokenId, this.owner);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender is not authorized for the token id', function () {
|
||||
if (opts.unrestricted) {
|
||||
it('does not revert', async function () {
|
||||
await this.token.connect(this.other)[fragment](this.owner, this.other, tokenId, ...(opts.extra ?? []));
|
||||
});
|
||||
} else {
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.other)[fragment](this.owner, this.other, tokenId, ...(opts.extra ?? [])),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
|
||||
.withArgs(this.other, tokenId);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('when the given token ID does not exist', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.owner)
|
||||
[fragment](this.owner, this.other, nonExistentTokenId, ...(opts.extra ?? [])),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(nonExistentTokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the address to transfer the token to is the zero address', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.owner)[fragment](this.owner, ethers.ZeroAddress, tokenId, ...(opts.extra ?? [])),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const shouldTransferSafely = function (fragment, data, opts = {}) {
|
||||
// sanity
|
||||
it('function exists', async function () {
|
||||
expect(this.token.interface.hasFunction(fragment)).to.be.true;
|
||||
});
|
||||
|
||||
describe('to a user account', function () {
|
||||
shouldTransferTokensByUsers(fragment, opts);
|
||||
});
|
||||
|
||||
describe('to a valid receiver contract', function () {
|
||||
beforeEach(async function () {
|
||||
this.to = await ethers.deployContract('ERC721ReceiverMock', [RECEIVER_MAGIC_VALUE, RevertType.None]);
|
||||
});
|
||||
|
||||
shouldTransferTokensByUsers(fragment, opts);
|
||||
|
||||
it('calls onERC721Received', async function () {
|
||||
await expect(this.token.connect(this.owner)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? [])))
|
||||
.to.emit(this.to, 'Received')
|
||||
.withArgs(this.owner, this.owner, tokenId, data, anyValue);
|
||||
});
|
||||
|
||||
it('calls onERC721Received from approved', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.approved)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? [])),
|
||||
)
|
||||
.to.emit(this.to, 'Received')
|
||||
.withArgs(this.approved, this.owner, tokenId, data, anyValue);
|
||||
});
|
||||
|
||||
describe('with an invalid token id', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(
|
||||
this.token
|
||||
.connect(this.approved)
|
||||
[fragment](this.owner, this.to, nonExistentTokenId, ...(opts.extra ?? [])),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(nonExistentTokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
for (const { fnName, opts } of [
|
||||
{ fnName: 'transferFrom', opts: {} },
|
||||
{ fnName: '$_transfer', opts: { unrestricted: true } },
|
||||
]) {
|
||||
describe(`via ${fnName}`, function () {
|
||||
shouldTransferTokensByUsers(fnName, opts);
|
||||
});
|
||||
}
|
||||
|
||||
for (const { fnName, opts } of [
|
||||
{ fnName: 'safeTransferFrom', opts: {} },
|
||||
{ fnName: '$_safeTransfer', opts: { unrestricted: true } },
|
||||
]) {
|
||||
describe(`via ${fnName}`, function () {
|
||||
describe('with data', function () {
|
||||
shouldTransferSafely(fnName, data, { ...opts, extra: [ethers.Typed.bytes(data)] });
|
||||
});
|
||||
|
||||
describe('without data', function () {
|
||||
shouldTransferSafely(fnName, '0x', opts);
|
||||
});
|
||||
|
||||
describe('to a receiver contract returning unexpected value', function () {
|
||||
it('reverts', async function () {
|
||||
const invalidReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
'0xdeadbeef',
|
||||
RevertType.None,
|
||||
]);
|
||||
|
||||
await expect(this.token.connect(this.owner)[fnName](this.owner, invalidReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(invalidReceiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts with message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.RevertWithMessage,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId),
|
||||
).to.be.revertedWith('ERC721ReceiverMock: reverting');
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts without message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.RevertWithoutMessage,
|
||||
]);
|
||||
|
||||
await expect(this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(revertingReceiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts with custom error', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.RevertWithCustomError,
|
||||
]);
|
||||
|
||||
await expect(this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(revertingReceiver, 'CustomError')
|
||||
.withArgs(RECEIVER_MAGIC_VALUE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that panics', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.Panic,
|
||||
]);
|
||||
|
||||
await expect(
|
||||
this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId),
|
||||
).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a contract that does not implement the required function', function () {
|
||||
it('reverts', async function () {
|
||||
const nonReceiver = await ethers.deployContract('CallReceiverMock');
|
||||
|
||||
await expect(this.token.connect(this.owner)[fnName](this.owner, nonReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(nonReceiver);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('safe mint', function () {
|
||||
const tokenId = fourthTokenId;
|
||||
const data = '0x42';
|
||||
|
||||
describe('via safeMint', function () {
|
||||
// regular minting is tested in ERC721Mintable.test.js and others
|
||||
it('calls onERC721Received — with data', async function () {
|
||||
const receiver = await ethers.deployContract('ERC721ReceiverMock', [RECEIVER_MAGIC_VALUE, RevertType.None]);
|
||||
|
||||
await expect(await this.token.$_safeMint(receiver, tokenId, ethers.Typed.bytes(data)))
|
||||
.to.emit(receiver, 'Received')
|
||||
.withArgs(anyValue, ethers.ZeroAddress, tokenId, data, anyValue);
|
||||
});
|
||||
|
||||
it('calls onERC721Received — without data', async function () {
|
||||
const receiver = await ethers.deployContract('ERC721ReceiverMock', [RECEIVER_MAGIC_VALUE, RevertType.None]);
|
||||
|
||||
await expect(await this.token.$_safeMint(receiver, tokenId))
|
||||
.to.emit(receiver, 'Received')
|
||||
.withArgs(anyValue, ethers.ZeroAddress, tokenId, '0x', anyValue);
|
||||
});
|
||||
|
||||
describe('to a receiver contract returning unexpected value', function () {
|
||||
it('reverts', async function () {
|
||||
const invalidReceiver = await ethers.deployContract('ERC721ReceiverMock', ['0xdeadbeef', RevertType.None]);
|
||||
|
||||
await expect(this.token.$_safeMint(invalidReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(invalidReceiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts with message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.RevertWithMessage,
|
||||
]);
|
||||
|
||||
await expect(this.token.$_safeMint(revertingReceiver, tokenId)).to.be.revertedWith(
|
||||
'ERC721ReceiverMock: reverting',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts without message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.RevertWithoutMessage,
|
||||
]);
|
||||
|
||||
await expect(this.token.$_safeMint(revertingReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(revertingReceiver);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts with custom error', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.RevertWithCustomError,
|
||||
]);
|
||||
|
||||
await expect(this.token.$_safeMint(revertingReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(revertingReceiver, 'CustomError')
|
||||
.withArgs(RECEIVER_MAGIC_VALUE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that panics', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
|
||||
RECEIVER_MAGIC_VALUE,
|
||||
RevertType.Panic,
|
||||
]);
|
||||
|
||||
await expect(this.token.$_safeMint(revertingReceiver, tokenId)).to.be.revertedWithPanic(
|
||||
PANIC_CODES.DIVISION_BY_ZERO,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a contract that does not implement the required function', function () {
|
||||
it('reverts', async function () {
|
||||
const nonReceiver = await ethers.deployContract('CallReceiverMock');
|
||||
|
||||
await expect(this.token.$_safeMint(nonReceiver, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(nonReceiver);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('approve', function () {
|
||||
const tokenId = firstTokenId;
|
||||
|
||||
const itClearsApproval = function () {
|
||||
it('clears approval for the token', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
};
|
||||
|
||||
const itApproves = function () {
|
||||
it('sets the approval for the target address', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.equal(this.approved ?? this.approved);
|
||||
});
|
||||
};
|
||||
|
||||
const itEmitsApprovalEvent = function () {
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.owner, this.approved ?? this.approved, tokenId);
|
||||
});
|
||||
};
|
||||
|
||||
describe('when clearing approval', function () {
|
||||
describe('when there was no prior approval', function () {
|
||||
beforeEach(async function () {
|
||||
this.approved = ethers.ZeroAddress;
|
||||
this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
});
|
||||
|
||||
itClearsApproval();
|
||||
itEmitsApprovalEvent();
|
||||
});
|
||||
|
||||
describe('when there was a prior approval', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.other, tokenId);
|
||||
this.approved = ethers.ZeroAddress;
|
||||
this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
});
|
||||
|
||||
itClearsApproval();
|
||||
itEmitsApprovalEvent();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when approving a non-zero address', function () {
|
||||
describe('when there was no prior approval', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
});
|
||||
|
||||
itApproves();
|
||||
itEmitsApprovalEvent();
|
||||
});
|
||||
|
||||
describe('when there was a prior approval to the same address', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
});
|
||||
|
||||
itApproves();
|
||||
itEmitsApprovalEvent();
|
||||
});
|
||||
|
||||
describe('when there was a prior approval to a different address', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.other, tokenId);
|
||||
this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
});
|
||||
|
||||
itApproves();
|
||||
itEmitsApprovalEvent();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender does not own the given token ID', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.connect(this.other).approve(this.approved, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidApprover')
|
||||
.withArgs(this.other);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender is approved for the given token ID', function () {
|
||||
it('reverts', async function () {
|
||||
await this.token.connect(this.owner).approve(this.approved, tokenId);
|
||||
|
||||
await expect(this.token.connect(this.approved).approve(this.other, tokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidApprover')
|
||||
.withArgs(this.approved);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender is an operator', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
|
||||
|
||||
this.tx = await this.token.connect(this.operator).approve(this.approved, tokenId);
|
||||
});
|
||||
|
||||
itApproves();
|
||||
itEmitsApprovalEvent();
|
||||
});
|
||||
|
||||
describe('when the given token ID does not exist', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.connect(this.operator).approve(this.approved, nonExistentTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(nonExistentTokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setApprovalForAll', function () {
|
||||
describe('when the operator willing to approve is not the owner', function () {
|
||||
describe('when there is no operator approval set by the sender', function () {
|
||||
it('approves the operator', async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
|
||||
|
||||
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.true;
|
||||
});
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.token.connect(this.owner).setApprovalForAll(this.operator, true))
|
||||
.to.emit(this.token, 'ApprovalForAll')
|
||||
.withArgs(this.owner, this.operator, true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the operator was set as not approved', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, false);
|
||||
});
|
||||
|
||||
it('approves the operator', async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
|
||||
|
||||
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.true;
|
||||
});
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.token.connect(this.owner).setApprovalForAll(this.operator, true))
|
||||
.to.emit(this.token, 'ApprovalForAll')
|
||||
.withArgs(this.owner, this.operator, true);
|
||||
});
|
||||
|
||||
it('can unset the operator approval', async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, false);
|
||||
|
||||
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the operator was already approved', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
|
||||
});
|
||||
|
||||
it('keeps the approval to the given address', async function () {
|
||||
await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
|
||||
|
||||
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.true;
|
||||
});
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
await expect(this.token.connect(this.owner).setApprovalForAll(this.operator, true))
|
||||
.to.emit(this.token, 'ApprovalForAll')
|
||||
.withArgs(this.owner, this.operator, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the operator is address zero', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.connect(this.owner).setApprovalForAll(ethers.ZeroAddress, true))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidOperator')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getApproved', function () {
|
||||
describe('when token is not minted', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.getApproved(nonExistentTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(nonExistentTokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when token has been minted ', function () {
|
||||
it('should return the zero address', async function () {
|
||||
expect(await this.token.getApproved(firstTokenId)).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('when account has been approved', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).approve(this.approved, firstTokenId);
|
||||
});
|
||||
|
||||
it('returns approved account', async function () {
|
||||
expect(await this.token.getApproved(firstTokenId)).to.equal(this.approved);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_mint(address, uint256)', function () {
|
||||
it('reverts with a null destination address', async function () {
|
||||
await expect(this.token.$_mint(ethers.ZeroAddress, firstTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('with minted token', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.$_mint(this.owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('emits a Transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('creates the token', async function () {
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(1n);
|
||||
expect(await this.token.ownerOf(firstTokenId)).to.equal(this.owner);
|
||||
});
|
||||
|
||||
it('reverts when adding a token id that already exists', async function () {
|
||||
await expect(this.token.$_mint(this.owner, firstTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it('reverts when burning a non-existent token id', async function () {
|
||||
await expect(this.token.$_burn(nonExistentTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(nonExistentTokenId);
|
||||
});
|
||||
|
||||
describe('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, firstTokenId);
|
||||
await this.token.$_mint(this.owner, secondTokenId);
|
||||
});
|
||||
|
||||
describe('with burnt token', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.$_burn(firstTokenId);
|
||||
});
|
||||
|
||||
it('emits a Transfer event', async function () {
|
||||
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.owner, ethers.ZeroAddress, firstTokenId);
|
||||
});
|
||||
|
||||
it('deletes the token', async function () {
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(1n);
|
||||
await expect(this.token.ownerOf(firstTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(firstTokenId);
|
||||
});
|
||||
|
||||
it('reverts when burning a token id that has been deleted', async function () {
|
||||
await expect(this.token.$_burn(firstTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(firstTokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function shouldBehaveLikeERC721Enumerable() {
|
||||
beforeEach(async function () {
|
||||
const [owner, newOwner, approved, operator, other] = this.accounts;
|
||||
Object.assign(this, { owner, newOwner, approved, operator, other });
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC721Enumerable']);
|
||||
|
||||
describe('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, firstTokenId);
|
||||
await this.token.$_mint(this.owner, secondTokenId);
|
||||
this.to = this.other;
|
||||
});
|
||||
|
||||
describe('totalSupply', function () {
|
||||
it('returns total token supply', async function () {
|
||||
expect(await this.token.totalSupply()).to.equal(2n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokenOfOwnerByIndex', function () {
|
||||
describe('when the given index is lower than the amount of tokens owned by the given address', function () {
|
||||
it('returns the token ID placed at the given index', async function () {
|
||||
expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.equal(firstTokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.tokenOfOwnerByIndex(this.owner, 2n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
|
||||
.withArgs(this.owner, 2n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the given address does not own any token', function () {
|
||||
it('reverts', async function () {
|
||||
await expect(this.token.tokenOfOwnerByIndex(this.other, 0n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
|
||||
.withArgs(this.other, 0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('after transferring all tokens to another user', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.owner).transferFrom(this.owner, this.other, firstTokenId);
|
||||
await this.token.connect(this.owner).transferFrom(this.owner, this.other, secondTokenId);
|
||||
});
|
||||
|
||||
it('returns correct token IDs for target', async function () {
|
||||
expect(await this.token.balanceOf(this.other)).to.equal(2n);
|
||||
|
||||
expect(await Promise.all([0n, 1n].map(i => this.token.tokenOfOwnerByIndex(this.other, i)))).to.have.members([
|
||||
firstTokenId,
|
||||
secondTokenId,
|
||||
]);
|
||||
});
|
||||
|
||||
it('returns empty collection for original owner', async function () {
|
||||
expect(await this.token.balanceOf(this.owner)).to.equal(0n);
|
||||
await expect(this.token.tokenOfOwnerByIndex(this.owner, 0n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
|
||||
.withArgs(this.owner, 0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokenByIndex', function () {
|
||||
it('returns all tokens', async function () {
|
||||
expect(await Promise.all([0n, 1n].map(i => this.token.tokenByIndex(i)))).to.have.members([
|
||||
firstTokenId,
|
||||
secondTokenId,
|
||||
]);
|
||||
});
|
||||
|
||||
it('reverts if index is greater than supply', async function () {
|
||||
await expect(this.token.tokenByIndex(2n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
|
||||
.withArgs(ethers.ZeroAddress, 2n);
|
||||
});
|
||||
|
||||
for (const tokenId of [firstTokenId, secondTokenId]) {
|
||||
it(`returns all tokens after burning token ${tokenId} and minting new tokens`, async function () {
|
||||
const newTokenId = 300n;
|
||||
const anotherNewTokenId = 400n;
|
||||
|
||||
await this.token.$_burn(tokenId);
|
||||
await this.token.$_mint(this.newOwner, newTokenId);
|
||||
await this.token.$_mint(this.newOwner, anotherNewTokenId);
|
||||
|
||||
expect(await this.token.totalSupply()).to.equal(3n);
|
||||
|
||||
expect(await Promise.all([0n, 1n, 2n].map(i => this.token.tokenByIndex(i))))
|
||||
.to.have.members([firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(x => x !== tokenId))
|
||||
.to.not.include(tokenId);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('_mint(address, uint256)', function () {
|
||||
it('reverts with a null destination address', async function () {
|
||||
await expect(this.token.$_mint(ethers.ZeroAddress, firstTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('with minted token', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('adjusts owner tokens by index', async function () {
|
||||
expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.equal(firstTokenId);
|
||||
});
|
||||
|
||||
it('adjusts all tokens list', async function () {
|
||||
expect(await this.token.tokenByIndex(0n)).to.equal(firstTokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it('reverts when burning a non-existent token id', async function () {
|
||||
await expect(this.token.$_burn(firstTokenId))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
|
||||
.withArgs(firstTokenId);
|
||||
});
|
||||
|
||||
describe('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, firstTokenId);
|
||||
await this.token.$_mint(this.owner, secondTokenId);
|
||||
});
|
||||
|
||||
describe('with burnt token', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_burn(firstTokenId);
|
||||
});
|
||||
|
||||
it('removes that token from the token list of the owner', async function () {
|
||||
expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.equal(secondTokenId);
|
||||
});
|
||||
|
||||
it('adjusts all tokens list', async function () {
|
||||
expect(await this.token.tokenByIndex(0n)).to.equal(secondTokenId);
|
||||
});
|
||||
|
||||
it('burns all tokens', async function () {
|
||||
await this.token.$_burn(secondTokenId);
|
||||
expect(await this.token.totalSupply()).to.equal(0n);
|
||||
|
||||
await expect(this.token.tokenByIndex(0n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
|
||||
.withArgs(ethers.ZeroAddress, 0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function shouldBehaveLikeERC721Metadata(name, symbol) {
|
||||
shouldSupportInterfaces(['ERC721Metadata']);
|
||||
|
||||
describe('metadata', function () {
|
||||
it('has a name', async function () {
|
||||
expect(await this.token.name()).to.equal(name);
|
||||
});
|
||||
|
||||
it('has a symbol', async function () {
|
||||
expect(await this.token.symbol()).to.equal(symbol);
|
||||
});
|
||||
|
||||
describe('token URI', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('return empty string by default', async function () {
|
||||
expect(await this.token.tokenURI(firstTokenId)).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);
|
||||
});
|
||||
|
||||
describe('base URI', function () {
|
||||
beforeEach(function () {
|
||||
if (!this.token.interface.hasFunction('setBaseURI')) {
|
||||
this.skip();
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.equal(baseURI + firstTokenId.toString());
|
||||
});
|
||||
|
||||
it('token URI can be changed by changing the base URI', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
const newBaseURI = 'https://api.example.com/v2/';
|
||||
await this.token.setBaseURI(newBaseURI);
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.equal(newBaseURI + firstTokenId.toString());
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeERC721,
|
||||
shouldBehaveLikeERC721Enumerable,
|
||||
shouldBehaveLikeERC721Metadata,
|
||||
};
|
||||
23
lib_openzeppelin_contracts/test/token/ERC721/ERC721.test.js
Normal file
23
lib_openzeppelin_contracts/test/token/ERC721/ERC721.test.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC721, shouldBehaveLikeERC721Metadata } = require('./ERC721.behavior');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
async function fixture() {
|
||||
return {
|
||||
accounts: await ethers.getSigners(),
|
||||
token: await ethers.deployContract('$ERC721', [name, symbol]),
|
||||
};
|
||||
}
|
||||
|
||||
describe('ERC721', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC721();
|
||||
shouldBehaveLikeERC721Metadata(name, symbol);
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const {
|
||||
shouldBehaveLikeERC721,
|
||||
shouldBehaveLikeERC721Metadata,
|
||||
shouldBehaveLikeERC721Enumerable,
|
||||
} = require('./ERC721.behavior');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
async function fixture() {
|
||||
return {
|
||||
accounts: await ethers.getSigners(),
|
||||
token: await ethers.deployContract('$ERC721Enumerable', [name, symbol]),
|
||||
};
|
||||
}
|
||||
|
||||
describe('ERC721', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC721();
|
||||
shouldBehaveLikeERC721Metadata(name, symbol);
|
||||
shouldBehaveLikeERC721Enumerable();
|
||||
});
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
const tokenId = 1n;
|
||||
|
||||
describe('ERC721Holder', function () {
|
||||
it('receives an ERC721 token', async function () {
|
||||
const [owner] = await ethers.getSigners();
|
||||
|
||||
const token = await ethers.deployContract('$ERC721', [name, symbol]);
|
||||
await token.$_mint(owner, tokenId);
|
||||
|
||||
const receiver = await ethers.deployContract('$ERC721Holder');
|
||||
await token.connect(owner).safeTransferFrom(owner, receiver, tokenId);
|
||||
|
||||
expect(await token.ownerOf(tokenId)).to.equal(receiver);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { RevertType } = require('../../../helpers/enums');
|
||||
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||
|
||||
const tokenId = 1n;
|
||||
|
||||
const RECEIVER_MAGIC_VALUE = '0x150b7a02';
|
||||
|
||||
const deployReceiver = (revertType, returnValue = RECEIVER_MAGIC_VALUE) =>
|
||||
ethers.deployContract('$ERC721ReceiverMock', [returnValue, revertType]);
|
||||
|
||||
const fixture = async () => {
|
||||
const [eoa, operator, owner] = await ethers.getSigners();
|
||||
const utils = await ethers.deployContract('$ERC721Utils');
|
||||
|
||||
const receivers = {
|
||||
correct: await deployReceiver(RevertType.None),
|
||||
invalid: await deployReceiver(RevertType.None, '0xdeadbeef'),
|
||||
message: await deployReceiver(RevertType.RevertWithMessage),
|
||||
empty: await deployReceiver(RevertType.RevertWithoutMessage),
|
||||
customError: await deployReceiver(RevertType.RevertWithCustomError),
|
||||
panic: await deployReceiver(RevertType.Panic),
|
||||
nonReceiver: await ethers.deployContract('CallReceiverMock'),
|
||||
eoa,
|
||||
};
|
||||
|
||||
return { operator, owner, utils, receivers };
|
||||
};
|
||||
|
||||
describe('ERC721Utils', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('onERC721Received', function () {
|
||||
it('succeeds when called by an EOA', async function () {
|
||||
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.eoa, tokenId, '0x')).to
|
||||
.not.be.reverted;
|
||||
});
|
||||
|
||||
it('succeeds when data is passed', async function () {
|
||||
const data = '0x12345678';
|
||||
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.correct, tokenId, data))
|
||||
.to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('succeeds when data is empty', async function () {
|
||||
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.correct, tokenId, '0x'))
|
||||
.to.not.be.reverted;
|
||||
});
|
||||
|
||||
it('reverts when receiver returns invalid value', async function () {
|
||||
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.invalid, tokenId, '0x'))
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
|
||||
.withArgs(this.receivers.invalid);
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts with message', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.message, tokenId, '0x'),
|
||||
).to.be.revertedWith('ERC721ReceiverMock: reverting');
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts without message', async function () {
|
||||
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.empty, tokenId, '0x'))
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
|
||||
.withArgs(this.receivers.empty);
|
||||
});
|
||||
|
||||
it('reverts when receiver reverts with custom error', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.customError, tokenId, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
|
||||
.withArgs(RECEIVER_MAGIC_VALUE);
|
||||
});
|
||||
|
||||
it('reverts when receiver panics', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.panic, tokenId, '0x'),
|
||||
).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
|
||||
});
|
||||
|
||||
it('reverts when receiver does not implement onERC721Received', async function () {
|
||||
await expect(
|
||||
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.nonReceiver, tokenId, '0x'),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
|
||||
.withArgs(this.receivers.nonReceiver);
|
||||
});
|
||||
});
|
||||
});
|
||||
152
lib_openzeppelin_contracts/test/token/common/ERC2981.behavior.js
Normal file
152
lib_openzeppelin_contracts/test/token/common/ERC2981.behavior.js
Normal file
@@ -0,0 +1,152 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
function shouldBehaveLikeERC2981() {
|
||||
const royaltyFraction = 10n;
|
||||
|
||||
shouldSupportInterfaces(['ERC2981']);
|
||||
|
||||
describe('default royalty', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_setDefaultRoyalty(this.account1, royaltyFraction);
|
||||
});
|
||||
|
||||
it('checks royalty is set', async function () {
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
|
||||
this.account1.address,
|
||||
(this.salePrice * royaltyFraction) / 10_000n,
|
||||
]);
|
||||
});
|
||||
|
||||
it('updates royalty amount', async function () {
|
||||
const newFraction = 25n;
|
||||
|
||||
await this.token.$_setDefaultRoyalty(this.account1, newFraction);
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
|
||||
this.account1.address,
|
||||
(this.salePrice * newFraction) / 10_000n,
|
||||
]);
|
||||
});
|
||||
|
||||
it('holds same royalty value for different tokens', async function () {
|
||||
const newFraction = 20n;
|
||||
|
||||
await this.token.$_setDefaultRoyalty(this.account1, newFraction);
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal(
|
||||
await this.token.royaltyInfo(this.tokenId2, this.salePrice),
|
||||
);
|
||||
});
|
||||
|
||||
it('Remove royalty information', async function () {
|
||||
const newValue = 0n;
|
||||
await this.token.$_deleteDefaultRoyalty();
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([ethers.ZeroAddress, newValue]);
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([ethers.ZeroAddress, newValue]);
|
||||
});
|
||||
|
||||
it('reverts if invalid parameters', async function () {
|
||||
const royaltyDenominator = await this.token.$_feeDenominator();
|
||||
|
||||
await expect(this.token.$_setDefaultRoyalty(ethers.ZeroAddress, royaltyFraction))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidDefaultRoyaltyReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
|
||||
const anotherRoyaltyFraction = 11000n;
|
||||
|
||||
await expect(this.token.$_setDefaultRoyalty(this.account1, anotherRoyaltyFraction))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidDefaultRoyalty')
|
||||
.withArgs(anotherRoyaltyFraction, royaltyDenominator);
|
||||
});
|
||||
});
|
||||
|
||||
describe('token based royalty', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, royaltyFraction);
|
||||
});
|
||||
|
||||
it('updates royalty amount', async function () {
|
||||
const newFraction = 25n;
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
|
||||
this.account1.address,
|
||||
(this.salePrice * royaltyFraction) / 10_000n,
|
||||
]);
|
||||
|
||||
await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, newFraction);
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
|
||||
this.account1.address,
|
||||
(this.salePrice * newFraction) / 10_000n,
|
||||
]);
|
||||
});
|
||||
|
||||
it('holds different values for different tokens', async function () {
|
||||
const newFraction = 20n;
|
||||
|
||||
await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, newFraction);
|
||||
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.not.deep.equal(
|
||||
await this.token.royaltyInfo(this.tokenId2, this.salePrice),
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts if invalid parameters', async function () {
|
||||
const royaltyDenominator = await this.token.$_feeDenominator();
|
||||
|
||||
await expect(this.token.$_setTokenRoyalty(this.tokenId1, ethers.ZeroAddress, royaltyFraction))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidTokenRoyaltyReceiver')
|
||||
.withArgs(this.tokenId1, ethers.ZeroAddress);
|
||||
|
||||
const anotherRoyaltyFraction = 11000n;
|
||||
|
||||
await expect(this.token.$_setTokenRoyalty(this.tokenId1, this.account1, anotherRoyaltyFraction))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidTokenRoyalty')
|
||||
.withArgs(this.tokenId1, anotherRoyaltyFraction, royaltyDenominator);
|
||||
});
|
||||
|
||||
it('can reset token after setting royalty', async function () {
|
||||
const newFraction = 30n;
|
||||
|
||||
await this.token.$_setTokenRoyalty(this.tokenId1, this.account2, newFraction);
|
||||
|
||||
// Tokens must have own information
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
|
||||
this.account2.address,
|
||||
(this.salePrice * newFraction) / 10_000n,
|
||||
]);
|
||||
|
||||
await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, 0n);
|
||||
|
||||
// Token must not share default information
|
||||
expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([this.account1.address, 0n]);
|
||||
});
|
||||
|
||||
it('can hold default and token royalty information', async function () {
|
||||
const newFraction = 30n;
|
||||
|
||||
await this.token.$_setTokenRoyalty(this.tokenId2, this.account2, newFraction);
|
||||
|
||||
// Tokens must not have same values
|
||||
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.not.deep.equal([
|
||||
this.account2.address,
|
||||
(this.salePrice * newFraction) / 10_000n,
|
||||
]);
|
||||
|
||||
// Updated token must have new values
|
||||
expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([
|
||||
this.account2.address,
|
||||
(this.salePrice * newFraction) / 10_000n,
|
||||
]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeERC2981,
|
||||
};
|
||||
Reference in New Issue
Block a user