Skip to content

Commit

Permalink
refactor: add address existence check
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Mar 15, 2024
1 parent 0ddd058 commit 3050ada
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions contracts/VoteWeighting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ contract VoteWeighting is IErrors {
// Contract owner address
address public owner;

// Gauge parameters
// All numbers are "fixed point" on the basis of 1e18
// TODO: Convert both to cyclic map
// Set of gauges
address[] public gauges;
// mapping of gauges
mapping(address => bool) public mapGauges;

// user -> gauge_addr -> VotedSlope
mapping(address => mapping(address => VotedSlope)) public vote_user_slopes;
Expand Down Expand Up @@ -155,6 +157,11 @@ contract VoteWeighting is IErrors {
/// @param gauge_addr Address of the gauge.
/// @return Gauge weight.
function _getWeight(address gauge_addr) internal returns (uint256) {
// Check that the gauge exists
if (!mapGauges[gauge_addr]) {
revert("Does not exist");

Check warning on line 162 in contracts/VoteWeighting.sol

View workflow job for this annotation

GitHub Actions / build

Use Custom Errors instead of revert statements
}

uint256 t = time_weight[gauge_addr];
if (t > 0) {
Point memory pt = points_weight[gauge_addr][t];
Expand Down Expand Up @@ -188,9 +195,10 @@ contract VoteWeighting is IErrors {
/// @param addr Gauge address.
/// @param weight Gauge weight.
function add_gauge(address addr, uint256 weight) external {

Check warning on line 197 in contracts/VoteWeighting.sol

View workflow job for this annotation

GitHub Actions / build

Function name must be in mixedCase
require(msg.sender == owner, "Only owner can add gauge");
// TODO: Check that the addr was not added before?
//require(gauge_types_[addr] == 0, "Cannot add the same gauge twice");
if (mapGauges[addr]) {
revert("Cannot add the same gauge twice");
}
mapGauges[addr] = true;

gauges.push(addr);

Expand Down Expand Up @@ -266,7 +274,6 @@ contract VoteWeighting is IErrors {
/// @notice Get gauge weight normalized to 1e18 and also fill all the unfilled values for type and gauge records.
/// @dev Any address can call, however nothing is recorded if the values are filled already.
/// @param addr Gauge address.
/// @param time Relative weight at the specified timestamp in the past or present.
/// @return Value of relative weight normalized to 1e18.
function gauge_relative_weight_write_now(address addr) external returns (uint256) {
_getWeight(addr);
Expand Down Expand Up @@ -384,15 +391,13 @@ contract VoteWeighting is IErrors {

/// @notice Gets Gauge relative weight normalized to 1e18 at the last available timestamp.
/// @param addr Gauge address.
/// @param time Relative weight at the last available timestamp (must be recorded before that).
/// @return Value of relative weight normalized to 1e18.
function gauge_relative_weight_last(address addr) external view returns (uint256) {
return _gauge_relative_weight(addr, time_sum);
}

/// @notice Gets Gauge relative weight normalized to 1e18 at the block timestamp.
/// @param addr Gauge address.
/// @param time Relative weight at the block timestamp.
/// @return Value of relative weight normalized to 1e18.
function gauge_relative_weight_now(address addr) external view returns (uint256) {
return _gauge_relative_weight(addr, block.timestamp);
Expand Down

0 comments on commit 3050ada

Please sign in to comment.