ERC1155.sol 17 KB

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