ERC1155.sol 14 KB

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