ERC1155.sol 15 KB

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