-
Notifications
You must be signed in to change notification settings - Fork 0
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
F/pots #28
Open
lajarre
wants to merge
16
commits into
main
Choose a base branch
from
f/pots
base: main
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
F/pots #28
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
eb67b5f
⬆️ Upgrade all solidity pragmas to ^0.8.17
lajarre 0fdf02a
[wip] ✨ Molten pots
lajarre 4e558d6
📦 Update package name
lajarre 8a2110c
💚
lajarre af3e659
rename deposit -> stake
lajarre 3c3368e
✨ Add duration
lajarre c1559ea
wrong unstake
lajarre 7077abb
✨ Add mTokens minting
lajarre f662c38
✨ Add cooldown mgt
lajarre 86afd11
🐎 Prevent unstaking when no stake
lajarre e82a3a3
🔥 Simplify mocks
lajarre 65bd319
🎨 Rename things
lajarre ef605b5
✨ Add Campaign.takeOffice skeleton
lajarre ce84f9b
✨ Add election ending via campaign call
lajarre 507b2bb
✅ Make tests more robust and simple, use factories
lajarre 9cd08cd
✅ Improve takeOffice tests.
lajarre 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,171 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// solhint-disable not-rely-on-time | ||
pragma solidity ^0.8.17; | ||
|
||
import {MToken} from "./MToken.sol"; | ||
import {IERC20Votes} from "./interfaces/IERC20Votes.sol"; | ||
|
||
// [FIXME] Write interfaces and use them. | ||
// [TODO] Events. | ||
|
||
contract MoltenElectionFactory { | ||
function makeElection( | ||
address campaignFactoryAddress, | ||
address daoTokenAddress, | ||
uint256 _threshold, | ||
uint128 _duration, | ||
uint128 _cooldownDuration | ||
) public returns (address) { | ||
return | ||
address( | ||
new MoltenElection( | ||
campaignFactoryAddress, | ||
daoTokenAddress, | ||
_threshold, | ||
_duration, | ||
_cooldownDuration | ||
) | ||
); | ||
} | ||
} | ||
|
||
contract MoltenElection { | ||
address public campaignFactory; // ❓ use Owned? | ||
IERC20Votes public daoToken; | ||
// Threshold in daoToken-weis. | ||
uint256 public threshold; | ||
uint128 public duration; | ||
uint128 public cooldownDuration; | ||
bool public ended = false; | ||
mapping(address => bool) public hasCampaign; | ||
|
||
constructor( | ||
address campaignFactoryAddress, | ||
address daoTokenAddress, | ||
uint256 _threshold, | ||
uint128 _duration, | ||
uint128 _cooldownDuration | ||
) { | ||
campaignFactory = campaignFactoryAddress; | ||
daoToken = IERC20Votes(daoTokenAddress); | ||
threshold = _threshold; | ||
duration = _duration; | ||
cooldownDuration = _cooldownDuration; | ||
} | ||
|
||
function addCampaign(MoltenCampaign campaign) public { | ||
require(msg.sender == campaignFactory, "Molten: unauthorized"); | ||
require(!ended, "Molten: election ended"); | ||
require(campaign.election() == this, "Molten: different election"); | ||
hasCampaign[address(campaign)] = true; | ||
} | ||
|
||
function end() public { | ||
require(hasCampaign[msg.sender], "Molten: unauthorized"); | ||
MoltenCampaign campaign = MoltenCampaign(msg.sender); | ||
require( | ||
campaign.totalStaked() >= threshold, | ||
"Molten: threshold not reached" | ||
); | ||
require( | ||
block.timestamp >= campaign.cooldownEnd(), | ||
"Molten: cooldown not ended" | ||
); | ||
|
||
ended = true; | ||
} | ||
} | ||
|
||
contract MoltenCampaignFactory { | ||
function makeCampaign(address electionAddress, string calldata delegateName) | ||
public | ||
returns (MoltenCampaign) | ||
{ | ||
MoltenElection election = MoltenElection(electionAddress); | ||
MToken mToken = new MToken( | ||
string.concat( | ||
"Molten ", | ||
election.daoToken().name(), | ||
" by ", | ||
delegateName | ||
), | ||
string.concat("m", election.daoToken().symbol(), "-", delegateName), | ||
address(this) | ||
); | ||
MoltenCampaign campaign = new MoltenCampaign( | ||
msg.sender, | ||
address(this), | ||
address(mToken) | ||
); | ||
mToken.transferOwnership(address(campaign)); | ||
election.addCampaign(campaign); | ||
return campaign; | ||
} | ||
} | ||
|
||
contract MoltenCampaign { | ||
// Immutable props. | ||
address public representative; | ||
MoltenElection public election; | ||
MToken public mToken; | ||
|
||
// Mutable props. | ||
uint256 public totalStaked; | ||
mapping(address => uint256) public staked; | ||
uint256 public cooldownEnd; | ||
bool public inOffice; // [XXX] Replace with states and use modifiers | ||
|
||
constructor( | ||
address _representative, | ||
address electionAddress, | ||
address mTokenAddress | ||
) { | ||
representative = _representative; | ||
election = MoltenElection(electionAddress); | ||
mToken = MToken(mTokenAddress); | ||
} | ||
|
||
function _getDaoToken() private view returns (IERC20Votes) { | ||
return IERC20Votes(election.daoToken()); | ||
} | ||
|
||
function _resetCooldown() internal { | ||
cooldownEnd = block.timestamp + election.cooldownDuration(); | ||
} | ||
|
||
function stake(uint256 amount) public { | ||
require(!inOffice, "Molten: in office"); | ||
|
||
staked[msg.sender] += amount; | ||
totalStaked += amount; | ||
|
||
mToken.mint(msg.sender, amount); | ||
_resetCooldown(); | ||
_getDaoToken().transferFrom(msg.sender, address(this), amount); | ||
} | ||
|
||
function unstake() public { | ||
uint256 _staked = staked[msg.sender]; | ||
require(!inOffice, "Molten: in office"); | ||
require(_staked > 0, "Molten: unstake 0"); | ||
|
||
staked[msg.sender] = 0; | ||
totalStaked -= _staked; | ||
|
||
mToken.burn(msg.sender, _staked); | ||
_resetCooldown(); | ||
_getDaoToken().transferFrom(address(this), msg.sender, _staked); | ||
} | ||
|
||
function takeOffice() public { | ||
require( | ||
totalStaked >= election.threshold(), | ||
"Molten: threshold not reached" | ||
); | ||
require(block.timestamp >= cooldownEnd, "Molten: cooldown ongoing"); | ||
|
||
inOffice = true; | ||
|
||
election.end(); | ||
} | ||
} |
This file contains 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can further enforce immutability of these system variables by adding the
immutable
modifier to these variables:also: