ERC777.behavior.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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(this.token.mintInternal(operator, ZERO_ADDRESS, amount, data, operatorData));
  235. });
  236. }
  237. function shouldInternalMintTokens (operator, to, amount, data, operatorData) {
  238. it(`can (internal) mint an amount of ${amount}`, async function () {
  239. const initialTotalSupply = await this.token.totalSupply();
  240. const initialToBalance = await this.token.balanceOf(to);
  241. const { logs } = await this.token.mintInternal(operator, to, amount, data, operatorData);
  242. expectEvent.inLogs(logs, 'Minted', {
  243. operator,
  244. to,
  245. amount,
  246. data,
  247. operatorData,
  248. });
  249. expectEvent.inLogs(logs, 'Transfer', {
  250. from: ZERO_ADDRESS,
  251. to,
  252. value: amount,
  253. });
  254. const finalTotalSupply = await this.token.totalSupply();
  255. const finalToBalance = await this.token.balanceOf(to);
  256. expect(finalTotalSupply.sub(initialTotalSupply)).to.be.bignumber.equal(amount);
  257. expect(finalToBalance.sub(initialToBalance)).to.be.bignumber.equal(amount);
  258. });
  259. }
  260. function shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook (operator, amount, data, operatorData) {
  261. context('when TokensRecipient reverts', function () {
  262. beforeEach(async function () {
  263. await this.tokensRecipientImplementer.setShouldRevertReceive(true);
  264. });
  265. it('send reverts', async function () {
  266. await expectRevert.unspecified(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
  267. });
  268. it('operatorSend reverts', async function () {
  269. await expectRevert.unspecified(
  270. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator })
  271. );
  272. });
  273. it('mint (internal) reverts', async function () {
  274. await expectRevert.unspecified(
  275. this.token.mintInternal(operator, this.recipient, amount, data, operatorData)
  276. );
  277. });
  278. });
  279. context('when TokensRecipient does not revert', function () {
  280. beforeEach(async function () {
  281. await this.tokensRecipientImplementer.setShouldRevertSend(false);
  282. });
  283. it('TokensRecipient receives send data and is called after state mutation', async function () {
  284. const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
  285. const postSenderBalance = await this.token.balanceOf(this.sender);
  286. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  287. await assertTokensReceivedCalled(
  288. this.token,
  289. tx,
  290. this.sender,
  291. this.sender,
  292. this.recipient,
  293. amount,
  294. data,
  295. null,
  296. postSenderBalance,
  297. postRecipientBalance,
  298. );
  299. });
  300. it('TokensRecipient receives operatorSend data and is called after state mutation', async function () {
  301. const { tx } = await this.token.operatorSend(
  302. this.sender, this.recipient, amount, data, operatorData,
  303. { from: operator },
  304. );
  305. const postSenderBalance = await this.token.balanceOf(this.sender);
  306. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  307. await assertTokensReceivedCalled(
  308. this.token,
  309. tx,
  310. operator,
  311. this.sender,
  312. this.recipient,
  313. amount,
  314. data,
  315. operatorData,
  316. postSenderBalance,
  317. postRecipientBalance,
  318. );
  319. });
  320. it('TokensRecipient receives mint (internal) data and is called after state mutation', async function () {
  321. const { tx } = await this.token.mintInternal(
  322. operator, this.recipient, amount, data, operatorData,
  323. );
  324. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  325. await assertTokensReceivedCalled(
  326. this.token,
  327. tx,
  328. operator,
  329. ZERO_ADDRESS,
  330. this.recipient,
  331. amount,
  332. data,
  333. operatorData,
  334. new BN('0'),
  335. postRecipientBalance,
  336. );
  337. });
  338. });
  339. }
  340. function shouldBehaveLikeERC777SendBurnWithSendHook (operator, amount, data, operatorData) {
  341. context('when TokensSender reverts', function () {
  342. beforeEach(async function () {
  343. await this.tokensSenderImplementer.setShouldRevertSend(true);
  344. });
  345. it('send reverts', async function () {
  346. await expectRevert.unspecified(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
  347. });
  348. it('operatorSend reverts', async function () {
  349. await expectRevert.unspecified(
  350. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator })
  351. );
  352. });
  353. it('burn reverts', async function () {
  354. await expectRevert.unspecified(burnFromHolder(this.token, this.sender, amount, data));
  355. });
  356. it('operatorBurn reverts', async function () {
  357. await expectRevert.unspecified(
  358. this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator })
  359. );
  360. });
  361. });
  362. context('when TokensSender does not revert', function () {
  363. beforeEach(async function () {
  364. await this.tokensSenderImplementer.setShouldRevertSend(false);
  365. });
  366. it('TokensSender receives send data and is called before state mutation', async function () {
  367. const preSenderBalance = await this.token.balanceOf(this.sender);
  368. const preRecipientBalance = await this.token.balanceOf(this.recipient);
  369. const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
  370. await assertTokensToSendCalled(
  371. this.token,
  372. tx,
  373. this.sender,
  374. this.sender,
  375. this.recipient,
  376. amount,
  377. data,
  378. null,
  379. preSenderBalance,
  380. preRecipientBalance,
  381. );
  382. });
  383. it('TokensSender receives operatorSend data and is called before state mutation', async function () {
  384. const preSenderBalance = await this.token.balanceOf(this.sender);
  385. const preRecipientBalance = await this.token.balanceOf(this.recipient);
  386. const { tx } = await this.token.operatorSend(
  387. this.sender, this.recipient, amount, data, operatorData,
  388. { from: operator },
  389. );
  390. await assertTokensToSendCalled(
  391. this.token,
  392. tx,
  393. operator,
  394. this.sender,
  395. this.recipient,
  396. amount,
  397. data,
  398. operatorData,
  399. preSenderBalance,
  400. preRecipientBalance,
  401. );
  402. });
  403. it('TokensSender receives burn data and is called before state mutation', async function () {
  404. const preSenderBalance = await this.token.balanceOf(this.sender);
  405. const { tx } = await burnFromHolder(this.token, this.sender, amount, data, { from: this.sender });
  406. await assertTokensToSendCalled(
  407. this.token, tx, this.sender, this.sender, ZERO_ADDRESS, amount, data, null, 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, tx, operator, this.sender, ZERO_ADDRESS, amount, data, operatorData, preSenderBalance
  415. );
  416. });
  417. });
  418. }
  419. function removeBalance (holder) {
  420. beforeEach(async function () {
  421. await this.token.burn(await this.token.balanceOf(holder), '0x', { from: holder });
  422. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  423. });
  424. }
  425. async function assertTokensReceivedCalled (token, txHash, operator, from, to, amount, data, operatorData, fromBalance,
  426. toBalance = '0') {
  427. await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensReceivedCalled', {
  428. operator, from, to, amount, data, operatorData, token: token.address, fromBalance, toBalance,
  429. });
  430. }
  431. async function assertTokensToSendCalled (token, txHash, operator, from, to, amount, data, operatorData, fromBalance,
  432. toBalance = '0') {
  433. await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensToSendCalled', {
  434. operator, from, to, amount, data, operatorData, token: token.address, fromBalance, toBalance,
  435. });
  436. }
  437. async function sendFromHolder (token, holder, to, amount, data) {
  438. if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
  439. return token.send(to, amount, data, { from: holder });
  440. } else {
  441. // assume holder is ERC777SenderRecipientMock contract
  442. return (await ERC777SenderRecipientMock.at(holder)).send(token.address, to, amount, data);
  443. }
  444. }
  445. async function burnFromHolder (token, holder, amount, data) {
  446. if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
  447. return token.burn(amount, data, { from: holder });
  448. } else {
  449. // assume holder is ERC777SenderRecipientMock contract
  450. return (await ERC777SenderRecipientMock.at(holder)).burn(token.address, amount, data);
  451. }
  452. }
  453. module.exports = {
  454. shouldBehaveLikeERC777DirectSendBurn,
  455. shouldBehaveLikeERC777OperatorSendBurn,
  456. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn,
  457. shouldBehaveLikeERC777InternalMint,
  458. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook,
  459. shouldBehaveLikeERC777SendBurnWithSendHook,
  460. };