Skip to content

Commit

Permalink
ON-543: 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Dec 11, 2023
1 parent 6885f5d commit 13e7821
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 225 deletions.
38 changes: 15 additions & 23 deletions contracts/RolesRegistry/SftRolesRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ERC1155Holder, ERC1155Receiver } from '@openzeppelin/contracts/token/ER
import { ERC165Checker } from '@openzeppelin/contracts/utils/introspection/ERC165Checker.sol';

// Semi-fungible token (SFT) registry with only one role (UNIQUE_ROLE)
contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
contract SftRolesRegistrySingleRole is IERCXXXX, ERC1155Holder {
bytes32 public constant UNIQUE_ROLE = keccak256('UNIQUE_ROLE');

// grantor => tokenAddress => operator => isApproved
Expand All @@ -19,8 +19,8 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
// nonce => DepositInfo
mapping(uint256 => DepositInfo) public deposits;

// nonce => role => RoleAssignment
mapping(uint256 => mapping(bytes32 => RoleData)) internal roleAssignments;
// nonce => RoleAssignment
mapping(uint256 => RoleData) internal roleAssignments;

modifier validExpirationDate(uint64 _expirationDate) {
require(_expirationDate > block.timestamp, 'SftRolesRegistry: expiration date must be in the future');
Expand Down Expand Up @@ -54,7 +54,6 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
_grantRoleData.tokenAmount
)
{
require(_grantRoleData.role == UNIQUE_ROLE, 'SftRolesRegistry: role not supported');
require(_grantRoleData.nonce > 0, 'SftRolesRegistry: nonce must be greater than zero');

if (deposits[_grantRoleData.nonce].grantor == address(0)) {
Expand All @@ -71,18 +70,19 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {

} else {
// nonce exists
require(deposits[_grantRoleData.nonce].grantor == _grantRoleData.grantor, "SftRolesRegistry: grantor mismatch");
require(deposits[_grantRoleData.nonce].tokenAddress == _grantRoleData.tokenAddress, "SftRolesRegistry: tokenAddress mismatch");
require(deposits[_grantRoleData.nonce].tokenId == _grantRoleData.tokenId, "SftRolesRegistry: tokenId mismatch");
require(deposits[_grantRoleData.nonce].tokenAmount == _grantRoleData.tokenAmount, "SftRolesRegistry: tokenAmount mismatch");

RoleData storage _roleData = roleAssignments[_grantRoleData.nonce][_grantRoleData.role];
RoleData storage _roleData = roleAssignments[_grantRoleData.nonce];
require(_roleData.expirationDate < block.timestamp || _roleData.revocable, "SftRolesRegistry: nonce is not expired or is not revocable");
}
_grantOrUpdateRole(_grantRoleData);
}

function _grantOrUpdateRole(RoleAssignment calldata _grantRoleData) internal {
roleAssignments[_grantRoleData.nonce][_grantRoleData.role] = RoleData(
roleAssignments[_grantRoleData.nonce] = RoleData(
_grantRoleData.grantee,
_grantRoleData.expirationDate,
_grantRoleData.revocable,
Expand All @@ -91,7 +91,7 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {

emit RoleGranted(
_grantRoleData.nonce,
_grantRoleData.role,
UNIQUE_ROLE,
_grantRoleData.tokenAddress,
_grantRoleData.tokenId,
_grantRoleData.tokenAmount,
Expand All @@ -106,14 +106,6 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
function _deposit(uint256 _nonce, DepositInfo memory _depositInfo) internal {
deposits[_nonce] = _depositInfo;

emit Deposited(
_nonce,
_depositInfo.tokenAddress,
_depositInfo.tokenId,
_depositInfo.tokenAmount,
_depositInfo.grantor
);

_transferFrom(
_depositInfo.grantor,
address(this),
Expand All @@ -124,7 +116,7 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
}

function revokeRoleFrom(uint256 _nonce, bytes32 _role, address _grantee) external override {
RoleData memory _roleData = roleAssignments[_nonce][_role];
RoleData memory _roleData = roleAssignments[_nonce];
require(_roleData.grantee != address(0), 'SftRolesRegistry: invalid grantee');
DepositInfo memory _depositInfo = deposits[_nonce];

Expand All @@ -134,7 +126,7 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
require(caller == _roleData.grantee, 'SftRolesRegistry: nonce is not expired or is not revocable');
}

delete roleAssignments[_nonce][_role];
delete roleAssignments[_nonce];

emit RoleRevoked(
_nonce,
Expand All @@ -158,14 +150,14 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
{
DepositInfo memory _depositInfo = deposits[_nonce];
require(
roleAssignments[_nonce][UNIQUE_ROLE].grantee == address(0) ||
roleAssignments[_nonce][UNIQUE_ROLE].expirationDate < block.timestamp ||
roleAssignments[_nonce][UNIQUE_ROLE].revocable,
roleAssignments[_nonce].grantee == address(0) ||
roleAssignments[_nonce].expirationDate < block.timestamp ||
roleAssignments[_nonce].revocable,
'SftRolesRegistry: token has an active role'
);

delete deposits[_nonce];
delete roleAssignments[_nonce][UNIQUE_ROLE];
delete roleAssignments[_nonce];

_transferFrom(
address(this),
Expand All @@ -192,11 +184,11 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
/** View Functions **/

function roleData(uint256 _nonce, bytes32 _role, address _grantee) external view returns (RoleData memory) {
return roleAssignments[_nonce][_role];
return roleAssignments[_nonce];
}

function roleExpirationDate(uint256 _nonce, bytes32 _role, address _grantee) external view returns (uint64 expirationDate_) {
return roleAssignments[_nonce][_role].expirationDate;
return roleAssignments[_nonce].expirationDate;
}

function isRoleApprovedForAll(
Expand Down
8 changes: 0 additions & 8 deletions contracts/RolesRegistry/interfaces/IERCXXXX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ interface IERCXXXX is IERC165 {
/// @param _isApproved The approval status.
event RoleApprovalForAll(address indexed _tokenAddress, address indexed _operator, bool _isApproved);

/// @notice Emitted when a user deposits tokens to grant roles.
/// @param _nonce The identifier of the role assignment.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _tokenAmount The token amount deposited.
/// @param _grantor The user depositing the tokens.
event Deposited(uint256 indexed _nonce, address indexed _tokenAddress, uint256 indexed _tokenId, uint256 _tokenAmount, address _grantor);

/// @notice Emitted when a user withdraws tokens from a role assignment.
/// @param _nonce The identifier of the role assignment.
/// @param _grantor The user withdrawing the tokens.
Expand Down
Loading

0 comments on commit 13e7821

Please sign in to comment.