ERC1155.sol 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.2) (token/ERC1155/ERC1155.sol)
  3. pragma solidity ^0.8.0;
  4. import "./IERC1155.sol";
  5. import "./IERC1155Receiver.sol";
  6. import "./extensions/IERC1155MetadataURI.sol";
  7. import "../../utils/Address.sol";
  8. import "../../utils/Context.sol";
  9. import "../../utils/introspection/ERC165.sol";
  10. /**
  11. * @dev Implementation of the basic standard multi-token.
  12. * See https://eips.ethereum.org/EIPS/eip-1155
  13. * Originally based on code by Enjin: https://github.com/enjin/erc-1155
  14. *
  15. * _Available since v3.1._
  16. */
  17. contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
  18. using Address for address;
  19. // Mapping from token ID to account balances
  20. mapping(uint256 => mapping(address => uint256)) private _balances;
  21. // Mapping from account to operator approvals
  22. mapping(address => mapping(address => bool)) private _operatorApprovals;
  23. // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
  24. string private _uri;
  25. /**
  26. * @dev See {_setURI}.
  27. */
  28. constructor(string memory uri_) {
  29. _setURI(uri_);
  30. }
  31. /**
  32. * @dev See {IERC165-supportsInterface}.
  33. */
  34. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  35. return
  36. interfaceId == type(IERC1155).interfaceId ||
  37. interfaceId == type(IERC1155MetadataURI).interfaceId ||
  38. super.supportsInterface(interfaceId);
  39. }
  40. /**
  41. * @dev See {IERC1155MetadataURI-uri}.
  42. *
  43. * This implementation returns the same URI for *all* token types. It relies
  44. * on the token type ID substitution mechanism
  45. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  46. *
  47. * Clients calling this function must replace the `\{id\}` substring with the
  48. * actual token type ID.
  49. */
  50. function uri(uint256) public view virtual override returns (string memory) {
  51. return _uri;
  52. }
  53. /**
  54. * @dev See {IERC1155-balanceOf}.
  55. *
  56. * Requirements:
  57. *
  58. * - `account` cannot be the zero address.
  59. */
  60. function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
  61. require(account != address(0), "ERC1155: address zero is not a valid owner");
  62. return _balances[id][account];
  63. }
  64. /**
  65. * @dev See {IERC1155-balanceOfBatch}.
  66. *
  67. * Requirements:
  68. *
  69. * - `accounts` and `ids` must have the same length.
  70. */
  71. function balanceOfBatch(address[] memory accounts, uint256[] memory ids)
  72. public
  73. view
  74. virtual
  75. override
  76. returns (uint256[] memory)
  77. {
  78. require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
  79. uint256[] memory batchBalances = new uint256[](accounts.length);
  80. for (uint256 i = 0; i < accounts.length; ++i) {
  81. batchBalances[i] = balanceOf(accounts[i], ids[i]);
  82. }
  83. return batchBalances;
  84. }
  85. /**
  86. * @dev See {IERC1155-setApprovalForAll}.
  87. */
  88. function setApprovalForAll(address operator, bool approved) public virtual override {
  89. _setApprovalForAll(_msgSender(), operator, approved);
  90. }
  91. /**
  92. * @dev See {IERC1155-isApprovedForAll}.
  93. */
  94. function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
  95. return _operatorApprovals[account][operator];
  96. }
  97. /**
  98. * @dev See {IERC1155-safeTransferFrom}.
  99. */
  100. function safeTransferFrom(
  101. address from,
  102. address to,
  103. uint256 id,
  104. uint256 amount,
  105. bytes memory data
  106. ) public virtual override {
  107. require(
  108. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  109. "ERC1155: caller is not token owner or approved"
  110. );
  111. _safeTransferFrom(from, to, id, amount, data);
  112. }
  113. /**
  114. * @dev See {IERC1155-safeBatchTransferFrom}.
  115. */
  116. function safeBatchTransferFrom(
  117. address from,
  118. address to,
  119. uint256[] memory ids,
  120. uint256[] memory amounts,
  121. bytes memory data
  122. ) public virtual override {
  123. require(
  124. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  125. "ERC1155: caller is not token owner or approved"
  126. );
  127. _safeBatchTransferFrom(from, to, ids, amounts, data);
  128. }
  129. /**
  130. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
  131. *
  132. * Emits a {TransferSingle} event.
  133. *
  134. * Requirements:
  135. *
  136. * - `to` cannot be the zero address.
  137. * - `from` must have a balance of tokens of type `id` of at least `amount`.
  138. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  139. * acceptance magic value.
  140. */
  141. function _safeTransferFrom(
  142. address from,
  143. address to,
  144. uint256 id,
  145. uint256 amount,
  146. bytes memory data
  147. ) internal virtual {
  148. require(to != address(0), "ERC1155: transfer to the zero address");
  149. address operator = _msgSender();
  150. uint256[] memory ids = _asSingletonArray(id);
  151. uint256[] memory amounts = _asSingletonArray(amount);
  152. _beforeTokenTransfer(operator, from, to, ids, amounts, data);
  153. uint256 fromBalance = _balances[id][from];
  154. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  155. unchecked {
  156. _balances[id][from] = fromBalance - amount;
  157. }
  158. _balances[id][to] += amount;
  159. emit TransferSingle(operator, from, to, id, amount);
  160. _afterTokenTransfer(operator, from, to, ids, amounts, data);
  161. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  162. }
  163. /**
  164. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  165. *
  166. * Emits a {TransferBatch} event.
  167. *
  168. * Requirements:
  169. *
  170. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  171. * acceptance magic value.
  172. */
  173. function _safeBatchTransferFrom(
  174. address from,
  175. address to,
  176. uint256[] memory ids,
  177. uint256[] memory amounts,
  178. bytes memory data
  179. ) internal virtual {
  180. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  181. require(to != address(0), "ERC1155: transfer to the zero address");
  182. address operator = _msgSender();
  183. _beforeTokenTransfer(operator, from, to, ids, amounts, data);
  184. for (uint256 i = 0; i < ids.length; ++i) {
  185. uint256 id = ids[i];
  186. uint256 amount = amounts[i];
  187. uint256 fromBalance = _balances[id][from];
  188. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  189. unchecked {
  190. _balances[id][from] = fromBalance - amount;
  191. }
  192. _balances[id][to] += amount;
  193. }
  194. emit TransferBatch(operator, from, to, ids, amounts);
  195. _afterTokenTransfer(operator, from, to, ids, amounts, data);
  196. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  197. }
  198. /**
  199. * @dev Sets a new URI for all token types, by relying on the token type ID
  200. * substitution mechanism
  201. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  202. *
  203. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  204. * URI or any of the amounts in the JSON file at said URI will be replaced by
  205. * clients with the token type ID.
  206. *
  207. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  208. * interpreted by clients as
  209. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  210. * for token type ID 0x4cce0.
  211. *
  212. * See {uri}.
  213. *
  214. * Because these URIs cannot be meaningfully represented by the {URI} event,
  215. * this function emits no events.
  216. */
  217. function _setURI(string memory newuri) internal virtual {
  218. _uri = newuri;
  219. }
  220. /**
  221. * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
  222. *
  223. * Emits a {TransferSingle} event.
  224. *
  225. * Requirements:
  226. *
  227. * - `to` cannot be the zero address.
  228. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  229. * acceptance magic value.
  230. */
  231. function _mint(
  232. address to,
  233. uint256 id,
  234. uint256 amount,
  235. bytes memory data
  236. ) internal virtual {
  237. require(to != address(0), "ERC1155: mint to the zero address");
  238. address operator = _msgSender();
  239. uint256[] memory ids = _asSingletonArray(id);
  240. uint256[] memory amounts = _asSingletonArray(amount);
  241. _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
  242. _balances[id][to] += amount;
  243. emit TransferSingle(operator, address(0), to, id, amount);
  244. _afterTokenTransfer(operator, address(0), to, ids, amounts, data);
  245. _doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, 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(
  259. address to,
  260. uint256[] memory ids,
  261. uint256[] memory amounts,
  262. bytes memory data
  263. ) internal virtual {
  264. require(to != address(0), "ERC1155: mint to the zero address");
  265. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  266. address operator = _msgSender();
  267. _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
  268. for (uint256 i = 0; i < ids.length; i++) {
  269. _balances[ids[i]][to] += amounts[i];
  270. }
  271. emit TransferBatch(operator, address(0), to, ids, amounts);
  272. _afterTokenTransfer(operator, address(0), to, ids, amounts, data);
  273. _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
  274. }
  275. /**
  276. * @dev Destroys `amount` tokens of token type `id` from `from`
  277. *
  278. * Emits a {TransferSingle} event.
  279. *
  280. * Requirements:
  281. *
  282. * - `from` cannot be the zero address.
  283. * - `from` must have at least `amount` tokens of token type `id`.
  284. */
  285. function _burn(
  286. address from,
  287. uint256 id,
  288. uint256 amount
  289. ) internal virtual {
  290. require(from != address(0), "ERC1155: burn from the zero address");
  291. address operator = _msgSender();
  292. uint256[] memory ids = _asSingletonArray(id);
  293. uint256[] memory amounts = _asSingletonArray(amount);
  294. _beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
  295. uint256 fromBalance = _balances[id][from];
  296. require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
  297. unchecked {
  298. _balances[id][from] = fromBalance - amount;
  299. }
  300. emit TransferSingle(operator, from, address(0), id, amount);
  301. _afterTokenTransfer(operator, from, address(0), ids, amounts, "");
  302. }
  303. /**
  304. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  305. *
  306. * Emits a {TransferBatch} event.
  307. *
  308. * Requirements:
  309. *
  310. * - `ids` and `amounts` must have the same length.
  311. */
  312. function _burnBatch(
  313. address from,
  314. uint256[] memory ids,
  315. uint256[] memory amounts
  316. ) internal virtual {
  317. require(from != address(0), "ERC1155: burn from the zero address");
  318. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  319. address operator = _msgSender();
  320. _beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
  321. for (uint256 i = 0; i < ids.length; i++) {
  322. uint256 id = ids[i];
  323. uint256 amount = amounts[i];
  324. uint256 fromBalance = _balances[id][from];
  325. require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
  326. unchecked {
  327. _balances[id][from] = fromBalance - amount;
  328. }
  329. }
  330. emit TransferBatch(operator, from, address(0), ids, amounts);
  331. _afterTokenTransfer(operator, from, address(0), ids, amounts, "");
  332. }
  333. /**
  334. * @dev Approve `operator` to operate on all of `owner` tokens
  335. *
  336. * Emits an {ApprovalForAll} event.
  337. */
  338. function _setApprovalForAll(
  339. address owner,
  340. address operator,
  341. bool approved
  342. ) internal virtual {
  343. require(owner != operator, "ERC1155: setting approval status for self");
  344. _operatorApprovals[owner][operator] = approved;
  345. emit ApprovalForAll(owner, operator, approved);
  346. }
  347. /**
  348. * @dev Hook that is called before any token transfer. This includes minting
  349. * and burning, as well as batched variants.
  350. *
  351. * The same hook is called on both single and batched variants. For single
  352. * transfers, the length of the `ids` and `amounts` arrays will be 1.
  353. *
  354. * Calling conditions (for each `id` and `amount` pair):
  355. *
  356. * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  357. * of token type `id` will be transferred to `to`.
  358. * - When `from` is zero, `amount` tokens of token type `id` will be minted
  359. * for `to`.
  360. * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
  361. * will be burned.
  362. * - `from` and `to` are never both zero.
  363. * - `ids` and `amounts` have the same, non-zero length.
  364. *
  365. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  366. */
  367. function _beforeTokenTransfer(
  368. address operator,
  369. address from,
  370. address to,
  371. uint256[] memory ids,
  372. uint256[] memory amounts,
  373. bytes memory data
  374. ) internal virtual {}
  375. /**
  376. * @dev Hook that is called after any token transfer. This includes minting
  377. * and burning, as well as batched variants.
  378. *
  379. * The same hook is called on both single and batched variants. For single
  380. * transfers, the length of the `id` and `amount` arrays will be 1.
  381. *
  382. * Calling conditions (for each `id` and `amount` pair):
  383. *
  384. * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  385. * of token type `id` will be transferred to `to`.
  386. * - When `from` is zero, `amount` tokens of token type `id` will be minted
  387. * for `to`.
  388. * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
  389. * will be burned.
  390. * - `from` and `to` are never both zero.
  391. * - `ids` and `amounts` have the same, non-zero length.
  392. *
  393. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  394. */
  395. function _afterTokenTransfer(
  396. address operator,
  397. address from,
  398. address to,
  399. uint256[] memory ids,
  400. uint256[] memory amounts,
  401. bytes memory data
  402. ) internal virtual {}
  403. function _doSafeTransferAcceptanceCheck(
  404. address operator,
  405. address from,
  406. address to,
  407. uint256 id,
  408. uint256 amount,
  409. bytes memory data
  410. ) private {
  411. if (to.isContract()) {
  412. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  413. if (response != IERC1155Receiver.onERC1155Received.selector) {
  414. revert("ERC1155: ERC1155Receiver rejected tokens");
  415. }
  416. } catch Error(string memory reason) {
  417. revert(reason);
  418. } catch {
  419. revert("ERC1155: transfer to non-ERC1155Receiver implementer");
  420. }
  421. }
  422. }
  423. function _doSafeBatchTransferAcceptanceCheck(
  424. address operator,
  425. address from,
  426. address to,
  427. uint256[] memory ids,
  428. uint256[] memory amounts,
  429. bytes memory data
  430. ) private {
  431. if (to.isContract()) {
  432. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  433. bytes4 response
  434. ) {
  435. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  436. revert("ERC1155: ERC1155Receiver rejected tokens");
  437. }
  438. } catch Error(string memory reason) {
  439. revert(reason);
  440. } catch {
  441. revert("ERC1155: transfer to non-ERC1155Receiver implementer");
  442. }
  443. }
  444. }
  445. function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
  446. uint256[] memory array = new uint256[](1);
  447. array[0] = element;
  448. return array;
  449. }
  450. }