ERC1155.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "../../math/SafeMath.sol";
  7. import "../../utils/Address.sol";
  8. import "../../introspection/ERC165.sol";
  9. /**
  10. * @title Standard ERC1155 token
  11. *
  12. * @dev Implementation of the basic standard multi-token.
  13. * See https://eips.ethereum.org/EIPS/eip-1155
  14. * Originally based on code by Enjin: https://github.com/enjin/erc-1155
  15. */
  16. contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI {
  17. using SafeMath for uint256;
  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 substition, 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) public {
  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 substituion 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 override returns (string memory) {
  62. return _uri;
  63. }
  64. /**
  65. @dev Get the specified address' balance for token with specified ID.
  66. Attempting to query the zero account for a balance will result in a revert.
  67. @param account The address of the token holder
  68. @param id ID of the token
  69. @return The account's balance of the token type requested
  70. */
  71. function balanceOf(address account, uint256 id) public view override returns (uint256) {
  72. require(account != address(0), "ERC1155: balance query for the zero address");
  73. return _balances[id][account];
  74. }
  75. /**
  76. @dev Get the balance of multiple account/token pairs.
  77. If any of the query accounts is the zero account, this query will revert.
  78. @param accounts The addresses of the token holders
  79. @param ids IDs of the tokens
  80. @return Balances for each account and token id pair
  81. */
  82. function balanceOfBatch(
  83. address[] memory accounts,
  84. uint256[] memory ids
  85. )
  86. public
  87. view
  88. override
  89. returns (uint256[] memory)
  90. {
  91. require(accounts.length == ids.length, "ERC1155: accounts and IDs must have same lengths");
  92. uint256[] memory batchBalances = new uint256[](accounts.length);
  93. for (uint256 i = 0; i < accounts.length; ++i) {
  94. require(accounts[i] != address(0), "ERC1155: some address in batch balance query is zero");
  95. batchBalances[i] = _balances[ids[i]][accounts[i]];
  96. }
  97. return batchBalances;
  98. }
  99. /**
  100. * @dev Sets or unsets the approval of a given operator.
  101. *
  102. * An operator is allowed to transfer all tokens of the sender on their behalf.
  103. *
  104. * Because an account already has operator privileges for itself, this function will revert
  105. * if the account attempts to set the approval status for itself.
  106. *
  107. * @param operator address to set the approval
  108. * @param approved representing the status of the approval to be set
  109. */
  110. function setApprovalForAll(address operator, bool approved) public virtual override {
  111. require(msg.sender != operator, "ERC1155: cannot set approval status for self");
  112. _operatorApprovals[msg.sender][operator] = approved;
  113. emit ApprovalForAll(msg.sender, operator, approved);
  114. }
  115. /**
  116. @notice Queries the approval status of an operator for a given account.
  117. @param account The account of the Tokens
  118. @param operator Address of authorized operator
  119. @return True if the operator is approved, false if not
  120. */
  121. function isApprovedForAll(address account, address operator) public view override returns (bool) {
  122. return _operatorApprovals[account][operator];
  123. }
  124. /**
  125. @dev Transfers `value` amount of an `id` from the `from` address to the `to` address specified.
  126. Caller must be approved to manage the tokens being transferred out of the `from` account.
  127. If `to` is a smart contract, will call `onERC1155Received` on `to` and act appropriately.
  128. @param from Source address
  129. @param to Target address
  130. @param id ID of the token type
  131. @param value Transfer amount
  132. @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  133. */
  134. function safeTransferFrom(
  135. address from,
  136. address to,
  137. uint256 id,
  138. uint256 value,
  139. bytes memory data
  140. )
  141. public
  142. virtual
  143. override
  144. {
  145. require(to != address(0), "ERC1155: target address must be non-zero");
  146. require(
  147. from == msg.sender || isApprovedForAll(from, msg.sender) == true,
  148. "ERC1155: need operator approval for 3rd party transfers"
  149. );
  150. _balances[id][from] = _balances[id][from].sub(value, "ERC1155: insufficient balance for transfer");
  151. _balances[id][to] = _balances[id][to].add(value);
  152. emit TransferSingle(msg.sender, from, to, id, value);
  153. _doSafeTransferAcceptanceCheck(msg.sender, from, to, id, value, data);
  154. }
  155. /**
  156. @dev Transfers `values` amount(s) of `ids` from the `from` address to the
  157. `to` address specified. Caller must be approved to manage the tokens being
  158. transferred out of the `from` account. If `to` is a smart contract, will
  159. call `onERC1155BatchReceived` on `to` and act appropriately.
  160. @param from Source address
  161. @param to Target address
  162. @param ids IDs of each token type
  163. @param values Transfer amounts per token type
  164. @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  165. */
  166. function safeBatchTransferFrom(
  167. address from,
  168. address to,
  169. uint256[] memory ids,
  170. uint256[] memory values,
  171. bytes memory data
  172. )
  173. public
  174. virtual
  175. override
  176. {
  177. require(ids.length == values.length, "ERC1155: IDs and values must have same lengths");
  178. require(to != address(0), "ERC1155: target address must be non-zero");
  179. require(
  180. from == msg.sender || isApprovedForAll(from, msg.sender) == true,
  181. "ERC1155: need operator approval for 3rd party transfers"
  182. );
  183. for (uint256 i = 0; i < ids.length; ++i) {
  184. uint256 id = ids[i];
  185. uint256 value = values[i];
  186. _balances[id][from] = _balances[id][from].sub(
  187. value,
  188. "ERC1155: insufficient balance of some token type for transfer"
  189. );
  190. _balances[id][to] = _balances[id][to].add(value);
  191. }
  192. emit TransferBatch(msg.sender, from, to, ids, values);
  193. _doSafeBatchTransferAcceptanceCheck(msg.sender, from, to, ids, values, data);
  194. }
  195. /**
  196. * @dev Sets a new URI for all token types, by relying on the token type ID
  197. * substituion mechanism
  198. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  199. *
  200. * By this mechanism, any occurence of the `{id}` substring in either the
  201. * URI or any of the values in the JSON file at said URI will be replaced by
  202. * clients with the token type ID.
  203. *
  204. * For example, the `https://token-cdn-domain/{id}.json` URI would be
  205. * interpreted by clients as
  206. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  207. * for token type ID 0x4cce0.
  208. *
  209. * See {uri}.
  210. *
  211. * Because these URIs cannot be meaningfully represented by the {URI} event,
  212. * this function emits no events.
  213. */
  214. function _setURI(string memory newuri) internal virtual {
  215. _uri = newuri;
  216. }
  217. /**
  218. * @dev Internal function to mint an amount of a token with the given ID
  219. * @param to The address that will own the minted token
  220. * @param id ID of the token to be minted
  221. * @param value Amount of the token to be minted
  222. * @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  223. */
  224. function _mint(address to, uint256 id, uint256 value, bytes memory data) internal virtual {
  225. require(to != address(0), "ERC1155: mint to the zero address");
  226. _balances[id][to] = _balances[id][to].add(value);
  227. emit TransferSingle(msg.sender, address(0), to, id, value);
  228. _doSafeTransferAcceptanceCheck(msg.sender, address(0), to, id, value, data);
  229. }
  230. /**
  231. * @dev Internal function to batch mint amounts of tokens with the given IDs
  232. * @param to The address that will own the minted token
  233. * @param ids IDs of the tokens to be minted
  234. * @param values Amounts of the tokens to be minted
  235. * @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  236. */
  237. function _mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) internal virtual {
  238. require(to != address(0), "ERC1155: batch mint to the zero address");
  239. require(ids.length == values.length, "ERC1155: minted IDs and values must have same lengths");
  240. for(uint i = 0; i < ids.length; i++) {
  241. _balances[ids[i]][to] = values[i].add(_balances[ids[i]][to]);
  242. }
  243. emit TransferBatch(msg.sender, address(0), to, ids, values);
  244. _doSafeBatchTransferAcceptanceCheck(msg.sender, address(0), to, ids, values, data);
  245. }
  246. /**
  247. * @dev Internal function to burn an amount of a token with the given ID
  248. * @param account Account which owns the token to be burnt
  249. * @param id ID of the token to be burnt
  250. * @param value Amount of the token to be burnt
  251. */
  252. function _burn(address account, uint256 id, uint256 value) internal virtual {
  253. require(account != address(0), "ERC1155: attempting to burn tokens on zero account");
  254. _balances[id][account] = _balances[id][account].sub(
  255. value,
  256. "ERC1155: attempting to burn more than balance"
  257. );
  258. emit TransferSingle(msg.sender, account, address(0), id, value);
  259. }
  260. /**
  261. * @dev Internal function to batch burn an amounts of tokens with the given IDs
  262. * @param account Account which owns the token to be burnt
  263. * @param ids IDs of the tokens to be burnt
  264. * @param values Amounts of the tokens to be burnt
  265. */
  266. function _burnBatch(address account, uint256[] memory ids, uint256[] memory values) internal virtual {
  267. require(account != address(0), "ERC1155: attempting to burn batch of tokens on zero account");
  268. require(ids.length == values.length, "ERC1155: burnt IDs and values must have same lengths");
  269. for(uint i = 0; i < ids.length; i++) {
  270. _balances[ids[i]][account] = _balances[ids[i]][account].sub(
  271. values[i],
  272. "ERC1155: attempting to burn more than balance for some token"
  273. );
  274. }
  275. emit TransferBatch(msg.sender, account, address(0), ids, values);
  276. }
  277. function _doSafeTransferAcceptanceCheck(
  278. address operator,
  279. address from,
  280. address to,
  281. uint256 id,
  282. uint256 value,
  283. bytes memory data
  284. )
  285. private
  286. {
  287. if(to.isContract()) {
  288. require(
  289. IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) ==
  290. IERC1155Receiver(to).onERC1155Received.selector,
  291. "ERC1155: got unknown value from onERC1155Received"
  292. );
  293. }
  294. }
  295. function _doSafeBatchTransferAcceptanceCheck(
  296. address operator,
  297. address from,
  298. address to,
  299. uint256[] memory ids,
  300. uint256[] memory values,
  301. bytes memory data
  302. )
  303. private
  304. {
  305. if(to.isContract()) {
  306. require(
  307. IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) ==
  308. IERC1155Receiver(to).onERC1155BatchReceived.selector,
  309. "ERC1155: got unknown value from onERC1155BatchReceived"
  310. );
  311. }
  312. }
  313. }