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,66 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const ids = [42n, 1137n];
const values = [3000n, 9902n];
async function fixture() {
const [holder, operator, other] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC1155Burnable', ['https://token-cdn-domain/{id}.json']);
await token.$_mint(holder, ids[0], values[0], '0x');
await token.$_mint(holder, ids[1], values[1], '0x');
return { token, holder, operator, other };
}
describe('ERC1155Burnable', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('burn', function () {
it('holder can burn their tokens', async function () {
await this.token.connect(this.holder).burn(this.holder, ids[0], values[0] - 1n);
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
});
it("approved operators can burn the holder's tokens", async function () {
await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
await this.token.connect(this.operator).burn(this.holder, ids[0], values[0] - 1n);
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
});
it("unapproved accounts cannot burn the holder's tokens", async function () {
await expect(this.token.connect(this.other).burn(this.holder, ids[0], values[0] - 1n))
.to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
.withArgs(this.other, this.holder);
});
});
describe('burnBatch', function () {
it('holder can burn their tokens', async function () {
await this.token.connect(this.holder).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]);
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
expect(await this.token.balanceOf(this.holder, ids[1])).to.equal(2n);
});
it("approved operators can burn the holder's tokens", async function () {
await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
await this.token.connect(this.operator).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]);
expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
expect(await this.token.balanceOf(this.holder, ids[1])).to.equal(2n);
});
it("unapproved accounts cannot burn the holder's tokens", async function () {
await expect(this.token.connect(this.other).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]))
.to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
.withArgs(this.other, this.holder);
});
});
});

View File

