Skip to content

Commit

Permalink
refactor: service owner dependency first
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Oct 23, 2024
1 parent a43e44f commit edf4380
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
40 changes: 14 additions & 26 deletions contracts/contribute/ContributeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ contract ContributeManager {
/// @param stakingInstance Staking instance.
function _stake(uint256 socialId, uint256 serviceId, address multisig, address stakingInstance) internal {
// Add the service into its social Id corresponding record
IContributors(contributorsProxy).setServiceInfoForId(socialId, serviceId, multisig, stakingInstance, msg.sender);
IContributors(contributorsProxy).setServiceInfoForId(msg.sender, socialId, serviceId, multisig, stakingInstance);

// Approve service NFT for the staking instance
IToken(serviceRegistry).approve(stakingInstance, serviceId);
Expand All @@ -200,8 +200,8 @@ contract ContributeManager {
revert ZeroValue();
}

// Check for existing service corresponding to the social Id
(uint256 serviceId, address multisig, , ) = IContributors(contributorsProxy).mapSocialIdServiceInfo(socialId);
// Check for existing service corresponding to the msg.sender
(, uint256 serviceId, address multisig, ) = IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
if (serviceId > 0) {
revert ServiceAlreadyStaked(socialId, serviceId, multisig);
}
Expand Down Expand Up @@ -251,8 +251,8 @@ contract ContributeManager {
/// @param serviceId Service Id.
/// @param stakingInstance Staking instance.
function stake(uint256 socialId, uint256 serviceId, address stakingInstance) external {
// Check for existing service corresponding to the social Id
(uint256 serviceIdCheck, address multisig, , ) = IContributors(contributorsProxy).mapSocialIdServiceInfo(socialId);
// Check for existing service corresponding to the msg.sender
(, uint256 serviceIdCheck, address multisig, ) = IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
if (serviceIdCheck > 0) {
revert ServiceAlreadyStaked(socialId, serviceIdCheck, multisig);
}
Expand All @@ -276,21 +276,15 @@ contract ContributeManager {
emit Staked(socialId, msg.sender, serviceId, multisig, stakingInstance);
}

/// @dev Unstakes service Id corresponding to the social Id and clears the contributor record.
/// @param socialId Social Id.
function unstake(uint256 socialId) external {
/// @dev Unstakes service Id corresponding to the msg.sender and clears the contributor record.
function unstake() external {
// Check for existing service corresponding to the social Id
(uint256 serviceId, address multisig, address stakingInstance, address serviceOwner) =
IContributors(contributorsProxy).mapSocialIdServiceInfo(socialId);
(uint256 socialId, uint256 serviceId, address multisig, address stakingInstance) =
IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
if (serviceId == 0) {
revert ServiceNotDefined(socialId);
}

// Check for service owner
if (msg.sender != serviceOwner) {
revert ServiceOwnerOnly(serviceId, msg.sender, serviceOwner);
}

// Unstake the service
IStaking(stakingInstance).unstake(serviceId);

Expand All @@ -299,27 +293,21 @@ contract ContributeManager {

// Zero the service info: the service is out of the contribute records, however multisig activity is still valid
// If the same service is staked back, the multisig activity continues being tracked
IContributors(contributorsProxy).setServiceInfoForId(socialId, 0, address(0), address(0), address(0));
IContributors(contributorsProxy).setServiceInfoForId(msg.sender, 0, 0, address(0), address(0));

emit Unstaked(socialId, msg.sender, serviceId, multisig, stakingInstance);
}

/// @dev Claims rewards for the service.
/// @param socialId Social Id.
/// @dev Claims rewards for the service corresponding to msg.sender.
/// @return reward Staking reward.
function claim(uint256 socialId) external returns (uint256 reward) {
function claim() external returns (uint256 reward) {
// Check for existing service corresponding to the social Id
(uint256 serviceId, address multisig, address stakingInstance, address serviceOwner) =
IContributors(contributorsProxy).mapSocialIdServiceInfo(socialId);
(uint256 socialId, uint256 serviceId, address multisig, address stakingInstance) =
IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
if (serviceId == 0) {
revert ServiceNotDefined(socialId);
}

// Check for service owner
if (msg.sender != serviceOwner) {
revert ServiceOwnerOnly(serviceId, msg.sender, serviceOwner);
}

// Claim staking rewards
reward = IStaking(stakingInstance).claim(serviceId);

Expand Down
24 changes: 12 additions & 12 deletions contracts/contribute/Contributors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ error UnauthorizedAccount(address account);

// Struct for service info
struct ServiceInfo {
// Social Id
uint256 socialId;
// Service Id
uint256 serviceId;
// Corresponding service multisig
address multisig;
// Staking instance address
address stakingInstance;
// Service owner address
address serviceOwner;
}

/// @title Contributors - Smart contract for managing contributors
Expand All @@ -45,8 +45,8 @@ contract Contributors {
event ImplementationUpdated(address indexed implementation);
event OwnerUpdated(address indexed owner);
event ManagerUpdated(address indexed manager);
event SetServiceInfoForId(uint256 indexed socialId, uint256 indexed serviceId, address multisig,
address stakingInstance, address indexed serviceOwner);
event SetServiceInfoForId(address indexed serviceOwner, uint256 indexed socialId, uint256 indexed serviceId,
address multisig, address stakingInstance);
event SetContributeAgentStatuses(address[] contributeAgents, bool[] statuses);
event MultisigActivityChanged(address indexed senderAgent, address[] multisigs, uint256[] activityChanges);

Expand All @@ -60,8 +60,8 @@ contract Contributors {
// Service manager contract address
address public manager;

// Mapping of social id => service info
mapping(uint256 => ServiceInfo) public mapSocialIdServiceInfo;
// Mapping of address => service info
mapping(address => ServiceInfo) public mapSocialIdServiceInfo;
// Mapping of service multisig address => activity
mapping(address => uint256) public mapMutisigActivities;
// Mapping of whitelisted contributor agents
Expand Down Expand Up @@ -140,31 +140,31 @@ contract Contributors {
}

/// @dev Sets service info for the social id.
/// @param serviceOwner Service owner.
/// @param socialId Social id.
/// @param serviceId Service Id.
/// @param multisig Service multisig address.
/// @param stakingInstance Staking instance address.
/// @param serviceOwner Service owner.
function setServiceInfoForId(
address serviceOwner,
uint256 socialId,
uint256 serviceId,
address multisig,
address stakingInstance,
address serviceOwner
address stakingInstance
) external {
// Check for manager
if (msg.sender != manager) {
revert OnlyManager(msg.sender, manager);
}

// Set (or remove) multisig for the corresponding social id
ServiceInfo storage serviceInfo = mapSocialIdServiceInfo[socialId];
ServiceInfo storage serviceInfo = mapSocialIdServiceInfo[serviceOwner];
serviceInfo.socialId = socialId;
serviceInfo.serviceId = serviceId;
serviceInfo.multisig = multisig;
serviceInfo.stakingInstance = stakingInstance;
serviceInfo.serviceOwner = serviceOwner;

emit SetServiceInfoForId(socialId, serviceId, multisig, stakingInstance, serviceOwner);
emit SetServiceInfoForId(serviceOwner, socialId, serviceId, multisig, stakingInstance);
}

/// @dev Sets contribute agent statues.
Expand Down
14 changes: 7 additions & 7 deletions contracts/contribute/interfaces/IContributors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ pragma solidity ^0.8.28;
// Contributors interface
interface IContributors {
/// @dev Sets service info for the social id.
/// @param serviceOwner Service owner.
/// @param socialId Social id.
/// @param serviceId Service Id.
/// @param multisig Service multisig address.
/// @param stakingInstance Staking instance address.
/// @param serviceOwner Service owner.
function setServiceInfoForId(
address serviceOwner,
uint256 socialId,
uint256 serviceId,
address multisig,
address stakingInstance,
address serviceOwner
address stakingInstance
) external;

/// @dev Gets service info corresponding to a specified social Id.
/// @param socialId Social Id.
/// @param serviceOwner Service owner.
/// @return socialId Social Id.
/// @return serviceId Corresponding service Id.
/// @return multisig Corresponding service multisig.
/// @return stakingInstance Staking instance address.
/// @return serviceOwner Service owner.
function mapSocialIdServiceInfo(uint256 socialId) external view
returns (uint256 serviceId, address multisig, address stakingInstance, address serviceOwner);
function mapSocialIdServiceInfo(address serviceOwner) external view
returns (uint256 socialId, uint256 serviceId, address multisig, address stakingInstance);
}

0 comments on commit edf4380

Please sign in to comment.