ERC1155.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "./IERC1155.sol";
  4. import "./IERC1155Receiver.sol";
  5. import "../../math/SafeMath.sol";
  6. import "../../utils/Address.sol";
  7. import "../../introspection/ERC165.sol";
  8. /**
  9. * @title Standard ERC1155 token
  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. contract ERC1155 is ERC165, IERC1155
  16. {
  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. /*
  24. * bytes4(keccak256('balanceOf(address,uint256)')) == 0x00fdd58e
  25. * bytes4(keccak256('balanceOfBatch(address[],uint256[])')) == 0x4e1273f4
  26. * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
  27. * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
  28. * bytes4(keccak256('safeTransferFrom(address,address,uint256,uint256,bytes)')) == 0xf242432a
  29. * bytes4(keccak256('safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)')) == 0x2eb2c2d6
  30. *
  31. * => 0x00fdd58e ^ 0x4e1273f4 ^ 0xa22cb465 ^
  32. * 0xe985e9c5 ^ 0xf242432a ^ 0x2eb2c2d6 == 0xd9b67a26
  33. */
  34. bytes4 private constant _INTERFACE_ID_ERC1155 = 0xd9b67a26;
  35. constructor() public {
  36. // register the supported interfaces to conform to ERC1155 via ERC165
  37. _registerInterface(_INTERFACE_ID_ERC1155);
  38. }
  39. /**
  40. @dev Get the specified address' balance for token with specified ID.
  41. Attempting to query the zero account for a balance will result in a revert.
  42. @param account The address of the token holder
  43. @param id ID of the token
  44. @return The account's balance of the token type requested
  45. */
  46. function balanceOf(address account, uint256 id) public view override returns (uint256) {
  47. require(account != address(0), "ERC1155: balance query for the zero address");
  48. return _balances[id][account];
  49. }
  50. /**
  51. @dev Get the balance of multiple account/token pairs.
  52. If any of the query accounts is the zero account, this query will revert.
  53. @param accounts The addresses of the token holders
  54. @param ids IDs of the tokens
  55. @return Balances for each account and token id pair
  56. */
  57. function balanceOfBatch(
  58. address[] memory accounts,
  59. uint256[] memory ids
  60. )
  61. public
  62. view
  63. override
  64. returns (uint256[] memory)
  65. {
  66. require(accounts.length == ids.length, "ERC1155: accounts and IDs must have same lengths");
  67. uint256[] memory batchBalances = new uint256[](accounts.length);
  68. for (uint256 i = 0; i < accounts.length; ++i) {
  69. require(accounts[i] != address(0), "ERC1155: some address in batch balance query is zero");
  70. batchBalances[i] = _balances[ids[i]][accounts[i]];
  71. }
  72. return batchBalances;
  73. }
  74. /**
  75. * @dev Sets or unsets the approval of a given operator.
  76. *
  77. * An operator is allowed to transfer all tokens of the sender on their behalf.
  78. *
  79. * Because an account already has operator privileges for itself, this function will revert
  80. * if the account attempts to set the approval status for itself.
  81. *
  82. * @param operator address to set the approval
  83. * @param approved representing the status of the approval to be set
  84. */
  85. function setApprovalForAll(address operator, bool approved) public virtual override {
  86. require(msg.sender != operator, "ERC1155: cannot set approval status for self");
  87. _operatorApprovals[msg.sender][operator] = approved;
  88. emit ApprovalForAll(msg.sender, operator, approved);
  89. }
  90. /**
  91. @notice Queries the approval status of an operator for a given account.
  92. @param account The account of the Tokens
  93. @param operator Address of authorized operator
  94. @return True if the operator is approved, false if not
  95. */
  96. function isApprovedForAll(address account, address operator) public view override returns (bool) {
  97. return _operatorApprovals[account][operator];
  98. }
  99. /**
  100. @dev Transfers `value` amount of an `id` from the `from` address to the `to` address specified.
  101. Caller must be approved to manage the tokens being transferred out of the `from` account.
  102. If `to` is a smart contract, will call `onERC1155Received` on `to` and act appropriately.
  103. @param from Source address
  104. @param to Target address
  105. @param id ID of the token type
  106. @param value Transfer amount
  107. @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  108. */
  109. function safeTransferFrom(
  110. address from,
  111. address to,
  112. uint256 id,
  113. uint256 value,
  114. bytes memory data
  115. )
  116. public
  117. virtual
  118. override
  119. {
  120. require(to != address(0), "ERC1155: target address must be non-zero");
  121. require(
  122. from == msg.sender || isApprovedForAll(from, msg.sender) == true,
  123. "ERC1155: need operator approval for 3rd party transfers"
  124. );
  125. _balances[id][from] = _balances[id][from].sub(value, "ERC1155: insufficient balance for transfer");
  126. _balances[id][to] = _balances[id][to].add(value);
  127. emit TransferSingle(msg.sender, from, to, id, value);
  128. _doSafeTransferAcceptanceCheck(msg.sender, from, to, id, value, data);
  129. }
  130. /**
  131. @dev Transfers `values` amount(s) of `ids` from the `from` address to the
  132. `to` address specified. Caller must be approved to manage the tokens being
  133. transferred out of the `from` account. If `to` is a smart contract, will
  134. call `onERC1155BatchReceived` on `to` and act appropriately.
  135. @param from Source address
  136. @param to Target address
  137. @param ids IDs of each token type
  138. @param values Transfer amounts per token type
  139. @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  140. */
  141. function safeBatchTransferFrom(
  142. address from,
  143. address to,
  144. uint256[] memory ids,
  145. uint256[] memory values,
  146. bytes memory data
  147. )
  148. public
  149. virtual
  150. override
  151. {
  152. require(ids.length == values.length, "ERC1155: IDs and values must have same lengths");
  153. require(to != address(0), "ERC1155: target address must be non-zero");
  154. require(
  155. from == msg.sender || isApprovedForAll(from, msg.sender) == true,
  156. "ERC1155: need operator approval for 3rd party transfers"
  157. );
  158. for (uint256 i = 0; i < ids.length; ++i) {
  159. uint256 id = ids[i];
  160. uint256 value = values[i];
  161. _balances[id][from] = _balances[id][from].sub(
  162. value,
  163. "ERC1155: insufficient balance of some token type for transfer"
  164. );
  165. _balances[id][to] = _balances[id][to].add(value);
  166. }
  167. emit TransferBatch(msg.sender, from, to, ids, values);
  168. _doSafeBatchTransferAcceptanceCheck(msg.sender, from, to, ids, values, data);
  169. }
  170. /**
  171. * @dev Internal function to mint an amount of a token with the given ID
  172. * @param to The address that will own the minted token
  173. * @param id ID of the token to be minted
  174. * @param value Amount of the token to be minted
  175. * @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  176. */
  177. function _mint(address to, uint256 id, uint256 value, bytes memory data) internal virtual {
  178. require(to != address(0), "ERC1155: mint to the zero address");
  179. _balances[id][to] = _balances[id][to].add(value);
  180. emit TransferSingle(msg.sender, address(0), to, id, value);
  181. _doSafeTransferAcceptanceCheck(msg.sender, address(0), to, id, value, data);
  182. }
  183. /**
  184. * @dev Internal function to batch mint amounts of tokens with the given IDs
  185. * @param to The address that will own the minted token
  186. * @param ids IDs of the tokens to be minted
  187. * @param values Amounts of the tokens to be minted
  188. * @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver
  189. */
  190. function _mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) internal virtual {
  191. require(to != address(0), "ERC1155: batch mint to the zero address");
  192. require(ids.length == values.length, "ERC1155: minted IDs and values must have same lengths");
  193. for(uint i = 0; i < ids.length; i++) {
  194. _balances[ids[i]][to] = values[i].add(_balances[ids[i]][to]);
  195. }
  196. emit TransferBatch(msg.sender, address(0), to, ids, values);
  197. _doSafeBatchTransferAcceptanceCheck(msg.sender, address(0), to, ids, values, data);
  198. }
  199. /**
  200. * @dev Internal function to burn an amount of a token with the given ID
  201. * @param account Account which owns the token to be burnt
  202. * @param id ID of the token to be burnt
  203. * @param value Amount of the token to be burnt
  204. */
  205. function _burn(address account, uint256 id, uint256 value) internal virtual {
  206. require(account != address(0), "ERC1155: attempting to burn tokens on zero account");
  207. _balances[id][account] = _balances[id][account].sub(
  208. value,
  209. "ERC1155: attempting to burn more than balance"
  210. );
  211. emit TransferSingle(msg.sender, account, address(0), id, value);
  212. }
  213. /**
  214. * @dev Internal function to batch burn an amounts of tokens with the given IDs
  215. * @param account Account which owns the token to be burnt
  216. * @param ids IDs of the tokens to be burnt
  217. * @param values Amounts of the tokens to be burnt
  218. */
  219. function _burnBatch(address account, uint256[] memory ids, uint256[] memory values) internal virtual {
  220. require(account != address(0), "ERC1155: attempting to burn batch of tokens on zero account");
  221. require(ids.length == values.length, "ERC1155: burnt IDs and values must have same lengths");
  222. for(uint i = 0; i < ids.length; i++) {
  223. _balances[ids[i]][account] = _balances[ids[i]][account].sub(
  224. values[i],
  225. "ERC1155: attempting to burn more than balance for some token"
  226. );
  227. }
  228. emit TransferBatch(msg.sender, account, address(0), ids, values);
  229. }
  230. function _doSafeTransferAcceptanceCheck(
  231. address operator,
  232. address from,
  233. address to,
  234. uint256 id,
  235. uint256 value,
  236. bytes memory data
  237. )
  238. private
  239. {
  240. if(to.isContract()) {
  241. require(
  242. IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) ==
  243. IERC1155Receiver(to).onERC1155Received.selector,
  244. "ERC1155: got unknown value from onERC1155Received"
  245. );
  246. }
  247. }
  248. function _doSafeBatchTransferAcceptanceCheck(
  249. address operator,
  250. address from,
  251. address to,
  252. uint256[] memory ids,
  253. uint256[] memory values,
  254. bytes memory data
  255. )
  256. private
  257. {
  258. if(to.isContract()) {
  259. require(
  260. IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) ==
  261. IERC1155Receiver(to).onERC1155BatchReceived.selector,
  262. "ERC1155: got unknown value from onERC1155BatchReceived"
  263. );
  264. }
  265. }
  266. }