Skip to content

Commit

Permalink
Refactor/contribution description (#137)
Browse files Browse the repository at this point in the history
* refactor: descriptionId => uri

* ops: deploy v0.1.2
  • Loading branch information
pegahcarter authored Oct 30, 2024
1 parent dbc1920 commit 1ff6513
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 125 deletions.
72 changes: 4 additions & 68 deletions contracts/tasks/TaskFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ 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, Description, ITaskFactory} from "./interfaces/ITaskFactory.sol";
import {Contribution, 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 _contributionIdsInPeriod;

function version() external pure returns (uint256 major, uint256 minor, uint256 patch) {
return (0, 1, 0);
return (0, 1, 2);
}

constructor() {
Expand All @@ -32,36 +29,6 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
_init_PeriodUtils({_period0Start: _period0Start, _initPeriodId: _initPeriodId});
}

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

return newDescriptionIds;
}

/// @inheritdoc ITaskFactory
function registerDescription(Description calldata description) external returns (bytes32) {
_revertIfNotAdmin();
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);

return descriptionId;
}

/// @inheritdoc ITaskFactory
function createContributions(Contribution[] calldata contributions) external returns (bytes32[] memory) {
_revertIfNotAdmin();
Expand Down Expand Up @@ -107,7 +74,7 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
sender: msg.sender,
hub: hub(),
taskId: contribution.taskId,
descriptionId: contribution.descriptionId,
uri: contribution.uri,
role: contribution.role,
startDate: contribution.startDate,
endDate: contribution.endDate,
Expand Down Expand Up @@ -160,43 +127,22 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
return result;
}

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

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

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

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

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

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

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

/// @inheritdoc ITaskFactory
function contributionIds() external view returns (bytes32[] memory) {
return _contributionIds.values();
Expand All @@ -207,17 +153,12 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
return _contributionIdsInPeriod[periodId];
}

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

/// @inheritdoc ITaskFactory
function encodeContribution(Contribution memory contribution) public pure returns (bytes memory) {
return
abi.encodePacked(
contribution.taskId,
contribution.descriptionId,
contribution.uri,
contribution.role,
contribution.startDate,
contribution.endDate,
Expand All @@ -226,11 +167,6 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {
);
}

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

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

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

struct Description {
string uri;
}

interface ITaskFactory {
error NotContributionId();
error TaskIdNotRegistered();
error ContributionIdAlreadyExists();
error InvalidContributionQuantity();
error InvalidContributionPoints();
error InvalidContributionPeriod();
error DescriptionAlreadyRegistered();

event CreateContribution(
bytes32 indexed contributionId,
address indexed sender,
address indexed hub,
bytes32 taskId,
bytes32 descriptionId,
string uri,
uint256 role,
uint32 startDate,
uint32 endDate,
uint32 points,
uint128 quantity
);
event RegisterDescription(bytes32 indexed descriptionId);

/// @notice register descriptions of contributions to be used in `createContribution(s)`
/// @dev wraps the string uri into a bytes32 for predictable lengths
function registerDescriptions(Description[] calldata descriptions) external returns (bytes32[] memory);

/// @notice register description of a contribution to be used in `createContribution(s)`
/// @dev wraps the string uri into a bytes32 for predictable lengths
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 @@ -59,42 +45,24 @@ interface ITaskFactory {
/// @notice Off-chain helper to display contributions where startDate < timestamp & timestamp < endDate
function getContributionIdsActive(uint32 timestamp) external view returns (bytes32[] memory);

/// @notice return a description struct based on its' identifier
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);

/// @notice same as getDescriptionById except returns bytes instead of struct
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);

/// @notice return true if the Description identifier exists
function isDescriptionId(bytes32 descriptionId) external view returns (bool);

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

/// @notice return the array of all existing Description identifiers
function descriptionIds() external view returns (bytes32[] memory);

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

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

/// @notice convert a Description struct into its' respective bytes
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);

/// Hash a Description to get its' unique identifier
function calcDescriptionId(Description memory description) external pure returns (bytes32);

/// @notice hash a Contribution to get it's unique identifier
function calcContributionId(Contribution memory contribution) external pure returns (bytes32);
}
16 changes: 16 additions & 0 deletions deployments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@
4. taskRegistry: 0x16ba349260FfFA0f760ED0bA212A566BA15E06fc


80002 1730219241
0. globalParameters: 0xE49a40ceAfF5e189F3f2Bdd383fa40abdBcf22d4
1. autID: 0xe8428DB8Dad78d78999084f2535D56eC2a9FE3Da
2. hubRegistry: 0x1a3B7082b8BF4Fa21A828C0174a4106a7fF44fCD
3. hubDomainsRegistry: 0x681cE9fFeb0cEE8f7F8880C04595065730230aCc
4. taskRegistry: 0xCAA28aec1b51f6703e02aBda7aee7A46286bB946


80002 1730219271
0. globalParameters: 0xE49a40ceAfF5e189F3f2Bdd383fa40abdBcf22d4
1. autID: 0xe8428DB8Dad78d78999084f2535D56eC2a9FE3Da
2. hubRegistry: 0x1a3B7082b8BF4Fa21A828C0174a4106a7fF44fCD
3. hubDomainsRegistry: 0x681cE9fFeb0cEE8f7F8880C04595065730230aCc
4. taskRegistry: 0xCAA28aec1b51f6703e02aBda7aee7A46286bB946


