ERC777.sol 18 KB

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