ERC777.behavior.js 19 KB

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