Skip to content

Commit

Permalink
Update 2981 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulissaDantes committed Dec 10, 2021
1 parent 3fa2485 commit 7fb8bfd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 67 deletions.
1 change: 0 additions & 1 deletion contracts/interfaces/draft-IERC2981.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
pragma solidity ^0.8.0;

import "../token/ERC721/extensions/draft-IERC721Royalty.sol";

8 changes: 6 additions & 2 deletions contracts/mocks/ERC721RoyaltyMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import "../token/ERC721/extensions/draft-ERC721Royalty.sol";
contract ERC721RoyaltyMock is ERC721Royalty {
bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;

constructor () {
constructor() {
_registerInterface(_INTERFACE_ID_ERC2981);
}

function setTokenRoyalty(uint256 tokenId, address recipient, uint256 value) public {
function setTokenRoyalty(
uint256 tokenId,
address recipient,
uint256 value
) public {
_setTokenRoyalty(tokenId, recipient, value);
}

Expand Down
48 changes: 22 additions & 26 deletions contracts/token/ERC721/extensions/draft-ERC721Royalty.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,31 @@ abstract contract ERC721Royalty is IERC721Royalty, ERC165Storage {
mapping(uint256 => RoyaltyInfo) private _tokenRoyalty;

/*@dev Sets tokens royalties
*
* Requirements:
* - `tokenId` must be already mined.
* - `recipient` cannot be the zero address.
* - `value` must indicate the percentage value using two decimals.
*/
*
* Requirements:
* - `tokenId` must be already mined.
* - `recipient` cannot be the zero address.
* - `value` must indicate the percentage value using two decimals.
*/
function _setTokenRoyalty(
uint256 tokenId,
address recipient,
uint256 value
) internal virtual {
require(value < 100, 'ERC2981: Royalty percentage is too high');
require(value < 100, "ERC2981: Royalty percentage is too high");
require(recipient != address(0), "ERC2981: Invalid recipient");

_tokenRoyalty[tokenId] = RoyaltyInfo(recipient, value);
}

/*@dev Sets global royalty
*
* Requirements:
* - `recipient` cannot be the zero address.
* - `value` must indicate the percentage value.
*/
function _setRoyalty(
address recipient,
uint256 value
) internal virtual {
require(value < 100, 'ERC2981: Royalty percentage is too high');
*
* Requirements:
* - `recipient` cannot be the zero address.
* - `value` must indicate the percentage value.
*/
function _setRoyalty(address recipient, uint256 value) internal virtual {
require(value < 100, "ERC2981: Royalty percentage is too high");
require(recipient != address(0), "ERC2981: Invalid recipient");

_royaltyInfo = RoyaltyInfo(recipient, value);
Expand All @@ -58,18 +55,17 @@ abstract contract ERC721Royalty is IERC721Royalty, ERC165Storage {
/**
* @dev See {IERC721Royalty-royaltyInfo}
*/
function royaltyInfo(
uint256 _tokenId,
uint256 _salePrice
) external view override returns (
address receiver,
uint256 royaltyAmount
){
if(_tokenRoyalty[_tokenId].receiver != address(0)){
function royaltyInfo(uint256 _tokenId, uint256 _salePrice)
external
view
override
returns (address receiver, uint256 royaltyAmount)
{
if (_tokenRoyalty[_tokenId].receiver != address(0)) {
RoyaltyInfo memory royalty = _tokenRoyalty[_tokenId];
receiver = royalty.receiver;
royaltyAmount = (_salePrice * royalty.royaltyAmount) / 100;
}else{
} else {
receiver = _royaltyInfo.receiver;
royaltyAmount = (_salePrice * _royaltyInfo.royaltyAmount) / 100;
}
Expand Down
28 changes: 12 additions & 16 deletions contracts/token/ERC721/extensions/draft-IERC721Royalty.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@ import "../../../interfaces/IERC165.sol";
* _Available since v4.5._
*/
interface IERC721Royalty is IERC165 {

/**
* @dev Called with the sale price to determine how much royalty
* is owed and to whom.
*
* Requirements:
* - `_tokenId` must be already mined, and have its royalty info set
* - `_salePrice` cannot be the zero.
*
*/
function royaltyInfo(
uint256 _tokenId,
uint256 _salePrice
) external view returns (
address receiver,
uint256 royaltyAmount
);
* @dev Called with the sale price to determine how much royalty
* is owed and to whom.
*
* Requirements:
* - `_tokenId` must be already mined, and have its royalty info set
* - `_salePrice` cannot be the zero.
*
*/
function royaltyInfo(uint256 _tokenId, uint256 _salePrice)
external
view
returns (address receiver, uint256 royaltyAmount);
}
40 changes: 18 additions & 22 deletions test/token/ERC721/extensions/ERC721Royalty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,42 @@ contract('ERC721Royalty', function (accounts) {
this.token = await ERC721RoyaltyMock.new();
});

it("supports interface", async function () {
shouldSupportInterfaces(['ERC2981']);
});
shouldSupportInterfaces(['ERC2981']);

describe("global royalty", function () {
describe('global royalty', function () {
beforeEach(async function () {
await this.token.setRoyalty(account1, royaltyAmount);
});

it("updates royalty amount", async function () {
it('updates royalty amount', async function () {
const newAmount = new BN('25');
let royalty = new BN((salePrice * royaltyAmount)/100);
//Initial royalty check
let royalty = new BN((salePrice * royaltyAmount) / 100);
// Initial royalty check
const initInfo = await this.token.royaltyInfo(tokenId1, salePrice);

expect(initInfo.receiver).to.be.equal(account1);
expect(initInfo.royaltyAmount).to.be.bignumber.equal(royalty);

//Updated royalty check
// Updated royalty check
await this.token.setRoyalty(account1, newAmount);
royalty = new BN((salePrice * newAmount)/100);
royalty = new BN((salePrice * newAmount) / 100);
const newInfo = await this.token.royaltyInfo(tokenId1, salePrice);

expect(newInfo.receiver).to.be.equal(account1);
expect(newInfo.royaltyAmount).to.be.bignumber.equal(royalty);
});

it("holds same royalty value for different tokens", async function () {
it('holds same royalty value for different tokens', async function () {
const newAmount = new BN('20');
await this.token.setRoyalty(account1, newAmount);

const token1Info = await this.token.royaltyInfo(tokenId1, salePrice);
const token2Info = await this.token.royaltyInfo(tokenId2, salePrice);

expect(token1Info.royaltyAmount).to.be.bignumber.equal(token2Info.royaltyAmount);

});

it("reverts if invalid parameters", async function () {
it('reverts if invalid parameters', async function () {
await expectRevert(
this.token.setRoyalty(ZERO_ADDRESS, royaltyAmount),
'ERC2981: Invalid recipient',
Expand All @@ -68,42 +65,41 @@ contract('ERC721Royalty', function (accounts) {
});
});

describe("token based royalty", function () {
describe('token based royalty', function () {
beforeEach(async function () {
await this.token.setTokenRoyalty(tokenId1, account1, royaltyAmount);
});

it("updates royalty amount", async function () {
it('updates royalty amount', async function () {
const newAmount = new BN('25');
let royalty = new BN((salePrice * royaltyAmount)/100);
//Initial royalty check
let royalty = new BN((salePrice * royaltyAmount) / 100);
// Initial royalty check
const initInfo = await this.token.royaltyInfo(tokenId1, salePrice);

expect(initInfo.receiver).to.be.equal(account1);
expect(initInfo.royaltyAmount).to.be.bignumber.equal(royalty);

//Updated royalty check
// Updated royalty check
await this.token.setTokenRoyalty(tokenId1, account1, newAmount);
royalty = new BN((salePrice * newAmount)/100);
royalty = new BN((salePrice * newAmount) / 100);
const newInfo = await this.token.royaltyInfo(tokenId1, salePrice);

expect(newInfo.receiver).to.be.equal(account1);
expect(newInfo.royaltyAmount).to.be.bignumber.equal(royalty);
});

it("holds different values for different tokens", async function () {
it('holds different values for different tokens', async function () {
const newAmount = new BN('20');
await this.token.setTokenRoyalty(tokenId2, account1, newAmount);

const token1Info = await this.token.royaltyInfo(tokenId1, salePrice);
const token2Info = await this.token.royaltyInfo(tokenId2, salePrice);

//must be different even at the same SalePrice
// must be different even at the same SalePrice
expect(token1Info.royaltyAmount).to.not.be.equal(token2Info.royaltyAmount);

});

it("reverts if invalid parameters", async function () {
it('reverts if invalid parameters', async function () {
await expectRevert(
this.token.setTokenRoyalty(tokenId1, ZERO_ADDRESS, royaltyAmount),
'ERC2981: Invalid recipient',
Expand Down

0 comments on commit 7fb8bfd

Please sign in to comment.