mirror of
https://github.com/th30d4y/OpenLearnX.git
synced 2026-05-26 19:26:33 +00:00
Fix .gitignore: stop tracking ignored files
This commit is contained in:
@@ -0,0 +1,497 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/ERC1155.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "./IERC1155.sol";
|
||||
import "./IERC1155Receiver.sol";
|
||||
import "./extensions/IERC1155MetadataURI.sol";
|
||||
import "../../utils/Address.sol";
|
||||
import "../../utils/Context.sol";
|
||||
import "../../utils/introspection/ERC165.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
|
||||
*
|
||||
* _Available since v3.1._
|
||||
*/
|
||||
contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
|
||||
using Address for address;
|
||||
|
||||
// Mapping from token ID to account balances
|
||||
mapping(uint256 => mapping(address => uint256)) private _balances;
|
||||
|
||||
// Mapping from account to operator approvals
|
||||
mapping(address => mapping(address => 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 EIP].
|
||||
*
|
||||
* Clients calling this function must replace the `\{id\}` substring with the
|
||||
* actual token type ID.
|
||||
*/
|
||||
function uri(uint256) public view virtual override returns (string memory) {
|
||||
return _uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-balanceOf}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `account` cannot be the zero address.
|
||||
*/
|
||||
function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
|
||||
require(account != address(0), "ERC1155: address zero is not a valid owner");
|
||||
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 override returns (uint256[] memory) {
|
||||
require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
|
||||
|
||||
uint256[] memory batchBalances = new uint256[](accounts.length);
|
||||
|
||||
for (uint256 i = 0; i < accounts.length; ++i) {
|
||||
batchBalances[i] = balanceOf(accounts[i], ids[i]);
|
||||
}
|
||||
|
||||
return batchBalances;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-setApprovalForAll}.
|
||||
*/
|
||||
function setApprovalForAll(address operator, bool approved) public virtual override {
|
||||
_setApprovalForAll(_msgSender(), operator, approved);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-isApprovedForAll}.
|
||||
*/
|
||||
function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
|
||||
return _operatorApprovals[account][operator];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-safeTransferFrom}.
|
||||
*/
|
||||
function safeTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256 id,
|
||||
uint256 amount,
|
||||
bytes memory data
|
||||
) public virtual override {
|
||||
require(
|
||||
from == _msgSender() || isApprovedForAll(from, _msgSender()),
|
||||
"ERC1155: caller is not token owner or approved"
|
||||
);
|
||||
_safeTransferFrom(from, to, id, amount, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC1155-safeBatchTransferFrom}.
|
||||
*/
|
||||
function safeBatchTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) public virtual override {
|
||||
require(
|
||||
from == _msgSender() || isApprovedForAll(from, _msgSender()),
|
||||
"ERC1155: caller is not token owner or approved"
|
||||
);
|
||||
_safeBatchTransferFrom(from, to, ids, amounts, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Transfers `amount` 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 `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 amount,
|
||||
bytes memory data
|
||||
) internal virtual {
|
||||
require(to != address(0), "ERC1155: transfer to the zero address");
|
||||
|
||||
address operator = _msgSender();
|
||||
uint256[] memory ids = _asSingletonArray(id);
|
||||
uint256[] memory amounts = _asSingletonArray(amount);
|
||||
|
||||
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
|
||||
uint256 fromBalance = _balances[id][from];
|
||||
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
|
||||
unchecked {
|
||||
_balances[id][from] = fromBalance - amount;
|
||||
}
|
||||
_balances[id][to] += amount;
|
||||
|
||||
emit TransferSingle(operator, from, to, id, amount);
|
||||
|
||||
_afterTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
|
||||
_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, 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.
|
||||
*/
|
||||
function _safeBatchTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) internal virtual {
|
||||
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
|
||||
require(to != address(0), "ERC1155: transfer to the zero address");
|
||||
|
||||
address operator = _msgSender();
|
||||
|
||||
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
uint256 id = ids[i];
|
||||
uint256 amount = amounts[i];
|
||||
|
||||
uint256 fromBalance = _balances[id][from];
|
||||
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
|
||||
unchecked {
|
||||
_balances[id][from] = fromBalance - amount;
|
||||
}
|
||||
_balances[id][to] += amount;
|
||||
}
|
||||
|
||||
emit TransferBatch(operator, from, to, ids, amounts);
|
||||
|
||||
_afterTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
|
||||
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, 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 EIP].
|
||||
*
|
||||
* By this mechanism, any occurrence of the `\{id\}` substring in either the
|
||||
* URI or any of the amounts 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 `amount` tokens of token 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 amount, bytes memory data) internal virtual {
|
||||
require(to != address(0), "ERC1155: mint to the zero address");
|
||||
|
||||
address operator = _msgSender();
|
||||
uint256[] memory ids = _asSingletonArray(id);
|
||||
uint256[] memory amounts = _asSingletonArray(amount);
|
||||
|
||||
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
|
||||
|
||||
_balances[id][to] += amount;
|
||||
emit TransferSingle(operator, address(0), to, id, amount);
|
||||
|
||||
_afterTokenTransfer(operator, address(0), to, ids, amounts, data);
|
||||
|
||||
_doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
|
||||
*
|
||||
* Emits a {TransferBatch} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `ids` and `amounts` must have the same length.
|
||||
* - 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 amounts,
|
||||
bytes memory data
|
||||
) internal virtual {
|
||||
require(to != address(0), "ERC1155: mint to the zero address");
|
||||
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
|
||||
|
||||
address operator = _msgSender();
|
||||
|
||||
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
|
||||
|
||||
for (uint256 i = 0; i < ids.length; i++) {
|
||||
_balances[ids[i]][to] += amounts[i];
|
||||
}
|
||||
|
||||
emit TransferBatch(operator, address(0), to, ids, amounts);
|
||||
|
||||
_afterTokenTransfer(operator, address(0), to, ids, amounts, data);
|
||||
|
||||
_doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Destroys `amount` tokens of token type `id` from `from`
|
||||
*
|
||||
* Emits a {TransferSingle} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `from` cannot be the zero address.
|
||||
* - `from` must have at least `amount` tokens of token type `id`.
|
||||
*/
|
||||
function _burn(address from, uint256 id, uint256 amount) internal virtual {
|
||||
require(from != address(0), "ERC1155: burn from the zero address");
|
||||
|
||||
address operator = _msgSender();
|
||||
uint256[] memory ids = _asSingletonArray(id);
|
||||
uint256[] memory amounts = _asSingletonArray(amount);
|
||||
|
||||
_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
|
||||
|
||||
uint256 fromBalance = _balances[id][from];
|
||||
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
|
||||
unchecked {
|
||||
_balances[id][from] = fromBalance - amount;
|
||||
}
|
||||
|
||||
emit TransferSingle(operator, from, address(0), id, amount);
|
||||
|
||||
_afterTokenTransfer(operator, from, address(0), ids, amounts, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
|
||||
*
|
||||
* Emits a {TransferBatch} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `ids` and `amounts` must have the same length.
|
||||
*/
|
||||
function _burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) internal virtual {
|
||||
require(from != address(0), "ERC1155: burn from the zero address");
|
||||
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
|
||||
|
||||
address operator = _msgSender();
|
||||
|
||||
_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
|
||||
|
||||
for (uint256 i = 0; i < ids.length; i++) {
|
||||
uint256 id = ids[i];
|
||||
uint256 amount = amounts[i];
|
||||
|
||||
uint256 fromBalance = _balances[id][from];
|
||||
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
|
||||
unchecked {
|
||||
_balances[id][from] = fromBalance - amount;
|
||||
}
|
||||
}
|
||||
|
||||
emit TransferBatch(operator, from, address(0), ids, amounts);
|
||||
|
||||
_afterTokenTransfer(operator, from, address(0), ids, amounts, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Approve `operator` to operate on all of `owner` tokens
|
||||
*
|
||||
* Emits an {ApprovalForAll} event.
|
||||
*/
|
||||
function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
|
||||
require(owner != operator, "ERC1155: setting approval status for self");
|
||||
_operatorApprovals[owner][operator] = approved;
|
||||
emit ApprovalForAll(owner, operator, approved);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Hook that is called before any token transfer. This includes minting
|
||||
* and burning, as well as batched variants.
|
||||
*
|
||||
* The same hook is called on both single and batched variants. For single
|
||||
* transfers, the length of the `ids` and `amounts` arrays will be 1.
|
||||
*
|
||||
* Calling conditions (for each `id` and `amount` pair):
|
||||
*
|
||||
* - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
|
||||
* of token type `id` will be transferred to `to`.
|
||||
* - When `from` is zero, `amount` tokens of token type `id` will be minted
|
||||
* for `to`.
|
||||
* - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
|
||||
* will be burned.
|
||||
* - `from` and `to` are never both zero.
|
||||
* - `ids` and `amounts` have the same, non-zero length.
|
||||
*
|
||||
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
|
||||
*/
|
||||
function _beforeTokenTransfer(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) internal virtual {}
|
||||
|
||||
/**
|
||||
* @dev Hook that is called after any token transfer. This includes minting
|
||||
* and burning, as well as batched variants.
|
||||
*
|
||||
* The same hook is called on both single and batched variants. For single
|
||||
* transfers, the length of the `id` and `amount` arrays will be 1.
|
||||
*
|
||||
* Calling conditions (for each `id` and `amount` pair):
|
||||
*
|
||||
* - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
|
||||
* of token type `id` will be transferred to `to`.
|
||||
* - When `from` is zero, `amount` tokens of token type `id` will be minted
|
||||
* for `to`.
|
||||
* - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
|
||||
* will be burned.
|
||||
* - `from` and `to` are never both zero.
|
||||
* - `ids` and `amounts` have the same, non-zero length.
|
||||
*
|
||||
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
|
||||
*/
|
||||
function _afterTokenTransfer(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) internal virtual {}
|
||||
|
||||
function _doSafeTransferAcceptanceCheck(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256 id,
|
||||
uint256 amount,
|
||||
bytes memory data
|
||||
) private {
|
||||
if (to.isContract()) {
|
||||
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
|
||||
if (response != IERC1155Receiver.onERC1155Received.selector) {
|
||||
revert("ERC1155: ERC1155Receiver rejected tokens");
|
||||
}
|
||||
} catch Error(string memory reason) {
|
||||
revert(reason);
|
||||
} catch {
|
||||
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _doSafeBatchTransferAcceptanceCheck(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) private {
|
||||
if (to.isContract()) {
|
||||
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
|
||||
bytes4 response
|
||||
) {
|
||||
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
|
||||
revert("ERC1155: ERC1155Receiver rejected tokens");
|
||||
}
|
||||
} catch Error(string memory reason) {
|
||||
revert(reason);
|
||||
} catch {
|
||||
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
|
||||
uint256[] memory array = new uint256[](1);
|
||||
array[0] = element;
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/IERC1155.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../../utils/introspection/IERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev Required interface of an ERC1155 compliant contract, as defined in the
|
||||
* https://eips.ethereum.org/EIPS/eip-1155[EIP].
|
||||
*
|
||||
* _Available since v3.1._
|
||||
*/
|
||||
interface IERC1155 is IERC165 {
|
||||
/**
|
||||
* @dev Emitted when `value` tokens of token 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 amount of tokens of token type `id` owned by `account`.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `account` cannot be the zero address.
|
||||
*/
|
||||
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 caller.
|
||||
*/
|
||||
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 `amount` tokens of token type `id` from `from` to `to`.
|
||||
*
|
||||
* 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 `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 amount, bytes calldata data) external;
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
|
||||
*
|
||||
* Emits a {TransferBatch} event.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `ids` and `amounts` 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 amounts,
|
||||
bytes calldata data
|
||||
) external;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/IERC1155Receiver.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../../utils/introspection/IERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev _Available since v3.1._
|
||||
*/
|
||||
interface IERC1155Receiver is IERC165 {
|
||||
/**
|
||||
* @dev Handles the receipt of a single ERC1155 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 ERC1155 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,49 @@
|
||||
= 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[ERC1155 Multi Token Standard].
|
||||
|
||||
The EIP 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 ERC1155 (such as <<ERC1155-_mint-address-uint256-uint256-bytes-,`_mint`>>) and expose them as external functions in the way they prefer. On the other hand, xref:ROOT:erc1155.adoc#Presets[ERC1155 Presets] (such as {ERC1155PresetMinterPauser}) are designed using opinionated patterns to provide developers with ready to use, deployable contracts.
|
||||
|
||||
== Core
|
||||
|
||||
{{IERC1155}}
|
||||
|
||||
{{IERC1155MetadataURI}}
|
||||
|
||||
{{ERC1155}}
|
||||
|
||||
{{IERC1155Receiver}}
|
||||
|
||||
{{ERC1155Receiver}}
|
||||
|
||||
== Extensions
|
||||
|
||||
{{ERC1155Pausable}}
|
||||
|
||||
{{ERC1155Burnable}}
|
||||
|
||||
{{ERC1155Supply}}
|
||||
|
||||
{{ERC1155URIStorage}}
|
||||
|
||||
== Presets
|
||||
|
||||
These contracts are preconfigured combinations of the above features. They can be used through inheritance or as models to copy and paste their source code.
|
||||
|
||||
{{ERC1155PresetMinterPauser}}
|
||||
|
||||
== Utilities
|
||||
|
||||
{{ERC1155Holder}}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/extensions/ERC1155Burnable.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../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.
|
||||
*
|
||||
* _Available since v3.1._
|
||||
*/
|
||||
abstract contract ERC1155Burnable is ERC1155 {
|
||||
function burn(address account, uint256 id, uint256 value) public virtual {
|
||||
require(
|
||||
account == _msgSender() || isApprovedForAll(account, _msgSender()),
|
||||
"ERC1155: caller is not token owner or approved"
|
||||
);
|
||||
|
||||
_burn(account, id, value);
|
||||
}
|
||||
|
||||
function burnBatch(address account, uint256[] memory ids, uint256[] memory values) public virtual {
|
||||
require(
|
||||
account == _msgSender() || isApprovedForAll(account, _msgSender()),
|
||||
"ERC1155: caller is not token owner or approved"
|
||||
);
|
||||
|
||||
_burnBatch(account, ids, values);
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.8.2) (token/ERC1155/extensions/ERC1155Pausable.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../ERC1155.sol";
|
||||
import "../../../security/Pausable.sol";
|
||||
|
||||
/**
|
||||
* @dev ERC1155 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 unpausable.
|
||||
*
|
||||
* _Available since v3.1._
|
||||
*/
|
||||
abstract contract ERC1155Pausable is ERC1155, Pausable {
|
||||
/**
|
||||
* @dev See {ERC1155-_beforeTokenTransfer}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - the contract must not be paused.
|
||||
*/
|
||||
function _beforeTokenTransfer(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) internal virtual override {
|
||||
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
|
||||
require(!paused(), "ERC1155Pausable: token transfer while paused");
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC1155/extensions/ERC1155Supply.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../ERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev Extension of ERC1155 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.
|
||||
*/
|
||||
abstract contract ERC1155Supply is ERC1155 {
|
||||
mapping(uint256 => uint256) private _totalSupply;
|
||||
|
||||
/**
|
||||
* @dev Total amount of tokens in with a given id.
|
||||
*/
|
||||
function totalSupply(uint256 id) public view virtual returns (uint256) {
|
||||
return _totalSupply[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Indicates whether any token exist with a given id, or not.
|
||||
*/
|
||||
function exists(uint256 id) public view virtual returns (bool) {
|
||||
return ERC1155Supply.totalSupply(id) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {ERC1155-_beforeTokenTransfer}.
|
||||
*/
|
||||
function _beforeTokenTransfer(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) internal virtual override {
|
||||
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
|
||||
if (from == address(0)) {
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
_totalSupply[ids[i]] += amounts[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (to == address(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 {
|
||||
_totalSupply[id] = supply - amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC1155/extensions/ERC1155URIStorage.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../../../utils/Strings.sol";
|
||||
import "../ERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev ERC1155 token with storage based token URI management.
|
||||
* Inspired by the ERC721URIStorage extension
|
||||
*
|
||||
* _Available since v4.6._
|
||||
*/
|
||||
abstract contract ERC1155URIStorage is ERC1155 {
|
||||
using Strings for uint256;
|
||||
|
||||
// Optional base URI
|
||||
string private _baseURI = "";
|
||||
|
||||
// Optional mapping for token URIs
|
||||
mapping(uint256 => 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 abi.encodePacked).
|
||||
return bytes(tokenURI).length > 0 ? string(abi.encodePacked(_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;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/extensions/IERC1155MetadataURI.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../IERC1155.sol";
|
||||
|
||||
/**
|
||||
* @dev Interface of the optional ERC1155MetadataExtension interface, as defined
|
||||
* in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[EIP].
|
||||
*
|
||||
* _Available since v3.1._
|
||||
*/
|
||||
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);
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/presets/ERC1155PresetMinterPauser.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../ERC1155.sol";
|
||||
import "../extensions/ERC1155Burnable.sol";
|
||||
import "../extensions/ERC1155Pausable.sol";
|
||||
import "../../../access/AccessControlEnumerable.sol";
|
||||
import "../../../utils/Context.sol";
|
||||
|
||||
/**
|
||||
* @dev {ERC1155} token, including:
|
||||
*
|
||||
* - ability for holders to burn (destroy) their tokens
|
||||
* - a minter role that allows for token minting (creation)
|
||||
* - a pauser role that allows to stop all token transfers
|
||||
*
|
||||
* This contract uses {AccessControl} to lock permissioned functions using the
|
||||
* different roles - head to its documentation for details.
|
||||
*
|
||||
* The account that deploys the contract will be granted the minter and pauser
|
||||
* roles, as well as the default admin role, which will let it grant both minter
|
||||
* and pauser roles to other accounts.
|
||||
*
|
||||
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
|
||||
*/
|
||||
contract ERC1155PresetMinterPauser is Context, AccessControlEnumerable, ERC1155Burnable, ERC1155Pausable {
|
||||
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
|
||||
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
|
||||
|
||||
/**
|
||||
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE`, and `PAUSER_ROLE` to the account that
|
||||
* deploys the contract.
|
||||
*/
|
||||
constructor(string memory uri) ERC1155(uri) {
|
||||
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
|
||||
|
||||
_setupRole(MINTER_ROLE, _msgSender());
|
||||
_setupRole(PAUSER_ROLE, _msgSender());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Creates `amount` new tokens for `to`, of token type `id`.
|
||||
*
|
||||
* See {ERC1155-_mint}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - the caller must have the `MINTER_ROLE`.
|
||||
*/
|
||||
function mint(address to, uint256 id, uint256 amount, bytes memory data) public virtual {
|
||||
require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
|
||||
|
||||
_mint(to, id, amount, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}.
|
||||
*/
|
||||
function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public virtual {
|
||||
require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
|
||||
|
||||
_mintBatch(to, ids, amounts, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Pauses all token transfers.
|
||||
*
|
||||
* See {ERC1155Pausable} and {Pausable-_pause}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - the caller must have the `PAUSER_ROLE`.
|
||||
*/
|
||||
function pause() public virtual {
|
||||
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause");
|
||||
_pause();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Unpauses all token transfers.
|
||||
*
|
||||
* See {ERC1155Pausable} and {Pausable-_unpause}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - the caller must have the `PAUSER_ROLE`.
|
||||
*/
|
||||
function unpause() public virtual {
|
||||
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause");
|
||||
_unpause();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}.
|
||||
*/
|
||||
function supportsInterface(
|
||||
bytes4 interfaceId
|
||||
) public view virtual override(AccessControlEnumerable, ERC1155) returns (bool) {
|
||||
return super.supportsInterface(interfaceId);
|
||||
}
|
||||
|
||||
function _beforeTokenTransfer(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory amounts,
|
||||
bytes memory data
|
||||
) internal virtual override(ERC1155, ERC1155Pausable) {
|
||||
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Contract presets are now deprecated in favor of [Contracts Wizard](https://wizard.openzeppelin.com/) as a more powerful alternative.
|
||||
@@ -0,0 +1,36 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/utils/ERC1155Holder.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "./ERC1155Receiver.sol";
|
||||
|
||||
/**
|
||||
* Simple implementation of `ERC1155Receiver` that will allow a contract to hold ERC1155 tokens.
|
||||
*
|
||||
* IMPORTANT: When inheriting this contract, you must include a way to use the received tokens, otherwise they will be
|
||||
* stuck.
|
||||
*
|
||||
* @dev _Available since v3.1._
|
||||
*/
|
||||
contract ERC1155Holder is ERC1155Receiver {
|
||||
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,19 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/utils/ERC1155Receiver.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../IERC1155Receiver.sol";
|
||||
import "../../../utils/introspection/ERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev _Available since v3.1._
|
||||
*/
|
||||
abstract contract ERC1155Receiver 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user