-
Notifications
You must be signed in to change notification settings - Fork 21
Add reputation token trade scheme #775
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,155 @@ | ||
pragma solidity ^0.6.10; | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import "../votingMachines/VotingMachineCallbacks.sol"; | ||
|
||
|
||
/** | ||
* @title A scheme for trading ERC20 tokens for reputation with the DAO | ||
*/ | ||
contract ReputationTokenTrade is VotingMachineCallbacks, ProposalExecuteInterface { | ||
using SafeMath for uint256; | ||
using SafeERC20 for IERC20; | ||
|
||
event TokenTradeProposed( | ||
address indexed _avatar, | ||
bytes32 indexed _proposalId, | ||
string _descriptionHash, | ||
address indexed _beneficiary, | ||
IERC20 _sendToken, | ||
uint256 _sendTokenAmount, | ||
uint256 _reputationAmount | ||
); | ||
|
||
event TokenTradeProposalExecuted( | ||
address indexed _avatar, | ||
bytes32 indexed _proposalId, | ||
address indexed _beneficiary, | ||
IERC20 _sendToken, | ||
uint256 _sendTokenAmount, | ||
uint256 _reputationAmount | ||
); | ||
|
||
event ProposalExecuted(address indexed _avatar, bytes32 indexed _proposalId, int256 _decision); | ||
|
||
struct Proposal { | ||
address beneficiary; | ||
IERC20 sendToken; | ||
uint256 sendTokenAmount; | ||
uint256 reputationAmount; | ||
bool passed; | ||
bool decided; | ||
} | ||
|
||
mapping(bytes32=>Proposal) public proposals; | ||
|
||
/** | ||
* @dev initialize | ||
* @param _avatar the avatar this scheme referring to. | ||
* @param _votingMachine the voting machines address to | ||
* @param _votingParams genesisProtocol parameters - valid only if _voteParamsHash is zero | ||
* @param _voteOnBehalf genesisProtocol parameter - valid only if _voteParamsHash is zero | ||
* @param _voteParamsHash voting machine parameters. | ||
*/ | ||
function initialize( | ||
Avatar _avatar, | ||
IntVoteInterface _votingMachine, | ||
uint256[11] calldata _votingParams, | ||
address _voteOnBehalf, | ||
bytes32 _voteParamsHash | ||
) | ||
external | ||
{ | ||
super._initializeGovernance(_avatar, _votingMachine, _voteParamsHash, _votingParams, _voteOnBehalf); | ||
} | ||
|
||
/** | ||
* @dev execution of proposals, can only be called by the voting machine in which the vote is held. | ||
* @param _proposalId the ID of the voting in the voting machine | ||
* @param _decision a parameter of the voting result, 1 yes and 2 is no. | ||
* @return bool success | ||
*/ | ||
function executeProposal(bytes32 _proposalId, int256 _decision) | ||
external | ||
onlyVotingMachine(_proposalId) | ||
override | ||
returns(bool) { | ||
if (_decision == 1) { | ||
proposals[_proposalId].passed = true; | ||
} | ||
proposals[_proposalId].decided = true; | ||
|
||
emit ProposalExecuted(address(avatar), _proposalId, _decision); | ||
return true; | ||
} | ||
|
||
|
||
function execute(bytes32 _proposalId) public { | ||
Proposal memory proposal = proposals[_proposalId]; | ||
require(proposal.decided, "must be a decided proposal"); | ||
if (proposal.passed) { | ||
proposal.sendToken.safeTransfer(address(avatar), proposal.sendTokenAmount); | ||
require( | ||
Controller(avatar.owner()).mintReputation(proposal.reputationAmount, proposal.beneficiary), | ||
"mint reputation should succeed" | ||
); | ||
|
||
emit TokenTradeProposalExecuted( | ||
address(avatar), | ||
_proposalId, | ||
proposal.beneficiary, | ||
proposal.sendToken, | ||
proposal.sendTokenAmount, | ||
proposal.reputationAmount | ||
); | ||
} else { | ||
proposal.sendToken.safeTransfer(address(proposal.beneficiary), proposal.sendTokenAmount); | ||
} | ||
delete proposals[_proposalId]; | ||
} | ||
|
||
/** | ||
* @dev propose to trade tokens with the DAO | ||
* @param _sendToken token the proposer suggests to send to the DAO | ||
* @param _sendTokenAmount token amount the proposer suggests to send to the DAO | ||
* @param _reputationAmount reputation amount the proposer asks to receive from the DAO | ||
* @param _descriptionHash proposal description hash | ||
* @return proposalId an id which represents the proposal | ||
*/ | ||
function proposeTokenTrade( | ||
IERC20 _sendToken, | ||
uint256 _sendTokenAmount, | ||
uint256 _reputationAmount, | ||
string memory _descriptionHash | ||
) | ||
public | ||
returns(bytes32 proposalId) | ||
{ | ||
require(address(_sendToken) != address(0), "Token address must not be null"); | ||
require(_sendTokenAmount > 0 && _reputationAmount > 0, "Token and reputation amount must be greater than 0"); | ||
|
||
_sendToken.safeTransferFrom(msg.sender, address(this), _sendTokenAmount); | ||
proposalId = votingMachine.propose(2, voteParamsHash, msg.sender, address(avatar)); | ||
|
||
proposals[proposalId] = Proposal({ | ||
beneficiary: msg.sender, | ||
sendToken: _sendToken, | ||
sendTokenAmount: _sendTokenAmount, | ||
reputationAmount: _reputationAmount, | ||
passed: false, | ||
decided: false | ||
}); | ||
|
||
proposalsBlockNumber[proposalId] = block.number; | ||
|
||
emit TokenTradeProposed( | ||
address(avatar), | ||
proposalId, | ||
_descriptionHash, | ||
msg.sender, | ||
_sendToken, | ||
_sendTokenAmount, | ||
_reputationAmount | ||
); | ||
} | ||
} |
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.