ERC777.sol 12 KB

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