-
Notifications
You must be signed in to change notification settings - Fork 116
Introduce Korporatio extension #1122
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
Open
kronosapiens
wants to merge
3
commits into
develop
Choose a base branch
from
feat/korporatio
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| /* | ||
| 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.8.19; | ||
| pragma experimental ABIEncoderV2; | ||
|
|
||
| import "./../../lib/dappsys/erc20.sol"; | ||
| import "./../colonyNetwork/IColonyNetwork.sol"; | ||
| import "./ColonyExtensionMeta.sol"; | ||
|
|
||
| // ignore-file-swc-108 | ||
|
|
||
|
|
||
| contract Korporatio is ColonyExtensionMeta { | ||
|
|
||
| // Constants | ||
|
|
||
| // Events | ||
|
|
||
| event ApplicationCreated(uint256 indexed stakeId, address indexed applicant); | ||
| event ApplicationCancelled(uint256 indexed stakeId); | ||
| event StakeReclaimed(uint256 indexed stakeId); | ||
| event ApplicationDeleted(uint256 indexed stakeId, bool punish); | ||
| event ApplicationUpdated(uint256 indexed stakeId, bytes32 ipfsHash); | ||
| event ApplicationSubmitted(uint256 indexed stakeId); | ||
|
|
||
| // Data structures | ||
|
|
||
| struct Application { | ||
| address applicant; | ||
| uint256 stakeAmount; | ||
| uint256 cancelledAt; | ||
| } | ||
|
|
||
| // Storage | ||
|
|
||
| address colonyNetworkAddress; | ||
|
|
||
| uint256 stakeFraction; | ||
| uint256 repFraction; | ||
| uint256 claimDelay; | ||
|
|
||
| uint256 numApplications; | ||
| mapping (uint256 => Application) applications; | ||
|
|
||
| // Modifiers | ||
|
|
||
| modifier onlyApplicant(uint256 _applicationId) { | ||
| require(msgSender() == applications[_applicationId].applicant, "korporatio-not-applicant"); | ||
| _; | ||
| } | ||
|
|
||
| // Overrides | ||
|
|
||
| /// @notice Returns the identifier of the extension | ||
| function identifier() public override pure returns (bytes32) { | ||
| return keccak256("Korporatio"); | ||
| } | ||
|
|
||
| /// @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(payable(address(colony))); | ||
| } | ||
|
|
||
| // Public | ||
|
|
||
| function initialise(uint256 _stakeFraction, uint256 _repFraction, uint256 _claimDelay) public { | ||
| require( | ||
| colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Architecture), | ||
| "korporatio-not-root-architect" | ||
| ); | ||
|
|
||
| stakeFraction = _stakeFraction; | ||
| repFraction = _repFraction; | ||
| claimDelay = _claimDelay; | ||
| } | ||
|
|
||
| function createApplication( | ||
| bytes memory _colonyKey, | ||
| bytes memory _colonyValue, | ||
| uint256 _colonyBranchMask, | ||
| bytes32[] memory _colonySiblings, | ||
| bytes memory _userKey, | ||
| bytes memory _userValue, | ||
| uint256 _userBranchMask, | ||
| bytes32[] memory _userSiblings | ||
| ) | ||
| public | ||
| notDeprecated | ||
| { | ||
| require(stakeFraction > 0, "korporatio-not-initialised"); | ||
|
|
||
| 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 = wmul(colonyReputation, repFraction); | ||
| require(userReputation >= requiredStake, "korporatio-insufficient-rep"); | ||
|
|
||
| applications[++numApplications] = Application({ | ||
| applicant: msgSender(), | ||
| stakeAmount: requiredStake, | ||
| cancelledAt: UINT256_MAX | ||
| }); | ||
|
|
||
| colony.obligateStake(msgSender(), 1, requiredStake); | ||
|
|
||
| emit ApplicationCreated(numApplications, msgSender()); | ||
| } | ||
|
|
||
| function createApplication() public notDeprecated { | ||
| require ( | ||
| colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Root) || | ||
| colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Administration), | ||
| "korporatio-must-submit-stake" | ||
| ); | ||
|
|
||
| applications[++numApplications] = Application({ | ||
| applicant: msgSender(), | ||
| stakeAmount: 0, | ||
| cancelledAt: UINT256_MAX | ||
| }); | ||
|
|
||
| emit ApplicationCreated(numApplications, msgSender()); | ||
| } | ||
|
|
||
| function cancelApplication(uint256 _applicationId) public { | ||
| require(msgSender() == applications[_applicationId].applicant, "korporatio-not-applicant"); | ||
| applications[_applicationId].cancelledAt = block.timestamp; | ||
|
|
||
| emit ApplicationCancelled(_applicationId); | ||
| } | ||
|
|
||
| function reclaimStake(uint256 _applicationId) public { | ||
| Application storage application = applications[_applicationId]; | ||
| require(application.applicant == msgSender(), "korporatio-not-applicant"); | ||
| require(application.cancelledAt + claimDelay <= block.timestamp, "korporatio-cannot-reclaim"); | ||
|
|
||
| uint256 stakeAmount = application.stakeAmount; | ||
| delete applications[_applicationId]; | ||
|
|
||
| colony.deobligateStake(msgSender(), 1, stakeAmount); | ||
|
|
||
| emit StakeReclaimed(_applicationId); | ||
| } | ||
|
|
||
| function deleteApplication(uint256 _applicationId, bool _punish) public { | ||
| require(applications[_applicationId].stakeAmount > 0, "korporatio-cannot-slash"); | ||
|
|
||
| require( | ||
| colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Arbitration), | ||
| "korporatio-caller-not-arbitration" | ||
| ); | ||
|
|
||
| address applicant = applications[_applicationId].applicant; | ||
| uint256 stakeAmount = applications[_applicationId].stakeAmount; | ||
| delete applications[_applicationId]; | ||
|
|
||
| if (_punish) { | ||
| colony.emitDomainReputationPenalty(1, UINT256_MAX, 1, applicant, -int256(stakeAmount)); | ||
| colony.transferStake(1, UINT256_MAX, address(this), applicant, 1, stakeAmount, address(0x0)); | ||
| } else { | ||
| colony.deobligateStake(applicant, 1, stakeAmount); | ||
| } | ||
|
|
||
| emit ApplicationDeleted(_applicationId, _punish); | ||
| } | ||
|
|
||
| function updateApplication(uint256 _applicationId, bytes32 _ipfsHash) public { | ||
| require(applications[_applicationId].cancelledAt == UINT256_MAX, "korporatio-stake-cancelled"); | ||
| require( | ||
| msgSender() == applications[_applicationId].applicant || | ||
| colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Root), | ||
| "korporatio-not-applicant-or-root" | ||
| ); | ||
|
|
||
| emit ApplicationUpdated(_applicationId, _ipfsHash); | ||
| } | ||
|
|
||
| function submitApplication(uint256 _applicationId) public { | ||
| require(colony.hasUserRole(msgSender(), 1, ColonyDataTypes.ColonyRole.Root), "korporatio-caller-not-root"); | ||
|
kronosapiens marked this conversation as resolved.
|
||
| require(applications[_applicationId].cancelledAt == UINT256_MAX, "korporatio-stake-cancelled"); | ||
|
|
||
| applications[_applicationId].cancelledAt = block.timestamp; | ||
|
|
||
| emit ApplicationSubmitted(_applicationId); | ||
| } | ||
|
|
||
| // View | ||
|
|
||
| function getStakeFraction() external view returns (uint256) { | ||
| return stakeFraction; | ||
| } | ||
|
|
||
| function getClaimDelay() external view returns (uint256) { | ||
| return claimDelay; | ||
| } | ||
|
|
||
| function getNumApplications() external view returns (uint256) { | ||
| return numApplications; | ||
| } | ||
|
|
||
| function getApplication(uint256 _id) external view returns (Application memory application) { | ||
| application = applications[_id]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.