10 changes: 6 additions & 4 deletions script/DeployAll.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ contract DeployAll is Script {
deploying = true;
} else {
// testing
owner = address(123456);
initialContributionManager = address(11111111);
privateKey = 567890;
owner = vm.addr(privateKey);
initialContributionManager = address(11111111);
}
console.log("setUp -- done");

Expand Down Expand Up @@ -111,9 +111,11 @@ contract DeployAll is Script {

// other inits
taskRegistry.initialize();
taskRegistry.setApproved(owner);
if (deploying) {
taskRegistry.setApproved(owner);
}

// Setup initial tasks
// Setup initial tasks
Task[] memory tasks = new Task[](11);
// open tasks
tasks[0] = Task({
Expand Down
6 changes: 3 additions & 3 deletions test/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.20;
import "script/DeployAll.s.sol";
import { Hub } from "contracts/hub/Hub.sol";
import { TaskRegistry, ITaskRegistry, Task } from "contracts/tasks/TaskRegistry.sol";
import { TaskFactory, ITaskFactory, Contribution, Description } from "contracts/tasks/TaskFactory.sol";
import { TaskFactory, ITaskFactory, Contribution } from "contracts/tasks/TaskFactory.sol";
import { TaskManager, ITaskManager, MemberActivity } from "contracts/tasks/TaskManager.sol";

import { console, StdAssertions, StdChains, StdCheats, stdError, StdInvariant, stdJson, stdMath, StdStorage, stdStorage, StdUtils, Vm, StdStyle, TestBase, Test } from "forge-std/Test.sol";
Expand Down Expand Up @@ -91,7 +91,7 @@ abstract contract BaseTest is Test {
address who,
address hubAddress,
bytes32 taskId,
bytes32 descriptionId,
string memory uri,
uint256 role,
uint32 startDate,
uint32 endDate,
Expand All @@ -101,7 +101,7 @@ abstract contract BaseTest is Test {
vm.prank(who);
Contribution memory contribution = Contribution({
taskId: taskId,
descriptionId: descriptionId,
uri: uri,
role: role,
startDate: startDate,
endDate: endDate,
Expand Down
6 changes: 2 additions & 4 deletions test/unit/tasks/TaskFactory/CreateContributionsTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ contract TaskFactoryCreateContributionsTest is BaseTest {

// Generic contribution values
bytes32 taskId;
bytes32 descriptionId;
uint256 role = 1;
uint32 startDate;
uint32 endDate;
Expand All @@ -24,13 +23,12 @@ contract TaskFactoryCreateContributionsTest is BaseTest {

// init Contribution for testing
taskId = taskRegistry.registerTask(Task({uri: "abcde"}));
descriptionId = taskFactory.registerDescription(Description({uri: "fghij"}));

startDate = uint32(block.timestamp);
endDate = startDate + 7 days;
contribution = Contribution({
taskId: taskId,
descriptionId: descriptionId,
uri: "fghij",
role: role,
startDate: startDate,
endDate: endDate,
Expand Down Expand Up @@ -58,7 +56,7 @@ contract TaskFactoryCreateContributionsTest is BaseTest {

Contribution memory queriedContribution = taskFactory.getContributionById(contributionId);
assertEq(queriedContribution.taskId, taskId);
assertEq(queriedContribution.descriptionId, descriptionId);
assertEq(abi.encode(queriedContribution.uri), abi.encode("fghij"));
assertEq(queriedContribution.role, role);
assertEq(queriedContribution.startDate, startDate);
assertEq(queriedContribution.endDate, endDate);
Expand Down
4 changes: 1 addition & 3 deletions test/unit/tasks/TaskManager/CommitContributionTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "test/BaseTest.sol";
contract TaskManagerCommitContributionTest is BaseTest {
// Generic contribution values
bytes32 taskId;
bytes32 descriptionId;
uint256 role = 1;
uint32 startDate;
uint32 endDate;
Expand All @@ -28,12 +27,11 @@ contract TaskManagerCommitContributionTest is BaseTest {

// init Contribution for testing
taskId = taskRegistry.registerTask(Task({uri: "abcde"}));
descriptionId = taskFactory.registerDescription(Description({uri: "fghij"}));
startDate = uint32(block.timestamp);
endDate = startDate + 7 days;
contribution = Contribution({
taskId: taskId,
descriptionId: descriptionId,
uri: uri,
role: role,
startDate: startDate,
endDate: endDate,
Expand Down
6 changes: 2 additions & 4 deletions test/unit/tasks/TaskManager/CreateContributionsTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ contract TaskManagerCreateContributionsTest is BaseTest {

// Generic contribution values
bytes32 taskId;
bytes32 descriptionId;
uint256 role = 1;
uint32 startDate;
uint32 endDate;
uint32 points = 6;
uint128 quantity = 10;
string uri = "someUri";
string uri = "DiscordJoinIPFSUri";
Contribution contribution;

function setUp() public override {
Expand All @@ -25,12 +24,11 @@ contract TaskManagerCreateContributionsTest is BaseTest {

// init Contribution for testing
taskId = taskRegistry.registerTask(Task({uri: "abcde"}));
descriptionId = taskFactory.registerDescription(Description({uri: "fghij"}));
startDate = uint32(block.timestamp);
endDate = startDate + 7 days;
contribution = Contribution({
taskId: taskId,
descriptionId: descriptionId,
uri: uri,
role: role,
startDate: startDate,
endDate: endDate,
Expand Down
Loading

0 comments on commit 1ff6513

Please sign in to comment.