ERC777.sol 17 KB

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