ERC1155.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./IERC1155.sol";
  4. import "./IERC1155MetadataURI.sol";
  5. import "./IERC1155Receiver.sol";
  6. import "../../utils/Context.sol";
  7. import "../../introspection/ERC165.sol";
  8. import "../../utils/Address.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. // register the supported interfaces to conform to ERC1155 via ERC165
  31. _registerInterface(type(IERC1155).interfaceId);
  32. // register the supported interfaces to conform to ERC1155MetadataURI via ERC165
  33. _registerInterface(type(IERC1155MetadataURI).interfaceId);
  34. }
  35. /**
  36. * @dev See {IERC1155MetadataURI-uri}.
  37. *
  38. * This implementation returns the same URI for *all* token types. It relies
  39. * on the token type ID substitution mechanism
  40. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  41. *
  42. * Clients calling this function must replace the `\{id\}` substring with the
  43. * actual token type ID.
  44. */
  45. function uri(uint256) external view virtual override returns (string memory) {
  46. return _uri;
  47. }
  48. /**
  49. * @dev See {IERC1155-balanceOf}.
  50. *
  51. * Requirements:
  52. *
  53. * - `account` cannot be the zero address.
  54. */
  55. function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
  56. require(account != address(0), "ERC1155: balance query for the zero address");
  57. return _balances[id][account];
  58. }
  59. /**
  60. * @dev See {IERC1155-balanceOfBatch}.
  61. *
  62. * Requirements:
  63. *
  64. * - `accounts` and `ids` must have the same length.
  65. */
  66. function balanceOfBatch(
  67. address[] memory accounts,
  68. uint256[] memory ids
  69. )
  70. public
  71. view
  72. virtual
  73. override
  74. returns (uint256[] memory)
  75. {
  76. require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
  77. uint256[] memory batchBalances = new uint256[](accounts.length);
  78. for (uint256 i = 0; i < accounts.length; ++i) {
  79. batchBalances[i] = balanceOf(accounts[i], ids[i]);
  80. }
  81. return batchBalances;
  82. }
  83. /**
  84. * @dev See {IERC1155-setApprovalForAll}.
  85. */
  86. function setApprovalForAll(address operator, bool approved) public virtual override {
  87. require(_msgSender() != operator, "ERC1155: setting approval status for self");
  88. _operatorApprovals[_msgSender()][operator] = approved;
  89. emit ApprovalForAll(_msgSender(), operator, approved);
  90. }
  91. /**
  92. * @dev See {IERC1155-isApprovedForAll}.
  93. */
  94. function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
  95. return _operatorApprovals[account][operator];
  96. }
  97. /**
  98. * @dev See {IERC1155-safeTransferFrom}.
  99. */
  100. function safeTransferFrom(
  101. address from,
  102. address to,
  103. uint256 id,
  104. uint256 amount,
  105. bytes memory data
  106. )
  107. public
  108. virtual
  109. override
  110. {
  111. require(to != address(0), "ERC1155: transfer to the zero address");
  112. require(
  113. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  114. "ERC1155: caller is not owner nor approved"
  115. );
  116. address operator = _msgSender();
  117. _beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
  118. require(_balances[id][from] >= amount, "ERC1155: insufficient balance for transfer");
  119. _balances[id][from] -= amount;
  120. _balances[id][to] += amount;
  121. emit TransferSingle(operator, from, to, id, amount);
  122. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  123. }
  124. /**
  125. * @dev See {IERC1155-safeBatchTransferFrom}.
  126. */
  127. function safeBatchTransferFrom(
  128. address from,
  129. address to,
  130. uint256[] memory ids,
  131. uint256[] memory amounts,
  132. bytes memory data
  133. )
  134. public
  135. virtual
  136. override
  137. {
  138. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  139. require(to != address(0), "ERC1155: transfer to the zero address");
  140. require(
  141. from == _msgSender() || isApprovedForAll(from, _msgSender()),
  142. "ERC1155: transfer caller is not owner nor approved"
  143. );
  144. address operator = _msgSender();
  145. _beforeTokenTransfer(operator, from, to, ids, amounts, data);
  146. for (uint256 i = 0; i < ids.length; ++i) {
  147. uint256 id = ids[i];
  148. uint256 amount = amounts[i];
  149. require(_balances[id][from] >= amount, "ERC1155: insufficient balance for transfer");
  150. _balances[id][from] -= amount;
  151. _balances[id][to] += amount;
  152. }
  153. emit TransferBatch(operator, from, to, ids, amounts);
  154. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  155. }
  156. /**
  157. * @dev Sets a new URI for all token types, by relying on the token type ID
  158. * substitution mechanism
  159. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  160. *
  161. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  162. * URI or any of the amounts in the JSON file at said URI will be replaced by
  163. * clients with the token type ID.
  164. *
  165. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  166. * interpreted by clients as
  167. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  168. * for token type ID 0x4cce0.
  169. *
  170. * See {uri}.
  171. *
  172. * Because these URIs cannot be meaningfully represented by the {URI} event,
  173. * this function emits no events.
  174. */
  175. function _setURI(string memory newuri) internal virtual {
  176. _uri = newuri;
  177. }
  178. /**
  179. * @dev Creates `amount` tokens of token type `id`, and assigns them to `account`.
  180. *
  181. * Emits a {TransferSingle} event.
  182. *
  183. * Requirements:
  184. *
  185. * - `account` cannot be the zero address.
  186. * - If `account` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  187. * acceptance magic value.
  188. */
  189. function _mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual {
  190. require(account != address(0), "ERC1155: mint to the zero address");
  191. address operator = _msgSender();
  192. _beforeTokenTransfer(operator, address(0), account, _asSingletonArray(id), _asSingletonArray(amount), data);
  193. _balances[id][account] += amount;
  194. emit TransferSingle(operator, address(0), account, id, amount);
  195. _doSafeTransferAcceptanceCheck(operator, address(0), account, id, amount, data);
  196. }
  197. /**
  198. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  199. *
  200. * Requirements:
  201. *
  202. * - `ids` and `amounts` must have the same length.
  203. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  204. * acceptance magic value.
  205. */
  206. function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal virtual {
  207. require(to != address(0), "ERC1155: mint to the zero address");
  208. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  209. address operator = _msgSender();
  210. _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
  211. for (uint i = 0; i < ids.length; i++) {
  212. _balances[ids[i]][to] += amounts[i];
  213. }
  214. emit TransferBatch(operator, address(0), to, ids, amounts);
  215. _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
  216. }
  217. /**
  218. * @dev Destroys `amount` tokens of token type `id` from `account`
  219. *
  220. * Requirements:
  221. *
  222. * - `account` cannot be the zero address.
  223. * - `account` must have at least `amount` tokens of token type `id`.
  224. */
  225. function _burn(address account, uint256 id, uint256 amount) internal virtual {
  226. require(account != address(0), "ERC1155: burn from the zero address");
  227. address operator = _msgSender();
  228. _beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
  229. require(_balances[id][account] >= amount, "ERC1155: burn amount exceeds balance");
  230. _balances[id][account] -= amount;
  231. emit TransferSingle(operator, account, address(0), id, amount);
  232. }
  233. /**
  234. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  235. *
  236. * Requirements:
  237. *
  238. * - `ids` and `amounts` must have the same length.
  239. */
  240. function _burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) internal virtual {
  241. require(account != address(0), "ERC1155: burn from the zero address");
  242. require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
  243. address operator = _msgSender();
  244. _beforeTokenTransfer(operator, account, address(0), ids, amounts, "");
  245. for (uint i = 0; i < ids.length; i++) {
  246. uint256 id = ids[i];
  247. uint256 amount = amounts[i];
  248. require(_balances[id][account] >= amount, "ERC1155: burn amount exceeds balance");
  249. _balances[id][account] -= amount;
  250. }
  251. emit TransferBatch(operator, account, address(0), ids, amounts);
  252. }
  253. /**
  254. * @dev Hook that is called before any token transfer. This includes minting
  255. * and burning, as well as batched variants.
  256. *
  257. * The same hook is called on both single and batched variants. For single
  258. * transfers, the length of the `id` and `amount` arrays will be 1.
  259. *
  260. * Calling conditions (for each `id` and `amount` pair):
  261. *
  262. * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  263. * of token type `id` will be transferred to `to`.
  264. * - When `from` is zero, `amount` tokens of token type `id` will be minted
  265. * for `to`.
  266. * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
  267. * will be burned.
  268. * - `from` and `to` are never both zero.
  269. * - `ids` and `amounts` have the same, non-zero length.
  270. *
  271. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  272. */
  273. function _beforeTokenTransfer(
  274. address operator,
  275. address from,
  276. address to,
  277. uint256[] memory ids,
  278. uint256[] memory amounts,
  279. bytes memory data
  280. )
  281. internal
  282. virtual
  283. { }
  284. function _doSafeTransferAcceptanceCheck(
  285. address operator,
  286. address from,
  287. address to,
  288. uint256 id,
  289. uint256 amount,
  290. bytes memory data
  291. )
  292. private
  293. {
  294. if (to.isContract()) {
  295. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  296. if (response != IERC1155Receiver(to).onERC1155Received.selector) {
  297. revert("ERC1155: ERC1155Receiver rejected tokens");
  298. }
  299. } catch Error(string memory reason) {
  300. revert(reason);
  301. } catch {
  302. revert("ERC1155: transfer to non ERC1155Receiver implementer");
  303. }
  304. }
  305. }
  306. function _doSafeBatchTransferAcceptanceCheck(
  307. address operator,
  308. address from,
  309. address to,
  310. uint256[] memory ids,
  311. uint256[] memory amounts,
  312. bytes memory data
  313. )
  314. private
  315. {
  316. if (to.isContract()) {
  317. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (bytes4 response) {
  318. if (response != IERC1155Receiver(to).onERC1155BatchReceived.selector) {
  319. revert("ERC1155: ERC1155Receiver rejected tokens");
  320. }
  321. } catch Error(string memory reason) {
  322. revert(reason);
  323. } catch {
  324. revert("ERC1155: transfer to non ERC1155Receiver implementer");
  325. }
  326. }
  327. }
  328. function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
  329. uint256[] memory array = new uint256[](1);
  330. array[0] = element;
  331. return array;
  332. }
  333. }