ERC2981.behavior.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { ZERO_ADDRESS } = constants;
  4. const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
  5. function shouldBehaveLikeERC2981 () {
  6. const royaltyFraction = new BN('10');
  7. shouldSupportInterfaces(['ERC2981']);
  8. describe('default royalty', function () {
  9. beforeEach(async function () {
  10. await this.token.setDefaultRoyalty(this.account1, royaltyFraction);
  11. });
  12. it('checks royalty is set', async function () {
  13. const royalty = new BN((this.salePrice * royaltyFraction) / 10000);
  14. const initInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  15. expect(initInfo[0]).to.be.equal(this.account1);
  16. expect(initInfo[1]).to.be.bignumber.equal(royalty);
  17. });
  18. it('updates royalty amount', async function () {
  19. const newPercentage = new BN('25');
  20. // Updated royalty check
  21. await this.token.setDefaultRoyalty(this.account1, newPercentage);
  22. const royalty = new BN((this.salePrice * newPercentage) / 10000);
  23. const newInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  24. expect(newInfo[0]).to.be.equal(this.account1);
  25. expect(newInfo[1]).to.be.bignumber.equal(royalty);
  26. });
  27. it('holds same royalty value for different tokens', async function () {
  28. const newPercentage = new BN('20');
  29. await this.token.setDefaultRoyalty(this.account1, newPercentage);
  30. const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  31. const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  32. expect(token1Info[1]).to.be.bignumber.equal(token2Info[1]);
  33. });
  34. it('Remove royalty information', async function () {
  35. const newValue = new BN('0');
  36. await this.token.deleteDefaultRoyalty();
  37. const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  38. const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  39. // Test royalty info is still persistent across all tokens
  40. expect(token1Info[0]).to.be.bignumber.equal(token2Info[0]);
  41. expect(token1Info[1]).to.be.bignumber.equal(token2Info[1]);
  42. // Test information was deleted
  43. expect(token1Info[0]).to.be.equal(ZERO_ADDRESS);
  44. expect(token1Info[1]).to.be.bignumber.equal(newValue);
  45. });
  46. it('reverts if invalid parameters', async function () {
  47. await expectRevert(
  48. this.token.setDefaultRoyalty(ZERO_ADDRESS, royaltyFraction),
  49. 'ERC2981: invalid receiver',
  50. );
  51. await expectRevert(
  52. this.token.setDefaultRoyalty(this.account1, new BN('11000')),
  53. 'ERC2981: royalty fee will exceed salePrice',
  54. );
  55. });
  56. });
  57. describe('token based royalty', function () {
  58. beforeEach(async function () {
  59. await this.token.setTokenRoyalty(this.tokenId1, this.account1, royaltyFraction);
  60. });
  61. it('updates royalty amount', async function () {
  62. const newPercentage = new BN('25');
  63. let royalty = new BN((this.salePrice * royaltyFraction) / 10000);
  64. // Initial royalty check
  65. const initInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  66. expect(initInfo[0]).to.be.equal(this.account1);
  67. expect(initInfo[1]).to.be.bignumber.equal(royalty);
  68. // Updated royalty check
  69. await this.token.setTokenRoyalty(this.tokenId1, this.account1, newPercentage);
  70. royalty = new BN((this.salePrice * newPercentage) / 10000);
  71. const newInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  72. expect(newInfo[0]).to.be.equal(this.account1);
  73. expect(newInfo[1]).to.be.bignumber.equal(royalty);
  74. });
  75. it('holds different values for different tokens', async function () {
  76. const newPercentage = new BN('20');
  77. await this.token.setTokenRoyalty(this.tokenId2, this.account1, newPercentage);
  78. const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  79. const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  80. // must be different even at the same this.salePrice
  81. expect(token1Info[1]).to.not.be.equal(token2Info.royaltyFraction);
  82. });
  83. it('reverts if invalid parameters', async function () {
  84. await expectRevert(
  85. this.token.setTokenRoyalty(this.tokenId1, ZERO_ADDRESS, royaltyFraction),
  86. 'ERC2981: Invalid parameters',
  87. );
  88. await expectRevert(
  89. this.token.setTokenRoyalty(this.tokenId1, this.account1, new BN('11000')),
  90. 'ERC2981: royalty fee will exceed salePrice',
  91. );
  92. });
  93. it('can reset token after setting royalty', async function () {
  94. const newPercentage = new BN('30');
  95. const royalty = new BN((this.salePrice * newPercentage) / 10000);
  96. await this.token.setTokenRoyalty(this.tokenId1, this.account2, newPercentage);
  97. const tokenInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  98. // Tokens must have own information
  99. expect(tokenInfo[1]).to.be.bignumber.equal(royalty);
  100. expect(tokenInfo[0]).to.be.equal(this.account2);
  101. await this.token.setTokenRoyalty(this.tokenId2, this.account1, new BN('0'));
  102. const result = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  103. // Token must not share default information
  104. expect(result[0]).to.be.equal(this.account1);
  105. expect(result[1]).to.be.bignumber.equal(new BN('0'));
  106. });
  107. it('can hold default and token royalty information', async function () {
  108. const newPercentage = new BN('30');
  109. const royalty = new BN((this.salePrice * newPercentage) / 10000);
  110. await this.token.setTokenRoyalty(this.tokenId2, this.account2, newPercentage);
  111. const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  112. const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  113. // Tokens must not have same values
  114. expect(token1Info[1]).to.not.be.bignumber.equal(token2Info[1]);
  115. expect(token1Info[0]).to.not.be.equal(token2Info[0]);
  116. // Updated token must have new values
  117. expect(token2Info[0]).to.be.equal(this.account2);
  118. expect(token2Info[1]).to.be.bignumber.equal(royalty);
  119. });
  120. });
  121. }
  122. module.exports = {
  123. shouldBehaveLikeERC2981,
  124. };