Skip to content

Commit

Permalink
Migrate ERC1155 tests to ethers v6 (#4771)
Browse files Browse the repository at this point in the history
Co-authored-by: ernestognw <ernestognw@gmail.com>
  • Loading branch information
Amxx and ernestognw authored Dec 21, 2023
1 parent e70a011 commit be0572a
Show file tree
Hide file tree
Showing 13 changed files with 832 additions and 980 deletions.
5 changes: 4 additions & 1 deletion scripts/generate/templates/EnumerableMap.opts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const mapType = str => (str == 'uint256' ? 'Uint' : `${str.charAt(0).toUpperCase()}${str.slice(1)}`);
const { capitalize } = require('../../helpers');

const mapType = str => (str == 'uint256' ? 'Uint' : capitalize(str));

const formatType = (keyType, valueType) => ({
name: `${mapType(keyType)}To${mapType(valueType)}Map`,
keyType,
Expand Down
4 changes: 3 additions & 1 deletion scripts/generate/templates/EnumerableSet.opts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const mapType = str => (str == 'uint256' ? 'Uint' : `${str.charAt(0).toUpperCase()}${str.slice(1)}`);
const { capitalize } = require('../../helpers');

const mapType = str => (str == 'uint256' ? 'Uint' : capitalize(str));

const formatType = type => ({
name: `${mapType(type)}Set`,
Expand Down
2 changes: 1 addition & 1 deletion test/access/Ownable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Ownable', function () {
});

it('emits ownership transfer events during construction', async function () {
await expect(await this.ownable.deploymentTransaction())
await expect(this.ownable.deploymentTransaction())
.to.emit(this.ownable, 'OwnershipTransferred')
.withArgs(ethers.ZeroAddress, this.owner.address);
});
Expand Down
2 changes: 1 addition & 1 deletion test/access/manager/AccessManaged.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('AccessManaged', function () {
});

it('sets authority and emits AuthorityUpdated event during construction', async function () {
await expect(await this.managed.deploymentTransaction())
await expect(this.managed.deploymentTransaction())
.to.emit(this.managed, 'AuthorityUpdated')
.withArgs(this.authority.target);
});
Expand Down
1 change: 1 addition & 0 deletions test/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function createExport(Enum) {
VoteType: Enum('Against', 'For', 'Abstain'),
Rounding: Enum('Floor', 'Ceil', 'Trunc', 'Expand'),
OperationState: Enum('Unset', 'Waiting', 'Ready', 'Done'),
RevertType: Enum('None', 'RevertWithoutMessage', 'RevertWithMessage', 'RevertWithCustomError', 'Panic'),
};
}

Expand Down
1,119 changes: 510 additions & 609 deletions test/token/ERC1155/ERC1155.behavior.js

Large diffs are not rendered by default.

231 changes: 96 additions & 135 deletions test/token/ERC1155/ERC1155.test.js
Original file line number Diff line number Diff line change
@@ -1,251 +1,212 @@
const { BN, constants, expectEvent } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;

const { ethers } = require('hardhat');
const { expect } = require('chai');

const { expectRevertCustomError } = require('../../helpers/customError');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { zip } = require('../../helpers/iterate');

const { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior');
const ERC1155Mock = artifacts.require('$ERC1155');

contract('ERC1155', function (accounts) {
const [operator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts;
const initialURI = 'https://token-cdn-domain/{id}.json';

const initialURI = 'https://token-cdn-domain/{id}.json';
async function fixture() {
const [operator, holder, ...otherAccounts] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC1155', [initialURI]);
return { token, operator, holder, otherAccounts };
}

describe('ERC1155', function () {
beforeEach(async function () {
this.token = await ERC1155Mock.new(initialURI);
Object.assign(this, await loadFixture(fixture));
});

shouldBehaveLikeERC1155(otherAccounts);
shouldBehaveLikeERC1155();

describe('internal functions', function () {
const tokenId = new BN(1990);
const mintValue = new BN(9001);
const burnValue = new BN(3000);
const tokenId = 1990n;
const mintValue = 9001n;
const burnValue = 3000n;

const tokenBatchIds = [new BN(2000), new BN(2010), new BN(2020)];
const mintValues = [new BN(5000), new BN(10000), new BN(42195)];
const burnValues = [new BN(5000), new BN(9001), new BN(195)];
const tokenBatchIds = [2000n, 2010n, 2020n];
const mintValues = [5000n, 10000n, 42195n];
const burnValues = [5000n, 9001n, 195n];

const data = '0x12345678';

describe('_mint', function () {
it('reverts with a zero destination address', async function () {
await expectRevertCustomError(
this.token.$_mint(ZERO_ADDRESS, tokenId, mintValue, data),
'ERC1155InvalidReceiver',
[ZERO_ADDRESS],
);
await expect(this.token.$_mint(ethers.ZeroAddress, tokenId, mintValue, data))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
.withArgs(ethers.ZeroAddress);
});

context('with minted tokens', function () {
describe('with minted tokens', function () {
beforeEach(async function () {
this.receipt = await this.token.$_mint(tokenHolder, tokenId, mintValue, data, { from: operator });
this.tx = await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue, data);
});

it('emits a TransferSingle event', function () {
expectEvent(this.receipt, 'TransferSingle', {
operator,
from: ZERO_ADDRESS,
to: tokenHolder,
id: tokenId,
value: mintValue,
});
it('emits a TransferSingle event', async function () {
await expect(this.tx)
.to.emit(this.token, 'TransferSingle')
.withArgs(this.operator.address, ethers.ZeroAddress, this.holder.address, tokenId, mintValue);
});

it('credits the minted token value', async function () {
expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintValue);
expect(await this.token.balanceOf(this.holder, tokenId)).to.equal(mintValue);
});
});
});

describe('_mintBatch', function () {
it('reverts with a zero destination address', async function () {
await expectRevertCustomError(
this.token.$_mintBatch(ZERO_ADDRESS, tokenBatchIds, mintValues, data),
'ERC1155InvalidReceiver',
[ZERO_ADDRESS],
);
await expect(this.token.$_mintBatch(ethers.ZeroAddress, tokenBatchIds, mintValues, data))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidReceiver')
.withArgs(ethers.ZeroAddress);
});

it('reverts if length of inputs do not match', async function () {
await expectRevertCustomError(
this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintValues.slice(1), data),
'ERC1155InvalidArrayLength',
[tokenBatchIds.length, mintValues.length - 1],
);

await expectRevertCustomError(
this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds.slice(1), mintValues, data),
'ERC1155InvalidArrayLength',
[tokenBatchIds.length - 1, mintValues.length],
);
await expect(this.token.$_mintBatch(this.holder, tokenBatchIds, mintValues.slice(1), data))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
.withArgs(tokenBatchIds.length, mintValues.length - 1);

await expect(this.token.$_mintBatch(this.holder, tokenBatchIds.slice(1), mintValues, data))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
.withArgs(tokenBatchIds.length - 1, mintValues.length);
});

context('with minted batch of tokens', function () {
describe('with minted batch of tokens', function () {
beforeEach(async function () {
this.receipt = await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintValues, data, {
from: operator,
});
this.tx = await this.token.connect(this.operator).$_mintBatch(this.holder, tokenBatchIds, mintValues, data);
});

it('emits a TransferBatch event', function () {
expectEvent(this.receipt, 'TransferBatch', {
operator,
from: ZERO_ADDRESS,
to: tokenBatchHolder,
});
it('emits a TransferBatch event', async function () {
await expect(this.tx)
.to.emit(this.token, 'TransferBatch')
.withArgs(this.operator.address, ethers.ZeroAddress, this.holder.address, tokenBatchIds, mintValues);
});

it('credits the minted batch of tokens', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds.map(() => this.holder),
tokenBatchIds,
);

for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintValues[i]);
}
expect(holderBatchBalances).to.deep.equal(mintValues);
});
});
});

describe('_burn', function () {
it("reverts when burning the zero account's tokens", async function () {
await expectRevertCustomError(this.token.$_burn(ZERO_ADDRESS, tokenId, mintValue), 'ERC1155InvalidSender', [
ZERO_ADDRESS,
]);
await expect(this.token.$_burn(ethers.ZeroAddress, tokenId, mintValue))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidSender')
.withArgs(ethers.ZeroAddress);
});

it('reverts when burning a non-existent token id', async function () {
await expectRevertCustomError(
this.token.$_burn(tokenHolder, tokenId, mintValue),
'ERC1155InsufficientBalance',
[tokenHolder, 0, mintValue, tokenId],
);
await expect(this.token.$_burn(this.holder, tokenId, mintValue))
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
.withArgs(this.holder.address, 0, mintValue, tokenId);
});

it('reverts when burning more than available tokens', async function () {
await this.token.$_mint(tokenHolder, tokenId, mintValue, data, { from: operator });
await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue, data);

await expectRevertCustomError(
this.token.$_burn(tokenHolder, tokenId, mintValue.addn(1)),
'ERC1155InsufficientBalance',
[tokenHolder, mintValue, mintValue.addn(1), tokenId],
);
await expect(this.token.$_burn(this.holder, tokenId, mintValue + 1n))
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
.withArgs(this.holder.address, mintValue, mintValue + 1n, tokenId);
});

context('with minted-then-burnt tokens', function () {
describe('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.$_mint(tokenHolder, tokenId, mintValue, data);
this.receipt = await this.token.$_burn(tokenHolder, tokenId, burnValue, { from: operator });
await this.token.$_mint(this.holder, tokenId, mintValue, data);
this.tx = await this.token.connect(this.operator).$_burn(this.holder, tokenId, burnValue);
});

it('emits a TransferSingle event', function () {
expectEvent(this.receipt, 'TransferSingle', {
operator,
from: tokenHolder,
to: ZERO_ADDRESS,
id: tokenId,
value: burnValue,
});
it('emits a TransferSingle event', async function () {
await expect(this.tx)
.to.emit(this.token, 'TransferSingle')
.withArgs(this.operator.address, this.holder.address, ethers.ZeroAddress, tokenId, burnValue);
});

it('accounts for both minting and burning', async function () {
expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintValue.sub(burnValue));
expect(await this.token.balanceOf(this.holder, tokenId)).to.equal(mintValue - burnValue);
});
});
});

describe('_burnBatch', function () {
it("reverts when burning the zero account's tokens", async function () {
await expectRevertCustomError(
this.token.$_burnBatch(ZERO_ADDRESS, tokenBatchIds, burnValues),
'ERC1155InvalidSender',
[ZERO_ADDRESS],
);
await expect(this.token.$_burnBatch(ethers.ZeroAddress, tokenBatchIds, burnValues))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidSender')
.withArgs(ethers.ZeroAddress);
});

it('reverts if length of inputs do not match', async function () {
await expectRevertCustomError(
this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnValues.slice(1)),
'ERC1155InvalidArrayLength',
[tokenBatchIds.length, burnValues.length - 1],
);

await expectRevertCustomError(
this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds.slice(1), burnValues),
'ERC1155InvalidArrayLength',
[tokenBatchIds.length - 1, burnValues.length],
);
await expect(this.token.$_burnBatch(this.holder, tokenBatchIds, burnValues.slice(1)))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
.withArgs(tokenBatchIds.length, burnValues.length - 1);

await expect(this.token.$_burnBatch(this.holder, tokenBatchIds.slice(1), burnValues))
.to.be.revertedWithCustomError(this.token, 'ERC1155InvalidArrayLength')
.withArgs(tokenBatchIds.length - 1, burnValues.length);
});

it('reverts when burning a non-existent token id', async function () {
await expectRevertCustomError(
this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnValues),
'ERC1155InsufficientBalance',
[tokenBatchHolder, 0, tokenBatchIds[0], burnValues[0]],
);
await expect(this.token.$_burnBatch(this.holder, tokenBatchIds, burnValues))
.to.be.revertedWithCustomError(this.token, 'ERC1155InsufficientBalance')
.withArgs(this.holder.address, 0, burnValues[0], tokenBatchIds[0]);
});

context('with minted-then-burnt tokens', function () {
describe('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintValues, data);
this.receipt = await this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnValues, { from: operator });
await this.token.$_mintBatch(this.holder, tokenBatchIds, mintValues, data);
this.tx = await this.token.connect(this.operator).$_burnBatch(this.holder, tokenBatchIds, burnValues);
});

it('emits a TransferBatch event', function () {
expectEvent(this.receipt, 'TransferBatch', {
operator,
from: tokenBatchHolder,
to: ZERO_ADDRESS,
// ids: tokenBatchIds,
// values: burnValues,
});
it('emits a TransferBatch event', async function () {
await expect(this.tx)
.to.emit(this.token, 'TransferBatch')
.withArgs(this.operator.address, this.holder.address, ethers.ZeroAddress, tokenBatchIds, burnValues);
});

it('accounts for both minting and burning', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds.map(() => this.holder),
tokenBatchIds,
);

for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintValues[i].sub(burnValues[i]));
}
expect(holderBatchBalances).to.deep.equal(
zip(mintValues, burnValues).map(([mintValue, burnValue]) => mintValue - burnValue),
);
});
});
});
});

