ERC1155.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. import "../../interfaces/draft-IERC6093.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, IERC1155Errors {
  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 Indicates that an address can't be an owner.
  26. */
  27. error ERC1155InvalidOwner(address owner);
  28. /**
  29. * @dev See {_setURI}.
  30. */
  31. constructor(string memory uri_) {
  32. _setURI(uri_);
  33. }
  34. /**
  35. * @dev See {IERC165-supportsInterface}.
  36. */
  37. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  38. return
  39. interfaceId == type(IERC1155).interfaceId ||
  40. interfaceId == type(IERC1155MetadataURI).interfaceId ||
  41. super.supportsInterface(interfaceId);
  42. }
  43. /**
  44. * @dev See {IERC1155MetadataURI-uri}.
  45. *
  46. * This implementation returns the same URI for *all* token types. It relies
  47. * on the token type ID substitution mechanism
  48. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  49. *
  50. * Clients calling this function must replace the `\{id\}` substring with the
  51. * actual token type ID.
  52. */
  53. function uri(uint256) public view virtual override returns (string memory) {
  54. return _uri;
  55. }
  56. /**
  57. * @dev See {IERC1155-balanceOf}.
  58. *
  59. * Requirements:
  60. *
  61. * - `account` cannot be the zero address.
  62. */
  63. function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
  64. return _balances[id][account];
  65. }
  66. /**
  67. * @dev See {IERC1155-balanceOfBatch}.
  68. *
  69. * Requirements:
  70. *
  71. * - `accounts` and `ids` must have the same length.
  72. */
  73. function balanceOfBatch(
  74. address[] memory accounts,
  75. uint256[] memory ids
  76. ) public view virtual override returns (uint256[] memory) {
  77. if (accounts.length != ids.length) {
  78. revert ERC1155InvalidArrayLength(ids.length, accounts.length);
  79. }
  80. uint256[] memory batchBalances = new uint256[](accounts.length);
  81. for (uint256 i = 0; i < accounts.length; ++i) {
  82. batchBalances[i] = balanceOf(accounts[i], ids[i]);
  83. }
  84. return batchBalances;
  85. }
  86. /**
  87. * @dev See {IERC1155-setApprovalForAll}.
  88. */
  89. function setApprovalForAll(address operator, bool approved) public virtual override {
  90. _setApprovalForAll(_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. if (from != _msgSender() && !isApprovedForAll(from, _msgSender())) {
  109. revert ERC1155InsufficientApprovalForAll(_msgSender(), from);
  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. if (from != _msgSender() && !isApprovedForAll(from, _msgSender())) {
  124. revert ERC1155InsufficientApprovalForAll(_msgSender(), from);
  125. }
  126. _safeBatchTransferFrom(from, to, ids, amounts, data);
  127. }
  128. /**
  129. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`. Will mint (or burn) if `from` (or `to`) is the zero address.
  130. *
  131. * Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise.
  132. *
  133. * Requirements:
  134. *
  135. * - If `to` refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received}
  136. * or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  137. */
  138. function _update(
  139. address from,
  140. address to,
  141. uint256[] memory ids,
  142. uint256[] memory amounts,
  143. bytes memory data
  144. ) internal virtual {
  145. if (ids.length != amounts.length) {
  146. revert ERC1155InvalidArrayLength(ids.length, amounts.length);
  147. }
  148. address operator = _msgSender();
  149. for (uint256 i = 0; i < ids.length; ++i) {
  150. uint256 id = ids[i];
  151. uint256 amount = amounts[i];
  152. if (from != address(0)) {
  153. uint256 fromBalance = _balances[id][from];
  154. if (fromBalance < amount) {
  155. revert ERC1155InsufficientBalance(from, fromBalance, amount, id);
  156. }
  157. unchecked {
  158. _balances[id][from] = fromBalance - amount;
  159. }
  160. }
  161. if (to != address(0)) {
  162. _balances[id][to] += amount;
  163. }
  164. }
  165. if (ids.length == 1) {
  166. uint256 id = ids[0];
  167. uint256 amount = amounts[0];
  168. emit TransferSingle(operator, from, to, id, amount);
  169. if (to != address(0)) {
  170. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  171. }
  172. } else {
  173. emit TransferBatch(operator, from, to, ids, amounts);
  174. if (to != address(0)) {
  175. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  176. }
  177. }
  178. }
  179. /**
  180. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
  181. *
  182. * Emits a {TransferSingle} event.
  183. *
  184. * Requirements:
  185. *
  186. * - `to` cannot be the zero address.
  187. * - `from` must have a balance of tokens of type `id` of at least `amount`.
  188. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  189. * acceptance magic value.
  190. */
  191. function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
  192. if (to == address(0)) {
  193. revert ERC1155InvalidReceiver(address(0));
  194. }
  195. if (from == address(0)) {
  196. revert ERC1155InvalidSender(address(0));
  197. }
  198. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  199. _update(from, to, ids, amounts, data);
  200. }
  201. /**
  202. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  203. *
  204. * Emits a {TransferBatch} event.
  205. *
  206. * Requirements:
  207. *
  208. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  209. * acceptance magic value.
  210. */
  211. function _safeBatchTransferFrom(
  212. address from,
  213. address to,
  214. uint256[] memory ids,
  215. uint256[] memory amounts,
  216. bytes memory data
  217. ) internal {
  218. if (to == address(0)) {
  219. revert ERC1155InvalidReceiver(address(0));
  220. }
  221. if (from == address(0)) {
  222. revert ERC1155InvalidSender(address(0));
  223. }
  224. _update(from, to, ids, amounts, data);
  225. }
  226. /**
  227. * @dev Sets a new URI for all token types, by relying on the token type ID
  228. * substitution mechanism
  229. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  230. *
  231. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  232. * URI or any of the amounts in the JSON file at said URI will be replaced by
  233. * clients with the token type ID.
  234. *
  235. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  236. * interpreted by clients as
  237. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  238. * for token type ID 0x4cce0.
  239. *
  240. * See {uri}.
  241. *
  242. * Because these URIs cannot be meaningfully represented by the {URI} event,
  243. * this function emits no events.
  244. */
  245. function _setURI(string memory newuri) internal virtual {
  246. _uri = newuri;
  247. }
  248. /**
  249. * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
  250. *
  251. * Emits a {TransferSingle} event.
  252. *
  253. * Requirements:
  254. *
  255. * - `to` cannot be the zero address.
  256. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  257. * acceptance magic value.
  258. */
  259. function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
  260. if (to == address(0)) {
  261. revert ERC1155InvalidReceiver(address(0));
  262. }
  263. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  264. _update(address(0), to, ids, amounts, data);
  265. }
  266. /**
  267. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  268. *
  269. * Emits a {TransferBatch} event.
  270. *
  271. * Requirements:
  272. *
  273. * - `ids` and `amounts` must have the same length.
  274. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  275. * acceptance magic value.
  276. */
  277. function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal {
  278. if (to == address(0)) {
  279. revert ERC1155InvalidReceiver(address(0));
  280. }
  281. _update(address(0), to, ids, amounts, data);
  282. }
  283. /**
  284. * @dev Destroys `amount` tokens of token type `id` from `from`
  285. *
  286. * Emits a {TransferSingle} event.
  287. *
  288. * Requirements:
  289. *
  290. * - `from` cannot be the zero address.
  291. * - `from` must have at least `amount` tokens of token type `id`.
  292. */
  293. function _burn(address from, uint256 id, uint256 amount) internal {
  294. if (from == address(0)) {
  295. revert ERC1155InvalidSender(address(0));
  296. }
  297. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  298. _update(from, address(0), ids, amounts, "");
  299. }
  300. /**
  301. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  302. *
  303. * Emits a {TransferBatch} event.
  304. *
  305. * Requirements:
  306. *
  307. * - `ids` and `amounts` must have the same length.
  308. */
  309. function _burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) internal {
  310. if (from == address(0)) {
  311. revert ERC1155InvalidSender(address(0));
  312. }
  313. _update(from, address(0), ids, amounts, "");
  314. }
  315. /**
  316. * @dev Approve `operator` to operate on all of `owner` tokens
  317. *
  318. * Emits an {ApprovalForAll} event.
  319. */
  320. function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
  321. if (owner == operator) {
  322. revert ERC1155InvalidOperator(operator);
  323. }
  324. _operatorApprovals[owner][operator] = approved;
  325. emit ApprovalForAll(owner, operator, approved);
  326. }
  327. function _doSafeTransferAcceptanceCheck(
  328. address operator,
  329. address from,
  330. address to,
  331. uint256 id,
  332. uint256 amount,
  333. bytes memory data
  334. ) private {
  335. if (to.code.length > 0) {
  336. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  337. if (response != IERC1155Receiver.onERC1155Received.selector) {
  338. // Tokens rejected
  339. revert ERC1155InvalidReceiver(to);
  340. }
  341. } catch Error(string memory reason) {
  342. revert(reason);
  343. } catch {
  344. // non-ERC1155Receiver implementer
  345. revert ERC1155InvalidReceiver(to);
  346. }
  347. }
  348. }
  349. function _doSafeBatchTransferAcceptanceCheck(
  350. address operator,
  351. address from,
  352. address to,
  353. uint256[] memory ids,
  354. uint256[] memory amounts,
  355. bytes memory data
  356. ) private {
  357. if (to.code.length > 0) {
  358. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  359. bytes4 response
  360. ) {
  361. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  362. // Tokens rejected
  363. revert ERC1155InvalidReceiver(to);
  364. }
  365. } catch Error(string memory reason) {
  366. revert(reason);
  367. } catch {
  368. // non-ERC1155Receiver implementer
  369. revert ERC1155InvalidReceiver(to);
  370. }
  371. }
  372. }
  373. function _asSingletonArrays(
  374. uint256 element1,
  375. uint256 element2
  376. ) private pure returns (uint256[] memory array1, uint256[] memory array2) {
  377. /// @solidity memory-safe-assembly
  378. assembly {
  379. array1 := mload(0x40)
  380. mstore(array1, 1)
  381. mstore(add(array1, 0x20), element1)
  382. array2 := add(array1, 0x40)
  383. mstore(array2, 1)
  384. mstore(add(array2, 0x20), element2)
  385. mstore(0x40, add(array2, 0x40))
  386. }
  387. }
  388. }