@@ -0,0 +1,105 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
async function fixture() {
const [holder, operator, receiver, other] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC1155Pausable', ['https://token-cdn-domain/{id}.json']);
return { token, holder, operator, receiver, other };
}
describe('ERC1155Pausable', function () {
const firstTokenId = 37n;
const firstTokenValue = 42n;
const secondTokenId = 19842n;
const secondTokenValue = 23n;
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('when token is paused', function () {
beforeEach(async function () {
await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
await this.token.$_pause();
});
it('reverts when trying to safeTransferFrom from holder', async function () {
await expect(
this.token
.connect(this.holder)
.safeTransferFrom(this.holder, this.receiver, firstTokenId, firstTokenValue, '0x'),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to safeTransferFrom from operator', async function () {
await expect(
this.token
.connect(this.operator)
.safeTransferFrom(this.holder, this.receiver, firstTokenId, firstTokenValue, '0x'),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to safeBatchTransferFrom from holder', async function () {
await expect(
this.token
.connect(this.holder)
.safeBatchTransferFrom(this.holder, this.receiver, [firstTokenId], [firstTokenValue], '0x'),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to safeBatchTransferFrom from operator', async function () {
await expect(
this.token
.connect(this.operator)
.safeBatchTransferFrom(this.holder, this.receiver, [firstTokenId], [firstTokenValue], '0x'),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to mint', async function () {
await expect(this.token.$_mint(this.holder, secondTokenId, secondTokenValue, '0x')).to.be.revertedWithCustomError(
this.token,
'EnforcedPause',
);
});
it('reverts when trying to mintBatch', async function () {
await expect(
this.token.$_mintBatch(this.holder, [secondTokenId], [secondTokenValue], '0x'),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to burn', async function () {
await expect(this.token.$_burn(this.holder, firstTokenId, firstTokenValue)).to.be.revertedWithCustomError(
this.token,
'EnforcedPause',
);
});
it('reverts when trying to burnBatch', async function () {
await expect(
this.token.$_burnBatch(this.holder, [firstTokenId], [firstTokenValue]),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
describe('setApprovalForAll', function () {
it('approves an operator', async function () {
await this.token.connect(this.holder).setApprovalForAll(this.other, true);
expect(await this.token.isApprovedForAll(this.holder, this.other)).to.be.true;
});
});
describe('balanceOf', function () {
it('returns the token value owned by the given address', async function () {
expect(await this.token.balanceOf(this.holder, firstTokenId)).to.equal(firstTokenValue);
});
});
describe('isApprovedForAll', function () {
it('returns the approval of the operator', async function () {
expect(await this.token.isApprovedForAll(this.holder, this.operator)).to.be.true;
});
});
});
});

View File

@@ -0,0 +1,119 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
async function fixture() {
const [holder] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC1155Supply', ['https://token-cdn-domain/{id}.json']);
return { token, holder };
}
describe('ERC1155Supply', function () {
const firstTokenId = 37n;
const firstTokenValue = 42n;
const secondTokenId = 19842n;
const secondTokenValue = 23n;
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('before mint', function () {
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.false;
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
expect(await this.token.totalSupply()).to.equal(0n);
});
});
describe('after mint', function () {
describe('single', function () {
beforeEach(async function () {
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.true;
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(firstTokenValue);
expect(await this.token.totalSupply()).to.equal(firstTokenValue);
});
});
describe('batch', function () {
beforeEach(async function () {
await this.token.$_mintBatch(
this.holder,
[firstTokenId, secondTokenId],
[firstTokenValue, secondTokenValue],
'0x',
);
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.true;
expect(await this.token.exists(secondTokenId)).to.be.true;
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(firstTokenValue);
expect(await this.token.totalSupply(ethers.Typed.uint256(secondTokenId))).to.equal(secondTokenValue);
expect(await this.token.totalSupply()).to.equal(firstTokenValue + secondTokenValue);
});
});
});
describe('after burn', function () {
describe('single', function () {
beforeEach(async function () {
await this.token.$_mint(this.holder, firstTokenId, firstTokenValue, '0x');
await this.token.$_burn(this.holder, firstTokenId, firstTokenValue);
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.false;
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
expect(await this.token.totalSupply()).to.equal(0n);
});
});
describe('batch', function () {
beforeEach(async function () {
await this.token.$_mintBatch(
this.holder,
[firstTokenId, secondTokenId],
[firstTokenValue, secondTokenValue],
'0x',
);
await this.token.$_burnBatch(this.holder, [firstTokenId, secondTokenId], [firstTokenValue, secondTokenValue]);
});
it('exist', async function () {
expect(await this.token.exists(firstTokenId)).to.be.false;
expect(await this.token.exists(secondTokenId)).to.be.false;
});
it('totalSupply', async function () {
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
expect(await this.token.totalSupply(ethers.Typed.uint256(secondTokenId))).to.equal(0n);
expect(await this.token.totalSupply()).to.equal(0n);
});
});
});
describe('other', function () {
it('supply unaffected by no-op', async function () {
await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, [firstTokenId], [firstTokenValue]);
expect(await this.token.totalSupply(ethers.Typed.uint256(firstTokenId))).to.equal(0n);
expect(await this.token.totalSupply()).to.equal(0n);
});
});
});

View File

@@ -0,0 +1,70 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const erc1155Uri = 'https://token.com/nfts/';
const baseUri = 'https://token.com/';
const tokenId = 1n;
const value = 3000n;
describe('ERC1155URIStorage', function () {
describe('with base uri set', function () {
async function fixture() {
const [holder] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC1155URIStorage', [erc1155Uri]);
await token.$_setBaseURI(baseUri);
await token.$_mint(holder, tokenId, value, '0x');
return { token, holder };
}
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('can request the token uri, returning the erc1155 uri if no token uri was set', async function () {
expect(await this.token.uri(tokenId)).to.equal(erc1155Uri);
});
it('can request the token uri, returning the concatenated uri if a token uri was set', async function () {
const tokenUri = '1234/';
const expectedUri = `${baseUri}${tokenUri}`;
await expect(this.token.$_setURI(ethers.Typed.uint256(tokenId), tokenUri))
.to.emit(this.token, 'URI')
.withArgs(expectedUri, tokenId);
expect(await this.token.uri(tokenId)).to.equal(expectedUri);
});
});
describe('with base uri set to the empty string', function () {
async function fixture() {
const [holder] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC1155URIStorage', ['']);
await token.$_mint(holder, tokenId, value, '0x');
return { token, holder };
}
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('can request the token uri, returning an empty string if no token uri was set', async function () {
expect(await this.token.uri(tokenId)).to.equal('');
});
it('can request the token uri, returning the token uri if a token uri was set', async function () {
const tokenUri = 'ipfs://1234/';
await expect(this.token.$_setURI(ethers.Typed.uint256(tokenId), tokenUri))
.to.emit(this.token, 'URI')
.withArgs(tokenUri, tokenId);
expect(await this.token.uri(tokenId)).to.equal(tokenUri);
});
});
});