This commit is contained in:
dexorder
2024-10-17 02:42:28 -04:00
commit 25def69c66
878 changed files with 112489 additions and 0 deletions

View 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,
};

View 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);
});
});
});
}
});

View File

@@ -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);
});
});
});

View File

@@ -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);
});
});
}
});
});
});

View File

@@ -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);
});
});
});

View File

@@ -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);
});
});
});
});

View File

@@ -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');
});
});
});
});

View File

@@ -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);
});
});
});

View File

@@ -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);
});
});
});
}
});

View File

@@ -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);
});
});

View File

@@ -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();
}
}

View File

@@ -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
});
});

View File

@@ -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);
});
});
});
}