ERC1155.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC1155/ERC1155.sol)
  3. pragma solidity ^0.8.19;
  4. import "./IERC1155.sol";
  5. import "./IERC1155Receiver.sol";
  6. import "./extensions/IERC1155MetadataURI.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. // Mapping from token ID to account balances
  18. mapping(uint256 => mapping(address => uint256)) private _balances;
  19. // Mapping from account to operator approvals
  20. mapping(address => mapping(address => bool)) private _operatorApprovals;
  21. // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
  22. string private _uri;
  23. /**
  24. * @dev See {_setURI}.
  25. */
  26. constructor(string memory uri_) {
  27. _setURI(uri_);
  28. }
  29. /**
  30. * @dev See {IERC165-supportsInterface}.
  31. */
  32. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  33. return
  34. interfaceId == type(IERC1155).interfaceId ||
  35. interfaceId == type(IERC1155MetadataURI).interfaceId ||
  36. super.supportsInterface(interfaceId);
  37. }
  38. /**
  39. * @dev See {IERC1155MetadataURI-uri}.
  40. *
  41. * This implementation returns the same URI for *all* token types. It relies
  42. * on the token type ID substitution mechanism
  43. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  44. *
  45. * Clients calling this function must replace the `\{id\}` substring with the
  46. * actual token type ID.
  47. */
  48. function uri(uint256) public view virtual returns (string memory) {
  49. return _uri;
  50. }
  51. /**
  52. * @dev See {IERC1155-balanceOf}.
  53. *
  54. * Requirements:
  55. *
  56. * - `account` cannot be the zero address.
  57. */
  58. function balanceOf(address account, uint256 id) public view virtual returns (uint256) {
  59. return _balances[id][account];
  60. }
  61. /**
  62. * @dev See {IERC1155-balanceOfBatch}.
  63. *
  64. * Requirements:
  65. *
  66. * - `accounts` and `ids` must have the same length.
  67. */
  68. function balanceOfBatch(
  69. address[] memory accounts,
  70. uint256[] memory ids
  71. ) public view virtual returns (uint256[] memory) {
  72. require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
  73. uint256[] memory batchBalances = new uint256[](accounts.length);
  74. for (uint256 i = 0; i < accounts.length; ++i) {
  75. batchBalances[i] = balanceOf(accounts[i], ids[i]);
  76. }
  77. return batchBalances;
  78. }
  79. /**
  80. * @dev See {IERC1155-setApprovalForAll}.
  81. */
  82. function setApprovalForAll(address operator, bool approved) public virtual {
  83. _setApprovalForAll(_msgSender(), operator, approved);
  84. }
  85. /**
  86. * @dev See {IERC1155-isApprovedForAll}.
  87. */
  88. function isApprovedForAll(address account, address operator) public view virtual returns (bool) {
  89. return _operatorApprovals[account][operator];
  90. }
  91. /**
  92. * @dev See {IERC1155-safeTransferFrom}.
  93. */
  94. function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) public virtual {
  95. require(
  96. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  97. "ERC1155: caller is not token owner or approved"
  98. );
  99. _safeTransferFrom(from, to, id, amount, data);
  100. }
  101. /**
  102. * @dev See {IERC1155-safeBatchTransferFrom}.
  103. */
  104. function safeBatchTransferFrom(
  105. address from,
  106. address to,
  107. uint256[] memory ids,
  108. uint256[] memory amounts,
  109. bytes memory data
  110. ) public virtual {
  111. require(
  112. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  113. "ERC1155: caller is not token owner or approved"
  114. );
  115. _safeBatchTransferFrom(from, to, ids, amounts, data);
  116. }
  117. /**
  118. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`. Will mint (or burn) if `from` (or `to`) is the zero address.
  119. *
  120. * Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise.
  121. *
  122. * Requirements:
  123. *
  124. * - If `to` refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received}
  125. * or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  126. */
  127. function _update(
  128. address from,
  129. address to,
  130. uint256[] memory ids,
  131. uint256[] memory amounts,
  132. bytes memory data
  133. ) internal virtual {
  134. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  135. address operator = _msgSender();
  136. for (uint256 i = 0; i < ids.length; ++i) {
  137. uint256 id = ids[i];
  138. uint256 amount = amounts[i];
  139. if (from != address(0)) {
  140. uint256 fromBalance = _balances[id][from];
  141. require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
  142. unchecked {
  143. _balances[id][from] = fromBalance - amount;
  144. }
  145. }
  146. if (to != address(0)) {
  147. _balances[id][to] += amount;
  148. }
  149. }
  150. if (ids.length == 1) {
  151. uint256 id = ids[0];
  152. uint256 amount = amounts[0];
  153. emit TransferSingle(operator, from, to, id, amount);
  154. if (to != address(0)) {
  155. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  156. }
  157. } else {
  158. emit TransferBatch(operator, from, to, ids, amounts);
  159. if (to != address(0)) {
  160. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  161. }
  162. }
  163. }
  164. /**
  165. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
  166. *
  167. * Emits a {TransferSingle} event.
  168. *
  169. * Requirements:
  170. *
  171. * - `to` cannot be the zero address.
  172. * - `from` must have a balance of tokens of type `id` of at least `amount`.
  173. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  174. * acceptance magic value.
  175. */
  176. function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
  177. require(to != address(0), "ERC1155: transfer to the zero address");
  178. require(from != address(0), "ERC1155: transfer from the zero address");
  179. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  180. _update(from, to, ids, amounts, data);
  181. }
  182. /**
  183. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  184. *
  185. * Emits a {TransferBatch} event.
  186. *
  187. * Requirements:
  188. *
  189. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  190. * acceptance magic value.
  191. */
  192. function _safeBatchTransferFrom(
  193. address from,
  194. address to,
  195. uint256[] memory ids,
  196. uint256[] memory amounts,
  197. bytes memory data
  198. ) internal {
  199. require(to != address(0), "ERC1155: transfer to the zero address");
  200. require(from != address(0), "ERC1155: transfer from the zero address");
  201. _update(from, to, ids, amounts, data);
  202. }
  203. /**
  204. * @dev Sets a new URI for all token types, by relying on the token type ID
  205. * substitution mechanism
  206. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  207. *
  208. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  209. * URI or any of the amounts in the JSON file at said URI will be replaced by
  210. * clients with the token type ID.
  211. *
  212. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  213. * interpreted by clients as
  214. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  215. * for token type ID 0x4cce0.
  216. *
  217. * See {uri}.
  218. *
  219. * Because these URIs cannot be meaningfully represented by the {URI} event,
  220. * this function emits no events.
  221. */
  222. function _setURI(string memory newuri) internal virtual {
  223. _uri = newuri;
  224. }
  225. /**
  226. * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
  227. *
  228. * Emits a {TransferSingle} event.
  229. *
  230. * Requirements:
  231. *
  232. * - `to` cannot be the zero address.
  233. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  234. * acceptance magic value.
  235. */
  236. function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
  237. require(to != address(0), "ERC1155: mint to the zero address");
  238. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  239. _update(address(0), to, ids, amounts, data);
  240. }
  241. /**
  242. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  243. *
  244. * Emits a {TransferBatch} event.
  245. *
  246. * Requirements:
  247. *
  248. * - `ids` and `amounts` must have the same length.
  249. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  250. * acceptance magic value.
  251. */
  252. function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal {
  253. require(to != address(0), "ERC1155: mint to the zero address");
  254. _update(address(0), to, ids, amounts, data);
  255. }
  256. /**
  257. * @dev Destroys `amount` tokens of token type `id` from `from`
  258. *
  259. * Emits a {TransferSingle} event.
  260. *
  261. * Requirements:
  262. *
  263. * - `from` cannot be the zero address.
  264. * - `from` must have at least `amount` tokens of token type `id`.
  265. */
  266. function _burn(address from, uint256 id, uint256 amount) internal {
  267. require(from != address(0), "ERC1155: burn from the zero address");
  268. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  269. _update(from, address(0), ids, amounts, "");
  270. }
  271. /**
  272. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  273. *
  274. * Emits a {TransferBatch} event.
  275. *
  276. * Requirements:
  277. *
  278. * - `ids` and `amounts` must have the same length.
  279. */
  280. function _burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) internal {
  281. require(from != address(0), "ERC1155: burn from the zero address");
  282. _update(from, address(0), ids, amounts, "");
  283. }
  284. /**
  285. * @dev Approve `operator` to operate on all of `owner` tokens
  286. *
  287. * Emits an {ApprovalForAll} event.
  288. */
  289. function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
  290. require(owner != operator, "ERC1155: setting approval status for self");
  291. _operatorApprovals[owner][operator] = approved;
  292. emit ApprovalForAll(owner, operator, approved);
  293. }
  294. function _doSafeTransferAcceptanceCheck(
  295. address operator,
  296. address from,
  297. address to,
  298. uint256 id,
  299. uint256 amount,
  300. bytes memory data
  301. ) private {
  302. if (to.code.length > 0) {
  303. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  304. if (response != IERC1155Receiver.onERC1155Received.selector) {
  305. revert("ERC1155: ERC1155Receiver rejected tokens");
  306. }
  307. } catch Error(string memory reason) {
  308. revert(reason);
  309. } catch {
  310. revert("ERC1155: transfer to non-ERC1155Receiver implementer");
  311. }
  312. }
  313. }
  314. function _doSafeBatchTransferAcceptanceCheck(
  315. address operator,
  316. address from,
  317. address to,
  318. uint256[] memory ids,
  319. uint256[] memory amounts,
  320. bytes memory data
  321. ) private {
  322. if (to.code.length > 0) {
  323. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  324. bytes4 response
  325. ) {
  326. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  327. revert("ERC1155: ERC1155Receiver rejected tokens");
  328. }
  329. } catch Error(string memory reason) {
  330. revert(reason);
  331. } catch {
  332. revert("ERC1155: transfer to non-ERC1155Receiver implementer");
  333. }
  334. }
  335. }
  336. function _asSingletonArrays(
  337. uint256 element1,
  338. uint256 element2
  339. ) private pure returns (uint256[] memory array1, uint256[] memory array2) {
  340. /// @solidity memory-safe-assembly
  341. assembly {
  342. array1 := mload(0x40)
  343. mstore(array1, 1)
  344. mstore(add(array1, 0x20), element1)
  345. array2 := add(array1, 0x40)
  346. mstore(array2, 1)
  347. mstore(add(array2, 0x20), element2)
  348. mstore(0x40, add(array2, 0x40))
  349. }
  350. }
  351. }