ERC777.sol 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. pragma solidity ^0.5.0;
  2. import "./IERC777.sol";
  3. import "./IERC777Recipient.sol";
  4. import "./IERC777Sender.sol";
  5. import "../../token/ERC20/IERC20.sol";
  6. import "../../math/SafeMath.sol";
  7. import "../../utils/Address.sol";
  8. import "../../introspection/IERC1820Registry.sol";
  9. /**
  10. * @title ERC777 token implementation, with granularity harcoded to 1.
  11. * @author etsvigun <utgarda@gmail.com>, Bertrand Masius <github@catageeks.tk>
  12. */
  13. contract ERC777 is IERC777, IERC20 {
  14. using SafeMath for uint256;
  15. using Address for address;
  16. IERC1820Registry private _erc1820 = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
  17. mapping(address => uint256) private _balances;
  18. uint256 private _totalSupply;
  19. string private _name;
  20. string private _symbol;
  21. // We inline the result of the following hashes because Solidity doesn't resolve them at compile time.
  22. // See https://github.com/ethereum/solidity/issues/4024.
  23. // keccak256("ERC777TokensSender")
  24. bytes32 constant private TOKENS_SENDER_INTERFACE_HASH =
  25. 0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895;
  26. // keccak256("ERC777TokensRecipient")
  27. bytes32 constant private TOKENS_RECIPIENT_INTERFACE_HASH =
  28. 0xb281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b;
  29. // This isn't ever read from - it's only used to respond to the defaultOperators query.
  30. address[] private _defaultOperatorsArray;
  31. // Immutable, but accounts may revoke them (tracked in __revokedDefaultOperators).
  32. mapping(address => bool) private _defaultOperators;
  33. // For each account, a mapping of its operators and revoked default operators.
  34. mapping(address => mapping(address => bool)) private _operators;
  35. mapping(address => mapping(address => bool)) private _revokedDefaultOperators;
  36. // ERC20-allowances
  37. mapping (address => mapping (address => uint256)) private _allowances;
  38. constructor(
  39. string memory name,
  40. string memory symbol,
  41. address[] memory defaultOperators
  42. ) public {
  43. _name = name;
  44. _symbol = symbol;
  45. _defaultOperatorsArray = defaultOperators;
  46. for (uint256 i = 0; i < _defaultOperatorsArray.length; i++) {
  47. _defaultOperators[_defaultOperatorsArray[i]] = true;
  48. }
  49. // register interfaces
  50. _erc1820.setInterfaceImplementer(address(this), keccak256("ERC777Token"), address(this));
  51. _erc1820.setInterfaceImplementer(address(this), keccak256("ERC20Token"), address(this));
  52. }
  53. /**
  54. * @dev Send the amount of tokens from the address msg.sender to the address to
  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. */
  59. function send(address to, uint256 amount, bytes calldata data) external {
  60. _send(msg.sender, msg.sender, to, amount, data, "", true);
  61. }
  62. /**
  63. * @dev Send the amount of tokens on behalf of the address from to the address to
  64. * @param from address token holder address.
  65. * @param to address recipient address
  66. * @param amount uint256 amount of tokens to transfer
  67. * @param data bytes information attached to the send, and intended for the recipient (to)
  68. * @param operatorData bytes extra information provided by the operator (if any)
  69. */
  70. function operatorSend(
  71. address from,
  72. address to,
  73. uint256 amount,
  74. bytes calldata data,
  75. bytes calldata operatorData
  76. )
  77. external
  78. {
  79. require(isOperatorFor(msg.sender, from), "ERC777: caller is not an operator for holder");
  80. _send(msg.sender, from, to, amount, data, operatorData, true);
  81. }
  82. /**
  83. * @dev Transfer token to a specified address.
  84. * Required for ERC20 compatiblity. Note that transferring tokens this way may result in locked tokens (i.e. tokens
  85. * can be sent to a contract that does not implement the ERC777TokensRecipient interface).
  86. * @param to The address to transfer to.
  87. * @param value The amount to be transferred.
  88. */
  89. function transfer(address to, uint256 value) external returns (bool) {
  90. require(to != address(0), "ERC777: transfer to the zero address");
  91. address from = msg.sender;
  92. _callTokensToSend(from, from, to, value, "", "");
  93. _move(from, from, to, value, "", "");
  94. _callTokensReceived(from, from, to, value, "", "", false);
  95. return true;
  96. }
  97. /**
  98. * @dev Transfer tokens from one address to another.
  99. * Note that while this function emits an Approval event, this is not required as per the specification,
  100. * and other compliant implementations may not emit the event.
  101. * Required for ERC20 compatiblity. Note that transferring tokens this way may result in locked tokens (i.e. tokens
  102. * can be sent to a contract that does not implement the ERC777TokensRecipient interface).
  103. * @param from address The address which you want to send tokens from
  104. * @param to address The address which you want to transfer to
  105. * @param value uint256 the amount of tokens to be transferred
  106. */
  107. function transferFrom(address from, address to, uint256 value) external returns (bool) {
  108. require(to != address(0), "ERC777: transfer to the zero address");
  109. require(from != address(0), "ERC777: transfer from the zero address");
  110. address operator = msg.sender;
  111. _callTokensToSend(operator, from, to, value, "", "");
  112. _move(operator, from, to, value, "", "");
  113. _approve(from, operator, _allowances[from][operator].sub(value));
  114. _callTokensReceived(operator, from, to, value, "", "", false);
  115. return true;
  116. }
  117. /**
  118. * @dev Burn the amount of tokens from the address msg.sender
  119. * @param amount uint256 amount of tokens to transfer
  120. * @param data bytes extra information provided by the token holder
  121. */
  122. function burn(uint256 amount, bytes calldata data) external {
  123. _burn(msg.sender, msg.sender, amount, data, "");
  124. }
  125. /**
  126. * @dev Burn the amount of tokens on behalf of the address from
  127. * @param from address token holder address.
  128. * @param amount uint256 amount of tokens to transfer
  129. * @param data bytes extra information provided by the token holder
  130. * @param operatorData bytes extra information provided by the operator (if any)
  131. */
  132. function operatorBurn(address from, uint256 amount, bytes calldata data, bytes calldata operatorData) external {
  133. require(isOperatorFor(msg.sender, from), "ERC777: caller is not an operator for holder");
  134. _burn(msg.sender, from, amount, data, operatorData);
  135. }
  136. /**
  137. * @dev Authorize an operator for the sender
  138. * @param operator address to be authorized as operator
  139. */
  140. function authorizeOperator(address operator) external {
  141. require(msg.sender != operator, "ERC777: authorizing self as operator");
  142. if (_defaultOperators[operator]) {
  143. delete _revokedDefaultOperators[msg.sender][operator];
  144. } else {
  145. _operators[msg.sender][operator] = true;
  146. }
  147. emit AuthorizedOperator(operator, msg.sender);
  148. }
  149. /**
  150. * @dev Revoke operator rights from one of the default operators
  151. * @param operator address to revoke operator rights from
  152. */
  153. function revokeOperator(address operator) external {
  154. require(operator != msg.sender, "ERC777: revoking self as operator");
  155. if (_defaultOperators[operator]) {
  156. _revokedDefaultOperators[msg.sender][operator] = true;
  157. } else {
  158. delete _operators[msg.sender][operator];
  159. }
  160. emit RevokedOperator(operator, msg.sender);
  161. }
  162. /**
  163. * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  164. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  165. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  166. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  167. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  168. * Required for ERC20 compatilibity.
  169. * @param spender The address which will spend the funds.
  170. * @param value The amount of tokens to be spent.
  171. */
  172. function approve(address spender, uint256 value) external returns (bool) {
  173. _approve(msg.sender, spender, value);
  174. return true;
  175. }
  176. /**
  177. * @dev Total number of tokens in existence
  178. */
  179. function totalSupply() public view returns (uint256) {
  180. return _totalSupply;
  181. }
  182. /**
  183. * @dev Gets the balance of the specified address.
  184. * @param tokenHolder The address to query the balance of.
  185. * @return uint256 representing the amount owned by the specified address.
  186. */
  187. function balanceOf(address tokenHolder) public view returns (uint256) {
  188. return _balances[tokenHolder];
  189. }
  190. /**
  191. * @return the name of the token.
  192. */
  193. function name() public view returns (string memory) {
  194. return _name;
  195. }
  196. /**
  197. * @return the symbol of the token.
  198. */
  199. function symbol() public view returns (string memory) {
  200. return _symbol;
  201. }
  202. /**
  203. * @return the number of decimals of the token.
  204. */
  205. function decimals() public pure returns (uint8) {
  206. return 18; // The spec requires that decimals be 18
  207. }
  208. /**
  209. * @dev Gets the token's granularity,
  210. * i.e. the smallest number of tokens (in the basic unit)
  211. * which may be minted, sent or burned at any time
  212. * @return uint256 granularity
  213. */
  214. function granularity() public view returns (uint256) {
  215. return 1;
  216. }
  217. /**
  218. * @dev Get the list of default operators as defined by the token contract.
  219. * @return address[] default operators
  220. */
  221. function defaultOperators() public view returns (address[] memory) {
  222. return _defaultOperatorsArray;
  223. }
  224. /**
  225. * @dev Indicate whether an address
  226. * is an operator of the tokenHolder address
  227. * @param operator address which may be an operator of tokenHolder
  228. * @param tokenHolder address of a token holder which may have the operator
  229. * address as an operator.
  230. */
  231. function isOperatorFor(
  232. address operator,
  233. address tokenHolder
  234. ) public view returns (bool) {
  235. return operator == tokenHolder ||
  236. (_defaultOperators[operator] && !_revokedDefaultOperators[tokenHolder][operator]) ||
  237. _operators[tokenHolder][operator];
  238. }
  239. /**
  240. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  241. * Required for ERC20 compatibility.
  242. * @param owner address The address which owns the funds.
  243. * @param spender address The address which will spend the funds.
  244. * @return A uint256 specifying the amount of tokens still available for the spender.
  245. */
  246. function allowance(address owner, address spender) public view returns (uint256) {
  247. return _allowances[owner][spender];
  248. }
  249. /**
  250. * @dev Mint tokens. Does not check authorization of operator
  251. * @dev the caller may ckeck that operator is authorized before calling
  252. * @param operator address operator requesting the operation
  253. * @param to address token recipient address
  254. * @param amount uint256 amount of tokens to mint
  255. * @param userData bytes extra information defined by the token recipient (if any)
  256. * @param operatorData bytes extra information provided by the operator (if any)
  257. */
  258. function _mint(
  259. address operator,
  260. address to,
  261. uint256 amount,
  262. bytes memory userData,
  263. bytes memory operatorData
  264. )
  265. internal
  266. {
  267. require(to != address(0), "ERC777: mint to the zero address");
  268. // Update state variables
  269. _totalSupply = _totalSupply.add(amount);
  270. _balances[to] = _balances[to].add(amount);
  271. _callTokensReceived(operator, address(0), to, amount, userData, operatorData, true);
  272. emit Minted(operator, to, amount, userData, operatorData);
  273. emit Transfer(address(0), to, amount);
  274. }
  275. /**
  276. * @dev Send tokens
  277. * @param operator address operator requesting the transfer
  278. * @param from address token holder address
  279. * @param to address recipient address
  280. * @param amount uint256 amount of tokens to transfer
  281. * @param userData bytes extra information provided by the token holder (if any)
  282. * @param operatorData bytes extra information provided by the operator (if any)
  283. * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient
  284. */
  285. function _send(
  286. address operator,
  287. address from,
  288. address to,
  289. uint256 amount,
  290. bytes memory userData,
  291. bytes memory operatorData,
  292. bool requireReceptionAck
  293. )
  294. private
  295. {
  296. require(from != address(0), "ERC777: send from the zero address");
  297. require(to != address(0), "ERC777: send to the zero address");
  298. _callTokensToSend(operator, from, to, amount, userData, operatorData);
  299. _move(operator, from, to, amount, userData, operatorData);
  300. _callTokensReceived(operator, from, to, amount, userData, operatorData, requireReceptionAck);
  301. }
  302. /**
  303. * @dev Burn tokens
  304. * @param operator address operator requesting the operation
  305. * @param from address token holder address
  306. * @param amount uint256 amount of tokens to burn
  307. * @param data bytes extra information provided by the token holder
  308. * @param operatorData bytes extra information provided by the operator (if any)
  309. */
  310. function _burn(
  311. address operator,
  312. address from,
  313. uint256 amount,
  314. bytes memory data,
  315. bytes memory operatorData
  316. )
  317. private
  318. {
  319. require(from != address(0), "ERC777: burn from the zero address");
  320. _callTokensToSend(operator, from, address(0), amount, data, operatorData);
  321. // Update state variables
  322. _totalSupply = _totalSupply.sub(amount);
  323. _balances[from] = _balances[from].sub(amount);
  324. emit Burned(operator, from, amount, data, operatorData);
  325. emit Transfer(from, address(0), amount);
  326. }
  327. function _move(
  328. address operator,
  329. address from,
  330. address to,
  331. uint256 amount,
  332. bytes memory userData,
  333. bytes memory operatorData
  334. )
  335. private
  336. {
  337. _balances[from] = _balances[from].sub(amount);
  338. _balances[to] = _balances[to].add(amount);
  339. emit Sent(operator, from, to, amount, userData, operatorData);
  340. emit Transfer(from, to, amount);
  341. }
  342. function _approve(address owner, address spender, uint256 value) private {
  343. // TODO: restore this require statement if this function becomes internal, or is called at a new callsite. It is
  344. // currently unnecessary.
  345. //require(owner != address(0), "ERC777: approve from the zero address");
  346. require(spender != address(0), "ERC777: approve to the zero address");
  347. _allowances[owner][spender] = value;
  348. emit Approval(owner, spender, value);
  349. }
  350. /**
  351. * @dev Call from.tokensToSend() if the interface is registered
  352. * @param operator address operator requesting the transfer
  353. * @param from address token holder address
  354. * @param to address recipient address
  355. * @param amount uint256 amount of tokens to transfer
  356. * @param userData bytes extra information provided by the token holder (if any)
  357. * @param operatorData bytes extra information provided by the operator (if any)
  358. */
  359. function _callTokensToSend(
  360. address operator,
  361. address from,
  362. address to,
  363. uint256 amount,
  364. bytes memory userData,
  365. bytes memory operatorData
  366. )
  367. private
  368. {
  369. address implementer = _erc1820.getInterfaceImplementer(from, TOKENS_SENDER_INTERFACE_HASH);
  370. if (implementer != address(0)) {
  371. IERC777Sender(implementer).tokensToSend(operator, from, to, amount, userData, operatorData);
  372. }
  373. }
  374. /**
  375. * @dev Call to.tokensReceived() if the interface is registered. Reverts if the recipient is a contract but
  376. * tokensReceived() was not registered for the recipient
  377. * @param operator address operator requesting the transfer
  378. * @param from address token holder address
  379. * @param to address recipient address
  380. * @param amount uint256 amount of tokens to transfer
  381. * @param userData bytes extra information provided by the token holder (if any)
  382. * @param operatorData bytes extra information provided by the operator (if any)
  383. * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient
  384. */
  385. function _callTokensReceived(
  386. address operator,
  387. address from,
  388. address to,
  389. uint256 amount,
  390. bytes memory userData,
  391. bytes memory operatorData,
  392. bool requireReceptionAck
  393. )
  394. private
  395. {
  396. address implementer = _erc1820.getInterfaceImplementer(to, TOKENS_RECIPIENT_INTERFACE_HASH);
  397. if (implementer != address(0)) {
  398. IERC777Recipient(implementer).tokensReceived(operator, from, to, amount, userData, operatorData);
  399. } else if (requireReceptionAck) {
  400. require(!to.isContract(), "ERC777: token recipient contract has no implementer for ERC777TokensRecipient");
  401. }
  402. }
  403. }