Unverified Commit 943a663a by Andres Bachfischer Committed by GitHub

Updated ERC1155 tests (#2107)


Co-authored-by: Alan Lu <alanlu1023@gmail.com>
Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
parent 09014f90
......@@ -92,6 +92,14 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder,
),
'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 () {
......@@ -143,6 +151,18 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder,
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);
});
});
});
......@@ -298,8 +318,11 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder,
});
it('preserves operator\'s balances not involved in the transfer', async function () {
const balance = await this.token.balanceOf(proxy, firstTokenId);
expect(balance).to.be.a.bignumber.equal('0');
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');
});
});
});
......@@ -464,6 +487,16 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder,
),
'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 () {
......@@ -684,6 +717,41 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder,
});
});
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 },
);
({ logs: this.transferLogs } = this.transferReceipt);
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('should call 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;
......
......@@ -28,7 +28,7 @@ describe('ERC1155', function () {
const mintAmounts = [new BN(5000), new BN(10000), new BN(42195)];
const burnAmounts = [new BN(5000), new BN(9001), new BN(195)];
const data = '0xcafebabe';
const data = '0x12345678';
describe('_mint', function () {
it('reverts with a zero destination address', async function () {
......@@ -72,6 +72,11 @@ describe('ERC1155', function () {
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 () {
......@@ -121,6 +126,21 @@ describe('ERC1155', function () {
);
});
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);
......@@ -164,6 +184,11 @@ describe('ERC1155', function () {
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 () {
......
......@@ -8,41 +8,52 @@ const { expect } = require('chai');
describe('ERC1155Holder', function () {
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 ERC1155Mock.new(uri, { from: creator });
this.holder = await ERC1155Holder.new();
await this.multiToken.mintBatch(creator, multiTokenIds, multiTokenAmounts, '0x', { from: creator });
});
it('receives ERC1155 tokens', async function () {
const uri = 'https://token-cdn-domain/{id}.json';
const multiToken = await ERC1155Mock.new(uri, { from: creator });
const multiTokenIds = [new BN(1), new BN(2), new BN(3)];
const multiTokenAmounts = [new BN(1000), new BN(2000), new BN(3000)];
await multiToken.mintBatch(creator, multiTokenIds, multiTokenAmounts, '0x', { from: creator });
const transferData = '0xf00dbabe';
const holder = await ERC1155Holder.new();
await multiToken.safeTransferFrom(
it('receives ERC1155 tokens from a single ID', async function () {
await this.multiToken.safeTransferFrom(
creator,
holder.address,
this.holder.address,
multiTokenIds[0],
multiTokenAmounts[0],
transferData,
{ from: creator },
);
expect(await multiToken.balanceOf(holder.address, multiTokenIds[0])).to.be.bignumber.equal(multiTokenAmounts[0]);
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[0]))
.to.be.bignumber.equal(multiTokenAmounts[0]);
await multiToken.safeBatchTransferFrom(
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,
holder.address,
multiTokenIds.slice(1),
multiTokenAmounts.slice(1),
this.holder.address,
multiTokenIds,
multiTokenAmounts,
transferData,
{ from: creator },
);
for (let i = 1; i < multiTokenIds.length; i++) {
expect(await multiToken.balanceOf(holder.address, multiTokenIds[i])).to.be.bignumber.equal(multiTokenAmounts[i]);
for (let i = 0; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i]))
.to.be.bignumber.equal(multiTokenAmounts[i]);
}
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment