ERC1155.sol 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./IERC1155.sol";
  4. import "./IERC1155Receiver.sol";
  5. import "./extensions/IERC1155MetadataURI.sol";
  6. import "../../utils/Address.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. using Address for address;
  18. // Mapping from token ID to account balances
  19. mapping(uint256 => mapping(address => uint256)) private _balances;
  20. // Mapping from account to operator approvals
  21. mapping(address => mapping(address => bool)) private _operatorApprovals;
  22. // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
  23. string private _uri;
  24. /**
  25. * @dev See {_setURI}.
  26. */
  27. constructor(string memory uri_) {
  28. _setURI(uri_);
  29. }
  30. /**
  31. * @dev See {IERC165-supportsInterface}.
  32. */
  33. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  34. return
  35. interfaceId == type(IERC1155).interfaceId ||
  36. interfaceId == type(IERC1155MetadataURI).interfaceId ||
  37. super.supportsInterface(interfaceId);
  38. }
  39. /**
  40. * @dev See {IERC1155MetadataURI-uri}.
  41. *
  42. * This implementation returns the same URI for *all* token types. It relies
  43. * on the token type ID substitution mechanism
  44. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  45. *
  46. * Clients calling this function must replace the `\{id\}` substring with the
  47. * actual token type ID.
  48. */
  49. function uri(uint256) public view virtual override returns (string memory) {
  50. return _uri;
  51. }
  52. /**
  53. * @dev See {IERC1155-balanceOf}.
  54. *
  55. * Requirements:
  56. *
  57. * - `account` cannot be the zero address.
  58. */
  59. function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
  60. require(account != address(0), "ERC1155: balance query for the zero address");
  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(address[] memory accounts, uint256[] memory ids)
  71. public
  72. view
  73. virtual
  74. override
  75. returns (uint256[] memory)
  76. {
  77. require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
  78. uint256[] memory batchBalances = new uint256[](accounts.length);
  79. for (uint256 i = 0; i < accounts.length; ++i) {
  80. batchBalances[i] = balanceOf(accounts[i], ids[i]);
  81. }
  82. return batchBalances;
  83. }
  84. /**
  85. * @dev See {IERC1155-setApprovalForAll}.
  86. */
  87. function setApprovalForAll(address operator, bool approved) public virtual override {
  88. _setApprovalForAll(_msgSender(), operator, approved);
  89. }
  90. /**
  91. * @dev See {IERC1155-isApprovedForAll}.
  92. */
  93. function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
  94. return _operatorApprovals[account][operator];
  95. }
  96. /**
  97. * @dev See {IERC1155-safeTransferFrom}.
  98. */
  99. function safeTransferFrom(
  100. address from,
  101. address to,
  102. uint256 id,
  103. uint256 amount,
  104. bytes memory data
  105. ) public virtual override {
  106. require(
  107. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  108. "ERC1155: caller is not owner nor approved"
  109. );
  110. _safeTransferFrom(from, to, id, amount, data);
  111. }
  112. /**
  113. * @dev See {IERC1155-safeBatchTransferFrom}.
  114. */
  115. function safeBatchTransferFrom(
  116. address from,
  117. address to,
  118. uint256[] memory ids,
  119. uint256[] memory amounts,
  120. bytes memory data
  121. ) public virtual override {
  122. require(
  123. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  124. "ERC1155: transfer caller is not owner nor approved"
  125. );
  126. _safeBatchTransferFrom(from, to, ids, amounts, data);
  127. }
  128. /**
  129. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
  130. *
  131. * Emits a {TransferSingle} event.
  132. *
  133. * Requirements:
  134. *
  135. * - `to` cannot be the zero address.
  136. * - `from` must have a balance of tokens of type `id` of at least `amount`.
  137. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  138. * acceptance magic value.
  139. */
  140. function _safeTransferFrom(
  141. address from,
  142. address to,
  143. uint256 id,
  144. uint256 amount,
  145. bytes memory data
  146. ) internal virtual {
  147. require(to != address(0), "ERC1155: transfer to the zero address");
  148. address operator = _msgSender();
  149. _beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
  150. uint256 fromBalance = _balances[id][from];
  151. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  152. unchecked {
  153. _balances[id][from] = fromBalance - amount;
  154. }
  155. _balances[id][to] += amount;
  156. emit TransferSingle(operator, from, to, id, amount);
  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. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  192. }
  193. /**
  194. * @dev Sets a new URI for all token types, by relying on the token type ID
  195. * substitution mechanism
  196. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  197. *
  198. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  199. * URI or any of the amounts in the JSON file at said URI will be replaced by
  200. * clients with the token type ID.
  201. *
  202. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  203. * interpreted by clients as
  204. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  205. * for token type ID 0x4cce0.
  206. *
  207. * See {uri}.
  208. *
  209. * Because these URIs cannot be meaningfully represented by the {URI} event,
  210. * this function emits no events.
  211. */
  212. function _setURI(string memory newuri) internal virtual {
  213. _uri = newuri;
  214. }
  215. /**
  216. * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
  217. *
  218. * Emits a {TransferSingle} event.
  219. *
  220. * Requirements:
  221. *
  222. * - `to` cannot be the zero address.
  223. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  224. * acceptance magic value.
  225. */
  226. function _mint(
  227. address to,
  228. uint256 id,
  229. uint256 amount,
  230. bytes memory data
  231. ) internal virtual {
  232. require(to != address(0), "ERC1155: mint to the zero address");
  233. address operator = _msgSender();
  234. _beforeTokenTransfer(operator, address(0), to, _asSingletonArray(id), _asSingletonArray(amount), data);
  235. _balances[id][to] += amount;
  236. emit TransferSingle(operator, address(0), to, id, amount);
  237. _doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
  238. }
  239. /**
  240. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  241. *
  242. * Requirements:
  243. *
  244. * - `ids` and `amounts` must have the same length.
  245. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  246. * acceptance magic value.
  247. */
  248. function _mintBatch(
  249. address to,
  250. uint256[] memory ids,
  251. uint256[] memory amounts,
  252. bytes memory data
  253. ) internal virtual {
  254. require(to != address(0), "ERC1155: mint to the zero address");
  255. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  256. address operator = _msgSender();
  257. _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
  258. for (uint256 i = 0; i < ids.length; i++) {
  259. _balances[ids[i]][to] += amounts[i];
  260. }
  261. emit TransferBatch(operator, address(0), to, ids, amounts);
  262. _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
  263. }
  264. /**
  265. * @dev Destroys `amount` tokens of token type `id` from `from`
  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(
  273. address from,
  274. uint256 id,
  275. uint256 amount
  276. ) internal virtual {
  277. require(from != address(0), "ERC1155: burn from the zero address");
  278. address operator = _msgSender();
  279. _beforeTokenTransfer(operator, from, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
  280. uint256 fromBalance = _balances[id][from];
  281. require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
  282. unchecked {
  283. _balances[id][from] = fromBalance - amount;
  284. }
  285. emit TransferSingle(operator, from, address(0), id, amount);
  286. }
  287. /**
  288. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  289. *
  290. * Requirements:
  291. *
  292. * - `ids` and `amounts` must have the same length.
  293. */
  294. function _burnBatch(
  295. address from,
  296. uint256[] memory ids,
  297. uint256[] memory amounts
  298. ) internal virtual {
  299. require(from != address(0), "ERC1155: burn from the zero address");
  300. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  301. address operator = _msgSender();
  302. _beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
  303. for (uint256 i = 0; i < ids.length; i++) {
  304. uint256 id = ids[i];
  305. uint256 amount = amounts[i];
  306. uint256 fromBalance = _balances[id][from];
  307. require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
  308. unchecked {
  309. _balances[id][from] = fromBalance - amount;
  310. }
  311. }
  312. emit TransferBatch(operator, from, address(0), ids, amounts);
  313. }
  314. /**
  315. * @dev Approve `operator` to operate on all of `owner` tokens
  316. *
  317. * Emits a {ApprovalForAll} event.
  318. */
  319. function _setApprovalForAll(
  320. address owner,
  321. address operator,
  322. bool approved
  323. ) internal virtual {
  324. require(owner != operator, "ERC1155: setting approval status for self");
  325. _operatorApprovals[owner][operator] = approved;
  326. emit ApprovalForAll(owner, operator, approved);
  327. }
  328. /**
  329. * @dev Hook that is called before any token transfer. This includes minting
  330. * and burning, as well as batched variants.
  331. *
  332. * The same hook is called on both single and batched variants. For single
  333. * transfers, the length of the `id` and `amount` arrays will be 1.
  334. *
  335. * Calling conditions (for each `id` and `amount` pair):
  336. *
  337. * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  338. * of token type `id` will be transferred to `to`.
  339. * - When `from` is zero, `amount` tokens of token type `id` will be minted
  340. * for `to`.
  341. * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
  342. * will be burned.
  343. * - `from` and `to` are never both zero.
  344. * - `ids` and `amounts` have the same, non-zero length.
  345. *
  346. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  347. */
  348. function _beforeTokenTransfer(
  349. address operator,
  350. address from,
  351. address to,
  352. uint256[] memory ids,
  353. uint256[] memory amounts,
  354. bytes memory data
  355. ) internal virtual {}
  356. function _doSafeTransferAcceptanceCheck(
  357. address operator,
  358. address from,
  359. address to,
  360. uint256 id,
  361. uint256 amount,
  362. bytes memory data
  363. ) private {
  364. if (to.isContract()) {
  365. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  366. if (response != IERC1155Receiver.onERC1155Received.selector) {
  367. revert("ERC1155: ERC1155Receiver rejected tokens");
  368. }
  369. } catch Error(string memory reason) {
  370. revert(reason);
  371. } catch {
  372. revert("ERC1155: transfer to non ERC1155Receiver implementer");
  373. }
  374. }
  375. }
  376. function _doSafeBatchTransferAcceptanceCheck(
  377. address operator,
  378. address from,
  379. address to,
  380. uint256[] memory ids,
  381. uint256[] memory amounts,
  382. bytes memory data
  383. ) private {
  384. if (to.isContract()) {
  385. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  386. bytes4 response
  387. ) {
  388. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  389. revert("ERC1155: ERC1155Receiver rejected tokens");
  390. }
  391. } catch Error(string memory reason) {
  392. revert(reason);
  393. } catch {
  394. revert("ERC1155: transfer to non ERC1155Receiver implementer");
  395. }
  396. }
  397. }
  398. function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
  399. uint256[] memory array = new uint256[](1);
  400. array[0] = element;
  401. return array;
  402. }
  403. }