ERC777.sol 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.0;
  3. import "../../utils/Context.sol";
  4. import "./IERC777.sol";
  5. import "./IERC777Recipient.sol";
  6. import "./IERC777Sender.sol";
  7. import "../../token/ERC20/IERC20.sol";
  8. import "../../math/SafeMath.sol";
  9. import "../../utils/Address.sol";
  10. import "../../introspection/IERC1820Registry.sol";
  11. /**
  12. * @dev Implementation of the {IERC777} interface.
  13. *
  14. * This implementation is agnostic to the way tokens are created. This means
  15. * that a supply mechanism has to be added in a derived contract using {_mint}.
  16. *
  17. * Support for ERC20 is included in this contract, as specified by the EIP: both
  18. * the ERC777 and ERC20 interfaces can be safely used when interacting with it.
  19. * Both {IERC777-Sent} and {IERC20-Transfer} events are emitted on token
  20. * movements.
  21. *
  22. * Additionally, the {IERC777-granularity} value is hard-coded to `1`, meaning that there
  23. * are no special restrictions in the amount of tokens that created, moved, or
  24. * destroyed. This makes integration with ERC20 applications seamless.
  25. */
  26. contract ERC777 is Context, IERC777, IERC20 {
  27. using SafeMath for uint256;
  28. using Address for address;
  29. IERC1820Registry constant internal _ERC1820_REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
  30. mapping(address => uint256) private _balances;
  31. uint256 private _totalSupply;
  32. string private _name;
  33. string private _symbol;
  34. // We inline the result of the following hashes because Solidity doesn't resolve them at compile time.
  35. // See https://github.com/ethereum/solidity/issues/4024.
  36. // keccak256("ERC777TokensSender")
  37. bytes32 constant private _TOKENS_SENDER_INTERFACE_HASH =
  38. 0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895;
  39. // keccak256("ERC777TokensRecipient")
  40. bytes32 constant private _TOKENS_RECIPIENT_INTERFACE_HASH =
  41. 0xb281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b;
  42. // This isn't ever read from - it's only used to respond to the defaultOperators query.
  43. address[] private _defaultOperatorsArray;
  44. // Immutable, but accounts may revoke them (tracked in __revokedDefaultOperators).
  45. mapping(address => bool) private _defaultOperators;
  46. // For each account, a mapping of its operators and revoked default operators.
  47. mapping(address => mapping(address => bool)) private _operators;
  48. mapping(address => mapping(address => bool)) private _revokedDefaultOperators;
  49. // ERC20-allowances
  50. mapping (address => mapping (address => uint256)) private _allowances;
  51. /**
  52. * @dev `defaultOperators` may be an empty array.
  53. */
  54. constructor(
  55. string memory name_,
  56. string memory symbol_,
  57. address[] memory defaultOperators_
  58. ) {
  59. _name = name_;
  60. _symbol = symbol_;
  61. _defaultOperatorsArray = defaultOperators_;
  62. for (uint256 i = 0; i < _defaultOperatorsArray.length; i++) {
  63. _defaultOperators[_defaultOperatorsArray[i]] = true;
  64. }
  65. // register interfaces
  66. _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC777Token"), address(this));
  67. _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC20Token"), address(this));
  68. }
  69. /**
  70. * @dev See {IERC777-name}.
  71. */
  72. function name() public view virtual override returns (string memory) {
  73. return _name;
  74. }
  75. /**
  76. * @dev See {IERC777-symbol}.
  77. */
  78. function symbol() public view virtual override returns (string memory) {
  79. return _symbol;
  80. }
  81. /**
  82. * @dev See {ERC20-decimals}.
  83. *
  84. * Always returns 18, as per the
  85. * [ERC777 EIP](https://eips.ethereum.org/EIPS/eip-777#backward-compatibility).
  86. */
  87. function decimals() public pure virtual returns (uint8) {
  88. return 18;
  89. }
  90. /**
  91. * @dev See {IERC777-granularity}.
  92. *
  93. * This implementation always returns `1`.
  94. */
  95. function granularity() public view virtual override returns (uint256) {
  96. return 1;
  97. }
  98. /**
  99. * @dev See {IERC777-totalSupply}.
  100. */
  101. function totalSupply() public view virtual override(IERC20, IERC777) returns (uint256) {
  102. return _totalSupply;
  103. }
  104. /**
  105. * @dev Returns the amount of tokens owned by an account (`tokenHolder`).
  106. */
  107. function balanceOf(address tokenHolder) public view virtual override(IERC20, IERC777) returns (uint256) {
  108. return _balances[tokenHolder];
  109. }
  110. /**
  111. * @dev See {IERC777-send}.
  112. *
  113. * Also emits a {IERC20-Transfer} event for ERC20 compatibility.
  114. */
  115. function send(address recipient, uint256 amount, bytes memory data) public virtual override {
  116. _send(_msgSender(), recipient, amount, data, "", true);
  117. }
  118. /**
  119. * @dev See {IERC20-transfer}.
  120. *
  121. * Unlike `send`, `recipient` is _not_ required to implement the {IERC777Recipient}
  122. * interface if it is a contract.
  123. *
  124. * Also emits a {Sent} event.
  125. */
  126. function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
  127. require(recipient != address(0), "ERC777: transfer to the zero address");
  128. address from = _msgSender();
  129. _callTokensToSend(from, from, recipient, amount, "", "");
  130. _move(from, from, recipient, amount, "", "");
  131. _callTokensReceived(from, from, recipient, amount, "", "", false);
  132. return true;
  133. }
  134. /**
  135. * @dev See {IERC777-burn}.
  136. *
  137. * Also emits a {IERC20-Transfer} event for ERC20 compatibility.
  138. */
  139. function burn(uint256 amount, bytes memory data) public virtual override {
  140. _burn(_msgSender(), amount, data, "");
  141. }
  142. /**
  143. * @dev See {IERC777-isOperatorFor}.
  144. */
  145. function isOperatorFor(address operator, address tokenHolder) public view virtual override returns (bool) {
  146. return operator == tokenHolder ||
  147. (_defaultOperators[operator] && !_revokedDefaultOperators[tokenHolder][operator]) ||
  148. _operators[tokenHolder][operator];
  149. }
  150. /**
  151. * @dev See {IERC777-authorizeOperator}.
  152. */
  153. function authorizeOperator(address operator) public virtual override {
  154. require(_msgSender() != operator, "ERC777: authorizing self as operator");
  155. if (_defaultOperators[operator]) {
  156. delete _revokedDefaultOperators[_msgSender()][operator];
  157. } else {
  158. _operators[_msgSender()][operator] = true;
  159. }
  160. emit AuthorizedOperator(operator, _msgSender());
  161. }
  162. /**
  163. * @dev See {IERC777-revokeOperator}.
  164. */
  165. function revokeOperator(address operator) public virtual override {
  166. require(operator != _msgSender(), "ERC777: revoking self as operator");
  167. if (_defaultOperators[operator]) {
  168. _revokedDefaultOperators[_msgSender()][operator] = true;
  169. } else {
  170. delete _operators[_msgSender()][operator];
  171. }
  172. emit RevokedOperator(operator, _msgSender());
  173. }
  174. /**
  175. * @dev See {IERC777-defaultOperators}.
  176. */
  177. function defaultOperators() public view virtual override returns (address[] memory) {
  178. return _defaultOperatorsArray;
  179. }
  180. /**
  181. * @dev See {IERC777-operatorSend}.
  182. *
  183. * Emits {Sent} and {IERC20-Transfer} events.
  184. */
  185. function operatorSend(
  186. address sender,
  187. address recipient,
  188. uint256 amount,
  189. bytes memory data,
  190. bytes memory operatorData
  191. )
  192. public
  193. virtual
  194. override
  195. {
  196. require(isOperatorFor(_msgSender(), sender), "ERC777: caller is not an operator for holder");
  197. _send(sender, recipient, amount, data, operatorData, true);
  198. }
  199. /**
  200. * @dev See {IERC777-operatorBurn}.
  201. *
  202. * Emits {Burned} and {IERC20-Transfer} events.
  203. */
  204. function operatorBurn(address account, uint256 amount, bytes memory data, bytes memory operatorData) public virtual override {
  205. require(isOperatorFor(_msgSender(), account), "ERC777: caller is not an operator for holder");
  206. _burn(account, amount, data, operatorData);
  207. }
  208. /**
  209. * @dev See {IERC20-allowance}.
  210. *
  211. * Note that operator and allowance concepts are orthogonal: operators may
  212. * not have allowance, and accounts with allowance may not be operators
  213. * themselves.
  214. */
  215. function allowance(address holder, address spender) public view virtual override returns (uint256) {
  216. return _allowances[holder][spender];
  217. }
  218. /**
  219. * @dev See {IERC20-approve}.
  220. *
  221. * Note that accounts cannot have allowance issued by their operators.
  222. */
  223. function approve(address spender, uint256 value) public virtual override returns (bool) {
  224. address holder = _msgSender();
  225. _approve(holder, spender, value);
  226. return true;
  227. }
  228. /**
  229. * @dev See {IERC20-transferFrom}.
  230. *
  231. * Note that operator and allowance concepts are orthogonal: operators cannot
  232. * call `transferFrom` (unless they have allowance), and accounts with
  233. * allowance cannot call `operatorSend` (unless they are operators).
  234. *
  235. * Emits {Sent}, {IERC20-Transfer} and {IERC20-Approval} events.
  236. */
  237. function transferFrom(address holder, address recipient, uint256 amount) public virtual override returns (bool) {
  238. require(recipient != address(0), "ERC777: transfer to the zero address");
  239. require(holder != address(0), "ERC777: transfer from the zero address");
  240. address spender = _msgSender();
  241. _callTokensToSend(spender, holder, recipient, amount, "", "");
  242. _move(spender, holder, recipient, amount, "", "");
  243. _approve(holder, spender, _allowances[holder][spender].sub(amount, "ERC777: transfer amount exceeds allowance"));
  244. _callTokensReceived(spender, holder, recipient, amount, "", "", false);
  245. return true;
  246. }
  247. /**
  248. * @dev Creates `amount` tokens and assigns them to `account`, increasing
  249. * the total supply.
  250. *
  251. * If a send hook is registered for `account`, the corresponding function
  252. * will be called with `operator`, `data` and `operatorData`.
  253. *
  254. * See {IERC777Sender} and {IERC777Recipient}.
  255. *
  256. * Emits {Minted} and {IERC20-Transfer} events.
  257. *
  258. * Requirements
  259. *
  260. * - `account` cannot be the zero address.
  261. * - if `account` is a contract, it must implement the {IERC777Recipient}
  262. * interface.
  263. */
  264. function _mint(
  265. address account,
  266. uint256 amount,
  267. bytes memory userData,
  268. bytes memory operatorData
  269. )
  270. internal
  271. virtual
  272. {
  273. require(account != address(0), "ERC777: mint to the zero address");
  274. address operator = _msgSender();
  275. _beforeTokenTransfer(operator, address(0), account, amount);
  276. // Update state variables
  277. _totalSupply = _totalSupply.add(amount);
  278. _balances[account] = _balances[account].add(amount);
  279. _callTokensReceived(operator, address(0), account, amount, userData, operatorData, true);
  280. emit Minted(operator, account, amount, userData, operatorData);
  281. emit Transfer(address(0), account, amount);
  282. }
  283. /**
  284. * @dev Send tokens
  285. * @param from address token holder address
  286. * @param to address recipient address
  287. * @param amount uint256 amount of tokens to transfer
  288. * @param userData bytes extra information provided by the token holder (if any)
  289. * @param operatorData bytes extra information provided by the operator (if any)
  290. * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient
  291. */
  292. function _send(
  293. address from,
  294. address to,
  295. uint256 amount,
  296. bytes memory userData,
  297. bytes memory operatorData,
  298. bool requireReceptionAck
  299. )
  300. internal
  301. virtual
  302. {
  303. require(from != address(0), "ERC777: send from the zero address");
  304. require(to != address(0), "ERC777: send to the zero address");
  305. address operator = _msgSender();
  306. _callTokensToSend(operator, from, to, amount, userData, operatorData);
  307. _move(operator, from, to, amount, userData, operatorData);
  308. _callTokensReceived(operator, from, to, amount, userData, operatorData, requireReceptionAck);
  309. }
  310. /**
  311. * @dev Burn tokens
  312. * @param from address token holder address
  313. * @param amount uint256 amount of tokens to burn
  314. * @param data bytes extra information provided by the token holder
  315. * @param operatorData bytes extra information provided by the operator (if any)
  316. */
  317. function _burn(
  318. address from,
  319. uint256 amount,
  320. bytes memory data,
  321. bytes memory operatorData
  322. )
  323. internal
  324. virtual
  325. {
  326. require(from != address(0), "ERC777: burn from the zero address");
  327. address operator = _msgSender();
  328. _callTokensToSend(operator, from, address(0), amount, data, operatorData);
  329. _beforeTokenTransfer(operator, from, address(0), amount);
  330. // Update state variables
  331. _balances[from] = _balances[from].sub(amount, "ERC777: burn amount exceeds balance");
  332. _totalSupply = _totalSupply.sub(amount);
  333. emit Burned(operator, from, amount, data, operatorData);
  334. emit Transfer(from, address(0), amount);
  335. }
  336. function _move(
  337. address operator,
  338. address from,
  339. address to,
  340. uint256 amount,
  341. bytes memory userData,
  342. bytes memory operatorData
  343. )
  344. private
  345. {
  346. _beforeTokenTransfer(operator, from, to, amount);
  347. _balances[from] = _balances[from].sub(amount, "ERC777: transfer amount exceeds balance");
  348. _balances[to] = _balances[to].add(amount);
  349. emit Sent(operator, from, to, amount, userData, operatorData);
  350. emit Transfer(from, to, amount);
  351. }
  352. /**
  353. * @dev See {ERC20-_approve}.
  354. *
  355. * Note that accounts cannot have allowance issued by their operators.
  356. */
  357. function _approve(address holder, address spender, uint256 value) internal {
  358. require(holder != address(0), "ERC777: approve from the zero address");
  359. require(spender != address(0), "ERC777: approve to the zero address");
  360. _allowances[holder][spender] = value;
  361. emit Approval(holder, spender, value);
  362. }
  363. /**
  364. * @dev Call from.tokensToSend() if the interface is registered
  365. * @param operator address operator requesting the transfer
  366. * @param from address token holder address
  367. * @param to address recipient address
  368. * @param amount uint256 amount of tokens to transfer
  369. * @param userData bytes extra information provided by the token holder (if any)
  370. * @param operatorData bytes extra information provided by the operator (if any)
  371. */
  372. function _callTokensToSend(
  373. address operator,
  374. address from,
  375. address to,
  376. uint256 amount,
  377. bytes memory userData,
  378. bytes memory operatorData
  379. )
  380. private
  381. {
  382. address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(from, _TOKENS_SENDER_INTERFACE_HASH);
  383. if (implementer != address(0)) {
  384. IERC777Sender(implementer).tokensToSend(operator, from, to, amount, userData, operatorData);
  385. }
  386. }
  387. /**
  388. * @dev Call to.tokensReceived() if the interface is registered. Reverts if the recipient is a contract but
  389. * tokensReceived() was not registered for the recipient
  390. * @param operator address operator requesting the transfer
  391. * @param from address token holder address
  392. * @param to address recipient address
  393. * @param amount uint256 amount of tokens to transfer
  394. * @param userData bytes extra information provided by the token holder (if any)
  395. * @param operatorData bytes extra information provided by the operator (if any)
  396. * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient
  397. */
  398. function _callTokensReceived(
  399. address operator,
  400. address from,
  401. address to,
  402. uint256 amount,
  403. bytes memory userData,
  404. bytes memory operatorData,
  405. bool requireReceptionAck
  406. )
  407. private
  408. {
  409. address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(to, _TOKENS_RECIPIENT_INTERFACE_HASH);
  410. if (implementer != address(0)) {
  411. IERC777Recipient(implementer).tokensReceived(operator, from, to, amount, userData, operatorData);
  412. } else if (requireReceptionAck) {
  413. require(!to.isContract(), "ERC777: token recipient contract has no implementer for ERC777TokensRecipient");
  414. }
  415. }
  416. /**
  417. * @dev Hook that is called before any token transfer. This includes
  418. * calls to {send}, {transfer}, {operatorSend}, minting and burning.
  419. *
  420. * Calling conditions:
  421. *
  422. * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  423. * will be to transferred to `to`.
  424. * - when `from` is zero, `amount` tokens will be minted for `to`.
  425. * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
  426. * - `from` and `to` are never both zero.
  427. *
  428. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  429. */
  430. function _beforeTokenTransfer(address operator, address from, address to, uint256 amount) internal virtual { }
  431. }