describe('ERC1155MetadataURI', function () {
const firstTokenID = new BN('42');
const secondTokenID = new BN('1337');
const firstTokenID = 42n;
const secondTokenID = 1337n;

it('emits no URI event in constructor', async function () {
await expectEvent.notEmitted.inConstruction(this.token, 'URI');
await expect(this.token.deploymentTransaction()).to.not.emit(this.token, 'URI');
});

it('sets the initial URI for all token types', async function () {
expect(await this.token.uri(firstTokenID)).to.be.equal(initialURI);
expect(await this.token.uri(secondTokenID)).to.be.equal(initialURI);
expect(await this.token.uri(firstTokenID)).to.equal(initialURI);
expect(await this.token.uri(secondTokenID)).to.equal(initialURI);
});

describe('_setURI', function () {
const newURI = 'https://token-cdn-domain/{locale}/{id}.json';

it('emits no URI event', async function () {
const receipt = await this.token.$_setURI(newURI);

expectEvent.notEmitted(receipt, 'URI');
await expect(this.token.$_setURI(newURI)).to.not.emit(this.token, 'URI');
});

it('sets the new URI for all token types', async function () {
await this.token.$_setURI(newURI);

expect(await this.token.uri(firstTokenID)).to.be.equal(newURI);
expect(await this.token.uri(secondTokenID)).to.be.equal(newURI);
expect(await this.token.uri(firstTokenID)).to.equal(newURI);
expect(await this.token.uri(secondTokenID)).to.equal(newURI);
});
});
});
Expand Down
Loading

0 comments on commit be0572a

Please sign in to comment.