Skip to content

Commit

Permalink
Add a proper documentation to the contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador committed Jan 23, 2025
1 parent 6d2b1fc commit 28f5216
Show file tree
Hide file tree
Showing 33 changed files with 654 additions and 199 deletions.
21 changes: 11 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";

/// @dev RelayProxy is a contract for relaying calls to another contract
contract RelayProxy {
address public __destination;
address public __owner;
Expand All @@ -11,35 +12,35 @@ contract RelayProxy {
__destination = _destination;
}

/// @dev Emitted when ownership of the contract is transferred.
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/// @dev Emitted when ownership of the contract is transferred.
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.
*/
/// @dev 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;
}

/// @dev Sets the destination of the relay.
/// @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");
emit OwnershipTransferred(__destination, newDestination);
__destination = newDestination;
}

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

/// @dev Returns true if 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";

// todo maybe move to governable
/// @dev SFC is an interface which implements some functions from the SFC required for the Governance contract.
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);
}

contract SFCToGovernable is Governable {
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 used for decimals, 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";

/// @dev StatusConstants is a contract for managing status constants
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;

/// @dev 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();
}

/// @dev 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

0 comments on commit 28f5216

Please sign in to comment.