ERC1155.test.js 7.8 KB

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