Add ERC6909 Implementation along with extensions (#5394)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Ernesto García <ernestognw@gmail.com>
This commit is contained in:
216
test/token/ERC6909/ERC6909.behavior.js
Normal file
216
test/token/ERC6909/ERC6909.behavior.js
Normal file
@ -0,0 +1,216 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
function shouldBehaveLikeERC6909() {
|
||||
const firstTokenId = 1n;
|
||||
const secondTokenId = 2n;
|
||||
const randomTokenId = 125523n;
|
||||
|
||||
const firstTokenSupply = 2000n;
|
||||
const secondTokenSupply = 3000n;
|
||||
const amount = 100n;
|
||||
|
||||
describe('like an ERC6909', function () {
|
||||
describe('balanceOf', function () {
|
||||
describe("when accounts don't own tokens", function () {
|
||||
it('return zero', async function () {
|
||||
await expect(this.token.balanceOf(this.holder, firstTokenId)).to.eventually.be.equal(0n);
|
||||
await expect(this.token.balanceOf(this.holder, secondTokenId)).to.eventually.be.equal(0n);
|
||||
await expect(this.token.balanceOf(this.other, randomTokenId)).to.eventually.be.equal(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when accounts own some tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenSupply);
|
||||
await this.token.$_mint(this.holder, secondTokenId, secondTokenSupply);
|
||||
});
|
||||
|
||||
it('returns amount owned by the given address', async function () {
|
||||
await expect(this.token.balanceOf(this.holder, firstTokenId)).to.eventually.be.equal(firstTokenSupply);
|
||||
await expect(this.token.balanceOf(this.holder, secondTokenId)).to.eventually.be.equal(secondTokenSupply);
|
||||
await expect(this.token.balanceOf(this.other, firstTokenId)).to.eventually.be.equal(0n);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setOperator', function () {
|
||||
it('emits an an OperatorSet event and updated the value', async function () {
|
||||
await expect(this.token.connect(this.holder).setOperator(this.operator, true))
|
||||
.to.emit(this.token, 'OperatorSet')
|
||||
.withArgs(this.holder, this.operator, true);
|
||||
|
||||
// operator for holder
|
||||
await expect(this.token.isOperator(this.holder, this.operator)).to.eventually.be.true;
|
||||
|
||||
// not operator for other account
|
||||
await expect(this.token.isOperator(this.other, this.operator)).to.eventually.be.false;
|
||||
});
|
||||
|
||||
it('can unset the operator approval', async function () {
|
||||
await this.token.connect(this.holder).setOperator(this.operator, true);
|
||||
|
||||
// before
|
||||
await expect(this.token.isOperator(this.holder, this.operator)).to.eventually.be.true;
|
||||
|
||||
// unset
|
||||
await expect(this.token.connect(this.holder).setOperator(this.operator, false))
|
||||
.to.emit(this.token, 'OperatorSet')
|
||||
.withArgs(this.holder, this.operator, false);
|
||||
|
||||
// after
|
||||
await expect(this.token.isOperator(this.holder, this.operator)).to.eventually.be.false;
|
||||
});
|
||||
|
||||
it('cannot set address(0) as an operator', async function () {
|
||||
await expect(this.token.connect(this.holder).setOperator(ethers.ZeroAddress, true))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidSpender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('approve', function () {
|
||||
it('emits an Approval event and updates allowance', async function () {
|
||||
await expect(this.token.connect(this.holder).approve(this.operator, firstTokenId, firstTokenSupply))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.holder, this.operator, firstTokenId, firstTokenSupply);
|
||||
|
||||
// approved
|
||||
await expect(this.token.allowance(this.holder, this.operator, firstTokenId)).to.eventually.be.equal(
|
||||
firstTokenSupply,
|
||||
);
|
||||
// other account is not approved
|
||||
await expect(this.token.allowance(this.other, this.operator, firstTokenId)).to.eventually.be.equal(0n);
|
||||
});
|
||||
|
||||
it('can unset the approval', async function () {
|
||||
await expect(this.token.connect(this.holder).approve(this.operator, firstTokenId, 0n))
|
||||
.to.emit(this.token, 'Approval')
|
||||
.withArgs(this.holder, this.operator, firstTokenId, 0n);
|
||||
await expect(this.token.allowance(this.holder, this.operator, firstTokenId)).to.eventually.be.equal(0n);
|
||||
});
|
||||
|
||||
it('cannot give allowance to address(0)', async function () {
|
||||
await expect(this.token.connect(this.holder).approve(ethers.ZeroAddress, firstTokenId, firstTokenSupply))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidSpender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfer', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenSupply);
|
||||
await this.token.$_mint(this.holder, secondTokenId, secondTokenSupply);
|
||||
});
|
||||
|
||||
it('transfers to the zero address are blocked', async function () {
|
||||
await expect(this.token.connect(this.holder).transfer(ethers.ZeroAddress, firstTokenId, firstTokenSupply))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('reverts when insufficient balance', async function () {
|
||||
await expect(this.token.connect(this.holder).transfer(this.recipient, firstTokenId, firstTokenSupply + 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InsufficientBalance')
|
||||
.withArgs(this.holder, firstTokenSupply, firstTokenSupply + 1n, firstTokenId);
|
||||
});
|
||||
|
||||
it('emits event and transfers tokens', async function () {
|
||||
await expect(this.token.connect(this.holder).transfer(this.recipient, firstTokenId, amount))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.holder, this.recipient, firstTokenId, amount);
|
||||
|
||||
await expect(this.token.balanceOf(this.holder, firstTokenId)).to.eventually.equal(firstTokenSupply - amount);
|
||||
await expect(this.token.balanceOf(this.recipient, firstTokenId)).to.eventually.equal(amount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transferFrom', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenSupply);
|
||||
await this.token.$_mint(this.holder, secondTokenId, secondTokenSupply);
|
||||
});
|
||||
|
||||
it('transfer from self', async function () {
|
||||
await expect(this.token.connect(this.holder).transferFrom(this.holder, this.recipient, firstTokenId, amount))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.holder, this.holder, this.recipient, firstTokenId, amount);
|
||||
|
||||
await expect(this.token.balanceOf(this.holder, firstTokenId)).to.eventually.equal(firstTokenSupply - amount);
|
||||
await expect(this.token.balanceOf(this.recipient, firstTokenId)).to.eventually.equal(amount);
|
||||
});
|
||||
|
||||
describe('with approval', async function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).approve(this.operator, firstTokenId, amount);
|
||||
});
|
||||
|
||||
it('reverts when insufficient allowance', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.operator).transferFrom(this.holder, this.recipient, firstTokenId, amount + 1n),
|
||||
)
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InsufficientAllowance')
|
||||
.withArgs(this.operator, amount, amount + 1n, firstTokenId);
|
||||
});
|
||||
|
||||
it('should emit transfer event and update approval (without an Approval event)', async function () {
|
||||
await expect(
|
||||
this.token.connect(this.operator).transferFrom(this.holder, this.recipient, firstTokenId, amount - 1n),
|
||||
)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.operator, this.holder, this.recipient, firstTokenId, amount - 1n)
|
||||
.to.not.emit(this.token, 'Approval');
|
||||
|
||||
await expect(this.token.allowance(this.holder, this.operator, firstTokenId)).to.eventually.equal(1n);
|
||||
});
|
||||
|
||||
it("shouldn't reduce allowance when infinite", async function () {
|
||||
await this.token.connect(this.holder).approve(this.operator, firstTokenId, ethers.MaxUint256);
|
||||
|
||||
await this.token.connect(this.operator).transferFrom(this.holder, this.recipient, firstTokenId, amount);
|
||||
|
||||
await expect(this.token.allowance(this.holder, this.operator, firstTokenId)).to.eventually.equal(
|
||||
ethers.MaxUint256,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with operator approval', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.holder).setOperator(this.operator, true);
|
||||
await this.token.$_mint(this.holder, firstTokenId, firstTokenSupply);
|
||||
});
|
||||
|
||||
it('operator can transfer', async function () {
|
||||
await expect(this.token.connect(this.operator).transferFrom(this.holder, this.recipient, firstTokenId, amount))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.operator, this.holder, this.recipient, firstTokenId, amount);
|
||||
|
||||
await expect(this.token.balanceOf(this.holder, firstTokenId)).to.eventually.equal(firstTokenSupply - amount);
|
||||
await expect(this.token.balanceOf(this.recipient, firstTokenId)).to.eventually.equal(amount);
|
||||
});
|
||||
|
||||
it('operator transfer does not reduce allowance', async function () {
|
||||
// Also give allowance
|
||||
await this.token.connect(this.holder).approve(this.operator, firstTokenId, firstTokenSupply);
|
||||
|
||||
await expect(this.token.connect(this.operator).transferFrom(this.holder, this.recipient, firstTokenId, amount))
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.operator, this.holder, this.recipient, firstTokenId, amount);
|
||||
|
||||
await expect(this.token.allowance(this.holder, this.operator, firstTokenId)).to.eventually.equal(
|
||||
firstTokenSupply,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC6909']);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeERC6909,
|
||||
};
|
||||
104
test/token/ERC6909/ERC6909.test.js
Normal file
104
test/token/ERC6909/ERC6909.test.js
Normal file
@ -0,0 +1,104 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC6909 } = require('./ERC6909.behavior');
|
||||
|
||||
async function fixture() {
|
||||
const [holder, operator, recipient, other] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC6909');
|
||||
return { token, holder, operator, recipient, other };
|
||||
}
|
||||
|
||||
describe('ERC6909', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC6909();
|
||||
|
||||
describe('internal functions', function () {
|
||||
const tokenId = 1990n;
|
||||
const mintValue = 9001n;
|
||||
const burnValue = 3000n;
|
||||
|
||||
describe('_mint', function () {
|
||||
it('reverts with a zero destination address', async function () {
|
||||
await expect(this.token.$_mint(ethers.ZeroAddress, tokenId, mintValue))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
this.tx = await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue);
|
||||
});
|
||||
|
||||
it('emits a Transfer event from 0 address', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.operator, ethers.ZeroAddress, this.holder, tokenId, mintValue);
|
||||
});
|
||||
|
||||
it('credits the minted token value', async function () {
|
||||
await expect(this.token.balanceOf(this.holder, tokenId)).to.eventually.be.equal(mintValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_transfer', function () {
|
||||
it('reverts when transferring from the zero address', async function () {
|
||||
await expect(this.token.$_transfer(ethers.ZeroAddress, this.holder, 1n, 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('reverts when transferring to the zero address', async function () {
|
||||
await expect(this.token.$_transfer(this.holder, ethers.ZeroAddress, 1n, 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidReceiver')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it('reverts with a zero from address', async function () {
|
||||
await expect(this.token.$_burn(ethers.ZeroAddress, tokenId, burnValue))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidSender')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
describe('with burned tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue);
|
||||
this.tx = await this.token.connect(this.operator).$_burn(this.holder, tokenId, burnValue);
|
||||
});
|
||||
|
||||
it('emits a Transfer event to 0 address', async function () {
|
||||
await expect(this.tx)
|
||||
.to.emit(this.token, 'Transfer')
|
||||
.withArgs(this.operator, this.holder, ethers.ZeroAddress, tokenId, burnValue);
|
||||
});
|
||||
|
||||
it('debits the burned token value', async function () {
|
||||
await expect(this.token.balanceOf(this.holder, tokenId)).to.eventually.be.equal(mintValue - burnValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_approve', function () {
|
||||
it('reverts when the owner is the zero address', async function () {
|
||||
await expect(this.token.$_approve(ethers.ZeroAddress, this.recipient, 1n, 1n))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidApprover')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_setOperator', function () {
|
||||
it('reverts when the owner is the zero address', async function () {
|
||||
await expect(this.token.$_setOperator(ethers.ZeroAddress, this.operator, true))
|
||||
.to.be.revertedWithCustomError(this.token, 'ERC6909InvalidApprover')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
49
test/token/ERC6909/extensions/ERC6909ContentURI.test.js
Normal file
49
test/token/ERC6909/extensions/ERC6909ContentURI.test.js
Normal file
@ -0,0 +1,49 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
async function fixture() {
|
||||
const token = await ethers.deployContract('$ERC6909ContentURI');
|
||||
return { token };
|
||||
}
|
||||
|
||||
describe('ERC6909ContentURI', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('contractURI', function () {
|
||||
it('is empty string be default', async function () {
|
||||
await expect(this.token.contractURI()).to.eventually.equal('');
|
||||
});
|
||||
|
||||
it('is settable by internal setter', async function () {
|
||||
await this.token.$_setContractURI('https://example.com');
|
||||
await expect(this.token.contractURI()).to.eventually.equal('https://example.com');
|
||||
});
|
||||
|
||||
it('emits an event when set', async function () {
|
||||
await expect(this.token.$_setContractURI('https://example.com')).to.emit(this.token, 'ContractURIUpdated');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokenURI', function () {
|
||||
it('is empty string be default', async function () {
|
||||
await expect(this.token.tokenURI(1n)).to.eventually.equal('');
|
||||
});
|
||||
|
||||
it('can be set by dedicated setter', async function () {
|
||||
await this.token.$_setTokenURI(1n, 'https://example.com/1');
|
||||
await expect(this.token.tokenURI(1n)).to.eventually.equal('https://example.com/1');
|
||||
|
||||
// Only set for the specified token ID
|
||||
await expect(this.token.tokenURI(2n)).to.eventually.equal('');
|
||||
});
|
||||
|
||||
it('emits an event when set', async function () {
|
||||
await expect(this.token.$_setTokenURI(1n, 'https://example.com/1'))
|
||||
.to.emit(this.token, 'URI')
|
||||
.withArgs('https://example.com/1', 1n);
|
||||
});
|
||||
});
|
||||
});
|
||||
58
test/token/ERC6909/extensions/ERC6909Metadata.test.js
Normal file
58
test/token/ERC6909/extensions/ERC6909Metadata.test.js
Normal file
@ -0,0 +1,58 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
async function fixture() {
|
||||
const token = await ethers.deployContract('$ERC6909Metadata');
|
||||
return { token };
|
||||
}
|
||||
|
||||
describe('ERC6909Metadata', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('name', function () {
|
||||
it('is empty string be default', async function () {
|
||||
await expect(this.token.name(1n)).to.eventually.equal('');
|
||||
});
|
||||
|
||||
it('can be set by dedicated setter', async function () {
|
||||
await expect(this.token.$_setName(1n, 'My Token'))
|
||||
.to.emit(this.token, 'ERC6909NameUpdated')
|
||||
.withArgs(1n, 'My Token');
|
||||
await expect(this.token.name(1n)).to.eventually.equal('My Token');
|
||||
|
||||
// Only set for the specified token ID
|
||||
await expect(this.token.name(2n)).to.eventually.equal('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('symbol', function () {
|
||||
it('is empty string be default', async function () {
|
||||
await expect(this.token.symbol(1n)).to.eventually.equal('');
|
||||
});
|
||||
|
||||
it('can be set by dedicated setter', async function () {
|
||||
await expect(this.token.$_setSymbol(1n, 'MTK')).to.emit(this.token, 'ERC6909SymbolUpdated').withArgs(1n, 'MTK');
|
||||
await expect(this.token.symbol(1n)).to.eventually.equal('MTK');
|
||||
|
||||
// Only set for the specified token ID
|
||||
await expect(this.token.symbol(2n)).to.eventually.equal('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('decimals', function () {
|
||||
it('is 0 by default', async function () {
|
||||
await expect(this.token.decimals(1n)).to.eventually.equal(0);
|
||||
});
|
||||
|
||||
it('can be set by dedicated setter', async function () {
|
||||
await expect(this.token.$_setDecimals(1n, 18)).to.emit(this.token, 'ERC6909DecimalsUpdated').withArgs(1n, 18);
|
||||
await expect(this.token.decimals(1n)).to.eventually.equal(18);
|
||||
|
||||
// Only set for the specified token ID
|
||||
await expect(this.token.decimals(2n)).to.eventually.equal(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
53
test/token/ERC6909/extensions/ERC6909TokenSupply.test.js
Normal file
53
test/token/ERC6909/extensions/ERC6909TokenSupply.test.js
Normal file
@ -0,0 +1,53 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC6909 } = require('../ERC6909.behavior');
|
||||
|
||||
async function fixture() {
|
||||
const [holder, operator, recipient, other] = await ethers.getSigners();
|
||||
const token = await ethers.deployContract('$ERC6909TokenSupply');
|
||||
return { token, holder, operator, recipient, other };
|
||||
}
|
||||
|
||||
describe('ERC6909TokenSupply', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC6909();
|
||||
|
||||
describe('totalSupply', function () {
|
||||
it('is zero before any mint', async function () {
|
||||
await expect(this.token.totalSupply(1n)).to.eventually.be.equal(0n);
|
||||
});
|
||||
|
||||
it('minting tokens increases the total supply', async function () {
|
||||
await this.token.$_mint(this.holder, 1n, 17n);
|
||||
await expect(this.token.totalSupply(1n)).to.eventually.be.equal(17n);
|
||||
});
|
||||
|
||||
describe('with tokens minted', function () {
|
||||
const supply = 1000n;
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(this.holder, 1n, supply);
|
||||
});
|
||||
|
||||
it('burning tokens decreases the total supply', async function () {
|
||||
await this.token.$_burn(this.holder, 1n, 17n);
|
||||
await expect(this.token.totalSupply(1n)).to.eventually.be.equal(supply - 17n);
|
||||
});
|
||||
|
||||
it('supply unaffected by transfers', async function () {
|
||||
await this.token.$_transfer(this.holder, this.recipient, 1n, 42n);
|
||||
await expect(this.token.totalSupply(1n)).to.eventually.be.equal(supply);
|
||||
});
|
||||
|
||||
it('supply unaffected by no-op', async function () {
|
||||
await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, 1n, 42n);
|
||||
await expect(this.token.totalSupply(1n)).to.eventually.be.equal(supply);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user