ERC777.sol 17 KB

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