Skip to content

Commit

Permalink
chore: interface docs (#126)
Browse files Browse the repository at this point in the history
* docs: wip

* docs: wip

* chore: interface docs, inheritance
  • Loading branch information
pegahcarter authored Oct 14, 2024
1 parent d57abda commit 8ddfc75
Show file tree
Hide file tree
Showing 21 changed files with 373 additions and 121 deletions.
22 changes: 9 additions & 13 deletions contracts/autid/AutID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {AutIDUtils} from "./AutIDUtils.sol";
import {IHub} from "../hub/interfaces/IHub.sol";
import {IHubRegistry} from "../hub/interfaces/IHubRegistry.sol";
import {IMembership} from "../membership/IMembership.sol";

contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, ERC2771ContextUpgradeable, IAutID {
error ConflictingRecord();
Expand Down Expand Up @@ -59,16 +60,6 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
emit LocalReputationSet(newLocalReputation);
}

/// @inheritdoc IAutID
function updateTokenURI(string memory uri) external {
address account = _msgSender();
_revertForZeroAddress(account);
uint256 tokenId = tokenIdForAccount[account];
_revertForInvalidTokenId(tokenId);

_setTokenURI(tokenId, uri);
}

/// @inheritdoc IAutID
function mint(
uint256 role,
Expand Down Expand Up @@ -96,8 +87,12 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
_joinHub(account, role, commitment, hub);
}

function listUserHubs(address user) external view returns (address[] memory) {
return IHubRegistry(hubRegistry).listUserHubs(user);
function getUserHubs(address user) external view returns (address[] memory) {
return IHubRegistry(hubRegistry).getUserHubs(user);
}

function currentRole(address hub, address user) external view returns (uint256) {
return IMembership(hub).currentRole(user);
}

// function userHubRole(address hub, address user) external view returns (uint256) {
Expand All @@ -116,6 +111,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
revert UntransferableToken();
}

/// @inheritdoc IAutID
function setTokenURI(string memory uri) external {
address account = _msgSender();
_revertForZeroAddress(account);
Expand All @@ -141,7 +137,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
_revertForZeroAddress(hubRegistryAddress);
_revertForZeroAddress(hub);
_revertForInvalidCommitment(commitment);
_revertForUncheckedHub(hubRegistryAddress, hub);
_revertIfHubDoesNotExist(hubRegistryAddress, hub);
_revertForCanNotJoinHub(hub, account, role);
_revertForMinCommitmentNotReached(hub, commitment);

Expand Down
4 changes: 2 additions & 2 deletions contracts/autid/AutIDUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ abstract contract AutIDUtils {
}
}

function _revertForUncheckedHub(address hubRegistry_, address hub) internal view {
if (!IHubRegistry(hubRegistry_).checkHub(hub)) {
function _revertIfHubDoesNotExist(address hubRegistry_, address hub) internal view {
if (!IHubRegistry(hubRegistry_).isHub(hub)) {
revert UncheckedHub();
}
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/autid/IAutID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ interface IAutID {
/// @notice Set LocalReputation contract address
function setLocalReputation(address) external;

/// @notice Update a metadata URI string associated with the sender's AutID NFT
function updateTokenURI(string memory uri) external;
/// @notice Set the metadata URI string associated with the sender's AutID NFT
function setTokenURI(string memory uri) external;

/// @notice Mint an AutID NFT and join a Hub community
/// @param role Role to join the Hub
Expand Down
27 changes: 20 additions & 7 deletions contracts/hub/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// MUTATIVE
// -----------------------------------------------------------

/// @inheritdoc IHub
function join(address who, uint256 role, uint8 _commitment) external {
IMembership(membership).join(who, role, _commitment);
}
Expand All @@ -120,21 +121,25 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// ADMIN-MANAGEMENT
// -----------------------------------------------------------

/// @inheritdoc IHub
function admins() external view returns (address[] memory) {
return _admins.values();
}

/// @inheritdoc IHub
function isAdmin(address who) public view returns (bool) {
return _admins.contains(who);
}

/// @inheritdoc IHub
function addAdmins(address[] calldata whos) external {
if (!isAdmin(msg.sender)) revert NotAdmin();
for (uint256 i = 0; i < whos.length; i++) {
_addAdmin(whos[i]);
}
}

/// @inheritdoc IHub
function addAdmin(address who) external {
if (!isAdmin(msg.sender)) revert NotAdmin();
_addAdmin(who);
Expand All @@ -147,6 +152,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
emit AdminGranted(who, address(this));
}

/// @inheritdoc IHub
function removeAdmin(address who) external {
if (!isAdmin(msg.sender)) revert NotAdmin();
if (msg.sender == who) revert AdminCannotRenounceSelf();
Expand All @@ -159,28 +165,29 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// VIEWS
// -----------------------------------------------------------

/// @inheritdoc IHub
function membersCount() external view returns (uint256) {
return IMembership(membership).membersCount();
}

/// @inheritdoc IHub
function isMember(address who) public view override returns (bool) {
return IMembership(membership).isMember(who);
}

function periodCount() external view returns (uint32) {
return TimeLibrary.periodId({period0Start: period0Start, timestamp: uint32(block.timestamp)});
}

/// @inheritdoc IHub
function roles() external view returns (uint256[] memory) {
return _roles.values();
}

function roleOf(address who) external view returns (uint256) {
/// @inheritdoc IHub
function currentRole(address who) public view returns (uint256) {
return IMembership(membership).currentRole(who);
}

/// @inheritdoc IHub
function hasRole(address who, uint256 role) external view returns (bool) {
// TODO
return currentRole(who) == role;
}

function hadRole(address who, uint256 role, uint32 periodId) external view returns (bool) {
Expand All @@ -191,6 +198,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// TODO
}

/// @inheritdoc IHub
function canJoin(address who, uint256 role) public view returns (bool) {
if (IMembership(membership).currentRole(who) != 0) {
return false;
Expand All @@ -206,11 +214,13 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
return true;
}

/// @inheritdoc IHub
function constraintFactor() external view returns (uint128) {
return
localConstraintFactor == 0 ? IGlobalParameters(globalParameters).constraintFactor() : localConstraintFactor;
}

/// @inheritdoc IHub
function penaltyFactor() external view returns (uint128) {
return localPenaltyFactor == 0 ? IGlobalParameters(globalParameters).penaltyFactor() : localPenaltyFactor;
}
Expand All @@ -228,6 +238,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
IHubDomainsRegistry(hubDomainsRegistry).registerDomain(domain_, hubAddress_, metadataUri_);
}

/// @inheritdoc IHub
function setConstraintFactor(uint128 newConstraintFactor) external {
if (!isAdmin(msg.sender)) revert NotAdmin();
if (newConstraintFactor != 0 && (newConstraintFactor < 1e16 || newConstraintFactor > 1e18))
Expand All @@ -237,6 +248,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
emit SetConstraintFactor(oldConstraintFactor, newConstraintFactor);
}

/// @inheritdoc IHub
function setPenaltyFactor(uint128 newPenaltyFactor) external {
if (!isAdmin(msg.sender)) revert NotAdmin();
if (newPenaltyFactor != 0 && (newPenaltyFactor < 1e16 || newPenaltyFactor < 1e18))
Expand Down Expand Up @@ -281,7 +293,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {

function _setRoles(uint256[] memory roles_) internal {
for (uint256 i = 0; i < roles_.length; i++) {
_roles.add(roles_[i]);
require(_roles.add(roles_[i]), "Cannot add duplicate roles");
}
}

Expand Down Expand Up @@ -347,6 +359,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// VIEWS
// -----------------------------------------------------------

// TODO: figure these out
function getDomain(string calldata domain) external view returns (address, string memory) {
return IHubDomainsRegistry(hubDomainsRegistry).getDomain(domain);
}
Expand Down
6 changes: 5 additions & 1 deletion contracts/hub/HubDomainsRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own
import "./interfaces/IHubDomainsRegistry.sol";
import "./interfaces/IHubRegistry.sol";

// TODO: register properly from a hub
// TODO: is verifier needed?
contract HubDomainsRegistry is IHubDomainsRegistry, OwnableUpgradeable {
struct Domain {
string name;
Expand Down Expand Up @@ -38,10 +40,11 @@ contract HubDomainsRegistry is IHubDomainsRegistry, OwnableUpgradeable {
}

modifier onlyFromHub() {
require(IHubRegistry(hubRegistry).checkHub(msg.sender));
require(IHubRegistry(hubRegistry).isHub(msg.sender));
_;
}

/// @inheritdoc IHubDomainsRegistry
function registerDomain(
string calldata domain,
address hubAddress,
Expand All @@ -59,6 +62,7 @@ contract HubDomainsRegistry is IHubDomainsRegistry, OwnableUpgradeable {
emit DomainRegistered(hubAddress, msg.sender, domain, tokenIdCounter, metadataUri);
}

/// @inheritdoc IHubDomainsRegistry
function getDomain(string calldata domain) external view returns (address, string memory) {
return (domains[domain].hubAddress, domains[domain].metadataUri);
}
Expand Down
48 changes: 25 additions & 23 deletions contracts/hub/HubRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import {IHubModule} from "./interfaces/IHubModule.sol";
contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradeable {
event HubCreated(address deployer, address hubAddress, uint256 market, uint256 commitment, string metadata);

mapping(address => address[]) public hubDeployers;
mapping(address => address[]) internal _userHubList;
mapping(address => mapping(address => uint256)) internal _userHubListIds;
mapping(address => bool) public checkHub;
mapping(address => address[]) public hubsDeployed;
mapping(address => address[]) public userHubs;
mapping(address => bool) public isHub;
address[] public hubs;

struct HubContracts {
Expand All @@ -49,6 +48,7 @@ contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradea

constructor(address trustedForwarder_) ERC2771ContextUpgradeable(trustedForwarder_) {}

/// @inheritdoc IHubRegistry
function initialize(
address autId_,
address hubLogic,
Expand Down Expand Up @@ -80,31 +80,37 @@ contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradea
taskManagerImplementation = _taskManagerImplementation;
}

/// @inheritdoc IHubRegistry
function setInitialContributionManager(address _initialContributionManager) public onlyOwner {
initialContributionManager = _initialContributionManager;
}

/// @inheritdoc IHubRegistry
function currentPeriodId() public view returns (uint32) {
return IGlobalParameters(globalParameters).currentPeriodId();
}

/// @inheritdoc IHubRegistry
function period0Start() public view returns (uint32) {
return IGlobalParameters(globalParameters).period0Start();
}

// the only reason for this function is to keep interface compatible with sdk
// `hubs` variable is public anyway
// the only reason for `hubs` variable is that TheGraph is not connected
/// @inheritdoc IHubRegistry
function getHubs() public view returns (address[] memory) {
return hubs;
}

// the same comment as for `getHubs` method
function getHubByDeployer(address deployer) public view returns (address[] memory) {
return hubDeployers[deployer];
/// @inheritdoc IHubRegistry
function getHubsDeployed(address who) public view returns (address[] memory) {
return hubsDeployed[who];
}

/// @dev depoloy beacon proxy for a new hub
/// @inheritdoc IHubRegistry
function getUserHubs(address who) external view returns (address[] memory) {
return userHubs[who];
}

/// @inheritdoc IHubRegistry
function deployHub(
uint256[] calldata roles,
uint256 market,
Expand Down Expand Up @@ -147,9 +153,9 @@ contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradea
// Set the initial contribution manager as stored in this contract
ITaskManager(taskManager).initialize2(initialContributionManager);

hubDeployers[_msgSender()].push(hub);
hubsDeployed[_msgSender()].push(hub);
hubs.push(hub);
checkHub[hub] = true;
isHub[hub] = true;
hubContracts[hub] = HubContracts({
taskFactory: taskFactory,
taskManager: taskManager,
Expand All @@ -160,18 +166,14 @@ contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradea
emit HubCreated(_msgSender(), hub, market, commitment, metadata);
}

function listUserHubs(address user) external view returns (address[] memory) {
return _userHubList[user];
}

function join(address hub, address member, uint256 role, uint8 commitment) external {
require(checkHub[hub], "HubRegistry: sender not a hub");
/// @inheritdoc IHubRegistry
function join(address hub, address who, uint256 role, uint8 commitment) external {
require(msg.sender == autId, "HubRegistry: sender not autId");
require(isHub[hub], "HubRegistry: hub does not exist");

IHub(hub).join({who: member, role: role, _commitment: commitment});
IHub(hub).join({who: who, role: role, _commitment: commitment});

uint256 position = _userHubList[member].length;
_userHubList[member].push(hub);
_userHubListIds[member][hub] = position;
userHubs[who].push(hub);
}

/// @dev upgrades hub beacon to the new logic contract
Expand Down
8 changes: 5 additions & 3 deletions contracts/hub/HubUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {StorageSlot} from "@openzeppelin/contracts/utils/StorageSlot.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

abstract contract HubUpgradeable is Initializable {
// 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50
bytes32 private constant _BEACON_SLOT = bytes32(uint256(keccak256("eip1967.proxy.beacon")));
bytes32 private constant _IMPLEMENTATION_SLOT = bytes32(uint256(keccak256("id.aut.os.proxy.implementation")) - 1);
// bytes32(uint256(keccak256("eip1967.proxy.beacon")))
bytes32 private constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;

// bytes32(uint256(keccak256("aut.hub.implementation")) - 1)
bytes32 private constant _IMPLEMENTATION_SLOT = 0xcd0912f71386cff9878081e1c75800d1c9ded2720c4f877d0b3f713d15203c60;

function implementation() external view returns (address) {
return _getImplementationSlot().value;
Expand Down
Loading

0 comments on commit 8ddfc75

Please sign in to comment.