Skip to content

Commit

Permalink
Introduce UxGate extension WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Feb 24, 2023
1 parent d60cbb8 commit 0cc0363
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 0 deletions.
172 changes: 172 additions & 0 deletions contracts/extensions/UxGate.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
This file is part of The Colony Network.
The Colony Network is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Colony Network is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Colony Network. If not, see <http://www.gnu.org/licenses/>.
*/

pragma solidity 0.7.3;
pragma experimental ABIEncoderV2;

import "./../../lib/dappsys/erc20.sol";
import "./../colonyNetwork/IColonyNetwork.sol";
import "./ColonyExtensionMeta.sol";

// ignore-file-swc-108


contract UxGate is ColonyExtensionMeta {

// Constants

// Events

event StakeSubmitted(bytes32 indexed stakeType, address indexed staker);

// Data structures

struct Stake {
bytes32 stakeType;
address staker;
uint256 amount;
uint256 cancelledAt;
}

// Storage

address colonyNetworkAddress;

// Parameters keyed by stake type
mapping (bytes32 => uint256) stakeFractions;
mapping (bytes32 => uint256) securityDelays;

uint256 numStakes;
mapping (uint256 => Stake) stakes;

// Modifiers

modifier onlyRootArchitect() {
require(colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Architecture), "ux-gate-caller-not-root-architect");
_;
}

// Overrides

/// @notice Returns the identifier of the extension
function identifier() public override pure returns (bytes32) {
return keccak256("UxGate");
}

/// @notice Returns the version of the extension
function version() public override pure returns (uint256) {
return 1;
}

/// @notice Configures the extension
/// @param _colony The colony in which the extension holds permissions
function install(address _colony) public override auth {
require(address(colony) == address(0x0), "extension-already-installed");

colony = IColony(_colony);
colonyNetworkAddress = colony.getColonyNetwork();
}

/// @notice Called when upgrading the extension
function finishUpgrade() public override auth {}

/// @notice Called when deprecating (or undeprecating) the extension
function deprecate(bool _deprecated) public override auth {
deprecated = _deprecated;
}

/// @notice Called when uninstalling the extension
function uninstall() public override auth {
selfdestruct(address(uint160(address(colony))));
}

// Public

function setStakeFraction(bytes32 _stakeType, uint256 _stakeFraction) public onlyRootArchitect {
stakeFractions[_stakeType] = _stakeFraction;
}

function setSecurityDelay(bytes32 _stakeType, uint256 securityDelay) public onlyRootArchitect {
securityDelays[_stakeType] = securityDelay;
}

function submitStake(
bytes32 _stakeType,
bytes memory _colonyKey,
bytes memory _colonyValue,
uint256 _colonyBranchMask,
bytes32[] memory _colonySiblings,
bytes memory _userKey,
bytes memory _userValue,
uint256 _userBranchMask,
bytes32[] memory _userSiblings
)
public
notDeprecated
{
bytes32 rootHash = IColonyNetwork(colonyNetworkAddress).getReputationRootHash();
uint256 rootSkillId = colony.getDomain(1).skillId;

uint256 colonyReputation = checkReputation(rootHash, rootSkillId, address(0x0), _colonyKey, _colonyValue, _colonyBranchMask, _colonySiblings);
uint256 userReputation = checkReputation(rootHash, rootSkillId, msgSender(), _userKey, _userValue, _userBranchMask, _userSiblings);

uint256 requiredStake = getRequiredReputation(_stakeType, colonyReputation);
require(userReputation >= requiredStake, "ux-gate-insufficient-rep");

stakes[++numStakes] = Stake({
staker: msgSender(),
stakeType: _stakeType,
amount: requiredStake,
cancelledAt: UINT256_MAX
});

colony.obligateStake(msgSender(), 1, requiredStake);

emit StakeSubmitted(_stakeType, msgSender());
}

function cancelStake() public {}

function reclaimStake() public {}

function slashStake() public {}

// View

function getNumStakes() external view returns (uint256) {
return numStakes;
}

function getStakeFraction(bytes32 _stakeType) external view returns (uint256) {
return stakeFractions[_stakeType];
}

function getSecurityDelay(bytes32 _stakeType) external view returns (uint256) {
return securityDelays[_stakeType];
}

function getStake(uint256 _id) external view returns (Stake memory stake) {
return stakes[_id];
}

// Internal

function getRequiredReputation(bytes32 _stakeType, uint256 _colonyRep) internal view returns (uint256) {
uint256 stakeFraction = stakeFractions[_stakeType] > 0 ? stakeFractions[_stakeType] : WAD;
return wmul(_colonyRep, stakeFraction);
}
}
2 changes: 2 additions & 0 deletions migrations/9_setup_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const VotingReputation = artifacts.require("./VotingReputation");
const VotingReputationMisalignedRecovery = artifacts.require("./VotingReputationMisalignedRecovery");
const TokenSupplier = artifacts.require("./TokenSupplier");
const Whitelist = artifacts.require("./Whitelist");
const UxGate = artifacts.require("./UxGate");

const Resolver = artifacts.require("./Resolver");
const EtherRouter = artifacts.require("./EtherRouter");
Expand Down Expand Up @@ -53,4 +54,5 @@ module.exports = async function (deployer, network, accounts) {
await addExtension(colonyNetwork, "TokenSupplier", "TokenSupplier", [TokenSupplier]);
await addExtension(colonyNetwork, "IVotingReputation", "VotingReputation", [VotingReputation, VotingReputationMisalignedRecovery]);
await addExtension(colonyNetwork, "Whitelist", "Whitelist", [Whitelist]);
await addExtension(colonyNetwork, "UxGate", "UxGate", [UxGate]);
};
Loading

0 comments on commit 0cc0363

Please sign in to comment.