ERC2981.behavior.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
  4. function shouldBehaveLikeERC2981() {
  5. const royaltyFraction = 10n;
  6. shouldSupportInterfaces(['ERC2981']);
  7. describe('default royalty', function () {
  8. beforeEach(async function () {
  9. await this.token.$_setDefaultRoyalty(this.account1, royaltyFraction);
  10. });
  11. it('checks royalty is set', async function () {
  12. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
  13. this.account1.address,
  14. (this.salePrice * royaltyFraction) / 10_000n,
  15. ]);
  16. });
  17. it('updates royalty amount', async function () {
  18. const newFraction = 25n;
  19. await this.token.$_setDefaultRoyalty(this.account1, newFraction);
  20. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
  21. this.account1.address,
  22. (this.salePrice * newFraction) / 10_000n,
  23. ]);
  24. });
  25. it('holds same royalty value for different tokens', async function () {
  26. const newFraction = 20n;
  27. await this.token.$_setDefaultRoyalty(this.account1, newFraction);
  28. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal(
  29. await this.token.royaltyInfo(this.tokenId2, this.salePrice),
  30. );
  31. });
  32. it('Remove royalty information', async function () {
  33. const newValue = 0n;
  34. await this.token.$_deleteDefaultRoyalty();
  35. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([ethers.ZeroAddress, newValue]);
  36. expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([ethers.ZeroAddress, newValue]);
  37. });
  38. it('reverts if invalid parameters', async function () {
  39. const royaltyDenominator = await this.token.$_feeDenominator();
  40. await expect(this.token.$_setDefaultRoyalty(ethers.ZeroAddress, royaltyFraction))
  41. .to.be.revertedWithCustomError(this.token, 'ERC2981InvalidDefaultRoyaltyReceiver')
  42. .withArgs(ethers.ZeroAddress);
  43. const anotherRoyaltyFraction = 11000n;
  44. await expect(this.token.$_setDefaultRoyalty(this.account1, anotherRoyaltyFraction))
  45. .to.be.revertedWithCustomError(this.token, 'ERC2981InvalidDefaultRoyalty')
  46. .withArgs(anotherRoyaltyFraction, royaltyDenominator);
  47. });
  48. });
  49. describe('token based royalty', function () {
  50. beforeEach(async function () {
  51. await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, royaltyFraction);
  52. });
  53. it('updates royalty amount', async function () {
  54. const newFraction = 25n;
  55. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
  56. this.account1.address,
  57. (this.salePrice * royaltyFraction) / 10_000n,
  58. ]);
  59. await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, newFraction);
  60. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
  61. this.account1.address,
  62. (this.salePrice * newFraction) / 10_000n,
  63. ]);
  64. });
  65. it('holds different values for different tokens', async function () {
  66. const newFraction = 20n;
  67. await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, newFraction);
  68. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.not.deep.equal(
  69. await this.token.royaltyInfo(this.tokenId2, this.salePrice),
  70. );
  71. });
  72. it('reverts if invalid parameters', async function () {
  73. const royaltyDenominator = await this.token.$_feeDenominator();
  74. await expect(this.token.$_setTokenRoyalty(this.tokenId1, ethers.ZeroAddress, royaltyFraction))
  75. .to.be.revertedWithCustomError(this.token, 'ERC2981InvalidTokenRoyaltyReceiver')
  76. .withArgs(this.tokenId1, ethers.ZeroAddress);
  77. const anotherRoyaltyFraction = 11000n;
  78. await expect(this.token.$_setTokenRoyalty(this.tokenId1, this.account1, anotherRoyaltyFraction))
  79. .to.be.revertedWithCustomError(this.token, 'ERC2981InvalidTokenRoyalty')
  80. .withArgs(this.tokenId1, anotherRoyaltyFraction, royaltyDenominator);
  81. });
  82. it('can reset token after setting royalty', async function () {
  83. const newFraction = 30n;
  84. await this.token.$_setTokenRoyalty(this.tokenId1, this.account2, newFraction);
  85. // Tokens must have own information
  86. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
  87. this.account2.address,
  88. (this.salePrice * newFraction) / 10_000n,
  89. ]);
  90. await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, 0n);
  91. // Token must not share default information
  92. expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([this.account1.address, 0n]);
  93. });
  94. it('can hold default and token royalty information', async function () {
  95. const newFraction = 30n;
  96. await this.token.$_setTokenRoyalty(this.tokenId2, this.account2, newFraction);
  97. // Tokens must not have same values
  98. expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.not.deep.equal([
  99. this.account2.address,
  100. (this.salePrice * newFraction) / 10_000n,
  101. ]);
  102. // Updated token must have new values
  103. expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([
  104. this.account2.address,
  105. (this.salePrice * newFraction) / 10_000n,
  106. ]);
  107. });
  108. });
  109. }
  110. module.exports = {
  111. shouldBehaveLikeERC2981,
  112. };