Skip to content

Commit

Permalink
feat: revokeContribution() (#132)
Browse files Browse the repository at this point in the history
* feat: revokeContribution()

* chore: fmt
  • Loading branch information
pegahcarter authored Oct 23, 2024
1 parent 8ea35ba commit 9a29c32
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
12 changes: 2 additions & 10 deletions contracts/hub/HubDomainsRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ contract HubDomainsRegistry is IHubDomainsRegistry, Initializable, ERC721Upgrade
_disableInitializers();
}

function initialize(
address _hubRegistry,
string memory name_,
string memory symbol_
) external initializer {
function initialize(address _hubRegistry, string memory name_, string memory symbol_) external initializer {
hubRegistry = _hubRegistry;

__ERC721_init(name_, symbol_);
Expand All @@ -37,11 +33,7 @@ contract HubDomainsRegistry is IHubDomainsRegistry, Initializable, ERC721Upgrade
}

/// @inheritdoc IHubDomainsRegistry
function registerDomain(
string calldata _name,
string calldata _uri,
address _owner
) external onlyFromHub {
function registerDomain(string calldata _name, string calldata _uri, address _owner) external onlyFromHub {
require(domains[msg.sender].tokenId == 0, "Domain already registered");
require(_isValidDomain(_name), "Invalid _name format");

Expand Down
51 changes: 45 additions & 6 deletions contracts/tasks/TaskManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ contract TaskManager is ITaskManager, Initializable, PeriodUtils, AccessUtils {
mapping(bytes32 => ContributionStatus) public contributionStatuses;
mapping(uint32 periodId => PointSummary) public pointSummaries;
mapping(address member => mapping(uint32 periodId => MemberActivity)) public memberActivities;
mapping(address member => EnumerableSet.Bytes32Set) private memberContributions;
mapping(address member => EnumerableSet.Bytes32Set) private memberContributionsGiven;
mapping(address member => EnumerableSet.Bytes32Set) private memberContributionsCommitted;
mapping(uint32 periodId => bytes32[] contributionIds) public contributionsGivenInPeriod;

EnumerableSet.AddressSet private _contributionManagers;
Expand Down Expand Up @@ -159,11 +160,48 @@ contract TaskManager is ITaskManager, Initializable, PeriodUtils, AccessUtils {
revert UnauthorizedContributionManager();
_revertIfNotMember(who);

// revert if the contribution is not in open status (NOTE: does not require commitment to be before Contribution.endDate)
ContributionStatus storage contributionStatus = contributionStatuses[contributionId];
if (uint8(contributionStatus.status) != uint8(Status.Open)) revert ContributionNotOpen();

// revert if contribution was already given to member
if (memberContributionsGiven[who].contains(contributionId)) revert ContributionAlreadyGiven();

// Add the commitment, revert if already committed
if (!memberContributionsCommitted[who].add(contributionId)) revert ContributionAlreadyCommitted();

emit CommitContribution({contributionId: contributionId, sender: msg.sender, hub: hub(), who: who, data: data});
}

/// @inheritdoc ITaskManager
function revokeContributions(
bytes32[] calldata contributionIds,
address[] calldata whos,
bytes[] calldata datas
) external {
uint256 length = contributionIds.length;
if (length != whos.length || length != datas.length) revert UnequalLengths();
for (uint256 i = 0; i < length; i++) {
_revokeContribution(contributionIds[i], whos[i], datas[i]);
}
}

/// @inheritdoc ITaskManager
function revokeContribution(bytes32 contributionId, address who, bytes calldata data) external {
_revokeContribution(contributionId, who, data);
}

function _revokeContribution(bytes32 contributionId, address who, bytes memory data) internal {
if (msg.sender != who && !isContributionManager(msg.sender) && !_isAdmin(msg.sender))
revert UnauthorizedContributionManager();
_revertIfNotMember(who);

// Remove the commitment, revert if not committed
if (!memberContributionsCommitted[who].remove(contributionId)) revert ContributionNotCommitted();

emit RevokeContribution({contributionId: contributionId, sender: msg.sender, hub: hub(), who: who, data: data});
}

/// @inheritdoc ITaskManager
function giveContributions(bytes32[] calldata contributionIds, address[] calldata whos) external {
if (!isContributionManager(msg.sender) && !_isAdmin(msg.sender)) revert UnauthorizedContributionManager();
Expand All @@ -188,17 +226,18 @@ contract TaskManager is ITaskManager, Initializable, PeriodUtils, AccessUtils {

ContributionStatus storage contributionStatus = contributionStatuses[contributionId];
if (uint8(contributionStatus.status) != uint8(Status.Open)) revert ContributionNotOpen();
uint32 points = contributionStatus.points;

if (!memberContributionsGiven[who].add(contributionId)) revert ContributionAlreadyGiven();
if (!memberContributionsCommitted[who].remove(contributionId)) revert ContributionNotCommitted();

uint32 points = contributionStatus.points;
uint32 currentPeriodId_ = currentPeriodId();

// update member activity
MemberActivity storage memberActivity = memberActivities[who][currentPeriodId_];
memberActivity.pointsGiven += points;
memberActivity.contributionIds.push(contributionId);

if (!memberContributions[who].add(contributionId)) revert MemberAlreadyContributed();

// update "hot" point summary
pointsActive -= points;
periodPointsGiven += points;
Expand Down Expand Up @@ -297,12 +336,12 @@ contract TaskManager is ITaskManager, Initializable, PeriodUtils, AccessUtils {

/// @inheritdoc ITaskManager
function isMemberGivenContributionId(address who, bytes32 contributionId) external view returns (bool) {
return memberContributions[who].contains(contributionId);
return memberContributionsGiven[who].contains(contributionId);
}

/// @inheritdoc ITaskManager
function getMemberContributionIds(address who) external view returns (bytes32[] memory) {
return memberContributions[who].values();
return memberContributionsGiven[who].values();
}

/// @inheritdoc ITaskManager
Expand Down
21 changes: 20 additions & 1 deletion contracts/tasks/interfaces/ITaskManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ struct MemberActivity {

interface ITaskManager {
error UnequalLengths();
error MemberAlreadyContributed();
error ContributionNotOpen();
error AlreadyContributionManager();
error NotContributionManager();
error UnauthorizedContributionManager();
error ContributionAlreadyCommitted();
error ContributionAlreadyGiven();
error ContributionNotCommitted();

event AddContributionManager(address who);
event RemoveContributionManager(address who);
Expand All @@ -60,6 +62,13 @@ interface ITaskManager {
address who,
bytes data
);
event RevokeContribution(
bytes32 indexed contributionId,
address indexed sender,
address indexed hub,
address who,
bytes data
);
event GiveContribution(
bytes32 indexed contributionId,
address indexed sender,
Expand Down Expand Up @@ -119,6 +128,16 @@ interface ITaskManager {
/// @notice Commit to an open Contribution with associated data
function commitContribution(bytes32 contributionId, address who, bytes calldata data) external;

/// @notice Revoke a set of commits to contributions(commitContribution(s))
function revokeContributions(
bytes32[] calldata contributionIds,
address[] calldata wwhos,
bytes[] calldata datas
) external;

/// @notice Revoke a commit to contribution (commitContribution())
function revokeContribution(bytes32 contributionId, address who, bytes calldata data) external;

/// @notice Give Contribution points to members who have completed the Contribution requirements
function giveContributions(bytes32[] calldata contributionIds, address[] calldata whos) external;

Expand Down

0 comments on commit 9a29c32

Please sign in to comment.