ERC777.test.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. const { BN, constants, expectEvent, expectRevert, singletons } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const {
  5. shouldBehaveLikeERC777DirectSendBurn,
  6. shouldBehaveLikeERC777OperatorSendBurn,
  7. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn,
  8. shouldBehaveLikeERC777InternalMint,
  9. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook,
  10. shouldBehaveLikeERC777SendBurnWithSendHook,
  11. } = require('./ERC777.behavior');
  12. const {
  13. shouldBehaveLikeERC20,
  14. shouldBehaveLikeERC20Approve,
  15. } = require('../ERC20/ERC20.behavior');
  16. const ERC777 = artifacts.require('$ERC777Mock');
  17. const ERC777SenderRecipientMock = artifacts.require('$ERC777SenderRecipientMock');
  18. contract('ERC777', function (accounts) {
  19. const [ registryFunder, holder, defaultOperatorA, defaultOperatorB, newOperator, anyone ] = accounts;
  20. const initialSupply = new BN('10000');
  21. const name = 'ERC777Test';
  22. const symbol = '777T';
  23. const data = web3.utils.sha3('OZ777TestData');
  24. const operatorData = web3.utils.sha3('OZ777TestOperatorData');
  25. const defaultOperators = [defaultOperatorA, defaultOperatorB];
  26. beforeEach(async function () {
  27. this.erc1820 = await singletons.ERC1820Registry(registryFunder);
  28. });
  29. context('with default operators', function () {
  30. beforeEach(async function () {
  31. this.token = await ERC777.new(name, symbol, defaultOperators);
  32. await this.token.$_mint(holder, initialSupply, '0x', '0x');
  33. });
  34. describe('as an ERC20 token', function () {
  35. shouldBehaveLikeERC20('ERC777', initialSupply, holder, anyone, defaultOperatorA);
  36. describe('_approve', function () {
  37. shouldBehaveLikeERC20Approve('ERC777', holder, anyone, initialSupply, function (owner, spender, amount) {
  38. return this.token.$_approve(owner, spender, amount);
  39. });
  40. describe('when the owner is the zero address', function () {
  41. it('reverts', async function () {
  42. await expectRevert(this.token.$_approve(ZERO_ADDRESS, anyone, initialSupply),
  43. 'ERC777: approve from the zero address',
  44. );
  45. });
  46. });
  47. });
  48. });
  49. it('does not emit AuthorizedOperator events for default operators', async function () {
  50. await expectEvent.notEmitted.inConstruction(this.token, 'AuthorizedOperator');
  51. });
  52. describe('basic information', function () {
  53. it('returns the name', async function () {
  54. expect(await this.token.name()).to.equal(name);
  55. });
  56. it('returns the symbol', async function () {
  57. expect(await this.token.symbol()).to.equal(symbol);
  58. });
  59. it('returns a granularity of 1', async function () {
  60. expect(await this.token.granularity()).to.be.bignumber.equal('1');
  61. });
  62. it('returns the default operators', async function () {
  63. expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
  64. });
  65. it('default operators are operators for all accounts', async function () {
  66. for (const operator of defaultOperators) {
  67. expect(await this.token.isOperatorFor(operator, anyone)).to.equal(true);
  68. }
  69. });
  70. it('returns the total supply', async function () {
  71. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  72. });
  73. it('returns 18 when decimals is called', async function () {
  74. expect(await this.token.decimals()).to.be.bignumber.equal('18');
  75. });
  76. it('the ERC777Token interface is registered in the registry', async function () {
  77. expect(await this.erc1820.getInterfaceImplementer(this.token.address, web3.utils.soliditySha3('ERC777Token')))
  78. .to.equal(this.token.address);
  79. });
  80. it('the ERC20Token interface is registered in the registry', async function () {
  81. expect(await this.erc1820.getInterfaceImplementer(this.token.address, web3.utils.soliditySha3('ERC20Token')))
  82. .to.equal(this.token.address);
  83. });
  84. });
  85. describe('balanceOf', function () {
  86. context('for an account with no tokens', function () {
  87. it('returns zero', async function () {
  88. expect(await this.token.balanceOf(anyone)).to.be.bignumber.equal('0');
  89. });
  90. });
  91. context('for an account with tokens', function () {
  92. it('returns their balance', async function () {
  93. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply);
  94. });
  95. });
  96. });
  97. context('with no ERC777TokensSender and no ERC777TokensRecipient implementers', function () {
  98. describe('send/burn', function () {
  99. shouldBehaveLikeERC777DirectSendBurn(holder, anyone, data);
  100. context('with self operator', function () {
  101. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, holder, data, operatorData);
  102. });
  103. context('with first default operator', function () {
  104. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, defaultOperatorA, data, operatorData);
  105. });
  106. context('with second default operator', function () {
  107. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, defaultOperatorB, data, operatorData);
  108. });
  109. context('before authorizing a new operator', function () {
  110. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, anyone, newOperator, data, operatorData);
  111. });
  112. context('with new authorized operator', function () {
  113. beforeEach(async function () {
  114. await this.token.authorizeOperator(newOperator, { from: holder });
  115. });
  116. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, newOperator, data, operatorData);
  117. context('with revoked operator', function () {
  118. beforeEach(async function () {
  119. await this.token.revokeOperator(newOperator, { from: holder });
  120. });
  121. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, anyone, newOperator, data, operatorData);
  122. });
  123. });
  124. });
  125. describe('mint (internal)', function () {
  126. const to = anyone;
  127. const amount = new BN('5');
  128. context('with default operator', function () {
  129. const operator = defaultOperatorA;
  130. shouldBehaveLikeERC777InternalMint(to, operator, amount, data, operatorData);
  131. });
  132. context('with non operator', function () {
  133. const operator = newOperator;
  134. shouldBehaveLikeERC777InternalMint(to, operator, amount, data, operatorData);
  135. });
  136. });
  137. describe('mint (internal extended)', function () {
  138. const amount = new BN('5');
  139. context('to anyone', function () {
  140. beforeEach(async function () {
  141. this.recipient = anyone;
  142. });
  143. context('with default operator', function () {
  144. const operator = defaultOperatorA;
  145. it('without requireReceptionAck', async function () {
  146. await this.token.$_mint(
  147. this.recipient,
  148. amount,
  149. data,
  150. operatorData,
  151. false,
  152. { from: operator },
  153. );
  154. });
  155. it('with requireReceptionAck', async function () {
  156. await this.token.$_mint(
  157. this.recipient,
  158. amount,
  159. data,
  160. operatorData,
  161. true,
  162. { from: operator },
  163. );
  164. });
  165. });
  166. context('with non operator', function () {
  167. const operator = newOperator;
  168. it('without requireReceptionAck', async function () {
  169. await this.token.$_mint(
  170. this.recipient,
  171. amount,
  172. data,
  173. operatorData,
  174. false,
  175. { from: operator },
  176. );
  177. });
  178. it('with requireReceptionAck', async function () {
  179. await this.token.$_mint(
  180. this.recipient,
  181. amount,
  182. data,
  183. operatorData,
  184. true,
  185. { from: operator },
  186. );
  187. });
  188. });
  189. });
  190. context('to non ERC777TokensRecipient implementer', function () {
  191. beforeEach(async function () {
  192. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  193. this.recipient = this.tokensRecipientImplementer.address;
  194. });
  195. context('with default operator', function () {
  196. const operator = defaultOperatorA;
  197. it('without requireReceptionAck', async function () {
  198. await this.token.$_mint(
  199. this.recipient,
  200. amount,
  201. data,
  202. operatorData,
  203. false,
  204. { from: operator },
  205. );
  206. });
  207. it('with requireReceptionAck', async function () {
  208. await expectRevert(
  209. this.token.$_mint(
  210. this.recipient,
  211. amount,
  212. data,
  213. operatorData,
  214. true,
  215. { from: operator },
  216. ),
  217. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  218. );
  219. });
  220. });
  221. context('with non operator', function () {
  222. const operator = newOperator;
  223. it('without requireReceptionAck', async function () {
  224. await this.token.$_mint(
  225. this.recipient,
  226. amount,
  227. data,
  228. operatorData,
  229. false,
  230. { from: operator },
  231. );
  232. });
  233. it('with requireReceptionAck', async function () {
  234. await expectRevert(
  235. this.token.$_mint(
  236. this.recipient,
  237. amount,
  238. data,
  239. operatorData,
  240. true,
  241. { from: operator },
  242. ),
  243. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  244. );
  245. });
  246. });
  247. });
  248. });
  249. });
  250. describe('operator management', function () {
  251. it('accounts are their own operator', async function () {
  252. expect(await this.token.isOperatorFor(holder, holder)).to.equal(true);
  253. });
  254. it('reverts when self-authorizing', async function () {
  255. await expectRevert(
  256. this.token.authorizeOperator(holder, { from: holder }), 'ERC777: authorizing self as operator',
  257. );
  258. });
  259. it('reverts when self-revoking', async function () {
  260. await expectRevert(
  261. this.token.revokeOperator(holder, { from: holder }), 'ERC777: revoking self as operator',
  262. );
  263. });
  264. it('non-operators can be revoked', async function () {
  265. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  266. const receipt = await this.token.revokeOperator(newOperator, { from: holder });
  267. expectEvent(receipt, 'RevokedOperator', { operator: newOperator, tokenHolder: holder });
  268. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  269. });
  270. it('non-operators can be authorized', async function () {
  271. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  272. const receipt = await this.token.authorizeOperator(newOperator, { from: holder });
  273. expectEvent(receipt, 'AuthorizedOperator', { operator: newOperator, tokenHolder: holder });
  274. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(true);
  275. });
  276. describe('new operators', function () {
  277. beforeEach(async function () {
  278. await this.token.authorizeOperator(newOperator, { from: holder });
  279. });
  280. it('are not added to the default operators list', async function () {
  281. expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
  282. });
  283. it('can be re-authorized', async function () {
  284. const receipt = await this.token.authorizeOperator(newOperator, { from: holder });
  285. expectEvent(receipt, 'AuthorizedOperator', { operator: newOperator, tokenHolder: holder });
  286. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(true);
  287. });
  288. it('can be revoked', async function () {
  289. const receipt = await this.token.revokeOperator(newOperator, { from: holder });
  290. expectEvent(receipt, 'RevokedOperator', { operator: newOperator, tokenHolder: holder });
  291. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  292. });
  293. });
  294. describe('default operators', function () {
  295. it('can be re-authorized', async function () {
  296. const receipt = await this.token.authorizeOperator(defaultOperatorA, { from: holder });
  297. expectEvent(receipt, 'AuthorizedOperator', { operator: defaultOperatorA, tokenHolder: holder });
  298. expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(true);
  299. });
  300. it('can be revoked', async function () {
  301. const receipt = await this.token.revokeOperator(defaultOperatorA, { from: holder });
  302. expectEvent(receipt, 'RevokedOperator', { operator: defaultOperatorA, tokenHolder: holder });
  303. expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(false);
  304. });
  305. it('cannot be revoked for themselves', async function () {
  306. await expectRevert(
  307. this.token.revokeOperator(defaultOperatorA, { from: defaultOperatorA }),
  308. 'ERC777: revoking self as operator',
  309. );
  310. });
  311. context('with revoked default operator', function () {
  312. beforeEach(async function () {
  313. await this.token.revokeOperator(defaultOperatorA, { from: holder });
  314. });
  315. it('default operator is not revoked for other holders', async function () {
  316. expect(await this.token.isOperatorFor(defaultOperatorA, anyone)).to.equal(true);
  317. });
  318. it('other default operators are not revoked', async function () {
  319. expect(await this.token.isOperatorFor(defaultOperatorB, holder)).to.equal(true);
  320. });
  321. it('default operators list is not modified', async function () {
  322. expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
  323. });
  324. it('revoked default operator can be re-authorized', async function () {
  325. const receipt = await this.token.authorizeOperator(defaultOperatorA, { from: holder });
  326. expectEvent(receipt, 'AuthorizedOperator', { operator: defaultOperatorA, tokenHolder: holder });
  327. expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(true);
  328. });
  329. });
  330. });
  331. });
  332. describe('send and receive hooks', function () {
  333. const amount = new BN('1');
  334. const operator = defaultOperatorA;
  335. // sender and recipient are stored inside 'this', since in some tests their addresses are determined dynamically
  336. describe('tokensReceived', function () {
  337. beforeEach(function () {
  338. this.sender = holder;
  339. });
  340. context('with no ERC777TokensRecipient implementer', function () {
  341. context('with contract recipient', function () {
  342. beforeEach(async function () {
  343. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  344. this.recipient = this.tokensRecipientImplementer.address;
  345. // Note that tokensRecipientImplementer doesn't implement the recipient interface for the recipient
  346. });
  347. it('send reverts', async function () {
  348. await expectRevert(
  349. this.token.send(this.recipient, amount, data, { from: holder }),
  350. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  351. );
  352. });
  353. it('operatorSend reverts', async function () {
  354. await expectRevert(
  355. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
  356. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  357. );
  358. });
  359. it('mint (internal) reverts', async function () {
  360. await expectRevert(
  361. this.token.$_mint(this.recipient, amount, data, operatorData, true, { from: operator }),
  362. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  363. );
  364. });
  365. it('(ERC20) transfer succeeds', async function () {
  366. await this.token.transfer(this.recipient, amount, { from: holder });
  367. });
  368. it('(ERC20) transferFrom succeeds', async function () {
  369. const approved = anyone;
  370. await this.token.approve(approved, amount, { from: this.sender });
  371. await this.token.transferFrom(this.sender, this.recipient, amount, { from: approved });
  372. });
  373. });
  374. });
  375. context('with ERC777TokensRecipient implementer', function () {
  376. context('with contract as implementer for an externally owned account', function () {
  377. beforeEach(async function () {
  378. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  379. this.recipient = anyone;
  380. await this.tokensRecipientImplementer.recipientFor(this.recipient);
  381. await this.erc1820.setInterfaceImplementer(
  382. this.recipient,
  383. web3.utils.soliditySha3('ERC777TokensRecipient'), this.tokensRecipientImplementer.address,
  384. { from: this.recipient },
  385. );
  386. });
  387. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
  388. });
  389. context('with contract as implementer for another contract', function () {
  390. beforeEach(async function () {
  391. this.recipientContract = await ERC777SenderRecipientMock.new();
  392. this.recipient = this.recipientContract.address;
  393. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  394. await this.tokensRecipientImplementer.recipientFor(this.recipient);
  395. await this.recipientContract.registerRecipient(this.tokensRecipientImplementer.address);
  396. });
  397. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
  398. });
  399. context('with contract as implementer for itself', function () {
  400. beforeEach(async function () {
  401. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  402. this.recipient = this.tokensRecipientImplementer.address;
  403. await this.tokensRecipientImplementer.recipientFor(this.recipient);
  404. });
  405. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
  406. });
  407. });
  408. });
  409. describe('tokensToSend', function () {
  410. beforeEach(function () {
  411. this.recipient = anyone;
  412. });
  413. context('with a contract as implementer for an externally owned account', function () {
  414. beforeEach(async function () {
  415. this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
  416. this.sender = holder;
  417. await this.tokensSenderImplementer.senderFor(this.sender);
  418. await this.erc1820.setInterfaceImplementer(
  419. this.sender,
  420. web3.utils.soliditySha3('ERC777TokensSender'), this.tokensSenderImplementer.address,
  421. { from: this.sender },
  422. );
  423. });
  424. shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
  425. });
  426. context('with contract as implementer for another contract', function () {
  427. beforeEach(async function () {
  428. this.senderContract = await ERC777SenderRecipientMock.new();
  429. this.sender = this.senderContract.address;
  430. this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
  431. await this.tokensSenderImplementer.senderFor(this.sender);
  432. await this.senderContract.registerSender(this.tokensSenderImplementer.address);
  433. // For the contract to be able to receive tokens (that it can later send), it must also implement the
  434. // recipient interface.
  435. await this.senderContract.recipientFor(this.sender);
  436. await this.token.send(this.sender, amount, data, { from: holder });
  437. });
  438. shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
  439. });
  440. context('with a contract as implementer for itself', function () {
  441. beforeEach(async function () {
  442. this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
  443. this.sender = this.tokensSenderImplementer.address;
  444. await this.tokensSenderImplementer.senderFor(this.sender);
  445. // For the contract to be able to receive tokens (that it can later send), it must also implement the
  446. // recipient interface.
  447. await this.tokensSenderImplementer.recipientFor(this.sender);
  448. await this.token.send(this.sender, amount, data, { from: holder });
  449. });
  450. shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
  451. });
  452. });
  453. });
  454. });
  455. context('with no default operators', function () {
  456. beforeEach(async function () {
  457. this.token = await ERC777.new(name, symbol, []);
  458. });
  459. it('default operators list is empty', async function () {
  460. expect(await this.token.defaultOperators()).to.deep.equal([]);
  461. });
  462. });
  463. describe('relative order of hooks', function () {
  464. beforeEach(async function () {
  465. await singletons.ERC1820Registry(registryFunder);
  466. this.sender = await ERC777SenderRecipientMock.new();
  467. await this.sender.registerRecipient(this.sender.address);
  468. await this.sender.registerSender(this.sender.address);
  469. this.token = await ERC777.new(name, symbol, []);
  470. await this.token.$_mint(this.sender.address, 1, '0x', '0x');
  471. });
  472. it('send', async function () {
  473. const { receipt } = await this.sender.send(this.token.address, anyone, 1, '0x');
  474. const internalBeforeHook = receipt.logs.findIndex(l => l.event === 'BeforeTokenTransfer');
  475. expect(internalBeforeHook).to.be.gte(0);
  476. const externalSendHook = receipt.logs.findIndex(l => l.event === 'TokensToSendCalled');
  477. expect(externalSendHook).to.be.gte(0);
  478. expect(externalSendHook).to.be.lt(internalBeforeHook);
  479. });
  480. it('burn', async function () {
  481. const { receipt } = await this.sender.burn(this.token.address, 1, '0x');
  482. const internalBeforeHook = receipt.logs.findIndex(l => l.event === 'BeforeTokenTransfer');
  483. expect(internalBeforeHook).to.be.gte(0);
  484. const externalSendHook = receipt.logs.findIndex(l => l.event === 'TokensToSendCalled');
  485. expect(externalSendHook).to.be.gte(0);
  486. expect(externalSendHook).to.be.lt(internalBeforeHook);
  487. });
  488. });
  489. });