ERC777.behavior.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const ERC777SenderRecipientMock = artifacts.require('ERC777SenderRecipientMock');
  5. function shouldBehaveLikeERC777DirectSendBurn(holder, recipient, data) {
  6. shouldBehaveLikeERC777DirectSend(holder, recipient, data);
  7. shouldBehaveLikeERC777DirectBurn(holder, data);
  8. }
  9. function shouldBehaveLikeERC777OperatorSendBurn(holder, recipient, operator, data, operatorData) {
  10. shouldBehaveLikeERC777OperatorSend(holder, recipient, operator, data, operatorData);
  11. shouldBehaveLikeERC777OperatorBurn(holder, operator, data, operatorData);
  12. }
  13. function shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, recipient, operator, data, operatorData) {
  14. shouldBehaveLikeERC777UnauthorizedOperatorSend(holder, recipient, operator, data, operatorData);
  15. shouldBehaveLikeERC777UnauthorizedOperatorBurn(holder, operator, data, operatorData);
  16. }
  17. function shouldBehaveLikeERC777DirectSend(holder, recipient, data) {
  18. describe('direct send', function () {
  19. context('when the sender has tokens', function () {
  20. shouldDirectSendTokens(holder, recipient, new BN('0'), data);
  21. shouldDirectSendTokens(holder, recipient, new BN('1'), data);
  22. it('reverts when sending more than the balance', async function () {
  23. const balance = await this.token.balanceOf(holder);
  24. await expectRevert.unspecified(this.token.send(recipient, balance.addn(1), data, { from: holder }));
  25. });
  26. it('reverts when sending to the zero address', async function () {
  27. await expectRevert.unspecified(this.token.send(ZERO_ADDRESS, new BN('1'), data, { from: holder }));
  28. });
  29. });
  30. context('when the sender has no tokens', function () {
  31. removeBalance(holder);
  32. shouldDirectSendTokens(holder, recipient, new BN('0'), data);
  33. it('reverts when sending a non-zero amount', async function () {
  34. await expectRevert.unspecified(this.token.send(recipient, new BN('1'), data, { from: holder }));
  35. });
  36. });
  37. });
  38. }
  39. function shouldBehaveLikeERC777OperatorSend(holder, recipient, operator, data, operatorData) {
  40. describe('operator send', function () {
  41. context('when the sender has tokens', async function () {
  42. shouldOperatorSendTokens(holder, operator, recipient, new BN('0'), data, operatorData);
  43. shouldOperatorSendTokens(holder, operator, recipient, new BN('1'), data, operatorData);
  44. it('reverts when sending more than the balance', async function () {
  45. const balance = await this.token.balanceOf(holder);
  46. await expectRevert.unspecified(
  47. this.token.operatorSend(holder, recipient, balance.addn(1), data, operatorData, { from: operator }),
  48. );
  49. });
  50. it('reverts when sending to the zero address', async function () {
  51. await expectRevert.unspecified(
  52. this.token.operatorSend(holder, ZERO_ADDRESS, new BN('1'), data, operatorData, { from: operator }),
  53. );
  54. });
  55. });
  56. context('when the sender has no tokens', function () {
  57. removeBalance(holder);
  58. shouldOperatorSendTokens(holder, operator, recipient, new BN('0'), data, operatorData);
  59. it('reverts when sending a non-zero amount', async function () {
  60. await expectRevert.unspecified(
  61. this.token.operatorSend(holder, recipient, new BN('1'), data, operatorData, { from: operator }),
  62. );
  63. });
  64. it('reverts when sending from the zero address', async function () {
  65. // This is not yet reflected in the spec
  66. await expectRevert.unspecified(
  67. this.token.operatorSend(ZERO_ADDRESS, recipient, new BN('0'), data, operatorData, { from: operator }),
  68. );
  69. });
  70. });
  71. });
  72. }
  73. function shouldBehaveLikeERC777UnauthorizedOperatorSend(holder, recipient, operator, data, operatorData) {
  74. describe('operator send', function () {
  75. it('reverts', async function () {
  76. await expectRevert.unspecified(this.token.operatorSend(holder, recipient, new BN('0'), data, operatorData));
  77. });
  78. });
  79. }
  80. function shouldBehaveLikeERC777DirectBurn(holder, data) {
  81. describe('direct burn', function () {
  82. context('when the sender has tokens', function () {
  83. shouldDirectBurnTokens(holder, new BN('0'), data);
  84. shouldDirectBurnTokens(holder, new BN('1'), data);
  85. it('reverts when burning more than the balance', async function () {
  86. const balance = await this.token.balanceOf(holder);
  87. await expectRevert.unspecified(this.token.burn(balance.addn(1), data, { from: holder }));
  88. });
  89. });
  90. context('when the sender has no tokens', function () {
  91. removeBalance(holder);
  92. shouldDirectBurnTokens(holder, new BN('0'), data);
  93. it('reverts when burning a non-zero amount', async function () {
  94. await expectRevert.unspecified(this.token.burn(new BN('1'), data, { from: holder }));
  95. });
  96. });
  97. });
  98. }
  99. function shouldBehaveLikeERC777OperatorBurn(holder, operator, data, operatorData) {
  100. describe('operator burn', function () {
  101. context('when the sender has tokens', async function () {
  102. shouldOperatorBurnTokens(holder, operator, new BN('0'), data, operatorData);
  103. shouldOperatorBurnTokens(holder, operator, new BN('1'), data, operatorData);
  104. it('reverts when burning more than the balance', async function () {
  105. const balance = await this.token.balanceOf(holder);
  106. await expectRevert.unspecified(
  107. this.token.operatorBurn(holder, balance.addn(1), data, operatorData, { from: operator }),
  108. );
  109. });
  110. });
  111. context('when the sender has no tokens', function () {
  112. removeBalance(holder);
  113. shouldOperatorBurnTokens(holder, operator, new BN('0'), data, operatorData);
  114. it('reverts when burning a non-zero amount', async function () {
  115. await expectRevert.unspecified(
  116. this.token.operatorBurn(holder, new BN('1'), data, operatorData, { from: operator }),
  117. );
  118. });
  119. it('reverts when burning from the zero address', async function () {
  120. // This is not yet reflected in the spec
  121. await expectRevert.unspecified(
  122. this.token.operatorBurn(ZERO_ADDRESS, new BN('0'), data, operatorData, { from: operator }),
  123. );
  124. });
  125. });
  126. });
  127. }
  128. function shouldBehaveLikeERC777UnauthorizedOperatorBurn(holder, operator, data, operatorData) {
  129. describe('operator burn', function () {
  130. it('reverts', async function () {
  131. await expectRevert.unspecified(this.token.operatorBurn(holder, new BN('0'), data, operatorData));
  132. });
  133. });
  134. }
  135. function shouldDirectSendTokens(from, to, amount, data) {
  136. shouldSendTokens(from, null, to, amount, data, null);
  137. }
  138. function shouldOperatorSendTokens(from, operator, to, amount, data, operatorData) {
  139. shouldSendTokens(from, operator, to, amount, data, operatorData);
  140. }
  141. function shouldSendTokens(from, operator, to, amount, data, operatorData) {
  142. const operatorCall = operator !== null;
  143. it(`${operatorCall ? 'operator ' : ''}can send an amount of ${amount}`, async function () {
  144. const initialTotalSupply = await this.token.totalSupply();
  145. const initialFromBalance = await this.token.balanceOf(from);
  146. const initialToBalance = await this.token.balanceOf(to);
  147. let receipt;
  148. if (!operatorCall) {
  149. receipt = await this.token.send(to, amount, data, { from });
  150. expectEvent(receipt, 'Sent', {
  151. operator: from,
  152. from,
  153. to,
  154. amount,
  155. data,
  156. operatorData: null,
  157. });
  158. } else {
  159. receipt = await this.token.operatorSend(from, to, amount, data, operatorData, { from: operator });
  160. expectEvent(receipt, 'Sent', {
  161. operator,
  162. from,
  163. to,
  164. amount,
  165. data,
  166. operatorData,
  167. });
  168. }
  169. expectEvent(receipt, 'Transfer', {
  170. from,
  171. to,
  172. value: amount,
  173. });
  174. const finalTotalSupply = await this.token.totalSupply();
  175. const finalFromBalance = await this.token.balanceOf(from);
  176. const finalToBalance = await this.token.balanceOf(to);
  177. expect(finalTotalSupply).to.be.bignumber.equal(initialTotalSupply);
  178. expect(finalToBalance.sub(initialToBalance)).to.be.bignumber.equal(amount);
  179. expect(finalFromBalance.sub(initialFromBalance)).to.be.bignumber.equal(amount.neg());
  180. });
  181. }
  182. function shouldDirectBurnTokens(from, amount, data) {
  183. shouldBurnTokens(from, null, amount, data, null);
  184. }
  185. function shouldOperatorBurnTokens(from, operator, amount, data, operatorData) {
  186. shouldBurnTokens(from, operator, amount, data, operatorData);
  187. }
  188. function shouldBurnTokens(from, operator, amount, data, operatorData) {
  189. const operatorCall = operator !== null;
  190. it(`${operatorCall ? 'operator ' : ''}can burn an amount of ${amount}`, async function () {
  191. const initialTotalSupply = await this.token.totalSupply();
  192. const initialFromBalance = await this.token.balanceOf(from);
  193. let receipt;
  194. if (!operatorCall) {
  195. receipt = await this.token.burn(amount, data, { from });
  196. expectEvent(receipt, 'Burned', {
  197. operator: from,
  198. from,
  199. amount,
  200. data,
  201. operatorData: null,
  202. });
  203. } else {
  204. receipt = await this.token.operatorBurn(from, amount, data, operatorData, { from: operator });
  205. expectEvent(receipt, 'Burned', {
  206. operator,
  207. from,
  208. amount,
  209. data,
  210. operatorData,
  211. });
  212. }
  213. expectEvent(receipt, 'Transfer', {
  214. from,
  215. to: ZERO_ADDRESS,
  216. value: amount,
  217. });
  218. const finalTotalSupply = await this.token.totalSupply();
  219. const finalFromBalance = await this.token.balanceOf(from);
  220. expect(finalTotalSupply.sub(initialTotalSupply)).to.be.bignumber.equal(amount.neg());
  221. expect(finalFromBalance.sub(initialFromBalance)).to.be.bignumber.equal(amount.neg());
  222. });
  223. }
  224. function shouldBehaveLikeERC777InternalMint(recipient, operator, amount, data, operatorData) {
  225. shouldInternalMintTokens(operator, recipient, new BN('0'), data, operatorData);
  226. shouldInternalMintTokens(operator, recipient, amount, data, operatorData);
  227. it('reverts when minting tokens for the zero address', async function () {
  228. await expectRevert.unspecified(
  229. this.token.$_mint(ZERO_ADDRESS, amount, data, operatorData, true, { from: operator }),
  230. );
  231. });
  232. }
  233. function shouldInternalMintTokens(operator, to, amount, data, operatorData) {
  234. it(`can (internal) mint an amount of ${amount}`, async function () {
  235. const initialTotalSupply = await this.token.totalSupply();
  236. const initialToBalance = await this.token.balanceOf(to);
  237. const receipt = await this.token.$_mint(to, amount, data, operatorData, true, { from: operator });
  238. expectEvent(receipt, 'Minted', {
  239. operator,
  240. to,
  241. amount,
  242. data,
  243. operatorData,
  244. });
  245. expectEvent(receipt, 'Transfer', {
  246. from: ZERO_ADDRESS,
  247. to,
  248. value: amount,
  249. });
  250. const finalTotalSupply = await this.token.totalSupply();
  251. const finalToBalance = await this.token.balanceOf(to);
  252. expect(finalTotalSupply.sub(initialTotalSupply)).to.be.bignumber.equal(amount);
  253. expect(finalToBalance.sub(initialToBalance)).to.be.bignumber.equal(amount);
  254. });
  255. }
  256. function shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData) {
  257. context('when TokensRecipient reverts', function () {
  258. beforeEach(async function () {
  259. await this.tokensRecipientImplementer.setShouldRevertReceive(true);
  260. });
  261. it('send reverts', async function () {
  262. await expectRevert.unspecified(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
  263. });
  264. it('operatorSend reverts', async function () {
  265. await expectRevert.unspecified(
  266. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
  267. );
  268. });
  269. it('mint (internal) reverts', async function () {
  270. await expectRevert.unspecified(
  271. this.token.$_mint(this.recipient, amount, data, operatorData, true, { from: operator }),
  272. );
  273. });
  274. });
  275. context('when TokensRecipient does not revert', function () {
  276. beforeEach(async function () {
  277. await this.tokensRecipientImplementer.setShouldRevertSend(false);
  278. });
  279. it('TokensRecipient receives send data and is called after state mutation', async function () {
  280. const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
  281. const postSenderBalance = await this.token.balanceOf(this.sender);
  282. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  283. await assertTokensReceivedCalled(
  284. this.token,
  285. tx,
  286. this.sender,
  287. this.sender,
  288. this.recipient,
  289. amount,
  290. data,
  291. null,
  292. postSenderBalance,
  293. postRecipientBalance,
  294. );
  295. });
  296. it('TokensRecipient receives operatorSend data and is called after state mutation', async function () {
  297. const { tx } = await this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, {
  298. from: operator,
  299. });
  300. const postSenderBalance = await this.token.balanceOf(this.sender);
  301. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  302. await assertTokensReceivedCalled(
  303. this.token,
  304. tx,
  305. operator,
  306. this.sender,
  307. this.recipient,
  308. amount,
  309. data,
  310. operatorData,
  311. postSenderBalance,
  312. postRecipientBalance,
  313. );
  314. });
  315. it('TokensRecipient receives mint (internal) data and is called after state mutation', async function () {
  316. const { tx } = await this.token.$_mint(this.recipient, amount, data, operatorData, true, { from: operator });
  317. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  318. await assertTokensReceivedCalled(
  319. this.token,
  320. tx,
  321. operator,
  322. ZERO_ADDRESS,
  323. this.recipient,
  324. amount,
  325. data,
  326. operatorData,
  327. new BN('0'),
  328. postRecipientBalance,
  329. );
  330. });
  331. });
  332. }
  333. function shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData) {
  334. context('when TokensSender reverts', function () {
  335. beforeEach(async function () {
  336. await this.tokensSenderImplementer.setShouldRevertSend(true);
  337. });
  338. it('send reverts', async function () {
  339. await expectRevert.unspecified(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
  340. });
  341. it('operatorSend reverts', async function () {
  342. await expectRevert.unspecified(
  343. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
  344. );
  345. });
  346. it('burn reverts', async function () {
  347. await expectRevert.unspecified(burnFromHolder(this.token, this.sender, amount, data));
  348. });
  349. it('operatorBurn reverts', async function () {
  350. await expectRevert.unspecified(
  351. this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator }),
  352. );
  353. });
  354. });
  355. context('when TokensSender does not revert', function () {
  356. beforeEach(async function () {
  357. await this.tokensSenderImplementer.setShouldRevertSend(false);
  358. });
  359. it('TokensSender receives send data and is called before state mutation', async function () {
  360. const preSenderBalance = await this.token.balanceOf(this.sender);
  361. const preRecipientBalance = await this.token.balanceOf(this.recipient);
  362. const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
  363. await assertTokensToSendCalled(
  364. this.token,
  365. tx,
  366. this.sender,
  367. this.sender,
  368. this.recipient,
  369. amount,
  370. data,
  371. null,
  372. preSenderBalance,
  373. preRecipientBalance,
  374. );
  375. });
  376. it('TokensSender receives operatorSend data and is called before state mutation', async function () {
  377. const preSenderBalance = await this.token.balanceOf(this.sender);
  378. const preRecipientBalance = await this.token.balanceOf(this.recipient);
  379. const { tx } = await this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, {
  380. from: operator,
  381. });
  382. await assertTokensToSendCalled(
  383. this.token,
  384. tx,
  385. operator,
  386. this.sender,
  387. this.recipient,
  388. amount,
  389. data,
  390. operatorData,
  391. preSenderBalance,
  392. preRecipientBalance,
  393. );
  394. });
  395. it('TokensSender receives burn data and is called before state mutation', async function () {
  396. const preSenderBalance = await this.token.balanceOf(this.sender);
  397. const { tx } = await burnFromHolder(this.token, this.sender, amount, data, { from: this.sender });
  398. await assertTokensToSendCalled(
  399. this.token,
  400. tx,
  401. this.sender,
  402. this.sender,
  403. ZERO_ADDRESS,
  404. amount,
  405. data,
  406. null,
  407. preSenderBalance,
  408. );
  409. });
  410. it('TokensSender receives operatorBurn data and is called before state mutation', async function () {
  411. const preSenderBalance = await this.token.balanceOf(this.sender);
  412. const { tx } = await this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator });
  413. await assertTokensToSendCalled(
  414. this.token,
  415. tx,
  416. operator,
  417. this.sender,
  418. ZERO_ADDRESS,
  419. amount,
  420. data,
  421. operatorData,
  422. preSenderBalance,
  423. );
  424. });
  425. });
  426. }
  427. function removeBalance(holder) {
  428. beforeEach(async function () {
  429. await this.token.burn(await this.token.balanceOf(holder), '0x', { from: holder });
  430. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  431. });
  432. }
  433. async function assertTokensReceivedCalled(
  434. token,
  435. txHash,
  436. operator,
  437. from,
  438. to,
  439. amount,
  440. data,
  441. operatorData,
  442. fromBalance,
  443. toBalance = '0',
  444. ) {
  445. await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensReceivedCalled', {
  446. operator,
  447. from,
  448. to,
  449. amount,
  450. data,
  451. operatorData,
  452. token: token.address,
  453. fromBalance,
  454. toBalance,
  455. });
  456. }
  457. async function assertTokensToSendCalled(
  458. token,
  459. txHash,
  460. operator,
  461. from,
  462. to,
  463. amount,
  464. data,
  465. operatorData,
  466. fromBalance,
  467. toBalance = '0',
  468. ) {
  469. await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensToSendCalled', {
  470. operator,
  471. from,
  472. to,
  473. amount,
  474. data,
  475. operatorData,
  476. token: token.address,
  477. fromBalance,
  478. toBalance,
  479. });
  480. }
  481. async function sendFromHolder(token, holder, to, amount, data) {
  482. if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
  483. return token.send(to, amount, data, { from: holder });
  484. } else {
  485. // assume holder is ERC777SenderRecipientMock contract
  486. return (await ERC777SenderRecipientMock.at(holder)).send(token.address, to, amount, data);
  487. }
  488. }
  489. async function burnFromHolder(token, holder, amount, data) {
  490. if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
  491. return token.burn(amount, data, { from: holder });
  492. } else {
  493. // assume holder is ERC777SenderRecipientMock contract
  494. return (await ERC777SenderRecipientMock.at(holder)).burn(token.address, amount, data);
  495. }
  496. }
  497. module.exports = {
  498. shouldBehaveLikeERC777DirectSendBurn,
  499. shouldBehaveLikeERC777OperatorSendBurn,
  500. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn,
  501. shouldBehaveLikeERC777InternalMint,
  502. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook,
  503. shouldBehaveLikeERC777SendBurnWithSendHook,
  504. };