dexorder
This commit is contained in:
402
lib_openzeppelin_contracts/contracts/token/ERC1155/ERC1155.sol
Normal file
402
lib_openzeppelin_contracts/contracts/token/ERC1155/ERC1155.sol
Normal file
@@ -0,0 +1,402 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/ERC1155.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC1155} from "./IERC1155.sol";
|
||||
import {IERC1155MetadataURI} from "./extensions/IERC1155MetadataURI.sol";
|
||||
import {ERC1155Utils} from "./utils/ERC1155Utils.sol";
|
||||
import {Context} from "../../utils/Context.sol";
|
||||
import {IERC165, ERC165} from "../../utils/introspection/ERC165.sol";
|
||||
import {Arrays} from "../../utils/Arrays.sol";
|
||||
import {IERC1155Errors} from "../../interfaces/draft-IERC6093.sol";
|
||||
|
||||
/**
|
||||
* @dev Implementation of the basic standard multi-token.
|
||||
* See https://eips.ethereum.org/EIPS/eip-1155
|
||||
* Originally based on code by Enjin: https://github.com/enjin/erc-1155
|
||||
*/
|
||||
abstract contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI, IERC1155Errors {
|
||||
using Arrays for uint256[];
|
||||
using Arrays for address[];
|
||||
|
||||
mapping(uint256 id => mapping(address account => uint256)) private _balances;
|
||||
|
||||
mapping(address account => mapping(address operator => bool)) private _operatorApprovals;
|
||||
|
||||
// Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
|
||||
string private _uri;
|
||||
|
||||
/**
|
||||
* @dev See {_setURI}.
|
||||
*/
|
||||
constructor(string memory uri_) {
|
||||
_setURI(uri_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}.
|
||||
*/
|
||||
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
|
||||
return
|
||||
interfaceId == type(IERC1155).interfaceId ||
|
||||
interfaceId == type(IERC1155MetadataURI).interfaceId ||
|
||||
super.supportsInterface(interfaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155MetadataURI-uri}.
|
||||
*
|
||||
* This implementation returns the same URI for *all* token types. It relies
|
||||
* on the token type ID substitution mechanism
|
||||
* https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the ERC].
|
||||
*
|
||||
* Clients calling this function must replace the `\{id\}` substring with the
|
||||
* actual token type ID.
|
||||
*/
|
||||
function uri(uint256 /* id */) public view virtual returns (string memory) {
|
||||
return _uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-balanceOf}.
|
||||
*/
|
||||
function balanceOf(address account, uint256 id) public view virtual returns (uint256) {
|
||||
return _balances[id][account];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-balanceOfBatch}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `accounts` and `ids` must have the same length.
|
||||
*/
|
||||
function balanceOfBatch(
|
||||
address[] memory accounts,
|
||||
uint256[] memory ids
|
||||
) public view virtual returns (uint256[] memory) {
|
||||
if (accounts.length != ids.length) {
|
||||
revert ERC1155InvalidArrayLength(ids.length, accounts.length);
|
||||
}
|
||||
|
||||
uint256[] memory batchBalances = new uint256[](accounts.length);
|
||||
|
||||
for (uint256 i = 0; i < accounts.length; ++i) {
|
||||
batchBalances[i] = balanceOf(accounts.unsafeMemoryAccess(i), ids.unsafeMemoryAccess(i));
|
||||
}
|
||||
|
||||
return batchBalances;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-setApprovalForAll}.
|
||||
*/
|
||||
function setApprovalForAll(address operator, bool approved) public virtual {
|
||||
_setApprovalForAll(_msgSender(), operator, approved);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-isApprovedForAll}.
|
||||
*/
|
||||
function isApprovedForAll(address account, address operator) public view virtual returns (bool) {
|
||||
return _operatorApprovals[account][operator];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-safeTransferFrom}.
|
||||
*/
|
||||
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) public virtual {
|
||||
address sender = _msgSender();
|
||||
if (from != sender && !isApprovedForAll(from, sender)) {
|
||||
revert ERC1155MissingApprovalForAll(sender, from);
|
||||
}
|
||||
_safeTransferFrom(from, to, id, value, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-safeBatchTransferFrom}.
|
||||
*/
|
||||
function safeBatchTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values,
|
||||
bytes memory data
|
||||
) public virtual {
|
||||
address sender = _msgSender();
|
||||
if (from != sender && !isApprovedForAll(from, sender)) {
|
||||
revert ERC1155MissingApprovalForAll(sender, from);
|
||||
}
|
||||
_safeBatchTransferFrom(from, to, ids, values, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Transfers a `value` amount of tokens of type `id` from `from` to `to`. Will mint (or burn) if `from`
|
||||
* (or `to`) is the zero address.
|
||||
*
|
||||
* Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - If `to` refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received}
|
||||
* or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
|
||||
* - `ids` and `values` must have the same length.
|
||||
*
|
||||
* NOTE: The ERC-1155 acceptance check is not performed in this function. See {_updateWithAcceptanceCheck} instead.
|
||||
*/
|
||||
function _update(address from, address to, uint256[] memory ids, uint256[] memory values) internal virtual {
|
||||
if (ids.length != values.length) {
|
||||
revert ERC1155InvalidArrayLength(ids.length, values.length);
|
||||
}
|
||||
|
||||
address operator = _msgSender();
|
||||
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
uint256 id = ids.unsafeMemoryAccess(i);
|
||||
uint256 value = values.unsafeMemoryAccess(i);
|
||||
|
||||
if (from != address(0)) {
|
||||
uint256 fromBalance = _balances[id][from];
|
||||
if (fromBalance < value) {
|
||||
revert ERC1155InsufficientBalance(from, fromBalance, value, id);
|
||||
}
|
||||
unchecked {
|
||||
// Overflow not possible: value <= fromBalance
|
||||
_balances[id][from] = fromBalance - value;
|
||||
}
|
||||
}
|
||||
|
||||
if (to != address(0)) {
|
||||
_balances[id][to] += value;
|
||||
}
|
||||
}
|
||||
|
||||
if (ids.length == 1) {
|
||||
uint256 id = ids.unsafeMemoryAccess(0);
|
||||
uint256 value = values.unsafeMemoryAccess(0);
|
||||
emit TransferSingle(operator, from, to, id, value);
|
||||
} else {
|
||||
emit TransferBatch(operator, from, to, ids, values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Version of {_update} that performs the token acceptance check by calling
|
||||
* {IERC1155Receiver-onERC1155Received} or {IERC1155Receiver-onERC1155BatchReceived} on the receiver address if it
|
||||
* contains code (eg. is a smart contract at the moment of execution).
|
||||
*
|
||||
* IMPORTANT: Overriding this function is discouraged because it poses a reentrancy risk from the receiver. So any
|
||||
* update to the contract state after this function would break the check-effect-interaction pattern. Consider
|
||||
* overriding {_update} instead.
|
||||
*/
|
||||
function _updateWithAcceptanceCheck(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values,
|
||||
bytes memory data
|
||||
) internal virtual {
|
||||
_update(from, to, ids, values);
|
||||
if (to != address(0)) {
|
||||
address operator = _msgSender();
|
||||
if (ids.length == 1) {
|
||||
uint256 id = ids.unsafeMemoryAccess(0);
|
||||
uint256 value = values.unsafeMemoryAccess(0);
|
||||
ERC1155Utils.checkOnERC1155Received(operator, from, to, id, value, data);
|
||||
} else {
|
||||
ERC1155Utils.checkOnERC1155BatchReceived(operator, from, to, ids, values, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Transfers a `value` tokens of token type `id` from `from` to `to`.
|
||||
*
|
||||
* Emits a {TransferSingle} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `to` cannot be the zero address.
|
||||
* - `from` must have a balance of tokens of type `id` of at least `value` amount.
|
||||
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
|
||||
* acceptance magic value.
|
||||
*/
|
||||
function _safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) internal {
|
||||
if (to == address(0)) {
|
||||
revert ERC1155InvalidReceiver(address(0));
|
||||
}
|
||||
if (from == address(0)) {
|
||||
revert ERC1155InvalidSender(address(0));
|
||||
}
|
||||
(uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
|
||||
_updateWithAcceptanceCheck(from, to, ids, values, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
|
||||
*
|
||||
* Emits a {TransferBatch} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
|
||||
* acceptance magic value.
|
||||
* - `ids` and `values` must have the same length.
|
||||
*/
|
||||
function _safeBatchTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values,
|
||||
bytes memory data
|
||||
) internal {
|
||||
if (to == address(0)) {
|
||||
revert ERC1155InvalidReceiver(address(0));
|
||||
}
|
||||
if (from == address(0)) {
|
||||
revert ERC1155InvalidSender(address(0));
|
||||
}
|
||||
_updateWithAcceptanceCheck(from, to, ids, values, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Sets a new URI for all token types, by relying on the token type ID
|
||||
* substitution mechanism
|
||||
* https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the ERC].
|
||||
*
|
||||
* By this mechanism, any occurrence of the `\{id\}` substring in either the
|
||||
* URI or any of the values in the JSON file at said URI will be replaced by
|
||||
* clients with the token type ID.
|
||||
*
|
||||
* For example, the `https://token-cdn-domain/\{id\}.json` URI would be
|
||||
* interpreted by clients as
|
||||
* `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
|
||||
* for token type ID 0x4cce0.
|
||||
*
|
||||
* See {uri}.
|
||||
*
|
||||
* Because these URIs cannot be meaningfully represented by the {URI} event,
|
||||
* this function emits no events.
|
||||
*/
|
||||
function _setURI(string memory newuri) internal virtual {
|
||||
_uri = newuri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Creates a `value` amount of tokens of type `id`, and assigns them to `to`.
|
||||
*
|
||||
* Emits a {TransferSingle} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `to` cannot be the zero address.
|
||||
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
|
||||
* acceptance magic value.
|
||||
*/
|
||||
function _mint(address to, uint256 id, uint256 value, bytes memory data) internal {
|
||||
if (to == address(0)) {
|
||||
revert ERC1155InvalidReceiver(address(0));
|
||||
}
|
||||
(uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
|
||||
_updateWithAcceptanceCheck(address(0), to, ids, values, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
|
||||
*
|
||||
* Emits a {TransferBatch} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `ids` and `values` must have the same length.
|
||||
* - `to` cannot be the zero address.
|
||||
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
|
||||
* acceptance magic value.
|
||||
*/
|
||||
function _mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) internal {
|
||||
if (to == address(0)) {
|
||||
revert ERC1155InvalidReceiver(address(0));
|
||||
}
|
||||
_updateWithAcceptanceCheck(address(0), to, ids, values, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Destroys a `value` amount of tokens of type `id` from `from`
|
||||
*
|
||||
* Emits a {TransferSingle} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `from` cannot be the zero address.
|
||||
* - `from` must have at least `value` amount of tokens of type `id`.
|
||||
*/
|
||||
function _burn(address from, uint256 id, uint256 value) internal {
|
||||
if (from == address(0)) {
|
||||
revert ERC1155InvalidSender(address(0));
|
||||
}
|
||||
(uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
|
||||
_updateWithAcceptanceCheck(from, address(0), ids, values, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
|
||||
*
|
||||
* Emits a {TransferBatch} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `from` cannot be the zero address.
|
||||
* - `from` must have at least `value` amount of tokens of type `id`.
|
||||
* - `ids` and `values` must have the same length.
|
||||
*/
|
||||
function _burnBatch(address from, uint256[] memory ids, uint256[] memory values) internal {
|
||||
if (from == address(0)) {
|
||||
revert ERC1155InvalidSender(address(0));
|
||||
}
|
||||
_updateWithAcceptanceCheck(from, address(0), ids, values, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Approve `operator` to operate on all of `owner` tokens
|
||||
*
|
||||
* Emits an {ApprovalForAll} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `operator` cannot be the zero address.
|
||||
*/
|
||||
function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
|
||||
if (operator == address(0)) {
|
||||
revert ERC1155InvalidOperator(address(0));
|
||||
}
|
||||
_operatorApprovals[owner][operator] = approved;
|
||||
emit ApprovalForAll(owner, operator, approved);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Creates an array in memory with only one value for each of the elements provided.
|
||||
*/
|
||||
function _asSingletonArrays(
|
||||
uint256 element1,
|
||||
uint256 element2
|
||||
) private pure returns (uint256[] memory array1, uint256[] memory array2) {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
// Load the free memory pointer
|
||||
array1 := mload(0x40)
|
||||
// Set array length to 1
|
||||
mstore(array1, 1)
|
||||
// Store the single element at the next word after the length (where content starts)
|
||||
mstore(add(array1, 0x20), element1)
|
||||
|
||||
// Repeat for next array locating it right after the first array
|
||||
array2 := add(array1, 0x40)
|
||||
mstore(array2, 1)
|
||||
mstore(add(array2, 0x20), element2)
|
||||
|
||||
// Update the free memory pointer by pointing after the second array
|
||||
mstore(0x40, add(array2, 0x40))
|
||||
}
|
||||
}
|
||||
}
|
||||
123
lib_openzeppelin_contracts/contracts/token/ERC1155/IERC1155.sol
Normal file
123
lib_openzeppelin_contracts/contracts/token/ERC1155/IERC1155.sol
Normal file
@@ -0,0 +1,123 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.1) (token/ERC1155/IERC1155.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC165} from "../../utils/introspection/IERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev Required interface of an ERC-1155 compliant contract, as defined in the
|
||||
* https://eips.ethereum.org/EIPS/eip-1155[ERC].
|
||||
*/
|
||||
interface IERC1155 is IERC165 {
|
||||
/**
|
||||
* @dev Emitted when `value` amount of tokens of type `id` are transferred from `from` to `to` by `operator`.
|
||||
*/
|
||||
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
|
||||
|
||||
/**
|
||||
* @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
|
||||
* transfers.
|
||||
*/
|
||||
event TransferBatch(
|
||||
address indexed operator,
|
||||
address indexed from,
|
||||
address indexed to,
|
||||
uint256[] ids,
|
||||
uint256[] values
|
||||
);
|
||||
|
||||
/**
|
||||
* @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
|
||||
* `approved`.
|
||||
*/
|
||||
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
|
||||
|
||||
/**
|
||||
* @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
|
||||
*
|
||||
* If an {URI} event was emitted for `id`, the standard
|
||||
* https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
|
||||
* returned by {IERC1155MetadataURI-uri}.
|
||||
*/
|
||||
event URI(string value, uint256 indexed id);
|
||||
|
||||
/**
|
||||
* @dev Returns the value of tokens of token type `id` owned by `account`.
|
||||
*/
|
||||
function balanceOf(address account, uint256 id) external view returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `accounts` and `ids` must have the same length.
|
||||
*/
|
||||
function balanceOfBatch(
|
||||
address[] calldata accounts,
|
||||
uint256[] calldata ids
|
||||
) external view returns (uint256[] memory);
|
||||
|
||||
/**
|
||||
* @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
|
||||
*
|
||||
* Emits an {ApprovalForAll} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `operator` cannot be the zero address.
|
||||
*/
|
||||
function setApprovalForAll(address operator, bool approved) external;
|
||||
|
||||
/**
|
||||
* @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
|
||||
*
|
||||
* See {setApprovalForAll}.
|
||||
*/
|
||||
function isApprovedForAll(address account, address operator) external view returns (bool);
|
||||
|
||||
/**
|
||||
* @dev Transfers a `value` amount of tokens of type `id` from `from` to `to`.
|
||||
*
|
||||
* WARNING: This function can potentially allow a reentrancy attack when transferring tokens
|
||||
* to an untrusted contract, when invoking {onERC1155Received} on the receiver.
|
||||
* Ensure to follow the checks-effects-interactions pattern and consider employing
|
||||
* reentrancy guards when interacting with untrusted contracts.
|
||||
*
|
||||
* Emits a {TransferSingle} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `to` cannot be the zero address.
|
||||
* - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
|
||||
* - `from` must have a balance of tokens of type `id` of at least `value` amount.
|
||||
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
|
||||
* acceptance magic value.
|
||||
*/
|
||||
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes calldata data) external;
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
|
||||
*
|
||||
* WARNING: This function can potentially allow a reentrancy attack when transferring tokens
|
||||
* to an untrusted contract, when invoking {onERC1155BatchReceived} on the receiver.
|
||||
* Ensure to follow the checks-effects-interactions pattern and consider employing
|
||||
* reentrancy guards when interacting with untrusted contracts.
|
||||
*
|
||||
* Emits either a {TransferSingle} or a {TransferBatch} event, depending on the length of the array arguments.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `ids` and `values` must have the same length.
|
||||
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
|
||||
* acceptance magic value.
|
||||
*/
|
||||
function safeBatchTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] calldata ids,
|
||||
uint256[] calldata values,
|
||||
bytes calldata data
|
||||
) external;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/IERC1155Receiver.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC165} from "../../utils/introspection/IERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev Interface that must be implemented by smart contracts in order to receive
|
||||
* ERC-1155 token transfers.
|
||||
*/
|
||||
interface IERC1155Receiver is IERC165 {
|
||||
/**
|
||||
* @dev Handles the receipt of a single ERC-1155 token type. This function is
|
||||
* called at the end of a `safeTransferFrom` after the balance has been updated.
|
||||
*
|
||||
* NOTE: To accept the transfer, this must return
|
||||
* `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
|
||||
* (i.e. 0xf23a6e61, or its own function selector).
|
||||
*
|
||||
* @param operator The address which initiated the transfer (i.e. msg.sender)
|
||||
* @param from The address which previously owned the token
|
||||
* @param id The ID of the token being transferred
|
||||
* @param value The amount of tokens being transferred
|
||||
* @param data Additional data with no specified format
|
||||
* @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
|
||||
*/
|
||||
function onERC1155Received(
|
||||
address operator,
|
||||
address from,
|
||||
uint256 id,
|
||||
uint256 value,
|
||||
bytes calldata data
|
||||
) external returns (bytes4);
|
||||
|
||||
/**
|
||||
* @dev Handles the receipt of a multiple ERC-1155 token types. This function
|
||||
* is called at the end of a `safeBatchTransferFrom` after the balances have
|
||||
* been updated.
|
||||
*
|
||||
* NOTE: To accept the transfer(s), this must return
|
||||
* `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
|
||||
* (i.e. 0xbc197c81, or its own function selector).
|
||||
*
|
||||
* @param operator The address which initiated the batch transfer (i.e. msg.sender)
|
||||
* @param from The address which previously owned the token
|
||||
* @param ids An array containing ids of each token being transferred (order and length must match values array)
|
||||
* @param values An array containing amounts of each token being transferred (order and length must match ids array)
|
||||
* @param data Additional data with no specified format
|
||||
* @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
|
||||
*/
|
||||
function onERC1155BatchReceived(
|
||||
address operator,
|
||||
address from,
|
||||
uint256[] calldata ids,
|
||||
uint256[] calldata values,
|
||||
bytes calldata data
|
||||
) external returns (bytes4);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
= ERC-1155
|
||||
|
||||
[.readme-notice]
|
||||
NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/token/erc1155
|
||||
|
||||
This set of interfaces and contracts are all related to the https://eips.ethereum.org/EIPS/eip-1155[ERC-1155 Multi Token Standard].
|
||||
|
||||
The ERC consists of three interfaces which fulfill different roles, found here as {IERC1155}, {IERC1155MetadataURI} and {IERC1155Receiver}.
|
||||
|
||||
{ERC1155} implements the mandatory {IERC1155} interface, as well as the optional extension {IERC1155MetadataURI}, by relying on the substitution mechanism to use the same URI for all token types, dramatically reducing gas costs.
|
||||
|
||||
Additionally there are multiple custom extensions, including:
|
||||
|
||||
* designation of addresses that can pause token transfers for all users ({ERC1155Pausable}).
|
||||
* destruction of own tokens ({ERC1155Burnable}).
|
||||
|
||||
NOTE: This core set of contracts is designed to be unopinionated, allowing developers to access the internal functions in ERC-1155 (such as <<ERC1155-_mint-address-uint256-uint256-bytes-,`_mint`>>) and expose them as external functions in the way they prefer.
|
||||
|
||||
== Core
|
||||
|
||||
{{IERC1155}}
|
||||
|
||||
{{IERC1155MetadataURI}}
|
||||
|
||||
{{ERC1155}}
|
||||
|
||||
{{IERC1155Receiver}}
|
||||
|
||||
== Extensions
|
||||
|
||||
{{ERC1155Pausable}}
|
||||
|
||||
{{ERC1155Burnable}}
|
||||
|
||||
{{ERC1155Supply}}
|
||||
|
||||
{{ERC1155URIStorage}}
|
||||
|
||||
== Utilities
|
||||
|
||||
{{ERC1155Holder}}
|
||||
@@ -0,0 +1,28 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/extensions/ERC1155Burnable.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC1155} from "../ERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev Extension of {ERC1155} that allows token holders to destroy both their
|
||||
* own tokens and those that they have been approved to use.
|
||||
*/
|
||||
abstract contract ERC1155Burnable is ERC1155 {
|
||||
function burn(address account, uint256 id, uint256 value) public virtual {
|
||||
if (account != _msgSender() && !isApprovedForAll(account, _msgSender())) {
|
||||
revert ERC1155MissingApprovalForAll(_msgSender(), account);
|
||||
}
|
||||
|
||||
_burn(account, id, value);
|
||||
}
|
||||
|
||||
function burnBatch(address account, uint256[] memory ids, uint256[] memory values) public virtual {
|
||||
if (account != _msgSender() && !isApprovedForAll(account, _msgSender())) {
|
||||
revert ERC1155MissingApprovalForAll(_msgSender(), account);
|
||||
}
|
||||
|
||||
_burnBatch(account, ids, values);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/extensions/ERC1155Pausable.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC1155} from "../ERC1155.sol";
|
||||
import {Pausable} from "../../../utils/Pausable.sol";
|
||||
|
||||
/**
|
||||
* @dev ERC-1155 token with pausable token transfers, minting and burning.
|
||||
*
|
||||
* Useful for scenarios such as preventing trades until the end of an evaluation
|
||||
* period, or having an emergency switch for freezing all token transfers in the
|
||||
* event of a large bug.
|
||||
*
|
||||
* IMPORTANT: This contract does not include public pause and unpause functions. In
|
||||
* addition to inheriting this contract, you must define both functions, invoking the
|
||||
* {Pausable-_pause} and {Pausable-_unpause} internal functions, with appropriate
|
||||
* access control, e.g. using {AccessControl} or {Ownable}. Not doing so will
|
||||
* make the contract pause mechanism of the contract unreachable, and thus unusable.
|
||||
*/
|
||||
abstract contract ERC1155Pausable is ERC1155, Pausable {
|
||||
/**
|
||||
* @dev See {ERC1155-_update}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - the contract must not be paused.
|
||||
*/
|
||||
function _update(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values
|
||||
) internal virtual override whenNotPaused {
|
||||
super._update(from, to, ids, values);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/extensions/ERC1155Supply.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC1155} from "../ERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev Extension of ERC-1155 that adds tracking of total supply per id.
|
||||
*
|
||||
* Useful for scenarios where Fungible and Non-fungible tokens have to be
|
||||
* clearly identified. Note: While a totalSupply of 1 might mean the
|
||||
* corresponding is an NFT, there is no guarantees that no other token with the
|
||||
* same id are not going to be minted.
|
||||
*
|
||||
* NOTE: This contract implies a global limit of 2**256 - 1 to the number of tokens
|
||||
* that can be minted.
|
||||
*
|
||||
* CAUTION: This extension should not be added in an upgrade to an already deployed contract.
|
||||
*/
|
||||
abstract contract ERC1155Supply is ERC1155 {
|
||||
mapping(uint256 id => uint256) private _totalSupply;
|
||||
uint256 private _totalSupplyAll;
|
||||
|
||||
/**
|
||||
* @dev Total value of tokens in with a given id.
|
||||
*/
|
||||
function totalSupply(uint256 id) public view virtual returns (uint256) {
|
||||
return _totalSupply[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Total value of tokens.
|
||||
*/
|
||||
function totalSupply() public view virtual returns (uint256) {
|
||||
return _totalSupplyAll;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Indicates whether any token exist with a given id, or not.
|
||||
*/
|
||||
function exists(uint256 id) public view virtual returns (bool) {
|
||||
return totalSupply(id) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {ERC1155-_update}.
|
||||
*/
|
||||
function _update(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values
|
||||
) internal virtual override {
|
||||
super._update(from, to, ids, values);
|
||||
|
||||
if (from == address(0)) {
|
||||
uint256 totalMintValue = 0;
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
uint256 value = values[i];
|
||||
// Overflow check required: The rest of the code assumes that totalSupply never overflows
|
||||
_totalSupply[ids[i]] += value;
|
||||
totalMintValue += value;
|
||||
}
|
||||
// Overflow check required: The rest of the code assumes that totalSupplyAll never overflows
|
||||
_totalSupplyAll += totalMintValue;
|
||||
}
|
||||
|
||||
if (to == address(0)) {
|
||||
uint256 totalBurnValue = 0;
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
uint256 value = values[i];
|
||||
|
||||
unchecked {
|
||||
// Overflow not possible: values[i] <= balanceOf(from, ids[i]) <= totalSupply(ids[i])
|
||||
_totalSupply[ids[i]] -= value;
|
||||
// Overflow not possible: sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
|
||||
totalBurnValue += value;
|
||||
}
|
||||
}
|
||||
unchecked {
|
||||
// Overflow not possible: totalBurnValue = sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
|
||||
_totalSupplyAll -= totalBurnValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/extensions/ERC1155URIStorage.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {Strings} from "../../../utils/Strings.sol";
|
||||
import {ERC1155} from "../ERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev ERC-1155 token with storage based token URI management.
|
||||
* Inspired by the {ERC721URIStorage} extension
|
||||
*/
|
||||
abstract contract ERC1155URIStorage is ERC1155 {
|
||||
using Strings for uint256;
|
||||
|
||||
// Optional base URI
|
||||
string private _baseURI = "";
|
||||
|
||||
// Optional mapping for token URIs
|
||||
mapping(uint256 tokenId => string) private _tokenURIs;
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155MetadataURI-uri}.
|
||||
*
|
||||
* This implementation returns the concatenation of the `_baseURI`
|
||||
* and the token-specific uri if the latter is set
|
||||
*
|
||||
* This enables the following behaviors:
|
||||
*
|
||||
* - if `_tokenURIs[tokenId]` is set, then the result is the concatenation
|
||||
* of `_baseURI` and `_tokenURIs[tokenId]` (keep in mind that `_baseURI`
|
||||
* is empty per default);
|
||||
*
|
||||
* - if `_tokenURIs[tokenId]` is NOT set then we fallback to `super.uri()`
|
||||
* which in most cases will contain `ERC1155._uri`;
|
||||
*
|
||||
* - if `_tokenURIs[tokenId]` is NOT set, and if the parents do not have a
|
||||
* uri value set, then the result is empty.
|
||||
*/
|
||||
function uri(uint256 tokenId) public view virtual override returns (string memory) {
|
||||
string memory tokenURI = _tokenURIs[tokenId];
|
||||
|
||||
// If token URI is set, concatenate base URI and tokenURI (via string.concat).
|
||||
return bytes(tokenURI).length > 0 ? string.concat(_baseURI, tokenURI) : super.uri(tokenId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Sets `tokenURI` as the tokenURI of `tokenId`.
|
||||
*/
|
||||
function _setURI(uint256 tokenId, string memory tokenURI) internal virtual {
|
||||
_tokenURIs[tokenId] = tokenURI;
|
||||
emit URI(uri(tokenId), tokenId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Sets `baseURI` as the `_baseURI` for all tokens
|
||||
*/
|
||||
function _setBaseURI(string memory baseURI) internal virtual {
|
||||
_baseURI = baseURI;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/extensions/IERC1155MetadataURI.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC1155} from "../IERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev Interface of the optional ERC1155MetadataExtension interface, as defined
|
||||
* in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[ERC].
|
||||
*/
|
||||
interface IERC1155MetadataURI is IERC1155 {
|
||||
/**
|
||||
* @dev Returns the URI for token type `id`.
|
||||
*
|
||||
* If the `\{id\}` substring is present in the URI, it must be replaced by
|
||||
* clients with the actual token type ID.
|
||||
*/
|
||||
function uri(uint256 id) external view returns (string memory);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC1155/utils/ERC1155Holder.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC165, ERC165} from "../../../utils/introspection/ERC165.sol";
|
||||
import {IERC1155Receiver} from "../IERC1155Receiver.sol";
|
||||
|
||||
/**
|
||||
* @dev Simple implementation of `IERC1155Receiver` that will allow a contract to hold ERC-1155 tokens.
|
||||
*
|
||||
* IMPORTANT: When inheriting this contract, you must include a way to use the received tokens, otherwise they will be
|
||||
* stuck.
|
||||
*/
|
||||
abstract contract ERC1155Holder is ERC165, IERC1155Receiver {
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}.
|
||||
*/
|
||||
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
|
||||
return interfaceId == type(IERC1155Receiver).interfaceId || super.supportsInterface(interfaceId);
|
||||
}
|
||||
|
||||
function onERC1155Received(
|
||||
address,
|
||||
address,
|
||||
uint256,
|
||||
uint256,
|
||||
bytes memory
|
||||
) public virtual override returns (bytes4) {
|
||||
return this.onERC1155Received.selector;
|
||||
}
|
||||
|
||||
function onERC1155BatchReceived(
|
||||
address,
|
||||
address,
|
||||
uint256[] memory,
|
||||
uint256[] memory,
|
||||
bytes memory
|
||||
) public virtual override returns (bytes4) {
|
||||
return this.onERC1155BatchReceived.selector;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC1155Receiver} from "../IERC1155Receiver.sol";
|
||||
import {IERC1155Errors} from "../../../interfaces/draft-IERC6093.sol";
|
||||
|
||||
/**
|
||||
* @dev Library that provide common ERC-1155 utility functions.
|
||||
*
|
||||
* See https://eips.ethereum.org/EIPS/eip-1155[ERC-1155].
|
||||
*/
|
||||
library ERC1155Utils {
|
||||
/**
|
||||
* @dev Performs an acceptance check for the provided `operator` by calling {IERC1155-onERC1155Received}
|
||||
* on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
|
||||
*
|
||||
* The acceptance call is not executed and treated as a no-op if the target address is doesn't contain code (i.e. an EOA).
|
||||
* Otherwise, the recipient must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value to accept
|
||||
* the transfer.
|
||||
*/
|
||||
function checkOnERC1155Received(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256 id,
|
||||
uint256 value,
|
||||
bytes memory data
|
||||
) internal {
|
||||
if (to.code.length > 0) {
|
||||
try IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) returns (bytes4 response) {
|
||||
if (response != IERC1155Receiver.onERC1155Received.selector) {
|
||||
// Tokens rejected
|
||||
revert IERC1155Errors.ERC1155InvalidReceiver(to);
|
||||
}
|
||||
} catch (bytes memory reason) {
|
||||
if (reason.length == 0) {
|
||||
// non-IERC1155Receiver implementer
|
||||
revert IERC1155Errors.ERC1155InvalidReceiver(to);
|
||||
} else {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
revert(add(32, reason), mload(reason))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Performs a batch acceptance check for the provided `operator` by calling {IERC1155-onERC1155BatchReceived}
|
||||
* on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
|
||||
*
|
||||
* The acceptance call is not executed and treated as a no-op if the target address is doesn't contain code (i.e. an EOA).
|
||||
* Otherwise, the recipient must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value to accept
|
||||
* the transfer.
|
||||
*/
|
||||
function checkOnERC1155BatchReceived(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values,
|
||||
bytes memory data
|
||||
) internal {
|
||||
if (to.code.length > 0) {
|
||||
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) returns (
|
||||
bytes4 response
|
||||
) {
|
||||
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
|
||||
// Tokens rejected
|
||||
revert IERC1155Errors.ERC1155InvalidReceiver(to);
|
||||
}
|
||||
} catch (bytes memory reason) {
|
||||
if (reason.length == 0) {
|
||||
// non-IERC1155Receiver implementer
|
||||
revert IERC1155Errors.ERC1155InvalidReceiver(to);
|
||||
} else {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
revert(add(32, reason), mload(reason))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user