ERC2981.behavior.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(this.token.$_setDefaultRoyalty(ZERO_ADDRESS, royaltyFraction), 'ERC2981: invalid receiver');
  48. await expectRevert(
  49. this.token.$_setDefaultRoyalty(this.account1, new BN('11000')),
  50. 'ERC2981: royalty fee will exceed salePrice',
  51. );
  52. });
  53. });
  54. describe('token based royalty', function () {
  55. beforeEach(async function () {
  56. await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, royaltyFraction);
  57. });
  58. it('updates royalty amount', async function () {
  59. const newPercentage = new BN('25');
  60. let royalty = new BN((this.salePrice * royaltyFraction) / 10000);
  61. // Initial royalty check
  62. const initInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  63. expect(initInfo[0]).to.be.equal(this.account1);
  64. expect(initInfo[1]).to.be.bignumber.equal(royalty);
  65. // Updated royalty check
  66. await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, newPercentage);
  67. royalty = new BN((this.salePrice * newPercentage) / 10000);
  68. const newInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  69. expect(newInfo[0]).to.be.equal(this.account1);
  70. expect(newInfo[1]).to.be.bignumber.equal(royalty);
  71. });
  72. it('holds different values for different tokens', async function () {
  73. const newPercentage = new BN('20');
  74. await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, newPercentage);
  75. const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  76. const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  77. // must be different even at the same this.salePrice
  78. expect(token1Info[1]).to.not.be.equal(token2Info.royaltyFraction);
  79. });
  80. it('reverts if invalid parameters', async function () {
  81. await expectRevert(
  82. this.token.$_setTokenRoyalty(this.tokenId1, ZERO_ADDRESS, royaltyFraction),
  83. 'ERC2981: Invalid parameters',
  84. );
  85. await expectRevert(
  86. this.token.$_setTokenRoyalty(this.tokenId1, this.account1, new BN('11000')),
  87. 'ERC2981: royalty fee will exceed salePrice',
  88. );
  89. });
  90. it('can reset token after setting royalty', async function () {
  91. const newPercentage = new BN('30');
  92. const royalty = new BN((this.salePrice * newPercentage) / 10000);
  93. await this.token.$_setTokenRoyalty(this.tokenId1, this.account2, newPercentage);
  94. const tokenInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  95. // Tokens must have own information
  96. expect(tokenInfo[1]).to.be.bignumber.equal(royalty);
  97. expect(tokenInfo[0]).to.be.equal(this.account2);
  98. await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, new BN('0'));
  99. const result = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  100. // Token must not share default information
  101. expect(result[0]).to.be.equal(this.account1);
  102. expect(result[1]).to.be.bignumber.equal(new BN('0'));
  103. });
  104. it('can hold default and token royalty information', async function () {
  105. const newPercentage = new BN('30');
  106. const royalty = new BN((this.salePrice * newPercentage) / 10000);
  107. await this.token.$_setTokenRoyalty(this.tokenId2, this.account2, newPercentage);
  108. const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
  109. const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
  110. // Tokens must not have same values
  111. expect(token1Info[1]).to.not.be.bignumber.equal(token2Info[1]);
  112. expect(token1Info[0]).to.not.be.equal(token2Info[0]);
  113. // Updated token must have new values
  114. expect(token2Info[0]).to.be.equal(this.account2);
  115. expect(token2Info[1]).to.be.bignumber.equal(royalty);
  116. });
  117. });
  118. }
  119. module.exports = {
  120. shouldBehaveLikeERC2981,
  121. };