Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoAcosta committed Oct 12, 2024
1 parent 21e928c commit a554c00
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/points/FollowerSincePoints.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Points } from "./Points.sol";
import { IFollowerSincePoints } from "./interfaces/IFollowerSincePoints.sol";
import { IFollowerSinceStamp } from "../stamps/interfaces/IFollowerSinceStamp.sol";
Expand All @@ -10,7 +9,7 @@ import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
/// @title FollowerSincePoints - Non-transferable token based on follower duration
/// @notice Calculates and manages points based on how long a user has been a follower
/// @dev Inherits from Points and implements IFollowerSincePoints
contract FollowerSincePoints is Points, IFollowerSincePoints {
contract FollowerSincePoints is IFollowerSincePoints, Points {
/// @inheritdoc IFollowerSincePoints
IFollowerSinceStamp public immutable override followerStamp;

Expand Down
7 changes: 3 additions & 4 deletions src/points/MultipleFollowerSincePoints.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Points } from "./Points.sol";
import { IFollowerSinceStamp } from "../stamps/interfaces/IFollowerSinceStamp.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
Expand All @@ -26,13 +25,13 @@ contract MultipleFollowerSincePoints is Points, IMultipleFollowerSincePoints {
}

/// @inheritdoc IMultipleFollowerSincePoints
function stamps() public view returns (StampInfo[] memory) {
function stamps() external view returns (StampInfo[] memory) {
return _stamps;
}

/// @inheritdoc IMultipleFollowerSincePoints
function stampByIndex(uint256 index) public view returns (StampInfo memory) {
require(index < _stamps.length, "Index out of bounds");
function stampByIndex(uint256 index) external view returns (StampInfo memory) {
if (index >= _stamps.length) revert IndexOutOfBounds();
return _stamps[index];
}

Expand Down
6 changes: 5 additions & 1 deletion src/points/interfaces/IMultipleFollowerSincePoints.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ interface IMultipleFollowerSincePoints is IPoints {
/// @dev This error is used in functions that expect arrays of equal length
error ArrayLengthMismatch();

/// @notice Custom error thrown when attempting to access an index that is out of the valid range
/// @dev This error is used when trying to access a stamp at an invalid index in the stamps array
error IndexOutOfBounds();

/// @notice Struct to encapsulate information about each follower stamp and its point multiplier
/// @dev Combines an IFollowerSinceStamp with its corresponding point multiplier for efficient storage and retrieval
struct StampInfo {
Expand All @@ -25,7 +29,7 @@ interface IMultipleFollowerSincePoints is IPoints {
function stamps() external view returns (StampInfo[] memory);

/// @notice Fetches the stamp information at a specific index in the stamps array
/// @dev This function should revert if the provided index is out of bounds
/// @dev This function should revert with IndexOutOfBounds if the provided index is out of bounds
/// @param index The zero-based index of the stamp in the stamps array
/// @return A StampInfo struct containing the stamp and multiplier information at the given index
function stampByIndex(uint256 index) external view returns (StampInfo memory);
Expand Down
107 changes: 107 additions & 0 deletions src/voting/Question.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

abstract contract Question is Ownable {
// State variables
string public title;
string public description;
uint256 public deadline;

// Structs
struct Option {
string title;
string description;
}

// Arrays
Option[] public options;

// Mappings
mapping(address => uint256) public voterToOptionId;
mapping(uint256 => uint256) public voteCounts;

// Events
event QuestionUpdated(string newTitle, string newDescription, uint256 newDeadline);
event Voted(address indexed voter, uint256 indexed optionId, uint256 timestamp);
event NewOption(address indexed proposer, uint256 indexed optionId, string title);

// Errors
error VotingEnded();
error AlreadyVoted();
error InvalidOption();

// IERC20 points variable
IERC20 public points;

// Constructor
constructor(
address initialOwner,
string memory _title,
string memory _description,
uint256 _deadline,
address _pointsAddress
) Ownable(initialOwner) {
title = _title;
description = _description;
deadline = _deadline;
points = IERC20(_pointsAddress);

// Add an empty option at index 0
options.push(Option("", ""));
}

// External functions
function vote(uint256 optionId) public virtual {
if (block.timestamp >= deadline) revert VotingEnded();
if (optionId == 0 || optionId > options.length) revert InvalidOption();

_beforeVoting(optionId);
_vote(optionId);

uint256 timestamp = (block.timestamp / 1 days) * 1 days;
emit Voted(msg.sender, optionId, timestamp);
}

function getOptions() public view virtual returns (Option[] memory) {
return options;
}

function getOption(uint256 optionId) public view virtual returns (Option memory) {
if (optionId == 0 || optionId > options.length) revert InvalidOption();
return options[optionId - 1];
}

function getOptionVoteCount(uint256 optionId) public view virtual returns (uint256);

function getOptionPointsAccrued(uint256 optionId) public view virtual returns (uint256);

// External functions (onlyOwner)
function updateTitle(string memory _title) external onlyOwner {
title = _title;
emit QuestionUpdated(_title, description, deadline);
}

function updateDescription(string memory _description) external onlyOwner {
description = _description;
emit QuestionUpdated(title, _description, deadline);
}

function updateDeadline(uint256 _deadline) external onlyOwner {
deadline = _deadline;
emit QuestionUpdated(title, description, _deadline);
}

// Internal functions
function _beforeVoting(uint256 optionId) internal virtual;

function _vote(uint256 optionId) internal virtual;

function _addOption(string memory _title, string memory _description) internal virtual {
uint256 optionId = options.length + 1;
options.push(Option(_title, _description));
emit NewOption(msg.sender, optionId, _title);
}
}

0 comments on commit a554c00

Please sign in to comment.