Created
May 11, 2019 05:48
-
-
Save yuyasugano/a990a9fab69ccfe02f06af480ac876cc to your computer and use it in GitHub Desktop.
SLCR Voting voting rights and rescue
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
/** | |
* @notice Loads _numTokens ERC20 tokens into the voting contract for one-to-one voting rights | |
* @dev Assumes that msg.sender has approved voting contract to spend on their behalf | |
* @param _numTokens The number of votingTokens desired in exchange for ERC20 tokens | |
*/ | |
function requestVotingRights(uint256 _numTokens) public { | |
require(token.balanceOf(msg.sender) >= _numTokens, "Cannot stake more than you have"); | |
voteTokenBalance[msg.sender] = voteTokenBalance[msg.sender].add(_numTokens); | |
// Transfer tokens to voting contract | |
// User must approve tokens to voting contract in advance token.approve(voting) | |
require(token.transferFrom(msg.sender, this, _numTokens)); | |
emit _VotingRightsGranted(_numTokens, msg.sender); | |
} | |
/** | |
* @notice Withdraw _numTokens ERC20 tokens from the voting contract, revoking these voting rights | |
* @param _numTokens The number of ERC20 tokens desired in exchange for voting rights | |
*/ | |
function withdrawVotingRights(uint256 _numTokens) external { | |
// withdrawing tokens only not locked should be available | |
require(voteTokenBalance[msg.sender] >= _numTokens, "Cannot withdraw more than used in the polls"); | |
voteTokenBalance[msg.sender] = voteTokenBalance[msg.sender].sub(_numTokens); | |
require(token.transfer(msg.sender, _numTokens)); | |
emit _VotingRightsWithdrawn(_numTokens, msg.sender); | |
} | |
/** | |
* @dev Unlocks tokens locked in unrevealed vote where poll has ended | |
* @param _pollID Integer identifier associated with the target poll | |
*/ | |
function rescueTokens(uint _pollID) public { | |
require(isExpired(pollMap[_pollID].revealEndDate), "The poll has not ended"); | |
require(dllMap[msg.sender].contains(_pollID)); | |
uint256 numTokens = getNumTokens(msg.sender, _pollID); | |
dllMap[msg.sender].remove(_pollID); | |
// No partial lock, add _numTokens back to the voting rights | |
// Tokens are now available to withdraw or for another vote | |
voteTokenBalance[msg.sender] = voteTokenBalance[msg.sender].add(numTokens); | |
emit _TokensRescued(_pollID, msg.sender); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment