ERC777.sol 17 KB

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