|
@@ -4,17 +4,21 @@ 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.anotherAccount)).to.equal(0n);
|
|
|
+ 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.initialHolder)).to.equal(initialSupply);
|
|
|
+ expect(await this.token.balanceOf(this.holder)).to.equal(initialSupply);
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -31,34 +35,26 @@ function shouldBehaveLikeERC20(initialSupply, opts = {}) {
|
|
|
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.initialHolder).approve(this.recipient, initialSupply);
|
|
|
+ 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.initialHolder, this.anotherAccount, value);
|
|
|
+ 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.initialHolder, this.anotherAccount],
|
|
|
- [-value, value],
|
|
|
- );
|
|
|
+ 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.initialHolder, this.recipient)).to.equal(0n);
|
|
|
+ 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.initialHolder, this.anotherAccount, value);
|
|
|
+ await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.other, value);
|
|
|
});
|
|
|
|
|
|
if (forcedApproval) {
|
|
@@ -66,9 +62,9 @@ function shouldBehaveLikeERC20(initialSupply, opts = {}) {
|
|
|
await expect(this.tx)
|
|
|
.to.emit(this.token, 'Approval')
|
|
|
.withArgs(
|
|
|
- this.initialHolder.address,
|
|
|
+ this.holder.address,
|
|
|
this.recipient.address,
|
|
|
- await this.token.allowance(this.initialHolder, this.recipient),
|
|
|
+ await this.token.allowance(this.holder, this.recipient),
|
|
|
);
|
|
|
});
|
|
|
} else {
|
|
@@ -80,12 +76,10 @@ function shouldBehaveLikeERC20(initialSupply, opts = {}) {
|
|
|
|
|
|
it('reverts when the token owner does not have enough balance', async function () {
|
|
|
const value = initialSupply;
|
|
|
- await this.token.connect(this.initialHolder).transfer(this.anotherAccount, 1n);
|
|
|
- await expect(
|
|
|
- this.token.connect(this.recipient).transferFrom(this.initialHolder, this.anotherAccount, value),
|
|
|
- )
|
|
|
+ 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.initialHolder, value - 1n, value);
|
|
|
+ .withArgs(this.holder, value - 1n, value);
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -93,39 +87,33 @@ function shouldBehaveLikeERC20(initialSupply, opts = {}) {
|
|
|
const allowance = initialSupply - 1n;
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
- await this.token.connect(this.initialHolder).approve(this.recipient, allowance);
|
|
|
+ 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.initialHolder, this.anotherAccount, value),
|
|
|
- )
|
|
|
+ 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.initialHolder).transfer(this.anotherAccount, 2);
|
|
|
- await expect(
|
|
|
- this.token.connect(this.recipient).transferFrom(this.initialHolder, this.anotherAccount, value),
|
|
|
- )
|
|
|
+ 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.initialHolder, value - 1n, value);
|
|
|
+ .withArgs(this.holder, value - 1n, value);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('when the spender has unlimited allowance', function () {
|
|
|
beforeEach(async function () {
|
|
|
- await this.token.connect(this.initialHolder).approve(this.recipient, ethers.MaxUint256);
|
|
|
- this.tx = await this.token
|
|
|
- .connect(this.recipient)
|
|
|
- .transferFrom(this.initialHolder, this.anotherAccount, 1n);
|
|
|
+ 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.initialHolder, this.recipient)).to.equal(ethers.MaxUint256);
|
|
|
+ expect(await this.token.allowance(this.holder, this.recipient)).to.equal(ethers.MaxUint256);
|
|
|
});
|
|
|
|
|
|
it('does not emit an approval event', async function () {
|
|
@@ -136,8 +124,8 @@ function shouldBehaveLikeERC20(initialSupply, opts = {}) {
|
|
|
|
|
|
it('reverts when the recipient is the zero address', async function () {
|
|
|
const value = initialSupply;
|
|
|
- await this.token.connect(this.initialHolder).approve(this.recipient, value);
|
|
|
- await expect(this.token.connect(this.recipient).transferFrom(this.initialHolder, ethers.ZeroAddress, value))
|
|
|
+ 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);
|
|
|
});
|
|
@@ -164,24 +152,24 @@ 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.initialHolder, this.recipient, value))
|
|
|
+ await expect(this.transfer(this.holder, this.recipient, value))
|
|
|
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
|
|
|
- .withArgs(this.initialHolder, balance, value);
|
|
|
+ .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.initialHolder, this.recipient, value);
|
|
|
+ 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.initialHolder, this.recipient], [-value, value]);
|
|
|
+ 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.initialHolder, this.recipient, value);
|
|
|
+ await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.recipient, value);
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -189,21 +177,21 @@ function shouldBehaveLikeERC20Transfer(balance) {
|
|
|
const value = 0n;
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
- this.tx = await this.transfer(this.initialHolder, this.recipient, value);
|
|
|
+ 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.initialHolder, this.recipient], [0n, 0n]);
|
|
|
+ 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.initialHolder, this.recipient, value);
|
|
|
+ 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.initialHolder, ethers.ZeroAddress, balance))
|
|
|
+ await expect(this.transfer(this.holder, ethers.ZeroAddress, balance))
|
|
|
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
|
|
|
.withArgs(ethers.ZeroAddress);
|
|
|
});
|
|
@@ -215,22 +203,22 @@ function shouldBehaveLikeERC20Approve(supply) {
|
|
|
const value = supply;
|
|
|
|
|
|
it('emits an approval event', async function () {
|
|
|
- await expect(this.approve(this.initialHolder, this.recipient, value))
|
|
|
+ await expect(this.approve(this.holder, this.recipient, value))
|
|
|
.to.emit(this.token, 'Approval')
|
|
|
- .withArgs(this.initialHolder, this.recipient, value);
|
|
|
+ .withArgs(this.holder, this.recipient, value);
|
|
|
});
|
|
|
|
|
|
it('approves the requested value when there was no approved value before', async function () {
|
|
|
- await this.approve(this.initialHolder, this.recipient, value);
|
|
|
+ await this.approve(this.holder, this.recipient, value);
|
|
|
|
|
|
- expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(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.initialHolder, this.recipient, 1n);
|
|
|
- await this.approve(this.initialHolder, this.recipient, value);
|
|
|
+ await this.approve(this.holder, this.recipient, 1n);
|
|
|
+ await this.approve(this.holder, this.recipient, value);
|
|
|
|
|
|
- expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(value);
|
|
|
+ expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -238,28 +226,28 @@ function shouldBehaveLikeERC20Approve(supply) {
|
|
|
const value = supply + 1n;
|
|
|
|
|
|
it('emits an approval event', async function () {
|
|
|
- await expect(this.approve(this.initialHolder, this.recipient, value))
|
|
|
+ await expect(this.approve(this.holder, this.recipient, value))
|
|
|
.to.emit(this.token, 'Approval')
|
|
|
- .withArgs(this.initialHolder, this.recipient, value);
|
|
|
+ .withArgs(this.holder, this.recipient, value);
|
|
|
});
|
|
|
|
|
|
it('approves the requested value when there was no approved value before', async function () {
|
|
|
- await this.approve(this.initialHolder, this.recipient, value);
|
|
|
+ await this.approve(this.holder, this.recipient, value);
|
|
|
|
|
|
- expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(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.initialHolder, this.recipient, 1n);
|
|
|
- await this.approve(this.initialHolder, this.recipient, value);
|
|
|
+ await this.approve(this.holder, this.recipient, 1n);
|
|
|
+ await this.approve(this.holder, this.recipient, value);
|
|
|
|
|
|
- expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(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.initialHolder, ethers.ZeroAddress, supply))
|
|
|
+ await expect(this.approve(this.holder, ethers.ZeroAddress, supply))
|
|
|
.to.be.revertedWithCustomError(this.token, `ERC20InvalidSpender`)
|
|
|
.withArgs(ethers.ZeroAddress);
|
|
|
});
|