ERC777.sol 17 KB

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