From 1ff65137913cb95f73db2d510de708a2100b22a7 Mon Sep 17 00:00:00 2001 From: Carter Carlson Date: Wed, 30 Oct 2024 16:58:12 +0100 Subject: [PATCH] Refactor/contribution description (#137) * refactor: descriptionId => uri * ops: deploy v0.1.2 --- contracts/tasks/TaskFactory.sol | 72 ++----------------- contracts/tasks/interfaces/ITaskFactory.sol | 36 +--------- deployments.txt | 16 +++++ script/DeployAll.s.sol | 10 +-- test/BaseTest.sol | 6 +- .../TaskFactory/CreateContributionsTest.t.sol | 6 +- .../TaskManager/CommitContributionTest.t.sol | 4 +- .../TaskManager/CreateContributionsTest.t.sol | 6 +- .../TaskManager/GiveContributionTest.t.sol | 4 +- .../tasks/TaskRegistry/RegisterTasksTest.sol | 4 +- 10 files changed, 39 insertions(+), 125 deletions(-) diff --git a/contracts/tasks/TaskFactory.sol b/contracts/tasks/TaskFactory.sol index 853ac3f2..2ebd8133 100644 --- a/contracts/tasks/TaskFactory.sol +++ b/contracts/tasks/TaskFactory.sol @@ -4,7 +4,7 @@ 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"; @@ -12,15 +12,12 @@ import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet 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() { @@ -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(); @@ -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, @@ -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(); @@ -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, @@ -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)); diff --git a/contracts/tasks/interfaces/ITaskFactory.sol b/contracts/tasks/interfaces/ITaskFactory.sol index 0e9dc050..bd00006e 100644 --- a/contracts/tasks/interfaces/ITaskFactory.sol +++ b/contracts/tasks/interfaces/ITaskFactory.sol @@ -2,7 +2,7 @@ pragma solidity >=0.8.0; struct Contribution { bytes32 taskId; - bytes32 descriptionId; + string uri; uint256 role; uint32 startDate; uint32 endDate; @@ -10,10 +10,6 @@ struct Contribution { uint128 quantity; } -struct Description { - string uri; -} - interface ITaskFactory { error NotContributionId(); error TaskIdNotRegistered(); @@ -21,29 +17,19 @@ interface ITaskFactory { 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) @@ -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); } diff --git a/deployments.txt b/deployments.txt index 849e5070..0c54e326 100644 --- a/deployments.txt +++ b/deployments.txt @@ -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 + + diff --git a/script/DeployAll.s.sol b/script/DeployAll.s.sol index bd6bc4f6..7e2e0f4d 100644 --- a/script/DeployAll.s.sol +++ b/script/DeployAll.s.sol @@ -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"); @@ -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({ diff --git a/test/BaseTest.sol b/test/BaseTest.sol index 4d02cce5..466aa654 100644 --- a/test/BaseTest.sol +++ b/test/BaseTest.sol @@ -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"; @@ -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, @@ -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, diff --git a/test/unit/tasks/TaskFactory/CreateContributionsTest.t.sol b/test/unit/tasks/TaskFactory/CreateContributionsTest.t.sol index 9ee70c79..c1518477 100644 --- a/test/unit/tasks/TaskFactory/CreateContributionsTest.t.sol +++ b/test/unit/tasks/TaskFactory/CreateContributionsTest.t.sol @@ -7,7 +7,6 @@ contract TaskFactoryCreateContributionsTest is BaseTest { // Generic contribution values bytes32 taskId; - bytes32 descriptionId; uint256 role = 1; uint32 startDate; uint32 endDate; @@ -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, @@ -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); diff --git a/test/unit/tasks/TaskManager/CommitContributionTest.t.sol b/test/unit/tasks/TaskManager/CommitContributionTest.t.sol index 10a9d386..a17e96bf 100644 --- a/test/unit/tasks/TaskManager/CommitContributionTest.t.sol +++ b/test/unit/tasks/TaskManager/CommitContributionTest.t.sol @@ -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; @@ -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, diff --git a/test/unit/tasks/TaskManager/CreateContributionsTest.t.sol b/test/unit/tasks/TaskManager/CreateContributionsTest.t.sol index 1fb3a110..d0148adb 100644 --- a/test/unit/tasks/TaskManager/CreateContributionsTest.t.sol +++ b/test/unit/tasks/TaskManager/CreateContributionsTest.t.sol @@ -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 { @@ -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, diff --git a/test/unit/tasks/TaskManager/GiveContributionTest.t.sol b/test/unit/tasks/TaskManager/GiveContributionTest.t.sol index 7d72ddad..ae5a8ec2 100644 --- a/test/unit/tasks/TaskManager/GiveContributionTest.t.sol +++ b/test/unit/tasks/TaskManager/GiveContributionTest.t.sol @@ -6,7 +6,6 @@ import "test/BaseTest.sol"; contract TaskManagerGiveContributionTest is BaseTest { // Generic contribution values bytes32 taskId; - bytes32 descriptionId; uint256 role = 1; uint32 startDate; uint32 endDate; @@ -28,12 +27,11 @@ contract TaskManagerGiveContributionTest 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, diff --git a/test/unit/tasks/TaskRegistry/RegisterTasksTest.sol b/test/unit/tasks/TaskRegistry/RegisterTasksTest.sol index ecbf63fb..88fa65ab 100644 --- a/test/unit/tasks/TaskRegistry/RegisterTasksTest.sol +++ b/test/unit/tasks/TaskRegistry/RegisterTasksTest.sol @@ -8,7 +8,7 @@ contract TaskRegistryRegisterTasksUnitTest is BaseTest { function setUp() public override { super.setUp(); bytes32[] memory taskIds = taskRegistry.taskIds(); - assertEq(taskIds.length, 3); // registered in DeployAll + assertEq(taskIds.length, 11); // registered in DeployAll } function test_RegisterTasks_succeeds() public { @@ -26,7 +26,7 @@ contract TaskRegistryRegisterTasksUnitTest is BaseTest { bytes32[] memory taskIds = taskRegistry.taskIds(); assertEq({ left: taskIds.length, - right: 6, // 3 registered in DeployAll, 3 registered here + right: 14, // 11 registered in DeployAll, 3 registered here err: "Incorrect amount of task ids" }); }