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:
Arr00
2025-02-04 07:47:21 -05:00
committed by GitHub
parent df878c87fc
commit 43b3319e5b
19 changed files with 1008 additions and 0 deletions

View File

@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC6909} from "../draft-ERC6909.sol";
import {IERC6909ContentURI} from "../../../interfaces/draft-IERC6909.sol";
/**
* @dev Implementation of the Content URI extension defined in ERC6909.
*/
contract ERC6909ContentURI is ERC6909, IERC6909ContentURI {
string private _contractURI;
mapping(uint256 id => string) private _tokenURIs;
/// @dev Event emitted when the contract URI is changed. See https://eips.ethereum.org/EIPS/eip-7572[ERC-7572] for details.
event ContractURIUpdated();
/// @dev See {IERC1155-URI}
event URI(string value, uint256 indexed id);
/// @inheritdoc IERC6909ContentURI
function contractURI() public view virtual override returns (string memory) {
return _contractURI;
}
/// @inheritdoc IERC6909ContentURI
function tokenURI(uint256 id) public view virtual override returns (string memory) {
return _tokenURIs[id];
}
/**
* @dev Sets the {contractURI} for the contract.
*
* Emits a {ContractURIUpdated} event.
*/
function _setContractURI(string memory newContractURI) internal virtual {
_contractURI = newContractURI;
emit ContractURIUpdated();
}
/**
* @dev Sets the {tokenURI} for a given token of type `id`.
*
* Emits a {URI} event.
*/
function _setTokenURI(uint256 id, string memory newTokenURI) internal virtual {
_tokenURIs[id] = newTokenURI;
emit URI(newTokenURI, id);
}
}

View File

@ -0,0 +1,76 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC6909} from "../draft-ERC6909.sol";
import {IERC6909Metadata} from "../../../interfaces/draft-IERC6909.sol";
/**
* @dev Implementation of the Metadata extension defined in ERC6909. Exposes the name, symbol, and decimals of each token id.
*/
contract ERC6909Metadata is ERC6909, IERC6909Metadata {
struct TokenMetadata {
string name;
string symbol;
uint8 decimals;
}
mapping(uint256 id => TokenMetadata) private _tokenMetadata;
/// @dev The name of the token of type `id` was updated to `newName`.
event ERC6909NameUpdated(uint256 indexed id, string newName);
/// @dev The symbol for the token of type `id` was updated to `newSymbol`.
event ERC6909SymbolUpdated(uint256 indexed id, string newSymbol);
/// @dev The decimals value for token of type `id` was updated to `newDecimals`.
event ERC6909DecimalsUpdated(uint256 indexed id, uint8 newDecimals);
/// @inheritdoc IERC6909Metadata
function name(uint256 id) public view virtual override returns (string memory) {
return _tokenMetadata[id].name;
}
/// @inheritdoc IERC6909Metadata
function symbol(uint256 id) public view virtual override returns (string memory) {
return _tokenMetadata[id].symbol;
}
/// @inheritdoc IERC6909Metadata
function decimals(uint256 id) public view virtual override returns (uint8) {
return _tokenMetadata[id].decimals;
}
/**
* @dev Sets the `name` for a given token of type `id`.
*
* Emits an {ERC6909NameUpdated} event.
*/
function _setName(uint256 id, string memory newName) internal virtual {
_tokenMetadata[id].name = newName;
emit ERC6909NameUpdated(id, newName);
}
/**
* @dev Sets the `symbol` for a given token of type `id`.
*
* Emits an {ERC6909SymbolUpdated} event.
*/
function _setSymbol(uint256 id, string memory newSymbol) internal virtual {
_tokenMetadata[id].symbol = newSymbol;
emit ERC6909SymbolUpdated(id, newSymbol);
}
/**
* @dev Sets the `decimals` for a given token of type `id`.
*
* Emits an {ERC6909DecimalsUpdated} event.
*/
function _setDecimals(uint256 id, uint8 newDecimals) internal virtual {
_tokenMetadata[id].decimals = newDecimals;
emit ERC6909DecimalsUpdated(id, newDecimals);
}
}

View File

@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC6909} from "../draft-ERC6909.sol";
import {IERC6909TokenSupply} from "../../../interfaces/draft-IERC6909.sol";
/**
* @dev Implementation of the Token Supply extension defined in ERC6909.
* Tracks the total supply of each token id individually.
*/
contract ERC6909TokenSupply is ERC6909, IERC6909TokenSupply {
mapping(uint256 id => uint256) private _totalSupplies;
/// @inheritdoc IERC6909TokenSupply
function totalSupply(uint256 id) public view virtual override returns (uint256) {
return _totalSupplies[id];
}
/// @dev Override the `_update` function to update the total supply of each token id as necessary.
function _update(address from, address to, uint256 id, uint256 amount) internal virtual override {
super._update(from, to, id, amount);
if (from == address(0)) {
_totalSupplies[id] += amount;
}
if (to == address(0)) {
unchecked {
// amount <= _balances[id][from] <= _totalSupplies[id]
_totalSupplies[id] -= amount;
}
}
}
}