Skip to content

Commit

Permalink
fix(autId): createRecordAndJoinHub() (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
pegahcarter authored Sep 17, 2024
1 parent 005304a commit 326e55c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
23 changes: 11 additions & 12 deletions contracts/autid/AutID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
uint256 role,
uint8 commitment,
address hub,
string memory username_,
string memory username,
string memory optionalURI
) external {
createRecordAndJoinHub(role, commitment, hub, username_, optionalURI);
createRecordAndJoinHub(role, commitment, hub, username, optionalURI);
}

/// @inheritdoc IAutID
function createRecordAndJoinHub(
uint256 role,
uint8 commitment,
address hub,
string memory username_,
string memory username,
string memory optionalURI
) public {
address account = _msgSender();
AutIDUtils._revertForZeroAddress(account);
mintedAt[account] = uint32(block.timestamp);

_createRecord(account, username_, optionalURI);
_createRecord(account, username, optionalURI);
_joinHub(account, role, commitment, hub);
}

Expand Down Expand Up @@ -146,28 +146,27 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
_revertForMinCommitmentNotReached(hub, commitment);

IHubRegistry(hubRegistryAddress).join({hub: hub, member: account, role: role, commitment: commitment});
IHub(hub).join(account, role, commitment);

emit HubJoined(account, role, commitment, hub);
}

function _createRecord(address account, string memory username_, string memory optionalURI) internal {
_revertForInvalidUsername(username_);
bytes32 username;
function _createRecord(address account, string memory username, string memory optionalURI) internal {
_revertForInvalidUsername(username);
bytes32 username_;
assembly {
username := mload(add(username_, 32))
username_ := mload(add(username, 32))
}
if (tokenIdForUsername[username] != 0 || tokenIdForAccount[account] != 0) {
if (tokenIdForUsername[username_] != 0 || tokenIdForAccount[account] != 0) {
revert ConflictingRecord();
}

uint256 tokenId = ++_tokenId;
_mint(account, tokenId);
_setTokenURI(tokenId, optionalURI);
tokenIdForUsername[username] = tokenId;
tokenIdForUsername[username_] = tokenId;
tokenIdForAccount[account] = tokenId;

emit RecordCreated(tokenId, account, username_, optionalURI);
emit RecordCreated(tokenId, account, username, optionalURI);
}

function _msgSender() internal view override(ERC2771ContextUpgradeable, ContextUpgradeable) returns (address) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/hub/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
}

function canJoin(address who, uint256 role) public view returns (bool) {
if (IMembership(participation).currentRole(who) != 0) {
if (IMembership(membership).currentRole(who) != 0) {
return false;
}

Expand Down
22 changes: 22 additions & 0 deletions test/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.21;

import "script/DeployAll.s.sol";
import { Hub } from "contracts/hub/Hub.sol";
import { console, StdAssertions, StdChains, StdCheats, stdError, StdInvariant, stdJson, stdMath, StdStorage, stdStorage, StdUtils, Vm, StdStyle, TestBase, Test } from "forge-std/Test.sol";

abstract contract BaseTest is Test {
Expand All @@ -10,6 +11,8 @@ abstract contract BaseTest is Test {
GlobalParameters public globalParameters;
HubDomainsRegistry public hubDomainsRegistry;

Hub public hub;

address public owner = address(this);
address public alice = address(0x411Ce);
address public bob = address(0xb0b);
Expand All @@ -20,13 +23,16 @@ abstract contract BaseTest is Test {
deploy.setUp();
deploy.setOwner(owner);
deploy.run();
vm.stopBroadcast();

// set env
autId = deploy.autId();
hubRegistry = deploy.hubRegistry();
globalParameters = deploy.globalParameters();
hubDomainsRegistry = deploy.hubDomainsRegistry();

_deployHub();

// labeling
vm.label(owner, "Owner");
vm.label(alice, "Alice");
Expand All @@ -36,4 +42,20 @@ abstract contract BaseTest is Test {
vm.label(address(globalParameters), "globalParameters");
vm.label(address(hubDomainsRegistry), "hubDomainsRegistry");
}

/// @dev deploy a basic hub
function _deployHub() internal {
uint256[] memory roles = new uint256[](3);
roles[0] = 1;
roles[1] = 2;
roles[2] = 3;

address hubAddress = hubRegistry.deployHub({
roles: roles,
market: 1,
metadata: "Mock Metadata",
commitment: 1
});
hub = Hub(hubAddress);
}
}
10 changes: 9 additions & 1 deletion test/unit/autId/AutIDCreateRecordAndJoinNovaUnitTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ contract AutIDCreateRecordandJoinHubUnitTest is BaseTest {
}

function test_createRecordAndJoinHub_succeeds() public {
// TODO

vm.prank(alice);
autId.createRecordAndJoinHub({
role: 1,
commitment: 1,
hub: address(hub),
username: "alice",
optionalURI: "https://facebook.com/alice"
});

// check autId.mintedAt()

Expand Down

0 comments on commit 326e55c

Please sign in to comment.