Skip to content

Commit

Permalink
fix: small fixes to delegate set
Browse files Browse the repository at this point in the history
  • Loading branch information
agusduha committed Jan 3, 2024
1 parent 845b3db commit da0db81
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions solidity/contracts/governance/utils/DelegateSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ library DelegateSet {
* present.
*/
function remove(Set storage set, IWonderVotes.Delegate memory _delegate) internal returns (bool) {
return remove(set, _delegate.account);
}

/**
* @dev Removes a delegate from a set. O(1).
*
* Returns true if the delegate was removed from the set, that is if it was
* present.
*/
function remove(Set storage set, address _account) internal returns (bool) {
// We cache the delegate's position to prevent multiple reads from the same storage slot
uint256 position = set._positions[_delegate.account];
uint256 position = set._positions[_account];

if (position != 0) {
// Equivalent to contains(set, delegate)
Expand All @@ -84,7 +94,7 @@ library DelegateSet {
set._delegates.pop();

// Delete the tracked position for the deleted slot
delete set._positions[_delegate.account];
delete set._positions[_account];

return true;
} else {
Expand Down Expand Up @@ -123,14 +133,14 @@ library DelegateSet {
*
* - `index` must be strictly less than {length}.
*/
function at(Set storage set, uint256 index) internal view returns (IWonderVotes.Delegate memory) {
function at(Set storage set, uint256 index) internal view returns (IWonderVotes.Delegate storage) {
return set._delegates[index];
}

/**
* @dev Returns the delegate from the set with the given account. O(1).
*/
function get(Set storage set, address _account) internal view returns (IWonderVotes.Delegate memory) {
function get(Set storage set, address _account) internal view returns (IWonderVotes.Delegate storage) {
return at(set, set._positions[_account]);
}

Expand Down

0 comments on commit da0db81

Please sign in to comment.