Skip to content

Commit

Permalink
Merge pull request #101 from Aut-Labs/feat/sum-commitment
Browse files Browse the repository at this point in the history
feat: sum commitment level
  • Loading branch information
pegahcarter authored Jul 24, 2024
2 parents d1e1133 + 5136e7c commit e91ea05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions contracts/nova/INova.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface INova {
error MemberHasNotYetCommitted();
error MemberHasNotJoinedHub();
error SameCommitmentLevel();
error InvalidCommitmentLevel();

event AdminGranted(address to);
event AdminRenounced(address from);
Expand Down
45 changes: 38 additions & 7 deletions contracts/nova/Nova.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,26 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {

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

struct Participation {
uint32 commitmentLevel;
uint32 givenContributionPoints;
}

mapping(
address who => mapping(
uint32 periodId =>
Participation participation
)
) public participations;

mapping(
uint32 periodId =>
uint128 sumCommitmentLevel
) public historicalSumCommitmentLevel;
uint128 currentSumCommitmentLevel;

string[] private _urls;
mapping(bytes32 => uint256) private _urlHashIndex;
Expand Down Expand Up @@ -137,7 +142,10 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {

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

_writeHistoricalSumCommitmentLevel(currentPeriodId);
currentSumCommitmentLevel += commitmentLevel;

INovaRegistry(novaRegistry).joinNovaHook(who);

Expand All @@ -155,7 +163,7 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
return participation.commitmentLevel;
} else {
// User has *not* changed their commitment level: meaning their commitLevel is sync to current
return currentCommitmentLevel[who];
return currentCommitmentLevels[who];
}
}

Expand All @@ -170,7 +178,7 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
}

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

// TODO: globalParam
Expand All @@ -179,7 +187,7 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
uint32 periodIdJoined = getPeriodIdJoined(msg.sender);
uint32 currentPeriodId = IGlobalParametersAlpha(novaRegistry).currentPeriodId();

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

currentCommitmentLevel[msg.sender] = newCommitmentLevel;
currentCommitmentLevels[msg.sender] = newCommitmentLevel;

_writeHistoricalSumCommitmentLevel(currentPeriodId);
currentSumCommitmentLevel = currentSumCommitmentLevel - oldCommitmentLevel + newCommitmentLevel;

emit ChangeCommitmentLevel({
who: msg.sender,
Expand All @@ -199,6 +210,26 @@ contract Nova is INova, NovaUtils, NovaUpgradeable {
});
}


/// @notice write sumCommitmentLevel to history when needed
function writeHistoricalSumCommitmentLevel() public {
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--) {
if (historicalSumCommitmentLevel[i] == 0) {
// write current sum of commitment to storage as there is currently no stored value
historicalSumCommitmentLevel[i] = sumCommitmentLevel;
} else {
// historical commitment levels are up to date- do nothing
break;
}
}
}

/// @custom:sdk-legacy-interface-compatibility
function addAdmins(address[] calldata admins_) external {
_revertForNotAdmin(msg.sender);
Expand Down

0 comments on commit e91ea05

Please sign in to comment.