ERC1155.sol 13 KB

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