ERC1155.test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(
  93. this.token.$_burn(tokenHolder, tokenId, mintAmount),
  94. 'ERC1155: insufficient balance for transfer',
  95. );
  96. });
  97. it('reverts when burning more than available tokens', async function () {
  98. await this.token.$_mint(tokenHolder, tokenId, mintAmount, data, { from: operator });
  99. await expectRevert(
  100. this.token.$_burn(tokenHolder, tokenId, mintAmount.addn(1)),
  101. 'ERC1155: insufficient balance for transfer',
  102. );
  103. });
  104. context('with minted-then-burnt tokens', function () {
  105. beforeEach(async function () {
  106. await this.token.$_mint(tokenHolder, tokenId, mintAmount, data);
  107. this.receipt = await this.token.$_burn(tokenHolder, tokenId, burnAmount, { from: operator });
  108. });
  109. it('emits a TransferSingle event', function () {
  110. expectEvent(this.receipt, 'TransferSingle', {
  111. operator,
  112. from: tokenHolder,
  113. to: ZERO_ADDRESS,
  114. id: tokenId,
  115. value: burnAmount,
  116. });
  117. });
  118. it('accounts for both minting and burning', async function () {
  119. expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount.sub(burnAmount));
  120. });
  121. });
  122. });
  123. describe('_burnBatch', function () {
  124. it("reverts when burning the zero account's tokens", async function () {
  125. await expectRevert(
  126. this.token.$_burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts),
  127. 'ERC1155: burn from the zero address',
  128. );
  129. });
  130. it('reverts if length of inputs do not match', async function () {
  131. await expectRevert(
  132. this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts.slice(1)),
  133. 'ERC1155: ids and amounts length mismatch',
  134. );
  135. await expectRevert(
  136. this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds.slice(1), burnAmounts),
  137. 'ERC1155: ids and amounts length mismatch',
  138. );
  139. });
  140. it('reverts when burning a non-existent token id', async function () {
  141. await expectRevert(
  142. this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts),
  143. 'ERC1155: insufficient balance for transfer',
  144. );
  145. });
  146. context('with minted-then-burnt tokens', function () {
  147. beforeEach(async function () {
  148. await this.token.$_mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data);
  149. this.receipt = await this.token.$_burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts, { from: operator });
  150. });
  151. it('emits a TransferBatch event', function () {
  152. expectEvent(this.receipt, 'TransferBatch', {
  153. operator,
  154. from: tokenBatchHolder,
  155. to: ZERO_ADDRESS,
  156. // ids: tokenBatchIds,
  157. // values: burnAmounts,
  158. });
  159. });
  160. it('accounts for both minting and burning', async function () {
  161. const holderBatchBalances = await this.token.balanceOfBatch(
  162. new Array(tokenBatchIds.length).fill(tokenBatchHolder),
  163. tokenBatchIds,
  164. );
  165. for (let i = 0; i < holderBatchBalances.length; i++) {
  166. expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i].sub(burnAmounts[i]));
  167. }
  168. });
  169. });
  170. });
  171. });
  172. describe('ERC1155MetadataURI', function () {
  173. const firstTokenID = new BN('42');
  174. const secondTokenID = new BN('1337');
  175. it('emits no URI event in constructor', async function () {
  176. await expectEvent.notEmitted.inConstruction(this.token, 'URI');
  177. });
  178. it('sets the initial URI for all token types', async function () {
  179. expect(await this.token.uri(firstTokenID)).to.be.equal(initialURI);
  180. expect(await this.token.uri(secondTokenID)).to.be.equal(initialURI);
  181. });
  182. describe('_setURI', function () {
  183. const newURI = 'https://token-cdn-domain/{locale}/{id}.json';
  184. it('emits no URI event', async function () {
  185. const receipt = await this.token.$_setURI(newURI);
  186. expectEvent.notEmitted(receipt, 'URI');
  187. });
  188. it('sets the new URI for all token types', async function () {
  189. await this.token.$_setURI(newURI);
  190. expect(await this.token.uri(firstTokenID)).to.be.equal(newURI);
  191. expect(await this.token.uri(secondTokenID)).to.be.equal(newURI);
  192. });
  193. });
  194. });
  195. });