Skip to content

Commit

Permalink
refactor: contribution uri => descriptionId (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
pegahcarter authored Oct 9, 2024
1 parent 5b4fa68 commit 6882bd7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
58 changes: 55 additions & 3 deletions contracts/tasks/TaskFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ pragma solidity ^0.8.20;
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {ITaskRegistry} from "./interfaces/ITaskRegistry.sol";
import {ITaskManager} from "./interfaces/ITaskManager.sol";
import {Contribution, ITaskFactory} from "./interfaces/ITaskFactory.sol";
import {Contribution, Description, ITaskFactory} from "./interfaces/ITaskFactory.sol";
import {AccessUtils} from "../utils/AccessUtils.sol";
import {PeriodUtils} from "../utils/PeriodUtils.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
using EnumerableSet for EnumerableSet.Bytes32Set;

EnumerableSet.Bytes32Set private _descriptionIds;
mapping(bytes32 => Description) private _descriptions;

EnumerableSet.Bytes32Set private _contributionIds;
mapping(bytes32 => Contribution) public _contributions;
mapping(uint32 periodId => bytes32[] contributionIds) public _contributionsInPeriod;
Expand All @@ -25,6 +28,30 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
_init_PeriodUtils({_period0Start: _period0Start, _initPeriodId: _initPeriodId});
}

function registerDescriptions(Description[] calldata descriptions) external returns (bytes32[] memory) {
uint256 length = descriptions.length;
bytes32[] memory newDescriptionIds = new bytes32[](length);
for (uint256 i=0; i < length; i++) {
newDescriptionIds[i] = _registerDescription(descriptions[i]);
}

return newDescriptionIds;
}

function registerDescription(Description calldata description) external returns (bytes32) {
return _registerDescription(description);
}

function _registerDescription(Description memory description) internal returns (bytes32) {
bytes32 descriptionId = calcDescriptionId(description);

if (!_descriptionIds.add(descriptionId)) revert DescriptionAlreadyRegistered();

_descriptions[descriptionId] = description;

emit RegisterDescription(descriptionId);
}

function createContributions(Contribution[] calldata contributions) external returns (bytes32[] memory) {
_revertIfNotAdmin();
uint256 length = contributions.length;
Expand Down Expand Up @@ -105,19 +132,36 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
return result;
}

function getDescriptionById(bytes32 descriptionId) external view returns (Description memory) {
return _descriptions[descriptionId];
}

function getContributionById(bytes32 contributionId) external view returns (Contribution memory) {
return _contributions[contributionId];
}

function getDescriptionByIdEncoded(bytes32 descriptionId) external view returns (bytes memory) {
Description memory description = _descriptions[descriptionId];
return encodeDescription(description);
}

function getContributionByIdEncoded(bytes32 contributionId) external view returns (bytes memory) {
Contribution memory contribution = _contributions[contributionId];
return encodeContribution(contribution);
}

function isDescriptionId(bytes32 descriptionId) public view returns (bool) {
return _descriptionIds.contains(descriptionId);
}

function isContributionId(bytes32 contributionId) public view returns (bool) {
return _contributionIds.contains(contributionId);
}

function descriptionIds() external view returns (bytes32[] memory) {
return _descriptionIds.values();
}

function contributionIds() external view returns (bytes32[] memory) {
return _contributionIds.values();
}
Expand All @@ -126,19 +170,27 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
return _contributionsInPeriod[periodId];
}

function encodeDescription(Description memory description) public pure returns (bytes memory) {
return abi.encodePacked(description.uri);
}

function encodeContribution(Contribution memory contribution) public pure returns (bytes memory) {
return
abi.encodePacked(
contribution.taskId,
contribution.descriptionId,
contribution.role,
contribution.startDate,
contribution.endDate,
contribution.points,
contribution.quantity,
contribution.uri
contribution.quantity
);
}

function calcDescriptionId(Description memory description) public pure returns (bytes32) {
return keccak256(encodeDescription(description));
}

function calcContributionId(Contribution memory contribution) public pure returns (bytes32) {
return keccak256(encodeContribution(contribution));
}
Expand Down
21 changes: 21 additions & 0 deletions contracts/tasks/interfaces/ITaskFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ pragma solidity >=0.8.0;

struct Contribution {
bytes32 taskId;
bytes32 descriptionId;
uint256 role;
uint32 startDate;
uint32 endDate;
uint32 points;
uint128 quantity;
}

struct Description {
string uri;
}

Expand All @@ -16,8 +20,13 @@ interface ITaskFactory {
error ContributionIdAlreadyExists();
error InvalidContributionQuantity();
error InvalidContributionPoints();
error DescriptionAlreadyRegistered();

event CreateContribution(bytes32 indexed contributionId, address indexed sender, bytes encodedContribution);
event RegisterDescription(bytes32 indexed descriptionId);

function registerDescriptions(Description[] calldata descriptions) external returns (bytes32[] memory);
function registerDescription(Description calldata description) external returns (bytes32);

/// @notice Create contributions to be tracked by the TaskManager
/// @return Array of new bytes32 contributionIds (1:1 with contributions)
Expand All @@ -30,24 +39,36 @@ interface ITaskFactory {
function getContributionIdsBeforeEndDate(uint32 timestamp) external view returns (bytes32[] memory);
function getContributionIdsActive(uint32 timestamp) external view returns (bytes32[] memory);

function getDescriptionById(bytes32 descriptionId) external view returns (Description memory);

/// @notice Using the unique identifier of a contribution, get the data associated to it
function getContributionById(bytes32 contributionId) external view returns (Contribution memory);

function getDescriptionByIdEncoded(bytes32 descriptionId) external view returns (bytes memory);

/// @notice same as getContributionById but without requiring tuple support for external contracts
function getContributionByIdEncoded(bytes32 contributionId) external view returns (bytes memory);

function isDescriptionId(bytes32 descriptionId) external view returns (bool);

/// @notice Return true of a unique contribution identifier has been created, else false
function isContributionId(bytes32 contributionId) external view returns (bool);

function descriptionIds() external view returns (bytes32[] memory);

/// @notice return all created contribution identifiers
function contributionIds() external view returns (bytes32[] memory);

/// @notice return all contributions created within a given period
function contributionsInPeriod(uint32 periodId) external view returns (bytes32[] memory);

function encodeDescription(Description memory description) external pure returns (bytes memory);

/// @notice convert a Contribution struct to its' encoded type to hash / events
function encodeContribution(Contribution memory contribution) external pure returns (bytes memory);

function calcDescriptionId(Description memory description) external pure returns (bytes32);

/// @notice hash an encoded contribution to get it's unique identifier
function calcContributionId(Contribution memory contribution) external pure returns (bytes32);
}

0 comments on commit 6882bd7

Please sign in to comment.