Skip to content

Commit

Permalink
Merge pull request #99 from Aut-Labs/refactor/base-test
Browse files Browse the repository at this point in the history
Refactor/base test
  • Loading branch information
pegahcarter authored Jul 29, 2024
2 parents c48c148 + 84cdcb6 commit 19523af
Show file tree
Hide file tree
Showing 20 changed files with 10,155 additions and 32,901 deletions.
14 changes: 7 additions & 7 deletions contracts/autid/AutID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
address public localReputation;
mapping(bytes32 => uint256) public tokenIdForUsername;
mapping(address => uint256) public tokenIdForAccount;
mapping(address => uint64) public mintedAt;
mapping(address => uint32) public mintedAt;

constructor(address trustedForwarder_) ERC2771ContextUpgradeable(trustedForwarder_) {}

Expand Down Expand Up @@ -72,7 +72,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
/// @inheritdoc IAutID
function mint(
uint256 role,
uint256 commitment,
uint8 commitment,
address nova,
string memory username_,
string memory optionalURI
Expand All @@ -83,14 +83,14 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
/// @inheritdoc IAutID
function createRecordAndJoinNova(
uint256 role,
uint256 commitment,
uint8 commitment,
address nova,
string memory username_,
string memory optionalURI
) public {
address account = _msgSender();
AutIDUtils._revertForZeroAddress(account);
mintedAt[account] = uint64(block.timestamp);
mintedAt[account] = uint32(block.timestamp);

_createRecord(account, username_, optionalURI);
_joinNova(account, role, commitment, nova);
Expand All @@ -105,7 +105,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
}

function userNovaCommitmentLevel(address nova, address user) external view returns (uint256) {
return INova(nova).commitmentLevels(user);
return INova(nova).currentCommitmentLevels(user);
}

function userNovaJoinedAt(address nova, address user) external view returns (uint256) {
Expand All @@ -127,7 +127,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
}

/// @inheritdoc IAutID
function joinNova(uint256 role, uint256 commitment, address nova) public {
function joinNova(uint256 role, uint8 commitment, address nova) public {
address account = _msgSender();
_revertForZeroAddress(account);
uint256 tokenId = tokenIdForAccount[account];
Expand All @@ -136,7 +136,7 @@ contract AutID is AutIDUtils, ERC721URIStorageUpgradeable, OwnableUpgradeable, E
_joinNova(account, role, commitment, nova);
}

function _joinNova(address account, uint256 role, uint256 commitment, address nova) internal {
function _joinNova(address account, uint256 role, uint8 commitment, address nova) internal {
address novaRegistryAddress = novaRegistry;
_revertForZeroAddress(novaRegistryAddress);
_revertForZeroAddress(nova);
Expand Down
8 changes: 4 additions & 4 deletions contracts/autid/IAutID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.20;

interface IAutID {
event RecordCreated(uint256 tokenId, address account, string username, string uri);
event NovaJoined(address account, uint256 role, uint256 commitment, address nova);
event NovaJoined(address account, uint256 role, uint8 commitment, address nova);
event NovaRegistrySet(address);
event LocalReputationSet(address);
event TokenMetadataUpdated(uint256 tokenId, address account, string uri);
Expand Down Expand Up @@ -41,7 +41,7 @@ interface IAutID {
/// @param optionalURI Metadata URI string associated with the new AutID NFT
function mint(
uint256 role,
uint256 commitment,
uint8 commitment,
address nova,
string memory username_,
string memory optionalURI
Expand All @@ -55,7 +55,7 @@ interface IAutID {
/// @param optionalURI Metadata URI string associated with the new AutID NFT
function createRecordAndJoinNova(
uint256 role,
uint256 commitment,
uint8 commitment,
address nova,
string memory username_,
string memory optionalURI
Expand All @@ -65,5 +65,5 @@ interface IAutID {
/// @param role Role to join the Nova
/// @param commitment Commitment to join the Nova
/// @param nova Address of the Nova to join
function joinNova(uint256 role, uint256 commitment, address nova) external;
function joinNova(uint256 role, uint8 commitment, address nova) external;
}
4 changes: 2 additions & 2 deletions contracts/globalParameters/GlobalParametersAlpha.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract GlobalParametersAlpha is IGlobalParametersAlpha, OwnableUpgradeable {

/// @dev fill with default values
function initialize() external initializer {
period0Start = TimeLibrary.periodStart({timestamp: block.timestamp});
period0Start = TimeLibrary.periodStart({timestamp: uint32(block.timestamp)});
periodDuration = TimeLibrary.FOUR_WEEKS;
steepnessDegree3Exp = 300;
penaltyFactor3Exp = 500;
Expand All @@ -57,7 +57,7 @@ contract GlobalParametersAlpha is IGlobalParametersAlpha, OwnableUpgradeable {
}

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

function stagePeriodDuration(uint32 nextPeriodDuration) external {
Expand Down
2 changes: 1 addition & 1 deletion contracts/interactions/InteractionFactory.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
pragma solidity ^0.8.20;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {
Expand Down
2 changes: 1 addition & 1 deletion contracts/interactions/InteractionRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
pragma solidity ^0.8.20;

import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/TimeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ library TimeLibrary {
/// @dev returns the id of the curent period, starting at 1
function periodId(uint32 period0Start, uint32 timestamp) internal pure returns (uint32) {
unchecked {
((periodStart({timestamp: timestamp}) - period0Start) / FOUR_WEEKS) + 1;
return (((periodStart({timestamp: timestamp}) - period0Start) / FOUR_WEEKS) + 1);
}
}
}
7 changes: 4 additions & 3 deletions contracts/nova/INova.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface INova {
error MemberHasNotJoinedHub();
error SameCommitmentLevel();
error InvalidCommitmentLevel();
error UserHasNotYetCommited();

event AdminGranted(address to);
event AdminRenounced(address from);
Expand All @@ -37,8 +38,8 @@ interface INova {
function metadataUri() external view returns (string memory);

function roles(address) external view returns (uint256);
function commitmentLevels(address) external view returns (uint256);
function joinedAt(address) external view returns (uint256);
function currentCommitmentLevels(address) external view returns (uint8);
function joinedAt(address) external view returns (uint32);
function parameterWeight(uint256) external view returns (uint256);
function accountMasks(address) external view returns (uint256);

Expand All @@ -52,7 +53,7 @@ interface INova {

function removeUrl(string memory) external;

function join(address who, uint256 role, uint256 commitmentLevel) external;
function join(address who, uint256 role, uint8 commitmentLevel) external;

function canJoin(address who, uint256 role) external view returns (bool);

Expand Down
2 changes: 0 additions & 2 deletions contracts/nova/INovaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import "../hubContracts/IHubDomainsRegistry.sol";
interface INovaRegistry {
function checkNova(address) external view returns (bool);

function initialize(address autIdAddr_, address novaLogic, address pluginRegistry, address hubDomainsRegistry) external;

function deployNova(uint256 market, string memory metadata, uint256 commitment) external returns (address nova);

function joinNovaHook(address user) external;
Expand Down
32 changes: 16 additions & 16 deletions contracts/nova/Nova.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {INova} from "./INova.sol";
import {INovaRegistry} from "./INovaRegistry.sol";
import "../hubContracts/IHubDomainsRegistry.sol";
import {IGlobalParametersAlpha} from "../globalParameters/IGlobalParametersAlpha.sol";
import {TimeLibrary} from "../libraries/TimeLibrary.sol";

// todo: admin retro onboarding
contract Nova is INova, NovaUtils, NovaUpgradeable {
Expand Down Expand Up @@ -39,8 +40,8 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
uint32 public initPeriodId;

mapping(address => uint256) public roles;
mapping(address => uint256) public joinedAt;
mapping(address => uint256) public currentCommitmentLevels;
mapping(address => uint32) public joinedAt;
mapping(address => uint8) public currentCommitmentLevels;
mapping(uint256 => uint256) public parameterWeight;
mapping(address => uint256) public accountMasks;

Expand Down Expand Up @@ -91,7 +92,7 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
hubDomainsRegistry = hubDomainsRegistry_;
deployer = deployer_;

initTimestamp = block.timestamp;
initTimestamp = uint32(block.timestamp);
initPeriodId = IGlobalParametersAlpha(novaRegistry_).currentPeriodId();
}

Expand Down Expand Up @@ -131,21 +132,20 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
_removeUrl(url);
}

function join(address who, uint256 role, uint256 commitmentLevel) external {
function join(address who, uint256 role, uint8 commitmentLevel) external {
require(msg.sender == autID, "caller not AutID contract");
require(canJoin(who, role), "can not join");

roles[who] = role;
commitmentLevels[who] = commitmentLevel;
members.push(who);
joinedAt[who] = block.timestamp;
joinedAt[who] = uint32(block.timestamp);

uint32 currentPeriodId = IGlobalParametersAlpha(novaRegistry).currentPeriodId();
partipications[msg.sender][currentPeriodId].commitmentLevel = commitmentLevel;
currentCommitmentLevels[msg.sender] = commitmentLevel;
participations[who][currentPeriodId].commitmentLevel = commitmentLevel;
currentCommitmentLevels[who] = commitmentLevel;

_writeHistoricalSumCommitmentLevel(currentPeriodId);
currentSumCommitmentLevel += commitmentLevel;
currentSumCommitmentLevel += uint128(commitmentLevel);

INovaRegistry(novaRegistry).joinNovaHook(who);

Expand All @@ -156,7 +156,7 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
function getCommitmentLevel(address who, uint32 periodId) external view returns (uint32) {
if (periodId < getPeriodIdJoined(who)) revert UserHasNotYetCommited();

Participation memory participation = participations[who];
Participation memory participation = participations[who][periodId];
if (participation.commitmentLevel != 0) {
// user has changed their commitmentLevel in a period following `periodId`. We know this becuase
// participation.commitmentLevel state is non-zero as it is written following a commitmentLevel change.
Expand All @@ -177,8 +177,8 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
return periodIdJoined;
}

function changeCommitmentLevel(uint32 newCommitmentLevel) external {
uint32 oldCommitmentLevel = currentCommitmentLevels[msg.sender];
function changeCommitmentLevel(uint8 newCommitmentLevel) external {
uint8 oldCommitmentLevel = currentCommitmentLevels[msg.sender];
if (newCommitmentLevel == oldCommitmentLevel) revert SameCommitmentLevel();

// TODO: globalParam
Expand All @@ -188,7 +188,7 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
uint32 currentPeriodId = IGlobalParametersAlpha(novaRegistry).currentPeriodId();

// write to storage for all 0 values- as the currentCommitmentLevels is now different
for (uint256 i=currentPeriodId; i>periodIdJoined - 1; i--) {
for (uint32 i=currentPeriodId; i>periodIdJoined - 1; i--) {
Participation storage participation = participations[msg.sender][i];
if (participation.commitmentLevel == 0) {
participation.commitmentLevel = oldCommitmentLevel;
Expand All @@ -213,16 +213,16 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {

/// @notice write sumCommitmentLevel to history when needed
function writeHistoricalSumCommitmentLevel() public {
uint32 currentPeriodId = IGlobalParametersAlpha(novaRegistry_).currentPeriodId();
uint32 currentPeriodId = IGlobalParametersAlpha(novaRegistry).currentPeriodId();
_writeHistoricalSumCommitmentLevel(currentPeriodId);
}

function _writeHistoricalSumCommitmentLevel(uint32 _currentPeriodId) internal {
uint32 initPeriodId_ = initPeriodId; // gas
for (uint256 i=currentPeriodId - 1; i>initPeriodId_ - 1; i--) {
for (uint32 i=_currentPeriodId - 1; i>initPeriodId_ - 1; i--) {
if (historicalSumCommitmentLevel[i] == 0) {
// write current sum of commitment to storage as there is currently no stored value
historicalSumCommitmentLevel[i] = sumCommitmentLevel;
historicalSumCommitmentLevel[i] = currentSumCommitmentLevel;
} else {
// historical commitment levels are up to date- do nothing
break;
Expand Down
2 changes: 1 addition & 1 deletion contracts/nova/NovaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract NovaRegistry is INovaRegistry, ERC2771ContextUpgradeable, OwnableUpgrad
address public autIDAddr;
address public pluginRegistry;
address public hubDomainsRegistry;
address public glogalParameters;
address public globalParameters;
UpgradeableBeacon public upgradeableBeacon;
IAllowlist public allowlist;

Expand Down
Loading

0 comments on commit 19523af

Please sign in to comment.