ERC1155.test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const { accounts, contract } = 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 { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior');
  6. const ERC1155Mock = contract.fromArtifact('ERC1155Mock');
  7. describe('ERC1155', function () {
  8. const [creator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts;
  9. beforeEach(async function () {
  10. this.token = await ERC1155Mock.new({ from: creator });
  11. });
  12. shouldBehaveLikeERC1155(otherAccounts);
  13. describe('internal functions', function () {
  14. const tokenId = new BN(1990);
  15. const mintAmount = new BN(9001);
  16. const burnAmount = new BN(3000);
  17. const tokenBatchIds = [new BN(2000), new BN(2010), new BN(2020)];
  18. const mintAmounts = [new BN(5000), new BN(10000), new BN(42195)];
  19. const burnAmounts = [new BN(5000), new BN(9001), new BN(195)];
  20. const data = '0xcafebabe';
  21. describe('_mint(address, uint256, uint256, bytes memory)', function () {
  22. it('reverts with a null destination address', async function () {
  23. await expectRevert(
  24. this.token.mint(ZERO_ADDRESS, tokenId, mintAmount, data),
  25. 'ERC1155: mint to the zero address'
  26. );
  27. });
  28. context('with minted tokens', function () {
  29. beforeEach(async function () {
  30. ({ logs: this.logs } = await this.token.mint(
  31. tokenHolder,
  32. tokenId,
  33. mintAmount,
  34. data,
  35. { from: creator }
  36. ));
  37. });
  38. it('emits a TransferSingle event', function () {
  39. expectEvent.inLogs(this.logs, 'TransferSingle', {
  40. operator: creator,
  41. from: ZERO_ADDRESS,
  42. to: tokenHolder,
  43. id: tokenId,
  44. value: mintAmount,
  45. });
  46. });
  47. it('credits the minted amount of tokens', async function () {
  48. expect(await this.token.balanceOf(
  49. tokenHolder,
  50. tokenId
  51. )).to.be.bignumber.equal(mintAmount);
  52. });
  53. });
  54. });
  55. describe('_mintBatch(address, uint256[] memory, uint256[] memory, bytes memory)', function () {
  56. it('reverts with a null destination address', async function () {
  57. await expectRevert(
  58. this.token.mintBatch(ZERO_ADDRESS, tokenBatchIds, mintAmounts, data),
  59. 'ERC1155: batch mint to the zero address'
  60. );
  61. });
  62. it('reverts if length of inputs do not match', async function () {
  63. await expectRevert(
  64. this.token.mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts.slice(1), data),
  65. 'ERC1155: minted IDs and values must have same lengths'
  66. );
  67. });
  68. context('with minted batch of tokens', function () {
  69. beforeEach(async function () {
  70. ({ logs: this.logs } = await this.token.mintBatch(
  71. tokenBatchHolder,
  72. tokenBatchIds,
  73. mintAmounts,
  74. data,
  75. { from: creator }
  76. ));
  77. });
  78. it('emits a TransferBatch event', function () {
  79. expectEvent.inLogs(this.logs, 'TransferBatch', {
  80. operator: creator,
  81. from: ZERO_ADDRESS,
  82. to: tokenBatchHolder,
  83. // ids: tokenBatchIds,
  84. // values: mintAmounts,
  85. });
  86. });
  87. it('credits the minted batch of tokens', async function () {
  88. const holderBatchBalances = await this.token.balanceOfBatch(
  89. new Array(tokenBatchIds.length).fill(tokenBatchHolder),
  90. tokenBatchIds
  91. );
  92. for (let i = 0; i < holderBatchBalances.length; i++) {
  93. expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i]);
  94. }
  95. });
  96. });
  97. });
  98. describe('_burn(address, uint256, uint256)', function () {
  99. it('reverts when burning the zero account\'s tokens', async function () {
  100. await expectRevert(
  101. this.token.burn(ZERO_ADDRESS, tokenId, mintAmount),
  102. 'ERC1155: attempting to burn tokens on zero account'
  103. );
  104. });
  105. it('reverts when burning a non-existent token id', async function () {
  106. await expectRevert(
  107. this.token.burn(tokenHolder, tokenId, mintAmount),
  108. 'ERC1155: attempting to burn more than balance'
  109. );
  110. });
  111. context('with minted-then-burnt tokens', function () {
  112. beforeEach(async function () {
  113. await this.token.mint(tokenHolder, tokenId, mintAmount, data);
  114. ({ logs: this.logs } = await this.token.burn(
  115. tokenHolder,
  116. tokenId,
  117. burnAmount,
  118. { from: creator }
  119. ));
  120. });
  121. it('emits a TransferSingle event', function () {
  122. expectEvent.inLogs(this.logs, 'TransferSingle', {
  123. operator: creator,
  124. from: tokenHolder,
  125. to: ZERO_ADDRESS,
  126. id: tokenId,
  127. value: burnAmount,
  128. });
  129. });
  130. it('accounts for both minting and burning', async function () {
  131. expect(await this.token.balanceOf(
  132. tokenHolder,
  133. tokenId
  134. )).to.be.bignumber.equal(mintAmount.sub(burnAmount));
  135. });
  136. });
  137. });
  138. describe('_burnBatch(address, uint256[] memory, uint256[] memory)', function () {
  139. it('reverts when burning the zero account\'s tokens', async function () {
  140. await expectRevert(
  141. this.token.burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts),
  142. 'ERC1155: attempting to burn batch of tokens on zero account'
  143. );
  144. });
  145. it('reverts if length of inputs do not match', async function () {
  146. await expectRevert(
  147. this.token.burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts.slice(1)),
  148. 'ERC1155: burnt IDs and values must have same lengths'
  149. );
  150. });
  151. it('reverts when burning a non-existent token id', async function () {
  152. await expectRevert(
  153. this.token.burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts),
  154. 'ERC1155: attempting to burn more than balance for some token'
  155. );
  156. });
  157. context('with minted-then-burnt tokens', function () {
  158. beforeEach(async function () {
  159. await this.token.mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data);
  160. ({ logs: this.logs } = await this.token.burnBatch(
  161. tokenBatchHolder,
  162. tokenBatchIds,
  163. burnAmounts,
  164. { from: creator }
  165. ));
  166. });
  167. it('emits a TransferBatch event', function () {
  168. expectEvent.inLogs(this.logs, 'TransferBatch', {
  169. operator: creator,
  170. from: tokenBatchHolder,
  171. to: ZERO_ADDRESS,
  172. // ids: tokenBatchIds,
  173. // values: burnAmounts,
  174. });
  175. });
  176. it('accounts for both minting and burning', async function () {
  177. const holderBatchBalances = await this.token.balanceOfBatch(
  178. new Array(tokenBatchIds.length).fill(tokenBatchHolder),
  179. tokenBatchIds
  180. );
  181. for (let i = 0; i < holderBatchBalances.length; i++) {
  182. expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i].sub(burnAmounts[i]));
  183. }
  184. });
  185. });
  186. });
  187. });
  188. });