ERC1155.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/ERC1155.sol)
  3. pragma solidity ^0.8.19;
  4. import "./IERC1155.sol";
  5. import "./IERC1155Receiver.sol";
  6. import "./extensions/IERC1155MetadataURI.sol";
  7. import "../../utils/Context.sol";
  8. import "../../utils/introspection/ERC165.sol";
  9. /**
  10. * @dev Implementation of the basic standard multi-token.
  11. * See https://eips.ethereum.org/EIPS/eip-1155
  12. * Originally based on code by Enjin: https://github.com/enjin/erc-1155
  13. *
  14. * _Available since v3.1._
  15. */
  16. contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
  17. // Mapping from token ID to account balances
  18. mapping(uint256 => mapping(address => uint256)) private _balances;
  19. // Mapping from account to operator approvals
  20. mapping(address => mapping(address => bool)) private _operatorApprovals;
  21. // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
  22. string private _uri;
  23. /**
  24. * @dev See {_setURI}.
  25. */
  26. constructor(string memory uri_) {
  27. _setURI(uri_);
  28. }
  29. /**
  30. * @dev See {IERC165-supportsInterface}.
  31. */
  32. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  33. return
  34. interfaceId == type(IERC1155).interfaceId ||
  35. interfaceId == type(IERC1155MetadataURI).interfaceId ||
  36. super.supportsInterface(interfaceId);
  37. }
  38. /**
  39. * @dev See {IERC1155MetadataURI-uri}.
  40. *
  41. * This implementation returns the same URI for *all* token types. It relies
  42. * on the token type ID substitution mechanism
  43. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  44. *
  45. * Clients calling this function must replace the `\{id\}` substring with the
  46. * actual token type ID.
  47. */
  48. function uri(uint256) public view virtual override returns (string memory) {
  49. return _uri;
  50. }
  51. /**
  52. * @dev See {IERC1155-balanceOf}.
  53. *
  54. * Requirements:
  55. *
  56. * - `account` cannot be the zero address.
  57. */
  58. function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
  59. return _balances[id][account];
  60. }
  61. /**
  62. * @dev See {IERC1155-balanceOfBatch}.
  63. *
  64. * Requirements:
  65. *
  66. * - `accounts` and `ids` must have the same length.
  67. */
  68. function balanceOfBatch(
  69. address[] memory accounts,
  70. uint256[] memory ids
  71. ) public view virtual override returns (uint256[] memory) {
  72. require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
  73. uint256[] memory batchBalances = new uint256[](accounts.length);
  74. for (uint256 i = 0; i < accounts.length; ++i) {
  75. batchBalances[i] = balanceOf(accounts[i], ids[i]);
  76. }
  77. return batchBalances;
  78. }
  79. /**
  80. * @dev See {IERC1155-setApprovalForAll}.
  81. */
  82. function setApprovalForAll(address operator, bool approved) public virtual override {
  83. _setApprovalForAll(_msgSender(), operator, approved);
  84. }
  85. /**
  86. * @dev See {IERC1155-isApprovedForAll}.
  87. */
  88. function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
  89. return _operatorApprovals[account][operator];
  90. }
  91. /**
  92. * @dev See {IERC1155-safeTransferFrom}.
  93. */
  94. function safeTransferFrom(
  95. address from,
  96. address to,
  97. uint256 id,
  98. uint256 amount,
  99. bytes memory data
  100. ) public virtual override {
  101. require(
  102. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  103. "ERC1155: caller is not token owner or approved"
  104. );
  105. _safeTransferFrom(from, to, id, amount, data);
  106. }
  107. /**
  108. * @dev See {IERC1155-safeBatchTransferFrom}.
  109. */
  110. function safeBatchTransferFrom(
  111. address from,
  112. address to,
  113. uint256[] memory ids,
  114. uint256[] memory amounts,
  115. bytes memory data
  116. ) public virtual override {
  117. require(
  118. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  119. "ERC1155: caller is not token owner or approved"
  120. );
  121. _safeBatchTransferFrom(from, to, ids, amounts, data);
  122. }
  123. /**
  124. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`. Will mint (or burn) if `from` (or `to`) is the zero address.
  125. *
  126. * Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise.
  127. *
  128. * Requirements:
  129. *
  130. * - If `to` refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received}
  131. * or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  132. */
  133. function _update(
  134. address from,
  135. address to,
  136. uint256[] memory ids,
  137. uint256[] memory amounts,
  138. bytes memory data
  139. ) internal virtual {
  140. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  141. address operator = _msgSender();
  142. for (uint256 i = 0; i < ids.length; ++i) {
  143. uint256 id = ids[i];
  144. uint256 amount = amounts[i];
  145. if (from != address(0)) {
  146. uint256 fromBalance = _balances[id][from];
  147. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  148. unchecked {
  149. _balances[id][from] = fromBalance - amount;
  150. }
  151. }
  152. if (to != address(0)) {
  153. _balances[id][to] += amount;
  154. }
  155. }
  156. if (ids.length == 1) {
  157. uint256 id = ids[0];
  158. uint256 amount = amounts[0];
  159. emit TransferSingle(operator, from, to, id, amount);
  160. if (to != address(0)) {
  161. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  162. }
  163. } else {
  164. emit TransferBatch(operator, from, to, ids, amounts);
  165. if (to != address(0)) {
  166. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  167. }
  168. }
  169. }
  170. /**
  171. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
  172. *
  173. * Emits a {TransferSingle} event.
  174. *
  175. * Requirements:
  176. *
  177. * - `to` cannot be the zero address.
  178. * - `from` must have a balance of tokens of type `id` of at least `amount`.
  179. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  180. * acceptance magic value.
  181. */
  182. function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
  183. require(to != address(0), "ERC1155: transfer to the zero address");
  184. require(from != address(0), "ERC1155: transfer from the zero address");
  185. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  186. _update(from, to, ids, amounts, data);
  187. }
  188. /**
  189. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  190. *
  191. * Emits a {TransferBatch} event.
  192. *
  193. * Requirements:
  194. *
  195. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  196. * acceptance magic value.
  197. */
  198. function _safeBatchTransferFrom(
  199. address from,
  200. address to,
  201. uint256[] memory ids,
  202. uint256[] memory amounts,
  203. bytes memory data
  204. ) internal {
  205. require(to != address(0), "ERC1155: transfer to the zero address");
  206. require(from != address(0), "ERC1155: transfer from the zero address");
  207. _update(from, to, ids, amounts, data);
  208. }
  209. /**
  210. * @dev Sets a new URI for all token types, by relying on the token type ID
  211. * substitution mechanism
  212. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  213. *
  214. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  215. * URI or any of the amounts in the JSON file at said URI will be replaced by
  216. * clients with the token type ID.
  217. *
  218. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  219. * interpreted by clients as
  220. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  221. * for token type ID 0x4cce0.
  222. *
  223. * See {uri}.
  224. *
  225. * Because these URIs cannot be meaningfully represented by the {URI} event,
  226. * this function emits no events.
  227. */
  228. function _setURI(string memory newuri) internal virtual {
  229. _uri = newuri;
  230. }
  231. /**
  232. * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
  233. *
  234. * Emits a {TransferSingle} event.
  235. *
  236. * Requirements:
  237. *
  238. * - `to` cannot be the zero address.
  239. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  240. * acceptance magic value.
  241. */
  242. function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
  243. require(to != address(0), "ERC1155: mint to the zero address");
  244. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  245. _update(address(0), to, ids, amounts, data);
  246. }
  247. /**
  248. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  249. *
  250. * Emits a {TransferBatch} event.
  251. *
  252. * Requirements:
  253. *
  254. * - `ids` and `amounts` must have the same length.
  255. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  256. * acceptance magic value.
  257. */
  258. function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal {
  259. require(to != address(0), "ERC1155: mint to the zero address");
  260. _update(address(0), to, ids, amounts, data);
  261. }
  262. /**
  263. * @dev Destroys `amount` tokens of token type `id` from `from`
  264. *
  265. * Emits a {TransferSingle} event.
  266. *
  267. * Requirements:
  268. *
  269. * - `from` cannot be the zero address.
  270. * - `from` must have at least `amount` tokens of token type `id`.
  271. */
  272. function _burn(address from, uint256 id, uint256 amount) internal {
  273. require(from != address(0), "ERC1155: burn from the zero address");
  274. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  275. _update(from, address(0), ids, amounts, "");
  276. }
  277. /**
  278. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  279. *
  280. * Emits a {TransferBatch} event.
  281. *
  282. * Requirements:
  283. *
  284. * - `ids` and `amounts` must have the same length.
  285. */
  286. function _burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) internal {
  287. require(from != address(0), "ERC1155: burn from the zero address");
  288. _update(from, address(0), ids, amounts, "");
  289. }
  290. /**
  291. * @dev Approve `operator` to operate on all of `owner` tokens
  292. *
  293. * Emits an {ApprovalForAll} event.
  294. */
  295. function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
  296. require(owner != operator, "ERC1155: setting approval status for self");
  297. _operatorApprovals[owner][operator] = approved;
  298. emit ApprovalForAll(owner, operator, approved);
  299. }
  300. function _doSafeTransferAcceptanceCheck(
  301. address operator,
  302. address from,
  303. address to,
  304. uint256 id,
  305. uint256 amount,
  306. bytes memory data
  307. ) private {
  308. if (to.code.length > 0) {
  309. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  310. if (response != IERC1155Receiver.onERC1155Received.selector) {
  311. revert("ERC1155: ERC1155Receiver rejected tokens");
  312. }
  313. } catch Error(string memory reason) {
  314. revert(reason);
  315. } catch {
  316. revert("ERC1155: transfer to non-ERC1155Receiver implementer");
  317. }
  318. }
  319. }
  320. function _doSafeBatchTransferAcceptanceCheck(
  321. address operator,
  322. address from,
  323. address to,
  324. uint256[] memory ids,
  325. uint256[] memory amounts,
  326. bytes memory data
  327. ) private {
  328. if (to.code.length > 0) {
  329. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  330. bytes4 response
  331. ) {
  332. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  333. revert("ERC1155: ERC1155Receiver rejected tokens");
  334. }
  335. } catch Error(string memory reason) {
  336. revert(reason);
  337. } catch {
  338. revert("ERC1155: transfer to non-ERC1155Receiver implementer");
  339. }
  340. }
  341. }
  342. function _asSingletonArrays(
  343. uint256 element1,
  344. uint256 element2
  345. ) private pure returns (uint256[] memory array1, uint256[] memory array2) {
  346. /// @solidity memory-safe-assembly
  347. assembly {
  348. array1 := mload(0x40)
  349. mstore(array1, 1)
  350. mstore(add(array1, 0x20), element1)
  351. array2 := add(array1, 0x40)
  352. mstore(array2, 1)
  353. mstore(add(array2, 0x20), element2)
  354. mstore(0x40, add(array2, 0x40))
  355. }
  356. }
  357. }