ERC1155.sol 12 KB

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