Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a proper documentation to the contracts #32

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions contracts/adapters/RelayProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pragma solidity ^0.5.0;

import "../ownership/Ownable.sol";

/// @notice RelayProxy relays calls from the owner to the predefined destination contract
contract RelayProxy {
address public __destination;
address public __owner;
Expand All @@ -11,35 +12,36 @@ contract RelayProxy {
__destination = _destination;
}

/// @notice Emitted when ownership of the contract is transferred.
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/// @notice Emitted when destination of the contract is changed.
event DestinationChanged(address indexed previousRelay, address indexed newRelay);

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
/// @notice Transfers ownership of the contract to a new account. - Can only be called by the current owner.
/// @param newOwner The address to transfer ownership to.
function __transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "Relay: new owner is the zero address");
emit OwnershipTransferred(__owner, newOwner);
__owner = newOwner;
}

/// @notice Sets the destination of the relay - Can only be called by the current owner.
/// @param newDestination The address to relay calls to.
function __setDestination(address newDestination) public onlyOwner {
require(newDestination != address(0), "new owner address is the zero address");
// todo this should emit DestinationChanged
emit OwnershipTransferred(__destination, newDestination);
__destination = newDestination;
}

/**
* @dev Returns true if the caller is the current owner.
*/

/// @dev Returns whether the caller is the current owner.
function isOwner() internal view returns (bool) {
return msg.sender == __owner;
}

/**
* @dev Throws if called by any account other than the owner.
*/

/// @dev Throws if called by any account other than the owner.
modifier onlyOwner() {
require(isOwner(), "Relay: caller is not the owner");
_;
Expand Down
56 changes: 41 additions & 15 deletions contracts/adapters/SFCToGovernable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,74 @@ pragma solidity ^0.5.0;

import "../model/Governable.sol";

/// @dev SFC is representation of the network SFC contract for the purpose of the Governance contract. It provides weights of individual voters.
interface SFC {
function getStake(address _from, uint256 _toValidatorID) external view returns (uint256);
/// @dev Get the current stake of a delegator for a specific validator.
/// @param delegator The address of the delegator.
/// @param toValidatorID The ID of the validator to whom the stake is delegated.
/// @return The amount of stake delegated by given delegator to the specified validator.
function getStake(address delegator, uint256 toValidatorID) external view returns (uint256);

function getValidator(uint256 _validatorID) external view returns (uint256 status, uint256 deactivatedTime, uint256 deactivatedEpoch,
uint256 receivedStake, uint256 createdEpoch, uint256 createdTime, address auth);
/// @dev Get information about validator for the given ID.
/// @param validatorID The ID of the validator.
/// @return status The information about the validator.
function getValidator(uint256 validatorID) external view returns (
uint256 status,
uint256 receivedStake,
address auth,
uint256 createdEpoch,
uint256 createdTime,
uint256 deactivatedTime,
uint256 deactivatedEpoch
);

function getValidatorID(address _addr) external view returns (uint256);
/// @dev Get the current stake of a delegator for a specific validator.
/// @param validator The address of the validator.
/// @return The ID of the validator.
function getValidatorID(address validator) external view returns (uint256);

function totalActiveStake() external view returns (uint256);
/// @dev Get the sum of all active delegated stakes.
function getTotalActiveStake() external view returns (uint256);
}

// @dev SFCToGovernable is an adapter allowing to use the network SFC contract as Governable (governance votes weights provider).
contract SFCToGovernable is Governable {
cabrador marked this conversation as resolved.
Show resolved Hide resolved
SFC internal sfc = SFC(address(0xFC00FACE00000000000000000000000000000000));

// Gets the total weight of voters
/// @dev Retrieves the total active stake across all validators.
/// @return The sum of all active delegated stakes.
function getTotalWeight() external view returns (uint256) {
return sfc.totalActiveStake();
return sfc.getTotalActiveStake();
}

// Gets the received delegated weight
function getReceivedWeight(address addr) external view returns (uint256) {
uint256 validatorID = sfc.getValidatorID(addr);
/// @dev Retrieves the total delegated stake received by a specific validator.
/// @param validator The address of the validator whose received stake is being queried.
/// @return The total amount of stake delegated to the specified validator.
function getReceivedWeight(address validator) external view returns (uint256) {
uint256 validatorID = sfc.getValidatorID(validator);
if (validatorID == 0) {
return 0;
}
(uint256 status, , , uint256 receivedStake, , ,) = sfc.getValidator(validatorID);
(uint256 status, uint256 receivedStake, , , , ,) = sfc.getValidator(validatorID);
if (status != 0) {
return 0;
}
return receivedStake;
}

// Gets the voting weight which is delegated from the specified address to the specified address
function getWeight(address from, address to) external view returns (uint256) {
uint256 toValidatorID = sfc.getValidatorID(to);
/// @dev Retrieves the voting weight of a given delegator for a specified validator.
/// @param delegator The address of the delegator whose voting weight is being queried.
/// @param validator The address of the validator to whom the stake is delegated.
/// @return The voting weight (stake) of the delegator for the specified validator.
function getWeight(address delegator, address validator) external view returns (uint256) {
uint256 toValidatorID = sfc.getValidatorID(validator);
if (toValidatorID == 0) {
return 0;
}
(uint256 status, , , , , ,) = sfc.getValidator(toValidatorID);
if (status != 0) {
return 0;
}
return sfc.getStake(from, toValidatorID);
return sfc.getStake(delegator, toValidatorID);
}
}
3 changes: 2 additions & 1 deletion contracts/common/Decimal.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pragma solidity ^0.5.0;

/// @dev Decimal is a library for handling decimal numbers
library Decimal {
// unit is used for decimals, e.g. 0.123456
/// @dev unit is a fixed point decimal e.g. 0.123456
function unit() internal pure returns (uint256) {
return 1e18;
}
Expand Down
9 changes: 9 additions & 0 deletions contracts/governance/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.5.0;
import "../common/SafeMath.sol";
import "../common/Decimal.sol";

/// @notice StatusConstants defines status of governance proposals.
contract StatusConstants {
enum Status {
INITIAL,
Expand Down Expand Up @@ -45,13 +46,21 @@ contract StatusConstants {
uint256 constant TASK_VOTING = 1;
}

/// @dev Constants is a contract for managing constants
contract Constants is StatusConstants {
using SafeMath for uint256;

/// @notice calculates the minimum number of votes required for a proposal
/// @param totalWeight The total weight of the voters
/// @param minVotesRatio The minimum ratio of votes required
/// @return The minimum number of votes required
function minVotesAbsolute(uint256 totalWeight, uint256 minVotesRatio) public pure returns (uint256) {
return totalWeight * minVotesRatio / Decimal.unit();
}

/// @notice converts bytes32 to string
/// @param _bytes32 The bytes32 to convert
/// @return The converted string
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory) {
bytes memory bytesArray = new bytes(32);
for (uint256 i; i < 32; i++) {
Expand Down
Loading