ERC777.behavior.js 19 KB

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