ERC777.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. pragma solidity ^0.5.0;
  2. import "./IERC777.sol";
  3. import "./IERC777Recipient.sol";
  4. import "./IERC777Sender.sol";
  5. import "../../math/SafeMath.sol";
  6. import "../../utils/Address.sol";
  7. import "../IERC1820Registry.sol";
  8. /**
  9. * @title ERC777 token implementation, with granularity harcoded to 1.
  10. * @author etsvigun <utgarda@gmail.com>, Bertrand Masius <github@catageeks.tk>
  11. */
  12. contract ERC777 is IERC777 {
  13. using SafeMath for uint256;
  14. using Address for address;
  15. IERC1820Registry private _erc1820 = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
  16. string private _name;
  17. string private _symbol;
  18. mapping(address => uint256) private _balances;
  19. uint256 private _totalSupply;
  20. bytes32 constant private TOKENS_SENDER_INTERFACE_HASH = keccak256("ERC777TokensSender");
  21. bytes32 constant private TOKENS_RECIPIENT_INTERFACE_HASH = keccak256("ERC777TokensRecipient");
  22. // This isn't ever read from - it's only used to respond to the defaultOperators query.
  23. address[] private _defaultOperatorsArray;
  24. // Immutable, but accounts may revoke them (tracked in __revokedDefaultOperators).
  25. mapping(address => bool) private _defaultOperators;
  26. // For each account, a mapping of its operators and revoked default operators.
  27. mapping(address => mapping(address => bool)) private _operators;
  28. mapping(address => mapping(address => bool)) private _revokedDefaultOperators;
  29. constructor(
  30. string memory name,
  31. string memory symbol,
  32. address[] memory defaultOperators
  33. ) public {
  34. _name = name;
  35. _symbol = symbol;
  36. _defaultOperatorsArray = defaultOperators;
  37. for (uint256 i = 0; i < _defaultOperatorsArray.length; i++) {
  38. _defaultOperators[_defaultOperatorsArray[i]] = true;
  39. }
  40. // register interface
  41. _erc1820.setInterfaceImplementer(address(this), keccak256("ERC777Token"), address(this));
  42. }
  43. /**
  44. * @dev Send the amount of tokens from the address msg.sender to the address to
  45. * @param to address recipient address
  46. * @param amount uint256 amount of tokens to transfer
  47. * @param data bytes information attached to the send, and intended for the recipient (to)
  48. */
  49. function send(address to, uint256 amount, bytes calldata data) external {
  50. _send(msg.sender, msg.sender, to, amount, data, "");
  51. }
  52. /**
  53. * @dev Send the amount of tokens on behalf of the address from to the address to
  54. * @param from address token holder address.
  55. * @param to address recipient address
  56. * @param amount uint256 amount of tokens to transfer
  57. * @param data bytes information attached to the send, and intended for the recipient (to)
  58. * @param operatorData bytes extra information provided by the operator (if any)
  59. */
  60. function operatorSend(
  61. address from,
  62. address to,
  63. uint256 amount,
  64. bytes calldata data,
  65. bytes calldata operatorData
  66. )
  67. external
  68. {
  69. require(isOperatorFor(msg.sender, from));
  70. _send(msg.sender, from, to, amount, data, operatorData);
  71. }
  72. /**
  73. * @dev Burn the amount of tokens from the address msg.sender
  74. * @param amount uint256 amount of tokens to transfer
  75. * @param data bytes extra information provided by the token holder
  76. */
  77. function burn(uint256 amount, bytes calldata data) external {
  78. _burn(msg.sender, msg.sender, amount, data, "");
  79. }
  80. /**
  81. * @dev Burn the amount of tokens on behalf of the address from
  82. * @param from address token holder address.
  83. * @param amount uint256 amount of tokens to transfer
  84. * @param data bytes extra information provided by the token holder
  85. * @param operatorData bytes extra information provided by the operator (if any)
  86. */
  87. function operatorBurn(address from, uint256 amount, bytes calldata data, bytes calldata operatorData) external {
  88. require(isOperatorFor(msg.sender, from));
  89. _burn(msg.sender, from, amount, data, operatorData);
  90. }
  91. /**
  92. * @dev Authorize an operator for the sender
  93. * @param operator address to be authorized as operator
  94. */
  95. function authorizeOperator(address operator) external {
  96. require(msg.sender != operator);
  97. if (_defaultOperators[operator]) {
  98. delete _revokedDefaultOperators[msg.sender][operator];
  99. } else {
  100. _operators[msg.sender][operator] = true;
  101. }
  102. emit AuthorizedOperator(operator, msg.sender);
  103. }
  104. /**
  105. * @dev Revoke operator rights from one of the default operators
  106. * @param operator address to revoke operator rights from
  107. */
  108. function revokeOperator(address operator) external {
  109. require(operator != msg.sender);
  110. if (_defaultOperators[operator]) {
  111. _revokedDefaultOperators[msg.sender][operator] = true;
  112. } else {
  113. delete _operators[msg.sender][operator];
  114. }
  115. emit RevokedOperator(operator, msg.sender);
  116. }
  117. /**
  118. * @return the name of the token.
  119. */
  120. function name() public view returns (string memory) {
  121. return _name;
  122. }
  123. /**
  124. * @return the symbol of the token.
  125. */
  126. function symbol() public view returns (string memory) {
  127. return _symbol;
  128. }
  129. /**
  130. * @dev Total number of tokens in existence
  131. */
  132. function totalSupply() public view returns (uint256) {
  133. return _totalSupply;
  134. }
  135. /**
  136. * @dev Gets the balance of the specified address.
  137. * @param tokenHolder The address to query the balance of.
  138. * @return uint256 representing the amount owned by the specified address.
  139. */
  140. function balanceOf(address tokenHolder) public view returns (uint256) {
  141. return _balances[tokenHolder];
  142. }
  143. /**
  144. * @dev Gets the token's granularity,
  145. * i.e. the smallest number of tokens (in the basic unit)
  146. * which may be minted, sent or burned at any time
  147. * @return uint256 granularity
  148. */
  149. function granularity() public view returns (uint256) {
  150. return 1;
  151. }
  152. /**
  153. * @dev Get the list of default operators as defined by the token contract.
  154. * @return address[] default operators
  155. */
  156. function defaultOperators() public view returns (address[] memory) {
  157. return _defaultOperatorsArray;
  158. }
  159. /**
  160. * @dev Indicate whether an address
  161. * is an operator of the tokenHolder address
  162. * @param operator address which may be an operator of tokenHolder
  163. * @param tokenHolder address of a token holder which may have the operator
  164. * address as an operator.
  165. */
  166. function isOperatorFor(
  167. address operator,
  168. address tokenHolder
  169. ) public view returns (bool) {
  170. return operator == tokenHolder ||
  171. (_defaultOperators[operator] && !_revokedDefaultOperators[tokenHolder][operator]) ||
  172. _operators[tokenHolder][operator];
  173. }
  174. /**
  175. * @dev Mint tokens. Does not check authorization of operator
  176. * @dev the caller may ckeck that operator is authorized before calling
  177. * @param operator address operator requesting the operation
  178. * @param to address token recipient address
  179. * @param amount uint256 amount of tokens to mint
  180. * @param userData bytes extra information defined by the token recipient (if any)
  181. * @param operatorData bytes extra information provided by the operator (if any)
  182. */
  183. function _mint(
  184. address operator,
  185. address to,
  186. uint256 amount,
  187. bytes memory userData,
  188. bytes memory operatorData
  189. )
  190. internal
  191. {
  192. require(to != address(0));
  193. // Update state variables
  194. _totalSupply = _totalSupply.add(amount);
  195. _balances[to] = _balances[to].add(amount);
  196. _callTokensReceived(operator, address(0), to, amount, userData, operatorData);
  197. emit Minted(operator, to, amount, userData, operatorData);
  198. }
  199. /**
  200. * @dev Send tokens
  201. * @param operator address operator requesting the transfer
  202. * @param from address token holder address
  203. * @param to address recipient address
  204. * @param amount uint256 amount of tokens to transfer
  205. * @param userData bytes extra information provided by the token holder (if any)
  206. * @param operatorData bytes extra information provided by the operator (if any)
  207. */
  208. function _send(
  209. address operator,
  210. address from,
  211. address to,
  212. uint256 amount,
  213. bytes memory userData,
  214. bytes memory operatorData
  215. )
  216. private
  217. {
  218. require(from != address(0));
  219. require(to != address(0));
  220. _callTokensToSend(operator, from, to, amount, userData, operatorData);
  221. // Update state variables
  222. _balances[from] = _balances[from].sub(amount);
  223. _balances[to] = _balances[to].add(amount);
  224. _callTokensReceived(operator, from, to, amount, userData, operatorData);
  225. emit Sent(operator, from, to, amount, userData, operatorData);
  226. }
  227. /**
  228. * @dev Burn tokens
  229. * @param operator address operator requesting the operation
  230. * @param from address token holder address
  231. * @param amount uint256 amount of tokens to burn
  232. * @param data bytes extra information provided by the token holder
  233. * @param operatorData bytes extra information provided by the operator (if any)
  234. */
  235. function _burn(
  236. address operator,
  237. address from,
  238. uint256 amount,
  239. bytes memory data,
  240. bytes memory operatorData
  241. )
  242. private
  243. {
  244. require(from != address(0));
  245. _callTokensToSend(operator, from, address(0), amount, data, operatorData);
  246. // Update state variables
  247. _totalSupply = _totalSupply.sub(amount);
  248. _balances[from] = _balances[from].sub(amount);
  249. emit Burned(operator, from, amount, data, operatorData);
  250. }
  251. /**
  252. * @dev Call from.tokensToSend() if the interface is registered
  253. * @param operator address operator requesting the transfer
  254. * @param from address token holder address
  255. * @param to address recipient address
  256. * @param amount uint256 amount of tokens to transfer
  257. * @param userData bytes extra information provided by the token holder (if any)
  258. * @param operatorData bytes extra information provided by the operator (if any)
  259. */
  260. function _callTokensToSend(
  261. address operator,
  262. address from,
  263. address to,
  264. uint256 amount,
  265. bytes memory userData,
  266. bytes memory operatorData
  267. )
  268. private
  269. {
  270. address implementer = _erc1820.getInterfaceImplementer(from, TOKENS_SENDER_INTERFACE_HASH);
  271. if (implementer != address(0)) {
  272. IERC777Sender(implementer).tokensToSend(operator, from, to, amount, userData, operatorData);
  273. }
  274. }
  275. /**
  276. * @dev Call to.tokensReceived() if the interface is registered. Reverts if the recipient is a contract but
  277. * tokensReceived() was not registered for the recipient
  278. * @param operator address operator requesting the transfer
  279. * @param from address token holder address
  280. * @param to address recipient address
  281. * @param amount uint256 amount of tokens to transfer
  282. * @param userData bytes extra information provided by the token holder (if any)
  283. * @param operatorData bytes extra information provided by the operator (if any)
  284. */
  285. function _callTokensReceived(
  286. address operator,
  287. address from,
  288. address to,
  289. uint256 amount,
  290. bytes memory userData,
  291. bytes memory operatorData
  292. )
  293. private
  294. {
  295. address implementer = _erc1820.getInterfaceImplementer(to, TOKENS_RECIPIENT_INTERFACE_HASH);
  296. if (implementer != address(0)) {
  297. IERC777Recipient(implementer).tokensReceived(operator, from, to, amount, userData, operatorData);
  298. } else {
  299. require(!to.isContract());
  300. }
  301. }
  302. }