Skip to content

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 5 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions contracts/schemes/ReputationTokenTrade.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
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) {
Proposal memory proposal = proposals[_proposalId];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused var ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 storage proposal = proposals[_proposalId];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is storage ?

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
);
delete proposals[_proposalId];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can delete proposal be moved out of the if and make it one time

} else {
Proposal memory _proposal = proposals[_proposalId];
delete proposals[_proposalId];
_proposal.sendToken.safeTransfer(address(_proposal.beneficiary), _proposal.sendTokenAmount);
}
}

/**
* @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
);
}
}
3 changes: 3 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const FundingRequest = artifacts.require("./FundingRequest.sol");
const Dictator = artifacts.require("./Dictator.sol");
const ReputationAdmin = artifacts.require("./ReputationAdmin.sol");
const TokenTrade = artifacts.require("./TokenTrade.sol");
const ReputationTokenTrade = artifacts.require("./ReputationTokenTrade.sol");


const MAX_UINT_256 = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
Expand Down Expand Up @@ -161,6 +162,7 @@ const SOME_ADDRESS = '0x1000000000000000000000000000000000000000';
registration.dictator = await Dictator.new();
registration.reputationAdmin = await ReputationAdmin.new();
registration.tokenTrade = await TokenTrade.new();
registration.reputationTokenTrade = await ReputationTokenTrade.new();

await implementationDirectory.setImplementation("DAOToken",registration.daoToken.address);
await implementationDirectory.setImplementation("Reputation",registration.reputation.address);
Expand Down Expand Up @@ -193,6 +195,7 @@ const SOME_ADDRESS = '0x1000000000000000000000000000000000000000';
await implementationDirectory.setImplementation("Dictator",registration.dictator.address);
await implementationDirectory.setImplementation("ReputationAdmin",registration.reputationAdmin.address);
await implementationDirectory.setImplementation("TokenTrade",registration.tokenTrade.address);
await implementationDirectory.setImplementation("ReputationTokenTrade",registration.reputationTokenTrade.address);

registration.implementationDirectory = implementationDirectory;

Expand Down
Loading