ERC1155.sol 13 KB

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