ERC1155.sol 15 KB

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