ERC777.behavior.js 19 KB

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