ERC777Upgradeable.sol 19 KB

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