Skip to content

Commit

Permalink
store voters by tokenId (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketman-21 authored Oct 8, 2024
1 parent 610cb48 commit 6922e7c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ERC721Flow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract ERC721Flow is IERC721Flow, Flow {
fs.validateVotes(recipientIds, percentAllocations);
for (uint256 i = 0; i < tokenIds.length; i++) {
if (!canVoteWithToken(tokenIds[i], msg.sender)) revert NOT_ABLE_TO_VOTE_WITH_TOKEN();
_setVotesAllocationForTokenId(tokenIds[i], recipientIds, percentAllocations);
_setVotesAllocationForTokenId(tokenIds[i], recipientIds, percentAllocations, msg.sender);
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/Flow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,19 @@ abstract contract Flow is IFlow, UUPSUpgradeable, Ownable2StepUpgradeable, Reent
* @param bps The basis points of the vote to be split with the recipient.
* @param tokenId The tokenId owned by the voter.
* @param totalWeight The voting power of the voter.
* @param voter The address of the voter.
* @dev Requires that the recipient is valid, and the weight is greater than the minimum vote weight.
* Emits a VoteCast event upon successful execution.
*/
function _vote(bytes32 recipientId, uint32 bps, uint256 tokenId, uint256 totalWeight) internal {
function _vote(bytes32 recipientId, uint32 bps, uint256 tokenId, uint256 totalWeight, address voter) internal {
// calculate new member units for recipient and create vote
(uint128 memberUnits, address recipientAddress, RecipientType recipientType) = fs.createVote(
recipientId,
bps,
tokenId,
totalWeight,
PERCENTAGE_SCALE
PERCENTAGE_SCALE,
voter
);

// update member units
Expand Down Expand Up @@ -167,21 +169,23 @@ abstract contract Flow is IFlow, UUPSUpgradeable, Ownable2StepUpgradeable, Reent
function _setVotesAllocationForTokenId(
uint256 tokenId,
bytes32[] memory recipientIds,
uint32[] memory percentAllocations
uint32[] memory percentAllocations,
address voter
) internal {
uint256 sum = 0;
// overflow should be impossible in for-loop index
for (uint256 i = 0; i < percentAllocations.length; i++) {
sum += percentAllocations[i];
}
if (sum != PERCENTAGE_SCALE) revert INVALID_BPS_SUM();
if (voter == address(0)) revert ADDRESS_ZERO();

// update member units for previous votes
_clearPreviousVotes(tokenId);

// set new votes
for (uint256 i = 0; i < recipientIds.length; i++) {
_vote(recipientIds[i], percentAllocations[i], tokenId, fs.tokenVoteWeight);
_vote(recipientIds[i], percentAllocations[i], tokenId, fs.tokenVoteWeight, voter);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NounsFlow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ contract NounsFlow is INounsFlow, Flow {
for (uint256 i = 0; i < tokenIds.length; i++) {
if (!verifier.canVoteWithToken(tokenIds[i], owner, msg.sender, ownershipProofs[i], delegateProof))
revert NOT_ABLE_TO_VOTE_WITH_TOKEN();
_setVotesAllocationForTokenId(tokenIds[i], recipientIds, percentAllocations);
_setVotesAllocationForTokenId(tokenIds[i], recipientIds, percentAllocations, msg.sender);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/library/FlowVotes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ library FlowVotes {
uint32 bps,
uint256 tokenId,
uint256 totalWeight,
uint256 percentageScale
uint256 percentageScale,
address voter
) public returns (uint128 memberUnits, address recipientAddress, FlowTypes.RecipientType recipientType) {
recipientAddress = fs.recipients[recipientId].recipient;
recipientType = fs.recipients[recipientId].recipientType;
Expand All @@ -29,6 +30,7 @@ library FlowVotes {

// update votes, track recipient, bps, and total member units assigned
fs.votes[tokenId].push(FlowTypes.VoteAllocation({ recipientId: recipientId, bps: bps, memberUnits: newUnits }));
fs.voters[tokenId] = voter;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/storage/FlowStorageV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ interface FlowTypes {
uint256 tokenVoteWeight;
// The mapping of a token to a list of votes allocations (recipient, BPS)
mapping(uint256 => VoteAllocation[]) votes;
// The mapping of a token to the address that voted with it
mapping(uint256 => address) voters;
}
}

Expand Down

0 comments on commit 6922e7c

Please sign in to comment.