ERC777.behavior.js 19 KB

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