From cd1c4568baa9b1dae94d2507f2fd2290fd4e1598 Mon Sep 17 00:00:00 2001 From: Adhocmaster Date: Thu, 12 May 2022 10:06:07 -0700 Subject: [PATCH 1/2] refactored royalty code --- packages/contracts/.gitignore | 2 ++ .../NonFungibleRegistryEnumerableUpgradeable.sol | 13 ++++++++++--- packages/contracts/hardhat.config.js | 8 ++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/contracts/.gitignore b/packages/contracts/.gitignore index 03944dc45..348dbc4fb 100644 --- a/packages/contracts/.gitignore +++ b/packages/contracts/.gitignore @@ -4,3 +4,5 @@ node_modules #Hardhat files cache artifacts +.openzeppelin/* +workspace.code-workspace \ No newline at end of file diff --git a/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol b/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol index 0ac1f8946..1669db203 100644 --- a/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol +++ b/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol @@ -94,8 +94,10 @@ contract NonFungibleRegistryEnumerableUpgradeable is address public burnAddress; /// @notice The amount in basis points of registrationFee to send to the burnAddress account - /// @notice This variable must be less than or equal to 10000 + /// @notice This variable must be less than or equal to 10000 / burnDenom uint256 public burnFee; + uint constant burnDenom = 10000; + /** @dev address of primary NFR registry required for participation * if the primaryRegistry is address(0), then this variable is ignored @@ -273,7 +275,8 @@ contract NonFungibleRegistryEnumerableUpgradeable is _createLabeledToken(to, label, registrationData, tokenId); - uint256 burnAmount = registrationFee * burnFee / 10000; + uint256 burnAmount = calculateRoyalty(tokenId, registrationFee); + IERC20Upgradeable(registrationToken).transfer(burnAddress, burnAmount); // the fee stays with the token, not the token owner identityStakes[tokenId] = Fee(registrationToken, registrationFee-burnAmount); @@ -443,12 +446,16 @@ contract NonFungibleRegistryEnumerableUpgradeable is } } + function calculateRoyalty(uint256 tokenId, uint256 salePrice) internal view returns(uint256 amount) { + amount = salePrice * burnFee / burnDenom; + } + function royaltyInfo(uint256 tokenId, uint256 salePrice) external view override(IERC2981Upgradeable) returns (address receiver, uint256 royaltyAmount) { - royaltyAmount = salePrice * burnFee / 10000; + royaltyAmount = calculateRoyalty(tokenId, salePrice); receiver = burnAddress; } diff --git a/packages/contracts/hardhat.config.js b/packages/contracts/hardhat.config.js index c4954db03..3044d1f5e 100644 --- a/packages/contracts/hardhat.config.js +++ b/packages/contracts/hardhat.config.js @@ -54,6 +54,14 @@ module.exports = { chainId: 31337, url: "http://127.0.0.1:8569", }, + dev_ganache: { + accounts: { + accountsBalance: "10000000000000000000000", + mnemonic, + }, + chainId: 1337, + url: "http://127.0.0.1:7545" + }, DevNet: { accounts: { mnemonic, From b17ca4f11f29cad822e86ffa64cba3ebbd1e83df Mon Sep 17 00:00:00 2001 From: Adhocmaster Date: Thu, 12 May 2022 11:10:58 -0700 Subject: [PATCH 2/2] removed tokenId from calculateRoyalty --- .../NonFungibleRegistryEnumerableUpgradeable.sol | 15 +++++++++------ .../identity/NonFungibleRegistryUpgradeable.sol | 13 ++++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol b/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol index 1669db203..a0e2b6fef 100644 --- a/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol +++ b/packages/contracts/contracts/identity/NonFungibleRegistryEnumerableUpgradeable.sol @@ -96,7 +96,6 @@ contract NonFungibleRegistryEnumerableUpgradeable is /// @notice The amount in basis points of registrationFee to send to the burnAddress account /// @notice This variable must be less than or equal to 10000 / burnDenom uint256 public burnFee; - uint constant burnDenom = 10000; /** @dev address of primary NFR registry required for participation @@ -127,6 +126,10 @@ contract NonFungibleRegistryEnumerableUpgradeable is /// @dev The REGISTRAR_ROLE_ADMIN curates the address with REGISTRAR_ROLE permissions bytes32 public constant REGISTRAR_ROLE_ADMIN = keccak256("REGISTRAR_ROLE_ADMIN"); + + /// @notice used to calculate burn % in calculating royaltyAmount + uint constant burnDenom = 10000; + /** * @dev Emitted when updateLabel is called successfully */ @@ -145,7 +148,7 @@ contract NonFungibleRegistryEnumerableUpgradeable is /// @custom:oz-upgrades-unsafe-allow constructor constructor() initializer {} - /// @notice initialize is called once on the creation of an upgradable proxy + /// @notice initialize is called once on the creation of an upgradable proxy. /// @dev can only be called once due to the initializer modifier /// @param name_ name to be given to the Non Fungible Registry /// @param symbol_ shorthand symbol to be given to the Non Fungible Registry @@ -275,8 +278,8 @@ contract NonFungibleRegistryEnumerableUpgradeable is _createLabeledToken(to, label, registrationData, tokenId); - uint256 burnAmount = calculateRoyalty(tokenId, registrationFee); - + uint256 burnAmount = calculateRoyalty(registrationFee); + IERC20Upgradeable(registrationToken).transfer(burnAddress, burnAmount); // the fee stays with the token, not the token owner identityStakes[tokenId] = Fee(registrationToken, registrationFee-burnAmount); @@ -446,7 +449,7 @@ contract NonFungibleRegistryEnumerableUpgradeable is } } - function calculateRoyalty(uint256 tokenId, uint256 salePrice) internal view returns(uint256 amount) { + function calculateRoyalty(uint256 salePrice) internal view returns(uint256 amount) { amount = salePrice * burnFee / burnDenom; } @@ -455,7 +458,7 @@ contract NonFungibleRegistryEnumerableUpgradeable is view override(IERC2981Upgradeable) returns (address receiver, uint256 royaltyAmount) { - royaltyAmount = calculateRoyalty(tokenId, salePrice); + royaltyAmount = calculateRoyalty(salePrice); receiver = burnAddress; } diff --git a/packages/contracts/contracts/identity/NonFungibleRegistryUpgradeable.sol b/packages/contracts/contracts/identity/NonFungibleRegistryUpgradeable.sol index d2e21ec7b..fdc837d99 100644 --- a/packages/contracts/contracts/identity/NonFungibleRegistryUpgradeable.sol +++ b/packages/contracts/contracts/identity/NonFungibleRegistryUpgradeable.sol @@ -92,7 +92,7 @@ contract NonFungibleRegistryUpgradeable is address public burnAddress; /// @notice The amount in basis points of registrationFee to send to the burnAddress account - /// @notice This variable must be less than or equal to 10000 + /// @notice This variable must be less than or equal to 10000 / burnDenom uint256 public burnFee; /** @dev address of primary NFR registry required for participation @@ -123,6 +123,8 @@ contract NonFungibleRegistryUpgradeable is /// @dev The REGISTRAR_ROLE_ADMIN curates the address with REGISTRAR_ROLE permissions bytes32 public constant REGISTRAR_ROLE_ADMIN = keccak256("REGISTRAR_ROLE_ADMIN"); + /// @notice used to calculate burn % in calculating royaltyAmount + uint constant burnDenom = 10000; /** * @dev Emitted when updateLabel is called successfully */ @@ -270,7 +272,7 @@ contract NonFungibleRegistryUpgradeable is _createLabeledToken(to, label, registrationData, tokenId); - uint256 burnAmount = registrationFee * burnFee / 10000; + uint256 burnAmount = calculateRoyalty(registrationFee); IERC20Upgradeable(registrationToken).transfer(burnAddress, burnAmount); // the fee stays with the token, not the token owner identityStakes[tokenId] = Fee(registrationToken, registrationFee-burnAmount); @@ -440,12 +442,17 @@ contract NonFungibleRegistryUpgradeable is } } + function calculateRoyalty(uint256 salePrice) internal view returns(uint256 amount) { + amount = salePrice * burnFee / burnDenom; + } + + function royaltyInfo(uint256 tokenId, uint256 salePrice) external view override(IERC2981Upgradeable) returns (address receiver, uint256 royaltyAmount) { - royaltyAmount = salePrice * burnFee / 10000; + royaltyAmount = calculateRoyalty(salePrice); receiver = burnAddress; }