Add ERC1155.totalSupply that returns overall supply count (#3962)
This commit is contained in:
5
.changeset/chilled-spiders-attack.md
Normal file
5
.changeset/chilled-spiders-attack.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'openzeppelin-solidity': major
|
||||
---
|
||||
|
||||
`ERC1155Supply`: add a `totalSupply()` function that returns the total amount of token circulating, this change will restrict the total tokens minted across all ids to 2\*\*256-1 .
|
||||
@ -15,6 +15,7 @@ import "../ERC1155.sol";
|
||||
*/
|
||||
abstract contract ERC1155Supply is ERC1155 {
|
||||
mapping(uint256 => uint256) private _totalSupply;
|
||||
uint256 private _totalSupplyAll;
|
||||
|
||||
/**
|
||||
* @dev Total amount of tokens in with a given id.
|
||||
@ -23,6 +24,13 @@ abstract contract ERC1155Supply is ERC1155 {
|
||||
return _totalSupply[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Total amount of tokens.
|
||||
*/
|
||||
function totalSupply() public view virtual returns (uint256) {
|
||||
return _totalSupplyAll;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Indicates whether any token exist with a given id, or not.
|
||||
*/
|
||||
@ -41,21 +49,33 @@ abstract contract ERC1155Supply is ERC1155 {
|
||||
bytes memory data
|
||||
) internal virtual override {
|
||||
if (from == address(0)) {
|
||||
uint256 totalMintAmount = 0;
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
_totalSupply[ids[i]] += amounts[i];
|
||||
uint256 amount = amounts[i];
|
||||
_totalSupply[ids[i]] += amount;
|
||||
totalMintAmount += amount;
|
||||
}
|
||||
_totalSupplyAll += totalMintAmount;
|
||||
}
|
||||
|
||||
if (to == address(0)) {
|
||||
uint256 totalBurnAmount = 0;
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
uint256 id = ids[i];
|
||||
uint256 amount = amounts[i];
|
||||
uint256 supply = _totalSupply[id];
|
||||
require(supply >= amount, "ERC1155: burn amount exceeds totalSupply");
|
||||
unchecked {
|
||||
// Overflow not possible: amounts[i] <= totalSupply(i)
|
||||
_totalSupply[id] = supply - amount;
|
||||
// Overflow not possible: sum(amounts[i]) <= sum(totalSupply(i)) <= totalSupplyAll
|
||||
totalBurnAmount += amount;
|
||||
}
|
||||
}
|
||||
unchecked {
|
||||
// Overflow not possible: totalBurnAmount = sum(amounts[i]) <= sum(totalSupply(i)) <= totalSupplyAll
|
||||
_totalSupplyAll -= totalBurnAmount;
|
||||
}
|
||||
}
|
||||
super._update(from, to, ids, amounts, data);
|
||||
}
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
const { BN } = require('@openzeppelin/test-helpers');
|
||||
const { BN, constants } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const ERC1155Supply = artifacts.require('$ERC1155Supply');
|
||||
|
||||
contract('ERC1155Supply', function (accounts) {
|
||||
@ -25,7 +27,8 @@ contract('ERC1155Supply', function (accounts) {
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply(uint256)'](firstTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply()']()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
@ -40,7 +43,8 @@ contract('ERC1155Supply', function (accounts) {
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal(firstTokenAmount);
|
||||
expect(await this.token.methods['totalSupply(uint256)'](firstTokenId)).to.be.bignumber.equal(firstTokenAmount);
|
||||
expect(await this.token.methods['totalSupply()']()).to.be.bignumber.equal(firstTokenAmount);
|
||||
});
|
||||
});
|
||||
|
||||
@ -60,8 +64,13 @@ contract('ERC1155Supply', function (accounts) {
|
||||
});
|
||||
|
||||
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);
|
||||
expect(await this.token.methods['totalSupply(uint256)'](firstTokenId)).to.be.bignumber.equal(firstTokenAmount);
|
||||
expect(await this.token.methods['totalSupply(uint256)'](secondTokenId)).to.be.bignumber.equal(
|
||||
secondTokenAmount,
|
||||
);
|
||||
expect(await this.token.methods['totalSupply()']()).to.be.bignumber.equal(
|
||||
firstTokenAmount.add(secondTokenAmount),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -78,7 +87,8 @@ contract('ERC1155Supply', function (accounts) {
|
||||
});
|
||||
|
||||
it('totalSupply', async function () {
|
||||
expect(await this.token.totalSupply(firstTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply(uint256)'](firstTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply()']()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
@ -99,9 +109,20 @@ contract('ERC1155Supply', function (accounts) {
|
||||
});
|
||||
|
||||
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');
|
||||
expect(await this.token.methods['totalSupply(uint256)'](firstTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply(uint256)'](secondTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply()']()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('other', function () {
|
||||
it('supply unaffected by no-op', async function () {
|
||||
this.token.safeTransferFrom(ZERO_ADDRESS, ZERO_ADDRESS, firstTokenId, firstTokenAmount, '0x', {
|
||||
from: ZERO_ADDRESS,
|
||||
});
|
||||
expect(await this.token.methods['totalSupply(uint256)'](firstTokenId)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.methods['totalSupply()']()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user