ERC777.test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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(holder, initialSupply, name, symbol, defaultOperators);
  32. });
  33. describe('as an ERC20 token', function () {
  34. shouldBehaveLikeERC20('ERC777', initialSupply, holder, anyone, defaultOperatorA);
  35. describe('_approve', function () {
  36. shouldBehaveLikeERC20Approve('ERC777', holder, anyone, initialSupply, function (owner, spender, amount) {
  37. return this.token.approveInternal(owner, spender, amount);
  38. });
  39. describe('when the owner is the zero address', function () {
  40. it('reverts', async function () {
  41. await expectRevert(this.token.approveInternal(ZERO_ADDRESS, anyone, initialSupply),
  42. 'ERC777: approve from the zero address'
  43. );
  44. });
  45. });
  46. });
  47. });
  48. it('does not emit AuthorizedOperator events for default operators', async function () {
  49. await expectEvent.notEmitted.inConstruction(this.token, 'AuthorizedOperator');
  50. });
  51. describe('basic information', function () {
  52. it('returns the name', async function () {
  53. expect(await this.token.name()).to.equal(name);
  54. });
  55. it('returns the symbol', async function () {
  56. expect(await this.token.symbol()).to.equal(symbol);
  57. });
  58. it('returns a granularity of 1', async function () {
  59. expect(await this.token.granularity()).to.be.bignumber.equal('1');
  60. });
  61. it('returns the default operators', async function () {
  62. expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
  63. });
  64. it('default operators are operators for all accounts', async function () {
  65. for (const operator of defaultOperators) {
  66. expect(await this.token.isOperatorFor(operator, anyone)).to.equal(true);
  67. }
  68. });
  69. it('returns the total supply', async function () {
  70. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  71. });
  72. it('returns 18 when decimals is called', async function () {
  73. expect(await this.token.decimals()).to.be.bignumber.equal('18');
  74. });
  75. it('the ERC777Token interface is registered in the registry', async function () {
  76. expect(await this.erc1820.getInterfaceImplementer(this.token.address, web3.utils.soliditySha3('ERC777Token')))
  77. .to.equal(this.token.address);
  78. });
  79. it('the ERC20Token interface is registered in the registry', async function () {
  80. expect(await this.erc1820.getInterfaceImplementer(this.token.address, web3.utils.soliditySha3('ERC20Token')))
  81. .to.equal(this.token.address);
  82. });
  83. });
  84. describe('balanceOf', function () {
  85. context('for an account with no tokens', function () {
  86. it('returns zero', async function () {
  87. expect(await this.token.balanceOf(anyone)).to.be.bignumber.equal('0');
  88. });
  89. });
  90. context('for an account with tokens', function () {
  91. it('returns their balance', async function () {
  92. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply);
  93. });
  94. });
  95. });
  96. context('with no ERC777TokensSender and no ERC777TokensRecipient implementers', function () {
  97. describe('send/burn', function () {
  98. shouldBehaveLikeERC777DirectSendBurn(holder, anyone, data);
  99. context('with self operator', function () {
  100. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, holder, data, operatorData);
  101. });
  102. context('with first default operator', function () {
  103. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, defaultOperatorA, data, operatorData);
  104. });
  105. context('with second default operator', function () {
  106. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, defaultOperatorB, data, operatorData);
  107. });
  108. context('before authorizing a new operator', function () {
  109. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, anyone, newOperator, data, operatorData);
  110. });
  111. context('with new authorized operator', function () {
  112. beforeEach(async function () {
  113. await this.token.authorizeOperator(newOperator, { from: holder });
  114. });
  115. shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, newOperator, data, operatorData);
  116. context('with revoked operator', function () {
  117. beforeEach(async function () {
  118. await this.token.revokeOperator(newOperator, { from: holder });
  119. });
  120. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, anyone, newOperator, data, operatorData);
  121. });
  122. });
  123. });
  124. describe('mint (internal)', function () {
  125. const to = anyone;
  126. const amount = new BN('5');
  127. context('with default operator', function () {
  128. const operator = defaultOperatorA;
  129. shouldBehaveLikeERC777InternalMint(to, operator, amount, data, operatorData);
  130. });
  131. context('with non operator', function () {
  132. const operator = newOperator;
  133. shouldBehaveLikeERC777InternalMint(to, operator, amount, data, operatorData);
  134. });
  135. });
  136. });
  137. describe('operator management', function () {
  138. it('accounts are their own operator', async function () {
  139. expect(await this.token.isOperatorFor(holder, holder)).to.equal(true);
  140. });
  141. it('reverts when self-authorizing', async function () {
  142. await expectRevert(
  143. this.token.authorizeOperator(holder, { from: holder }), 'ERC777: authorizing self as operator'
  144. );
  145. });
  146. it('reverts when self-revoking', async function () {
  147. await expectRevert(
  148. this.token.revokeOperator(holder, { from: holder }), 'ERC777: revoking self as operator'
  149. );
  150. });
  151. it('non-operators can be revoked', async function () {
  152. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  153. const { logs } = await this.token.revokeOperator(newOperator, { from: holder });
  154. expectEvent.inLogs(logs, 'RevokedOperator', { operator: newOperator, tokenHolder: holder });
  155. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  156. });
  157. it('non-operators can be authorized', async function () {
  158. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  159. const { logs } = await this.token.authorizeOperator(newOperator, { from: holder });
  160. expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: newOperator, tokenHolder: holder });
  161. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(true);
  162. });
  163. describe('new operators', function () {
  164. beforeEach(async function () {
  165. await this.token.authorizeOperator(newOperator, { from: holder });
  166. });
  167. it('are not added to the default operators list', async function () {
  168. expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
  169. });
  170. it('can be re-authorized', async function () {
  171. const { logs } = await this.token.authorizeOperator(newOperator, { from: holder });
  172. expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: newOperator, tokenHolder: holder });
  173. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(true);
  174. });
  175. it('can be revoked', async function () {
  176. const { logs } = await this.token.revokeOperator(newOperator, { from: holder });
  177. expectEvent.inLogs(logs, 'RevokedOperator', { operator: newOperator, tokenHolder: holder });
  178. expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
  179. });
  180. });
  181. describe('default operators', function () {
  182. it('can be re-authorized', async function () {
  183. const { logs } = await this.token.authorizeOperator(defaultOperatorA, { from: holder });
  184. expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: defaultOperatorA, tokenHolder: holder });
  185. expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(true);
  186. });
  187. it('can be revoked', async function () {
  188. const { logs } = await this.token.revokeOperator(defaultOperatorA, { from: holder });
  189. expectEvent.inLogs(logs, 'RevokedOperator', { operator: defaultOperatorA, tokenHolder: holder });
  190. expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(false);
  191. });
  192. it('cannot be revoked for themselves', async function () {
  193. await expectRevert(
  194. this.token.revokeOperator(defaultOperatorA, { from: defaultOperatorA }),
  195. 'ERC777: revoking self as operator'
  196. );
  197. });
  198. context('with revoked default operator', function () {
  199. beforeEach(async function () {
  200. await this.token.revokeOperator(defaultOperatorA, { from: holder });
  201. });
  202. it('default operator is not revoked for other holders', async function () {
  203. expect(await this.token.isOperatorFor(defaultOperatorA, anyone)).to.equal(true);
  204. });
  205. it('other default operators are not revoked', async function () {
  206. expect(await this.token.isOperatorFor(defaultOperatorB, holder)).to.equal(true);
  207. });
  208. it('default operators list is not modified', async function () {
  209. expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
  210. });
  211. it('revoked default operator can be re-authorized', async function () {
  212. const { logs } = await this.token.authorizeOperator(defaultOperatorA, { from: holder });
  213. expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: defaultOperatorA, tokenHolder: holder });
  214. expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(true);
  215. });
  216. });
  217. });
  218. });
  219. describe('send and receive hooks', function () {
  220. const amount = new BN('1');
  221. const operator = defaultOperatorA;
  222. // sender and recipient are stored inside 'this', since in some tests their addresses are determined dynamically
  223. describe('tokensReceived', function () {
  224. beforeEach(function () {
  225. this.sender = holder;
  226. });
  227. context('with no ERC777TokensRecipient implementer', function () {
  228. context('with contract recipient', function () {
  229. beforeEach(async function () {
  230. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  231. this.recipient = this.tokensRecipientImplementer.address;
  232. // Note that tokensRecipientImplementer doesn't implement the recipient interface for the recipient
  233. });
  234. it('send reverts', async function () {
  235. await expectRevert(
  236. this.token.send(this.recipient, amount, data, { from: holder }),
  237. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  238. );
  239. });
  240. it('operatorSend reverts', async function () {
  241. await expectRevert(
  242. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
  243. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  244. );
  245. });
  246. it('mint (internal) reverts', async function () {
  247. await expectRevert(
  248. this.token.mintInternal(this.recipient, amount, data, operatorData, { from: operator }),
  249. 'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
  250. );
  251. });
  252. it('(ERC20) transfer succeeds', async function () {
  253. await this.token.transfer(this.recipient, amount, { from: holder });
  254. });
  255. it('(ERC20) transferFrom succeeds', async function () {
  256. const approved = anyone;
  257. await this.token.approve(approved, amount, { from: this.sender });
  258. await this.token.transferFrom(this.sender, this.recipient, amount, { from: approved });
  259. });
  260. });
  261. });
  262. context('with ERC777TokensRecipient implementer', function () {
  263. context('with contract as implementer for an externally owned account', function () {
  264. beforeEach(async function () {
  265. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  266. this.recipient = anyone;
  267. await this.tokensRecipientImplementer.recipientFor(this.recipient);
  268. await this.erc1820.setInterfaceImplementer(
  269. this.recipient,
  270. web3.utils.soliditySha3('ERC777TokensRecipient'), this.tokensRecipientImplementer.address,
  271. { from: this.recipient },
  272. );
  273. });
  274. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
  275. });
  276. context('with contract as implementer for another contract', function () {
  277. beforeEach(async function () {
  278. this.recipientContract = await ERC777SenderRecipientMock.new();
  279. this.recipient = this.recipientContract.address;
  280. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  281. await this.tokensRecipientImplementer.recipientFor(this.recipient);
  282. await this.recipientContract.registerRecipient(this.tokensRecipientImplementer.address);
  283. });
  284. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
  285. });
  286. context('with contract as implementer for itself', function () {
  287. beforeEach(async function () {
  288. this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
  289. this.recipient = this.tokensRecipientImplementer.address;
  290. await this.tokensRecipientImplementer.recipientFor(this.recipient);
  291. });
  292. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
  293. });
  294. });
  295. });
  296. describe('tokensToSend', function () {
  297. beforeEach(function () {
  298. this.recipient = anyone;
  299. });
  300. context('with a contract as implementer for an externally owned account', function () {
  301. beforeEach(async function () {
  302. this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
  303. this.sender = holder;
  304. await this.tokensSenderImplementer.senderFor(this.sender);
  305. await this.erc1820.setInterfaceImplementer(
  306. this.sender,
  307. web3.utils.soliditySha3('ERC777TokensSender'), this.tokensSenderImplementer.address,
  308. { from: this.sender },
  309. );
  310. });
  311. shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
  312. });
  313. context('with contract as implementer for another contract', function () {
  314. beforeEach(async function () {
  315. this.senderContract = await ERC777SenderRecipientMock.new();
  316. this.sender = this.senderContract.address;
  317. this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
  318. await this.tokensSenderImplementer.senderFor(this.sender);
  319. await this.senderContract.registerSender(this.tokensSenderImplementer.address);
  320. // For the contract to be able to receive tokens (that it can later send), it must also implement the
  321. // recipient interface.
  322. await this.senderContract.recipientFor(this.sender);
  323. await this.token.send(this.sender, amount, data, { from: holder });
  324. });
  325. shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
  326. });
  327. context('with a contract as implementer for itself', function () {
  328. beforeEach(async function () {
  329. this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
  330. this.sender = this.tokensSenderImplementer.address;
  331. await this.tokensSenderImplementer.senderFor(this.sender);
  332. // For the contract to be able to receive tokens (that it can later send), it must also implement the
  333. // recipient interface.
  334. await this.tokensSenderImplementer.recipientFor(this.sender);
  335. await this.token.send(this.sender, amount, data, { from: holder });
  336. });
  337. shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
  338. });
  339. });
  340. });
  341. });
  342. context('with no default operators', function () {
  343. beforeEach(async function () {
  344. this.token = await ERC777.new(holder, initialSupply, name, symbol, []);
  345. });
  346. it('default operators list is empty', async function () {
  347. expect(await this.token.defaultOperators()).to.deep.equal([]);
  348. });
  349. });
  350. });