ERC777.behavior.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. const { BN, constants, expectEvent, shouldFail } = 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 shouldFail.reverting(this.token.send(recipient, balance.addn(1), data, { from: holder }));
  24. });
  25. it('reverts when sending to the zero address', async function () {
  26. await shouldFail.reverting(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 shouldFail.reverting(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 shouldFail.reverting(
  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 shouldFail.reverting(
  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 shouldFail.reverting(
  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 shouldFail.reverting(
  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 shouldFail.reverting(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 shouldFail.reverting(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 shouldFail.reverting(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 shouldFail.reverting(
  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 shouldFail.reverting(
  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 shouldFail.reverting(
  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 shouldFail.reverting(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. if (!operatorCall) {
  153. const { logs } = await this.token.send(to, amount, data, { from });
  154. expectEvent.inLogs(logs, 'Sent', {
  155. operator: from,
  156. from,
  157. to,
  158. amount,
  159. data,
  160. operatorData: null,
  161. });
  162. } else {
  163. const { logs } = await this.token.operatorSend(from, to, amount, data, operatorData, { from: operator });
  164. expectEvent.inLogs(logs, 'Sent', {
  165. operator,
  166. from,
  167. to,
  168. amount,
  169. data,
  170. operatorData,
  171. });
  172. }
  173. const finalTotalSupply = await this.token.totalSupply();
  174. const finalFromBalance = await this.token.balanceOf(from);
  175. const finalToBalance = await this.token.balanceOf(to);
  176. finalTotalSupply.should.be.bignumber.equal(initialTotalSupply);
  177. finalToBalance.sub(initialToBalance).should.be.bignumber.equal(amount);
  178. finalFromBalance.sub(initialFromBalance).should.be.bignumber.equal(amount.neg());
  179. });
  180. }
  181. function shouldDirectBurnTokens (from, amount, data) {
  182. shouldBurnTokens(from, null, amount, data, null);
  183. }
  184. function shouldOperatorBurnTokens (from, operator, amount, data, operatorData) {
  185. shouldBurnTokens(from, operator, amount, data, operatorData);
  186. }
  187. function shouldBurnTokens (from, operator, amount, data, operatorData) {
  188. const operatorCall = operator !== null;
  189. it(`${operatorCall ? 'operator ' : ''}can burn an amount of ${amount}`, async function () {
  190. const initialTotalSupply = await this.token.totalSupply();
  191. const initialFromBalance = await this.token.balanceOf(from);
  192. if (!operatorCall) {
  193. const { logs } = await this.token.burn(amount, data, { from });
  194. expectEvent.inLogs(logs, 'Burned', {
  195. operator: from,
  196. from,
  197. amount,
  198. data,
  199. operatorData: null,
  200. });
  201. } else {
  202. const { logs } = await this.token.operatorBurn(from, amount, data, operatorData, { from: operator });
  203. expectEvent.inLogs(logs, 'Burned', {
  204. operator,
  205. from,
  206. amount,
  207. data,
  208. operatorData,
  209. });
  210. }
  211. const finalTotalSupply = await this.token.totalSupply();
  212. const finalFromBalance = await this.token.balanceOf(from);
  213. finalTotalSupply.sub(initialTotalSupply).should.be.bignumber.equal(amount.neg());
  214. finalFromBalance.sub(initialFromBalance).should.be.bignumber.equal(amount.neg());
  215. });
  216. }
  217. function shouldBehaveLikeERC777InternalMint (recipient, operator, amount, data, operatorData) {
  218. shouldInternalMintTokens(operator, recipient, new BN('0'), data, operatorData);
  219. shouldInternalMintTokens(operator, recipient, amount, data, operatorData);
  220. it('reverts when minting tokens for the zero address', async function () {
  221. await shouldFail.reverting(this.token.mintInternal(operator, ZERO_ADDRESS, amount, data, operatorData));
  222. });
  223. }
  224. function shouldInternalMintTokens (operator, to, amount, data, operatorData) {
  225. it(`can (internal) mint an amount of ${amount}`, async function () {
  226. const initialTotalSupply = await this.token.totalSupply();
  227. const initialToBalance = await this.token.balanceOf(to);
  228. const { logs } = await this.token.mintInternal(operator, to, amount, data, operatorData);
  229. expectEvent.inLogs(logs, 'Minted', {
  230. operator,
  231. to,
  232. amount,
  233. data,
  234. operatorData,
  235. });
  236. const finalTotalSupply = await this.token.totalSupply();
  237. const finalToBalance = await this.token.balanceOf(to);
  238. finalTotalSupply.sub(initialTotalSupply).should.be.bignumber.equal(amount);
  239. finalToBalance.sub(initialToBalance).should.be.bignumber.equal(amount);
  240. });
  241. }
  242. function shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook (operator, amount, data, operatorData) {
  243. context('when TokensRecipient reverts', function () {
  244. beforeEach(async function () {
  245. await this.tokensRecipientImplementer.setShouldRevertReceive(true);
  246. });
  247. it('send reverts', async function () {
  248. await shouldFail.reverting(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
  249. });
  250. it('operatorSend reverts', async function () {
  251. await shouldFail.reverting(
  252. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator })
  253. );
  254. });
  255. it('mint (internal) reverts', async function () {
  256. await shouldFail.reverting(
  257. this.token.mintInternal(operator, this.recipient, amount, data, operatorData)
  258. );
  259. });
  260. });
  261. context('when TokensRecipient does not revert', function () {
  262. beforeEach(async function () {
  263. await this.tokensRecipientImplementer.setShouldRevertSend(false);
  264. });
  265. it('TokensRecipient receives send data and is called after state mutation', async function () {
  266. const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
  267. const postSenderBalance = await this.token.balanceOf(this.sender);
  268. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  269. await assertTokensReceivedCalled(
  270. this.token,
  271. tx,
  272. this.sender,
  273. this.sender,
  274. this.recipient,
  275. amount,
  276. data,
  277. null,
  278. postSenderBalance,
  279. postRecipientBalance,
  280. );
  281. });
  282. it('TokensRecipient receives operatorSend data and is called after state mutation', async function () {
  283. const { tx } = await this.token.operatorSend(
  284. this.sender, this.recipient, amount, data, operatorData,
  285. { from: operator },
  286. );
  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. operator,
  293. this.sender,
  294. this.recipient,
  295. amount,
  296. data,
  297. operatorData,
  298. postSenderBalance,
  299. postRecipientBalance,
  300. );
  301. });
  302. it('TokensRecipient receives mint (internal) data and is called after state mutation', async function () {
  303. const { tx } = await this.token.mintInternal(
  304. operator, this.recipient, amount, data, operatorData,
  305. );
  306. const postRecipientBalance = await this.token.balanceOf(this.recipient);
  307. await assertTokensReceivedCalled(
  308. this.token,
  309. tx,
  310. operator,
  311. ZERO_ADDRESS,
  312. this.recipient,
  313. amount,
  314. data,
  315. operatorData,
  316. new BN('0'),
  317. postRecipientBalance,
  318. );
  319. });
  320. });
  321. }
  322. function shouldBehaveLikeERC777SendBurnWithSendHook (operator, amount, data, operatorData) {
  323. context('when TokensSender reverts', function () {
  324. beforeEach(async function () {
  325. await this.tokensSenderImplementer.setShouldRevertSend(true);
  326. });
  327. it('send reverts', async function () {
  328. await shouldFail.reverting(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
  329. });
  330. it('operatorSend reverts', async function () {
  331. await shouldFail.reverting(
  332. this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator })
  333. );
  334. });
  335. it('burn reverts', async function () {
  336. await shouldFail.reverting(burnFromHolder(this.token, this.sender, amount, data));
  337. });
  338. it('operatorBurn reverts', async function () {
  339. await shouldFail.reverting(
  340. this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator })
  341. );
  342. });
  343. });
  344. context('when TokensSender does not revert', function () {
  345. beforeEach(async function () {
  346. await this.tokensSenderImplementer.setShouldRevertSend(false);
  347. });
  348. it('TokensSender receives send data and is called before state mutation', async function () {
  349. const preSenderBalance = await this.token.balanceOf(this.sender);
  350. const preRecipientBalance = await this.token.balanceOf(this.recipient);
  351. const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
  352. await assertTokensToSendCalled(
  353. this.token,
  354. tx,
  355. this.sender,
  356. this.sender,
  357. this.recipient,
  358. amount,
  359. data,
  360. null,
  361. preSenderBalance,
  362. preRecipientBalance,
  363. );
  364. });
  365. it('TokensSender receives operatorSend 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 this.token.operatorSend(
  369. this.sender, this.recipient, amount, data, operatorData,
  370. { from: operator },
  371. );
  372. await assertTokensToSendCalled(
  373. this.token,
  374. tx,
  375. operator,
  376. this.sender,
  377. this.recipient,
  378. amount,
  379. data,
  380. operatorData,
  381. preSenderBalance,
  382. preRecipientBalance,
  383. );
  384. });
  385. it('TokensSender receives burn data and is called before state mutation', async function () {
  386. const preSenderBalance = await this.token.balanceOf(this.sender);
  387. const { tx } = await burnFromHolder(this.token, this.sender, amount, data, { from: this.sender });
  388. await assertTokensToSendCalled(
  389. this.token, tx, this.sender, this.sender, ZERO_ADDRESS, amount, data, null, preSenderBalance
  390. );
  391. });
  392. it('TokensSender receives operatorBurn data and is called before state mutation', async function () {
  393. const preSenderBalance = await this.token.balanceOf(this.sender);
  394. const { tx } = await this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator });
  395. await assertTokensToSendCalled(
  396. this.token, tx, operator, this.sender, ZERO_ADDRESS, amount, data, operatorData, preSenderBalance
  397. );
  398. });
  399. });
  400. }
  401. function removeBalance (holder) {
  402. beforeEach(async function () {
  403. await this.token.burn(await this.token.balanceOf(holder), '0x', { from: holder });
  404. (await this.token.balanceOf(holder)).should.be.bignumber.equal('0');
  405. });
  406. }
  407. async function assertTokensReceivedCalled (token, txHash, operator, from, to, amount, data, operatorData, fromBalance,
  408. toBalance = '0') {
  409. await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensReceivedCalled', {
  410. operator, from, to, amount, data, operatorData, token: token.address, fromBalance, toBalance,
  411. });
  412. }
  413. async function assertTokensToSendCalled (token, txHash, operator, from, to, amount, data, operatorData, fromBalance,
  414. toBalance = '0') {
  415. await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensToSendCalled', {
  416. operator, from, to, amount, data, operatorData, token: token.address, fromBalance, toBalance,
  417. });
  418. }
  419. async function sendFromHolder (token, holder, to, amount, data) {
  420. if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
  421. return token.send(to, amount, data, { from: holder });
  422. } else {
  423. // assume holder is ERC777SenderRecipientMock contract
  424. return (await ERC777SenderRecipientMock.at(holder)).send(token.address, to, amount, data);
  425. }
  426. }
  427. async function burnFromHolder (token, holder, amount, data) {
  428. if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
  429. return token.burn(amount, data, { from: holder });
  430. } else {
  431. // assume holder is ERC777SenderRecipientMock contract
  432. return (await ERC777SenderRecipientMock.at(holder)).burn(token.address, amount, data);
  433. }
  434. }
  435. module.exports = {
  436. shouldBehaveLikeERC777DirectSendBurn,
  437. shouldBehaveLikeERC777OperatorSendBurn,
  438. shouldBehaveLikeERC777UnauthorizedOperatorSendBurn,
  439. shouldDirectSendTokens,
  440. shouldDirectBurnTokens,
  441. shouldBehaveLikeERC777InternalMint,
  442. shouldInternalMintTokens,
  443. shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook,
  444. shouldBehaveLikeERC777SendBurnWithSendHook,
  445. };