ERC1155.sol 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.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. require(account != address(0), "ERC1155: balance query for the zero address");
  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 owner nor 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: transfer caller is not owner nor 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. _beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
  151. uint256 fromBalance = _balances[id][from];
  152. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  153. unchecked {
  154. _balances[id][from] = fromBalance - amount;
  155. }
  156. _balances[id][to] += amount;
  157. emit TransferSingle(operator, from, to, id, amount);
  158. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  159. }
  160. /**
  161. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  162. *
  163. * Emits a {TransferBatch} event.
  164. *
  165. * Requirements:
  166. *
  167. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  168. * acceptance magic value.
  169. */
  170. function _safeBatchTransferFrom(
  171. address from,
  172. address to,
  173. uint256[] memory ids,
  174. uint256[] memory amounts,
  175. bytes memory data
  176. ) internal virtual {
  177. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  178. require(to != address(0), "ERC1155: transfer to the zero address");
  179. address operator = _msgSender();
  180. _beforeTokenTransfer(operator, from, to, ids, amounts, data);
  181. for (uint256 i = 0; i < ids.length; ++i) {
  182. uint256 id = ids[i];
  183. uint256 amount = amounts[i];
  184. uint256 fromBalance = _balances[id][from];
  185. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  186. unchecked {
  187. _balances[id][from] = fromBalance - amount;
  188. }
  189. _balances[id][to] += amount;
  190. }
  191. emit TransferBatch(operator, from, to, ids, amounts);
  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(
  228. address to,
  229. uint256 id,
  230. uint256 amount,
  231. bytes memory data
  232. ) internal virtual {
  233. require(to != address(0), "ERC1155: mint to the zero address");
  234. address operator = _msgSender();
  235. _beforeTokenTransfer(operator, address(0), to, _asSingletonArray(id), _asSingletonArray(amount), data);
  236. _balances[id][to] += amount;
  237. emit TransferSingle(operator, address(0), to, id, amount);
  238. _doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
  239. }
  240. /**
  241. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  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. _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
  264. }
  265. /**
  266. * @dev Destroys `amount` tokens of token type `id` from `from`
  267. *
  268. * Requirements:
  269. *
  270. * - `from` cannot be the zero address.
  271. * - `from` must have at least `amount` tokens of token type `id`.
  272. */
  273. function _burn(
  274. address from,
  275. uint256 id,
  276. uint256 amount
  277. ) internal virtual {
  278. require(from != address(0), "ERC1155: burn from the zero address");
  279. address operator = _msgSender();
  280. _beforeTokenTransfer(operator, from, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
  281. uint256 fromBalance = _balances[id][from];
  282. require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
  283. unchecked {
  284. _balances[id][from] = fromBalance - amount;
  285. }
  286. emit TransferSingle(operator, from, address(0), id, amount);
  287. }
  288. /**
  289. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  290. *
  291. * Requirements:
  292. *
  293. * - `ids` and `amounts` must have the same length.
  294. */
  295. function _burnBatch(
  296. address from,
  297. uint256[] memory ids,
  298. uint256[] memory amounts
  299. ) 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. }
  315. /**
  316. * @dev Approve `operator` to operate on all of `owner` tokens
  317. *
  318. * Emits a {ApprovalForAll} event.
  319. */
  320. function _setApprovalForAll(
  321. address owner,
  322. address operator,
  323. bool approved
  324. ) internal virtual {
  325. require(owner != operator, "ERC1155: setting approval status for self");
  326. _operatorApprovals[owner][operator] = approved;
  327. emit ApprovalForAll(owner, operator, approved);
  328. }
  329. /**
  330. * @dev Hook that is called before any token transfer. This includes minting
  331. * and burning, as well as batched variants.
  332. *
  333. * The same hook is called on both single and batched variants. For single
  334. * transfers, the length of the `id` and `amount` arrays will be 1.
  335. *
  336. * Calling conditions (for each `id` and `amount` pair):
  337. *
  338. * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  339. * of token type `id` will be transferred to `to`.
  340. * - When `from` is zero, `amount` tokens of token type `id` will be minted
  341. * for `to`.
  342. * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
  343. * will be burned.
  344. * - `from` and `to` are never both zero.
  345. * - `ids` and `amounts` have the same, non-zero length.
  346. *
  347. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  348. */
  349. function _beforeTokenTransfer(
  350. address operator,
  351. address from,
  352. address to,
  353. uint256[] memory ids,
  354. uint256[] memory amounts,
  355. bytes memory data
  356. ) internal virtual {}
  357. function _doSafeTransferAcceptanceCheck(
  358. address operator,
  359. address from,
  360. address to,
  361. uint256 id,
  362. uint256 amount,
  363. bytes memory data
  364. ) private {
  365. if (to.isContract()) {
  366. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  367. if (response != IERC1155Receiver.onERC1155Received.selector) {
  368. revert("ERC1155: ERC1155Receiver rejected tokens");
  369. }
  370. } catch Error(string memory reason) {
  371. revert(reason);
  372. } catch {
  373. revert("ERC1155: transfer to non ERC1155Receiver implementer");
  374. }
  375. }
  376. }
  377. function _doSafeBatchTransferAcceptanceCheck(
  378. address operator,
  379. address from,
  380. address to,
  381. uint256[] memory ids,
  382. uint256[] memory amounts,
  383. bytes memory data
  384. ) private {
  385. if (to.isContract()) {
  386. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  387. bytes4 response
  388. ) {
  389. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  390. revert("ERC1155: ERC1155Receiver rejected tokens");
  391. }
  392. } catch Error(string memory reason) {
  393. revert(reason);
  394. } catch {
  395. revert("ERC1155: transfer to non ERC1155Receiver implementer");
  396. }
  397. }
  398. }
  399. function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
  400. uint256[] memory array = new uint256[](1);
  401. array[0] = element;
  402. return array;
  403. }
  404. }