Fix .gitignore: stop tracking ignored files

This commit is contained in:
5t4l1n
2025-07-27 10:39:02 +05:30
parent b42747e9a3
commit 3a87ef0576
625 changed files with 88566 additions and 63 deletions
@@ -0,0 +1,767 @@
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
const ERC1155ReceiverMock = artifacts.require('ERC1155ReceiverMock');
function shouldBehaveLikeERC1155([minter, firstTokenHolder, secondTokenHolder, multiTokenHolder, recipient, proxy]) {
const firstTokenId = new BN(1);
const secondTokenId = new BN(2);
const unknownTokenId = new BN(3);
const firstAmount = new BN(1000);
const secondAmount = new BN(2000);
const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
describe('like an ERC1155', function () {
describe('balanceOf', function () {
it('reverts when queried about the zero address', async function () {
await expectRevert(
this.token.balanceOf(ZERO_ADDRESS, firstTokenId),
'ERC1155: address zero is not a valid owner',
);
});
context("when accounts don't own tokens", function () {
it('returns zero for given addresses', async function () {
expect(await this.token.balanceOf(firstTokenHolder, firstTokenId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOf(secondTokenHolder, secondTokenId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOf(firstTokenHolder, unknownTokenId)).to.be.bignumber.equal('0');
});
});
context('when accounts own some tokens', function () {
beforeEach(async function () {
await this.token.$_mint(firstTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.$_mint(secondTokenHolder, secondTokenId, secondAmount, '0x', {
from: minter,
});
});
it('returns the amount of tokens owned by the given addresses', async function () {
expect(await this.token.balanceOf(firstTokenHolder, firstTokenId)).to.be.bignumber.equal(firstAmount);
expect(await this.token.balanceOf(secondTokenHolder, secondTokenId)).to.be.bignumber.equal(secondAmount);
expect(await this.token.balanceOf(firstTokenHolder, unknownTokenId)).to.be.bignumber.equal('0');
});
});
});
describe('balanceOfBatch', function () {
it("reverts when input arrays don't match up", async function () {
await expectRevert(
this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, firstTokenHolder, secondTokenHolder],
[firstTokenId, secondTokenId, unknownTokenId],
),
'ERC1155: accounts and ids length mismatch',
);
await expectRevert(
this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder],
[firstTokenId, secondTokenId, unknownTokenId],
),
'ERC1155: accounts and ids length mismatch',
);
});
it('reverts when one of the addresses is the zero address', async function () {
await expectRevert(
this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, ZERO_ADDRESS],
[firstTokenId, secondTokenId, unknownTokenId],
),
'ERC1155: address zero is not a valid owner',
);
});
context("when accounts don't own tokens", function () {
it('returns zeros for each account', async function () {
const result = await this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, firstTokenHolder],
[firstTokenId, secondTokenId, unknownTokenId],
);
expect(result).to.be.an('array');
expect(result[0]).to.be.a.bignumber.equal('0');
expect(result[1]).to.be.a.bignumber.equal('0');
expect(result[2]).to.be.a.bignumber.equal('0');
});
});
context('when accounts own some tokens', function () {
beforeEach(async function () {
await this.token.$_mint(firstTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.$_mint(secondTokenHolder, secondTokenId, secondAmount, '0x', {
from: minter,
});
});
it('returns amounts owned by each account in order passed', async function () {
const result = await this.token.balanceOfBatch(
[secondTokenHolder, firstTokenHolder, firstTokenHolder],
[secondTokenId, firstTokenId, unknownTokenId],
);
expect(result).to.be.an('array');
expect(result[0]).to.be.a.bignumber.equal(secondAmount);
expect(result[1]).to.be.a.bignumber.equal(firstAmount);
expect(result[2]).to.be.a.bignumber.equal('0');
});
it('returns multiple times the balance of the same address when asked', async function () {
const result = await this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, firstTokenHolder],
[firstTokenId, secondTokenId, firstTokenId],
);
expect(result).to.be.an('array');
expect(result[0]).to.be.a.bignumber.equal(result[2]);
expect(result[0]).to.be.a.bignumber.equal(firstAmount);
expect(result[1]).to.be.a.bignumber.equal(secondAmount);
expect(result[2]).to.be.a.bignumber.equal(firstAmount);
});
});
});
describe('setApprovalForAll', function () {
let receipt;
beforeEach(async function () {
receipt = await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
});
it('sets approval status which can be queried via isApprovedForAll', async function () {
expect(await this.token.isApprovedForAll(multiTokenHolder, proxy)).to.be.equal(true);
});
it('emits an ApprovalForAll log', function () {
expectEvent(receipt, 'ApprovalForAll', { account: multiTokenHolder, operator: proxy, approved: true });
});
it('can unset approval for an operator', async function () {
await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
expect(await this.token.isApprovedForAll(multiTokenHolder, proxy)).to.be.equal(false);
});
it('reverts if attempting to approve self as an operator', async function () {
await expectRevert(
this.token.setApprovalForAll(multiTokenHolder, true, { from: multiTokenHolder }),
'ERC1155: setting approval status for self',
);
});
});
describe('safeTransferFrom', function () {
beforeEach(async function () {
await this.token.$_mint(multiTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.$_mint(multiTokenHolder, secondTokenId, secondAmount, '0x', {
from: minter,
});
});
it('reverts when transferring more than balance', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount.addn(1), '0x', {
from: multiTokenHolder,
}),
'ERC1155: insufficient balance for transfer',
);
});
it('reverts when transferring to zero address', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, ZERO_ADDRESS, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}),
'ERC1155: transfer to the zero address',
);
});
function transferWasSuccessful({ operator, from, id, value }) {
it('debits transferred balance from sender', async function () {
const newBalance = await this.token.balanceOf(from, id);
expect(newBalance).to.be.a.bignumber.equal('0');
});
it('credits transferred balance to receiver', async function () {
const newBalance = await this.token.balanceOf(this.toWhom, id);
expect(newBalance).to.be.a.bignumber.equal(value);
});
it('emits a TransferSingle log', function () {
expectEvent(this.transferLogs, 'TransferSingle', {
operator,
from,
to: this.toWhom,
id,
value,
});
});
}
context('when called by the multiTokenHolder', async function () {
beforeEach(async function () {
this.toWhom = recipient;
this.transferLogs = await this.token.safeTransferFrom(
multiTokenHolder,
recipient,
firstTokenId,
firstAmount,
'0x',
{
from: multiTokenHolder,
},
);
});
transferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('preserves existing balances which are not transferred by multiTokenHolder', async function () {
const balance1 = await this.token.balanceOf(multiTokenHolder, secondTokenId);
expect(balance1).to.be.a.bignumber.equal(secondAmount);
const balance2 = await this.token.balanceOf(recipient, secondTokenId);
expect(balance2).to.be.a.bignumber.equal('0');
});
});
context('when called by an operator on behalf of the multiTokenHolder', function () {
context('when operator is not approved by multiTokenHolder', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
});
it('reverts', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
from: proxy,
}),
'ERC1155: caller is not token owner or approved',
);
});
});
context('when operator is approved by multiTokenHolder', function () {
beforeEach(async function () {
this.toWhom = recipient;
await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
this.transferLogs = await this.token.safeTransferFrom(
multiTokenHolder,
recipient,
firstTokenId,
firstAmount,
'0x',
{
from: proxy,
},
);
});
transferWasSuccessful.call(this, {
operator: proxy,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it("preserves operator's balances not involved in the transfer", async function () {
const balance1 = await this.token.balanceOf(proxy, firstTokenId);
expect(balance1).to.be.a.bignumber.equal('0');
const balance2 = await this.token.balanceOf(proxy, secondTokenId);
expect(balance2).to.be.a.bignumber.equal('0');
});
});
});
context('when sending to a valid receiver', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE,
false,
RECEIVER_BATCH_MAGIC_VALUE,
false,
);
});
context('without data', function () {
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeTransferFrom(
multiTokenHolder,
this.receiver.address,
firstTokenId,
firstAmount,
'0x',
{ from: multiTokenHolder },
);
this.transferLogs = this.transferReceipt;
});
transferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('calls onERC1155Received', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'Received', {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
data: null,
});
});
});
context('with data', function () {
const data = '0xf00dd00d';
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeTransferFrom(
multiTokenHolder,
this.receiver.address,
firstTokenId,
firstAmount,
data,
{ from: multiTokenHolder },
);
this.transferLogs = this.transferReceipt;
});
transferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('calls onERC1155Received', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'Received', {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
data,
});
});
});
});
context('to a receiver contract returning unexpected value', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new('0x00c0ffee', false, RECEIVER_BATCH_MAGIC_VALUE, false);
});
it('reverts', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}),
'ERC1155: ERC1155Receiver rejected tokens',
);
});
});
context('to a receiver contract that reverts', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE,
true,
RECEIVER_BATCH_MAGIC_VALUE,
false,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}),
'ERC1155ReceiverMock: reverting on receive',
);
});
});
context('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const invalidReceiver = this.token;
await expectRevert.unspecified(
this.token.safeTransferFrom(multiTokenHolder, invalidReceiver.address, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}),
);
});
});
});
describe('safeBatchTransferFrom', function () {
beforeEach(async function () {
await this.token.$_mint(multiTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.$_mint(multiTokenHolder, secondTokenId, secondAmount, '0x', {
from: minter,
});
});
it('reverts when transferring amount more than any of balances', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount.addn(1)],
'0x',
{ from: multiTokenHolder },
),
'ERC1155: insufficient balance for transfer',
);
});
it("reverts when ids array length doesn't match amounts array length", async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
recipient,
[firstTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
),
'ERC1155: ids and amounts length mismatch',
);
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
recipient,
[firstTokenId, secondTokenId],
[firstAmount],
'0x',
{ from: multiTokenHolder },
),
'ERC1155: ids and amounts length mismatch',
);
});
it('reverts when transferring to zero address', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
ZERO_ADDRESS,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
),
'ERC1155: transfer to the zero address',
);
});
function batchTransferWasSuccessful({ operator, from, ids, values }) {
it('debits transferred balances from sender', async function () {
const newBalances = await this.token.balanceOfBatch(new Array(ids.length).fill(from), ids);
for (const newBalance of newBalances) {
expect(newBalance).to.be.a.bignumber.equal('0');
}
});
it('credits transferred balances to receiver', async function () {
const newBalances = await this.token.balanceOfBatch(new Array(ids.length).fill(this.toWhom), ids);
for (let i = 0; i < newBalances.length; i++) {
expect(newBalances[i]).to.be.a.bignumber.equal(values[i]);
}
});
it('emits a TransferBatch log', function () {
expectEvent(this.transferLogs, 'TransferBatch', {
operator,
from,
to: this.toWhom,
// ids,
// values,
});
});
}
context('when called by the multiTokenHolder', async function () {
beforeEach(async function () {
this.toWhom = recipient;
this.transferLogs = await this.token.safeBatchTransferFrom(
multiTokenHolder,
recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
);
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
});
context('when called by an operator on behalf of the multiTokenHolder', function () {
context('when operator is not approved by multiTokenHolder', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
});
it('reverts', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: proxy },
),
'ERC1155: caller is not token owner or approved',
);
});
});
context('when operator is approved by multiTokenHolder', function () {
beforeEach(async function () {
this.toWhom = recipient;
await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
this.transferLogs = await this.token.safeBatchTransferFrom(
multiTokenHolder,
recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: proxy },
);
});
batchTransferWasSuccessful.call(this, {
operator: proxy,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it("preserves operator's balances not involved in the transfer", async function () {
const balance1 = await this.token.balanceOf(proxy, firstTokenId);
expect(balance1).to.be.a.bignumber.equal('0');
const balance2 = await this.token.balanceOf(proxy, secondTokenId);
expect(balance2).to.be.a.bignumber.equal('0');
});
});
});
context('when sending to a valid receiver', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE,
false,
RECEIVER_BATCH_MAGIC_VALUE,
false,
);
});
context('without data', function () {
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeBatchTransferFrom(
multiTokenHolder,
this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
);
this.transferLogs = this.transferReceipt;
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('calls onERC1155BatchReceived', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
operator: multiTokenHolder,
from: multiTokenHolder,
// ids: [firstTokenId, secondTokenId],
// values: [firstAmount, secondAmount],
data: null,
});
});
});
context('with data', function () {
const data = '0xf00dd00d';
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeBatchTransferFrom(
multiTokenHolder,
this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
data,
{ from: multiTokenHolder },
);
this.transferLogs = this.transferReceipt;
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('calls onERC1155Received', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
operator: multiTokenHolder,
from: multiTokenHolder,
// ids: [firstTokenId, secondTokenId],
// values: [firstAmount, secondAmount],
data,
});
});
});
});
context('to a receiver contract returning unexpected value', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE,
false,
RECEIVER_SINGLE_MAGIC_VALUE,
false,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
),
'ERC1155: ERC1155Receiver rejected tokens',
);
});
});
context('to a receiver contract that reverts', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE,
false,
RECEIVER_BATCH_MAGIC_VALUE,
true,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder,
this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
),
'ERC1155ReceiverMock: reverting on batch receive',
);
});
});
context('to a receiver contract that reverts only on single transfers', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE,
true,
RECEIVER_BATCH_MAGIC_VALUE,
false,
);
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeBatchTransferFrom(
multiTokenHolder,
this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
);
this.transferLogs = this.transferReceipt;
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('calls onERC1155BatchReceived', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
operator: multiTokenHolder,
from: multiTokenHolder,
// ids: [firstTokenId, secondTokenId],
// values: [firstAmount, secondAmount],
data: null,
});
});
});
context('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const invalidReceiver = this.token;
await expectRevert.unspecified(
this.token.safeBatchTransferFrom(
multiTokenHolder,
invalidReceiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x',
{ from: multiTokenHolder },
),
);
});
});
});
shouldSupportInterfaces(['ERC165', 'ERC1155']);
});
}
module.exports = {
shouldBehaveLikeERC1155,
};
@@ -0,0 +1,235 @@
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
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';
beforeEach(async function () {
this.token = await ERC1155Mock.new(initialURI);
});
shouldBehaveLikeERC1155(otherAccounts);
describe('internal functions', function () {
const tokenId = new BN(1990);
const mintAmount = new BN(9001);
const burnAmount = new BN(3000);
const tokenBatchIds = [new BN(2000), new BN(2010), new BN(2020)];
const mintAmounts = [new BN(5000), new BN(10000), new BN(42195)];
const burnAmounts = [new BN(5000), new BN(9001), new BN(195)];
const data = '0x12345678';
describe('_mint', function () {
it('reverts with a zero destination address', async function () {
await expectRevert(
this.token.$_mint(ZERO_ADDRESS, tokenId, mintAmount, data),
'ERC1155: mint to the zero address',
);
});
context('with minted tokens', function () {
beforeEach(async function () {
this.receipt = await this.token.$_mint(tokenHolder, tokenId, mintAmount, data, { from: operator });
});
it('emits a TransferSingle event', function () {
expectEvent(this.receipt, 'TransferSingle', {
operator,
from: ZERO_ADDRESS,
to: tokenHolder,
id: tokenId,
value: mintAmount,
});
});
it('credits the minted amount of tokens', async function () {
expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount);
});
});
});
describe('_mintBatch', function () {
it('reverts with a zero destination address', async function () {
await expectRevert(
this.token.$_mintBatch(ZERO_ADDRESS, tokenBatchIds, mintAmounts, data),
'ERC1155: mint to the zero address',
);
});
it('reverts if length of inputs do not match', async function () {
await expectRevert(
this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts.slice(1), data),
'ERC1155: ids and amounts length mismatch',
);
await expectRevert(
this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds.slice(1), mintAmounts, data),
'ERC1155: ids and amounts length mismatch',
);
});
context('with minted batch of tokens', function () {
beforeEach(async function () {
this.receipt = await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data, {
from: operator,
});
});
it('emits a TransferBatch event', function () {
expectEvent(this.receipt, 'TransferBatch', {
operator,
from: ZERO_ADDRESS,
to: tokenBatchHolder,
});
});
it('credits the minted batch of tokens', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds,
);
for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i]);
}
});
});
});
describe('_burn', function () {
it("reverts when burning the zero account's tokens", async function () {
await expectRevert(this.token.$_burn(ZERO_ADDRESS, tokenId, mintAmount), 'ERC1155: burn from the zero address');
});
it('reverts when burning a non-existent token id', async function () {
await expectRevert(this.token.$_burn(tokenHolder, tokenId, mintAmount), 'ERC1155: burn amount exceeds balance');
});
it('reverts when burning more than available tokens', async function () {
await this.token.$_mint(tokenHolder, tokenId, mintAmount, data, { from: operator });
await expectRevert(
this.token.$_burn(tokenHolder, tokenId, mintAmount.addn(1)),
'ERC1155: burn amount exceeds balance',
);
});
context('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.$_mint(tokenHolder, tokenId, mintAmount, data);
this.receipt = await this.token.$_burn(tokenHolder, tokenId, burnAmount, { from: operator });
});
it('emits a TransferSingle event', function () {
expectEvent(this.receipt, 'TransferSingle', {
operator,
from: tokenHolder,
to: ZERO_ADDRESS,
id: tokenId,
value: burnAmount,
});
});
it('accounts for both minting and burning', async function () {
expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount.sub(burnAmount));
});
});
});
describe('_burnBatch', function () {
it("reverts when burning the zero account's tokens", async function () {
await expectRevert(
this.token.$_burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts),
'ERC1155: burn from the zero address',
);
});
it('reverts if length of inputs do not match', async function () {
await expectRevert(
this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts.slice(1)),
'ERC1155: ids and amounts length mismatch',
);
await expectRevert(
this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds.slice(1), burnAmounts),
'ERC1155: ids and amounts length mismatch',
);
});
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts),
'ERC1155: burn amount exceeds balance',
);
});
context('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data);
this.receipt = await this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts, { from: operator });
});
it('emits a TransferBatch event', function () {
expectEvent(this.receipt, 'TransferBatch', {
operator,
from: tokenBatchHolder,
to: ZERO_ADDRESS,
// ids: tokenBatchIds,
// values: burnAmounts,
});
});
it('accounts for both minting and burning', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds,
);
for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i].sub(burnAmounts[i]));
}
});
});
});
});
describe('ERC1155MetadataURI', function () {
const firstTokenID = new BN('42');
const secondTokenID = new BN('1337');
it('emits no URI event in constructor', async function () {
await expectEvent.notEmitted.inConstruction(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);
});
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');
});
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);
});
});
});
});
@@ -0,0 +1,67 @@
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC1155Burnable = artifacts.require('$ERC1155Burnable');
contract('ERC1155Burnable', function (accounts) {
const [holder, operator, other] = accounts;
const uri = 'https://token.com';
const tokenIds = [new BN('42'), new BN('1137')];
const amounts = [new BN('3000'), new BN('9902')];
beforeEach(async function () {
this.token = await ERC1155Burnable.new(uri);
await this.token.$_mint(holder, tokenIds[0], amounts[0], '0x');
await this.token.$_mint(holder, tokenIds[1], amounts[1], '0x');
});
describe('burn', function () {
it('holder can burn their tokens', async function () {
await this.token.burn(holder, tokenIds[0], amounts[0].subn(1), { from: holder });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
});
it("approved operators can burn the holder's tokens", async function () {
await this.token.setApprovalForAll(operator, true, { from: holder });
await this.token.burn(holder, tokenIds[0], amounts[0].subn(1), { from: operator });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
});
it("unapproved accounts cannot burn the holder's tokens", async function () {
await expectRevert(
this.token.burn(holder, tokenIds[0], amounts[0].subn(1), { from: other }),
'ERC1155: caller is not token owner or approved',
);
});
});
describe('burnBatch', function () {
it('holder can burn their tokens', async function () {
await this.token.burnBatch(holder, tokenIds, [amounts[0].subn(1), amounts[1].subn(2)], { from: holder });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
expect(await this.token.balanceOf(holder, tokenIds[1])).to.be.bignumber.equal('2');
});
it("approved operators can burn the holder's tokens", async function () {
await this.token.setApprovalForAll(operator, true, { from: holder });
await this.token.burnBatch(holder, tokenIds, [amounts[0].subn(1), amounts[1].subn(2)], { from: operator });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
expect(await this.token.balanceOf(holder, tokenIds[1])).to.be.bignumber.equal('2');
});
it("unapproved accounts cannot burn the holder's tokens", async function () {
await expectRevert(
this.token.burnBatch(holder, tokenIds, [amounts[0].subn(1), amounts[1].subn(2)], { from: other }),
'ERC1155: caller is not token owner or approved',
);
});
});
});
@@ -0,0 +1,108 @@
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC1155Pausable = artifacts.require('$ERC1155Pausable');
contract('ERC1155Pausable', function (accounts) {
const [holder, operator, receiver, other] = accounts;
const uri = 'https://token.com';
beforeEach(async function () {
this.token = await ERC1155Pausable.new(uri);
});
context('when token is paused', function () {
const firstTokenId = new BN('37');
const firstTokenAmount = new BN('42');
const secondTokenId = new BN('19842');
const secondTokenAmount = new BN('23');
beforeEach(async function () {
await this.token.setApprovalForAll(operator, true, { from: holder });
await this.token.$_mint(holder, firstTokenId, firstTokenAmount, '0x');
await this.token.$_pause();
});
it('reverts when trying to safeTransferFrom from holder', async function () {
await expectRevert(
this.token.safeTransferFrom(holder, receiver, firstTokenId, firstTokenAmount, '0x', { from: holder }),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to safeTransferFrom from operator', async function () {
await expectRevert(
this.token.safeTransferFrom(holder, receiver, firstTokenId, firstTokenAmount, '0x', { from: operator }),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to safeBatchTransferFrom from holder', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(holder, receiver, [firstTokenId], [firstTokenAmount], '0x', { from: holder }),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to safeBatchTransferFrom from operator', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(holder, receiver, [firstTokenId], [firstTokenAmount], '0x', {
from: operator,
}),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to mint', async function () {
await expectRevert(
this.token.$_mint(holder, secondTokenId, secondTokenAmount, '0x'),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to mintBatch', async function () {
await expectRevert(
this.token.$_mintBatch(holder, [secondTokenId], [secondTokenAmount], '0x'),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to burn', async function () {
await expectRevert(
this.token.$_burn(holder, firstTokenId, firstTokenAmount),
'ERC1155Pausable: token transfer while paused',
);
});
it('reverts when trying to burnBatch', async function () {
await expectRevert(
this.token.$_burnBatch(holder, [firstTokenId], [firstTokenAmount]),
'ERC1155Pausable: token transfer while paused',
);
});
describe('setApprovalForAll', function () {
it('approves an operator', async function () {
await this.token.setApprovalForAll(other, true, { from: holder });
expect(await this.token.isApprovedForAll(holder, other)).to.equal(true);
});
});
describe('balanceOf', function () {
it('returns the amount of tokens owned by the given address', async function () {
const balance = await this.token.balanceOf(holder, firstTokenId);
expect(balance).to.be.bignumber.equal(firstTokenAmount);
});
});
describe('isApprovedForAll', function () {
it('returns the approval of the operator', async function () {
expect(await this.token.isApprovedForAll(holder, operator)).to.equal(true);
});
});
});
});
@@ -0,0 +1,107 @@
const { BN } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC1155Supply = artifacts.require('$ERC1155Supply');
contract('ERC1155Supply', function (accounts) {
const [holder] = accounts;
const uri = 'https://token.com';
const firstTokenId = new BN('37');
const firstTokenAmount = new BN('42');
const secondTokenId = new BN('19842');
const secondTokenAmount = new BN('23');
beforeEach(async function () {
this.token = await ERC1155Supply.new(uri);
});
context('before mint', function () {
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.equal(false);
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal('0');
});
});
context('after mint', function () {
context('single', function () {
beforeEach(async function () {
await this.token.$_mint(holder, firstTokenId, firstTokenAmount, '0x');
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.equal(true);
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal(firstTokenAmount);
});
});
context('batch', function () {
beforeEach(async function () {
await this.token.$_mintBatch(
holder,
[firstTokenId, secondTokenId],
[firstTokenAmount, secondTokenAmount],
'0x',
);
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.equal(true);
expect(await this.token.exists(secondTokenId)).to.be.equal(true);
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal(firstTokenAmount);
expect(await this.token.totalSupply(secondTokenId)).to.be.bignumber.equal(secondTokenAmount);
});
});
});
context('after burn', function () {
context('single', function () {
beforeEach(async function () {
await this.token.$_mint(holder, firstTokenId, firstTokenAmount, '0x');
await this.token.$_burn(holder, firstTokenId, firstTokenAmount);
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.equal(false);
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal('0');
});
});
context('batch', function () {
beforeEach(async function () {
await this.token.$_mintBatch(
holder,
[firstTokenId, secondTokenId],
[firstTokenAmount, secondTokenAmount],
'0x',
);
await this.token.$_burnBatch(holder, [firstTokenId, secondTokenId], [firstTokenAmount, secondTokenAmount]);
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.equal(false);
expect(await this.token.exists(secondTokenId)).to.be.equal(false);
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal('0');
expect(await this.token.totalSupply(secondTokenId)).to.be.bignumber.equal('0');
});
});
});
});
@@ -0,0 +1,66 @@
const { BN, expectEvent } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { artifacts } = require('hardhat');
const ERC1155URIStorage = artifacts.require('$ERC1155URIStorage');
contract(['ERC1155URIStorage'], function (accounts) {
const [holder] = accounts;
const erc1155Uri = 'https://token.com/nfts/';
const baseUri = 'https://token.com/';
const tokenId = new BN('1');
const amount = new BN('3000');
describe('with base uri set', function () {
beforeEach(async function () {
this.token = await ERC1155URIStorage.new(erc1155Uri);
await this.token.$_setBaseURI(baseUri);
await this.token.$_mint(holder, tokenId, amount, '0x');
});
it('can request the token uri, returning the erc1155 uri if no token uri was set', async function () {
const receivedTokenUri = await this.token.uri(tokenId);
expect(receivedTokenUri).to.be.equal(erc1155Uri);
});
it('can request the token uri, returning the concatenated uri if a token uri was set', async function () {
const tokenUri = '1234/';
const receipt = await this.token.$_setURI(tokenId, tokenUri);
const receivedTokenUri = await this.token.uri(tokenId);
const expectedUri = `${baseUri}${tokenUri}`;
expect(receivedTokenUri).to.be.equal(expectedUri);
expectEvent(receipt, 'URI', { value: expectedUri, id: tokenId });
});
});
describe('with base uri set to the empty string', function () {
beforeEach(async function () {
this.token = await ERC1155URIStorage.new('');
await this.token.$_mint(holder, tokenId, amount, '0x');
});
it('can request the token uri, returning an empty string if no token uri was set', async function () {
const receivedTokenUri = await this.token.uri(tokenId);
expect(receivedTokenUri).to.be.equal('');
});
it('can request the token uri, returning the token uri if a token uri was set', async function () {
const tokenUri = 'ipfs://1234/';
const receipt = await this.token.$_setURI(tokenId, tokenUri);
const receivedTokenUri = await this.token.uri(tokenId);
expect(receivedTokenUri).to.be.equal(tokenUri);
expectEvent(receipt, 'URI', { value: tokenUri, id: tokenId });
});
});
});
@@ -0,0 +1,156 @@
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
const { expect } = require('chai');
const ERC1155PresetMinterPauser = artifacts.require('ERC1155PresetMinterPauser');
contract('ERC1155PresetMinterPauser', function (accounts) {
const [deployer, other] = accounts;
const firstTokenId = new BN('845');
const firstTokenIdAmount = new BN('5000');
const secondTokenId = new BN('48324');
const secondTokenIdAmount = new BN('77875');
const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE');
const PAUSER_ROLE = web3.utils.soliditySha3('PAUSER_ROLE');
const uri = 'https://token.com';
beforeEach(async function () {
this.token = await ERC1155PresetMinterPauser.new(uri, { from: deployer });
});
shouldSupportInterfaces(['ERC1155', 'AccessControl', 'AccessControlEnumerable']);
it('deployer has the default admin role', async function () {
expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1');
expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer);
});
it('deployer has the minter role', async function () {
expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1');
expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer);
});
it('deployer has the pauser role', async function () {
expect(await this.token.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal('1');
expect(await this.token.getRoleMember(PAUSER_ROLE, 0)).to.equal(deployer);
});
it('minter and pauser role admin is the default admin', async function () {
expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
expect(await this.token.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
});
describe('minting', function () {
it('deployer can mint tokens', async function () {
const receipt = await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
expectEvent(receipt, 'TransferSingle', {
operator: deployer,
from: ZERO_ADDRESS,
to: other,
value: firstTokenIdAmount,
id: firstTokenId,
});
expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
});
it('other accounts cannot mint tokens', async function () {
await expectRevert(
this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: other }),
'ERC1155PresetMinterPauser: must have minter role to mint',
);
});
});
describe('batched minting', function () {
it('deployer can batch mint tokens', async function () {
const receipt = await this.token.mintBatch(
other,
[firstTokenId, secondTokenId],
[firstTokenIdAmount, secondTokenIdAmount],
'0x',
{ from: deployer },
);
expectEvent(receipt, 'TransferBatch', { operator: deployer, from: ZERO_ADDRESS, to: other });
expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
});
it('other accounts cannot batch mint tokens', async function () {
await expectRevert(
this.token.mintBatch(other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', {
from: other,
}),
'ERC1155PresetMinterPauser: must have minter role to mint',
);
});
});
describe('pausing', function () {
it('deployer can pause', async function () {
const receipt = await this.token.pause({ from: deployer });
expectEvent(receipt, 'Paused', { account: deployer });
expect(await this.token.paused()).to.equal(true);
});
it('deployer can unpause', async function () {
await this.token.pause({ from: deployer });
const receipt = await this.token.unpause({ from: deployer });
expectEvent(receipt, 'Unpaused', { account: deployer });
expect(await this.token.paused()).to.equal(false);
});
it('cannot mint while paused', async function () {
await this.token.pause({ from: deployer });
await expectRevert(
this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }),
'ERC1155Pausable: token transfer while paused',
);
});
it('other accounts cannot pause', async function () {
await expectRevert(
this.token.pause({ from: other }),
'ERC1155PresetMinterPauser: must have pauser role to pause',
);
});
it('other accounts cannot unpause', async function () {
await this.token.pause({ from: deployer });
await expectRevert(
this.token.unpause({ from: other }),
'ERC1155PresetMinterPauser: must have pauser role to unpause',
);
});
});
describe('burning', function () {
it('holders can burn their tokens', async function () {
await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
const receipt = await this.token.burn(other, firstTokenId, firstTokenIdAmount.subn(1), { from: other });
expectEvent(receipt, 'TransferSingle', {
operator: other,
from: other,
to: ZERO_ADDRESS,
value: firstTokenIdAmount.subn(1),
id: firstTokenId,
});
expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal('1');
});
});
});
@@ -0,0 +1,64 @@
const { BN } = require('@openzeppelin/test-helpers');
const ERC1155Holder = artifacts.require('ERC1155Holder');
const ERC1155 = artifacts.require('$ERC1155');
const { expect } = require('chai');
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
contract('ERC1155Holder', function (accounts) {
const [creator] = accounts;
const uri = 'https://token-cdn-domain/{id}.json';
const multiTokenIds = [new BN(1), new BN(2), new BN(3)];
const multiTokenAmounts = [new BN(1000), new BN(2000), new BN(3000)];
const transferData = '0x12345678';
beforeEach(async function () {
this.multiToken = await ERC1155.new(uri);
this.holder = await ERC1155Holder.new();
await this.multiToken.$_mintBatch(creator, multiTokenIds, multiTokenAmounts, '0x');
});
shouldSupportInterfaces(['ERC165', 'ERC1155Receiver']);
it('receives ERC1155 tokens from a single ID', async function () {
await this.multiToken.safeTransferFrom(
creator,
this.holder.address,
multiTokenIds[0],
multiTokenAmounts[0],
transferData,
{ from: creator },
);
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[0])).to.be.bignumber.equal(
multiTokenAmounts[0],
);
for (let i = 1; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i])).to.be.bignumber.equal(new BN(0));
}
});
it('receives ERC1155 tokens from a multiple IDs', async function () {
for (let i = 0; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i])).to.be.bignumber.equal(new BN(0));
}
await this.multiToken.safeBatchTransferFrom(
creator,
this.holder.address,
multiTokenIds,
multiTokenAmounts,
transferData,
{ from: creator },
);
for (let i = 0; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i])).to.be.bignumber.equal(
multiTokenAmounts[i],
);
}
});
});