ERC1155.test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior');
  5. const ERC1155Mock = artifacts.require('$ERC1155');
  6. contract('ERC1155', function (accounts) {
  7. const [operator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts;
  8. const initialURI = 'https://token-cdn-domain/{id}.json';
  9. beforeEach(async function () {
  10. this.token = await ERC1155Mock.new(initialURI);
  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 = '0x12345678';
  21. describe('_mint', function () {
  22. it('reverts with a zero 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. this.receipt = await this.token.$_mint(tokenHolder, tokenId, mintAmount, data, { from: operator });
  31. });
  32. it('emits a TransferSingle event', function () {
  33. expectEvent(this.receipt, 'TransferSingle', {
  34. operator,
  35. from: ZERO_ADDRESS,
  36. to: tokenHolder,
  37. id: tokenId,
  38. value: mintAmount,
  39. });
  40. });
  41. it('credits the minted amount of tokens', async function () {
  42. expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount);
  43. });
  44. });
  45. });
  46. describe('_mintBatch', function () {
  47. it('reverts with a zero destination address', async function () {
  48. await expectRevert(
  49. this.token.$_mintBatch(ZERO_ADDRESS, tokenBatchIds, mintAmounts, data),
  50. 'ERC1155: mint to the zero address',
  51. );
  52. });
  53. it('reverts if length of inputs do not match', async function () {
  54. await expectRevert(
  55. this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts.slice(1), data),
  56. 'ERC1155: ids and amounts length mismatch',
  57. );
  58. await expectRevert(
  59. this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds.slice(1), mintAmounts, data),
  60. 'ERC1155: ids and amounts length mismatch',
  61. );
  62. });
  63. context('with minted batch of tokens', function () {
  64. beforeEach(async function () {
  65. this.receipt = await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data, {
  66. from: operator,
  67. });
  68. });
  69. it('emits a TransferBatch event', function () {
  70. expectEvent(this.receipt, 'TransferBatch', {
  71. operator,
  72. from: ZERO_ADDRESS,
  73. to: tokenBatchHolder,
  74. });
  75. });
  76. it('credits the minted batch of tokens', async function () {
  77. const holderBatchBalances = await this.token.balanceOfBatch(
  78. new Array(tokenBatchIds.length).fill(tokenBatchHolder),
  79. tokenBatchIds,
  80. );
  81. for (let i = 0; i < holderBatchBalances.length; i++) {
  82. expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i]);
  83. }
  84. });
  85. });
  86. });
  87. describe('_burn', function () {
  88. it("reverts when burning the zero account's tokens", async function () {
  89. await expectRevert(this.token.$_burn(ZERO_ADDRESS, tokenId, mintAmount), 'ERC1155: burn from the zero address');
  90. });
  91. it('reverts when burning a non-existent token id', async function () {
  92. await expectRevert(this.token.$_burn(tokenHolder, tokenId, mintAmount), 'ERC1155: burn amount exceeds balance');
  93. });
  94. it('reverts when burning more than available tokens', async function () {
  95. await this.token.$_mint(tokenHolder, tokenId, mintAmount, data, { from: operator });
  96. await expectRevert(
  97. this.token.$_burn(tokenHolder, tokenId, mintAmount.addn(1)),
  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. this.receipt = await this.token.$_burn(tokenHolder, tokenId, burnAmount, { from: operator });
  105. });
  106. it('emits a TransferSingle event', function () {
  107. expectEvent(this.receipt, 'TransferSingle', {
  108. operator,
  109. from: tokenHolder,
  110. to: ZERO_ADDRESS,
  111. id: tokenId,
  112. value: burnAmount,
  113. });
  114. });
  115. it('accounts for both minting and burning', async function () {
  116. expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount.sub(burnAmount));
  117. });
  118. });
  119. });
  120. describe('_burnBatch', function () {
  121. it("reverts when burning the zero account's tokens", async function () {
  122. await expectRevert(
  123. this.token.$_burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts),
  124. 'ERC1155: burn from the zero address',
  125. );
  126. });
  127. it('reverts if length of inputs do not match', async function () {
  128. await expectRevert(
  129. this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts.slice(1)),
  130. 'ERC1155: ids and amounts length mismatch',
  131. );
  132. await expectRevert(
  133. this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds.slice(1), burnAmounts),
  134. 'ERC1155: ids and amounts length mismatch',
  135. );
  136. });
  137. it('reverts when burning a non-existent token id', async function () {
  138. await expectRevert(
  139. this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts),
  140. 'ERC1155: burn amount exceeds balance',
  141. );
  142. });
  143. context('with minted-then-burnt tokens', function () {
  144. beforeEach(async function () {
  145. await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data);
  146. this.receipt = await this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts, { from: operator });
  147. });
  148. it('emits a TransferBatch event', function () {
  149. expectEvent(this.receipt, 'TransferBatch', {
  150. operator,
  151. from: tokenBatchHolder,
  152. to: ZERO_ADDRESS,
  153. // ids: tokenBatchIds,
  154. // values: burnAmounts,
  155. });
  156. });
  157. it('accounts for both minting and burning', async function () {
  158. const holderBatchBalances = await this.token.balanceOfBatch(
  159. new Array(tokenBatchIds.length).fill(tokenBatchHolder),
  160. tokenBatchIds,
  161. );
  162. for (let i = 0; i < holderBatchBalances.length; i++) {
  163. expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i].sub(burnAmounts[i]));
  164. }
  165. });
  166. });
  167. });
  168. });
  169. describe('ERC1155MetadataURI', function () {
  170. const firstTokenID = new BN('42');
  171. const secondTokenID = new BN('1337');
  172. it('emits no URI event in constructor', async function () {
  173. await expectEvent.notEmitted.inConstruction(this.token, 'URI');
  174. });
  175. it('sets the initial URI for all token types', async function () {
  176. expect(await this.token.uri(firstTokenID)).to.be.equal(initialURI);
  177. expect(await this.token.uri(secondTokenID)).to.be.equal(initialURI);
  178. });
  179. describe('_setURI', function () {
  180. const newURI = 'https://token-cdn-domain/{locale}/{id}.json';
  181. it('emits no URI event', async function () {
  182. const receipt = await this.token.$_setURI(newURI);
  183. expectEvent.notEmitted(receipt, 'URI');
  184. });
  185. it('sets the new URI for all token types', async function () {
  186. await this.token.$_setURI(newURI);
  187. expect(await this.token.uri(firstTokenID)).to.be.equal(newURI);
  188. expect(await this.token.uri(secondTokenID)).to.be.equal(newURI);
  189. });
  190. });
  191. });
  192. });