ERC1155.sol 13 KB

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