ERC1155.sol 15 KB

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