Skip to content

Add TokenTrade scheme #774

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 7 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ jobs:

- stage: coverage
name: "Solidity Test Coverage"
if: branch = arc-factory
if: branch = master-2
script: npm run coveralls
171 changes: 171 additions & 0 deletions contracts/schemes/TokenTrade.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
pragma solidity ^0.6.10;
// SPDX-License-Identifier: GPL-3.0

import "../votingMachines/VotingMachineCallbacks.sol";


/**
* @title A scheme for trading ERC20 tokens with the DAO
*/
contract TokenTrade 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,
IERC20 _receiveToken,
uint256 _receiveTokenAmount
);

event TokenTradeProposalExecuted(
address indexed _avatar,
bytes32 indexed _proposalId,
address indexed _beneficiary,
IERC20 _sendToken,
uint256 _sendTokenAmount,
IERC20 _receiveToken,
uint256 _receiveTokenAmount
);

event ProposalExecuted(address indexed _avatar, bytes32 indexed _proposalId, int256 _decision);

struct Proposal {
address beneficiary;
IERC20 sendToken;
uint256 sendTokenAmount;
IERC20 receiveToken;
uint256 receiveTokenAmount;
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,
Copy link
Contributor

@orenyodfat orenyodfat Jul 16, 2020

Choose a reason for hiding this comment

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

I think it should get also the inTradingToken

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why not allow any token? What's the reason to limit it?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. easy in the UI.
  2. simple code
  3. more secure

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];
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];
require(address(_sendToken) != address(0), "must be a live proposal");
require(proposal.decided, "must be a decided proposal");
Copy link
Contributor

Choose a reason for hiding this comment

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

does this condition is not enough ? why need the previous one ?

if (proposal.passed) {
proposal.sendToken.safeTransfer(address(avatar), proposal.sendTokenAmount);
require(
Controller(avatar.owner()).externalTokenTransfer(
proposal.receiveToken, proposal.beneficiary, proposal.receiveTokenAmount
), "failed to transfer tokens from the DAO"
);

emit TokenTradeProposalExecuted(
address(avatar),
_proposalId,
proposal.beneficiary,
proposal.sendToken,
proposal.sendTokenAmount,
proposal.receiveToken,
proposal.receiveTokenAmount
);
delete proposals[_proposalId];
} 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 _receiveToken token the proposer asks to receive from the DAO
* @param _receiveTokenAmount token 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,
IERC20 _receiveToken,
uint256 _receiveTokenAmount,
string memory _descriptionHash
)
public
returns(bytes32 proposalId)
{
require(
address(_sendToken) != address(0) && address(_receiveToken) != address(0),
"Token address must not be null"
);
require(_sendTokenAmount > 0 && _receiveTokenAmount > 0, "Token amount must be greater than 0");

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need a way to lock the recieve token in the scheme.. so there will not be a case where the proposal execute and the dao cannot pay .
so maybe upon proposal the scheme will ask the receive token amount from the avatar.. ,if not enough exist will revert.
if proposal is accepted the user will redeem (from the scheme) .
if proposal is rejected the allocated token will be sent back to the avatar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is bad because an attacker can open dummy proposal and lock the avatar's balance. The way I handled that case is that if upon execution the DAO doesn't have enough funds, the proposal will be canceled as if it was rejected, and the user will get his tokens back.

_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,
receiveToken: _receiveToken,
receiveTokenAmount: _receiveTokenAmount,
passed: false,
decided: false
});

proposalsBlockNumber[proposalId] = block.number;

emit TokenTradeProposed(
address(avatar),
proposalId,
_descriptionHash,
msg.sender,
_sendToken,
_sendTokenAmount,
_receiveToken,
_receiveTokenAmount
);
}
}
1 change: 0 additions & 1 deletion contracts/utils/DAOFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma experimental ABIEncoderV2;

import "../registry/App.sol";
import "../registry/ImplementationDirectory.sol";
import "@daostack/upgrades/contracts/upgradeability/ProxyAdmin.sol";
import "@daostack/upgrades/contracts/upgradeability/AdminUpgradeabilityProxy.sol";
import "../libs/BytesLib.sol";
import "../controller/Controller.sol";
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@daostack/arc-experimental",
"version": "0.1.2-rc.2",
"version": "0.1.2-rc.3",
"description": "A platform for building DAOs",
"files": [
"contracts/",
Expand Down
2 changes: 1 addition & 1 deletion release-experimental.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

rm -rf ./node_modules
rm -rf ./build
git checkout origin/arc-factory
git checkout origin/master-2
echo "npm install ..."
npm i
echo "truffle compile ..."
Expand Down
5 changes: 3 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const JoinAndQuit = artifacts.require("./JoinAndQuit.sol");
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 MAX_UINT_256 = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
Expand Down Expand Up @@ -159,7 +160,7 @@ const SOME_ADDRESS = '0x1000000000000000000000000000000000000000';
registration.rewarderMock = await RewarderMock.new();
registration.dictator = await Dictator.new();
registration.reputationAdmin = await ReputationAdmin.new();

registration.tokenTrade = await TokenTrade.new();

await implementationDirectory.setImplementation("DAOToken",registration.daoToken.address);
await implementationDirectory.setImplementation("Reputation",registration.reputation.address);
Expand Down Expand Up @@ -191,7 +192,7 @@ const SOME_ADDRESS = '0x1000000000000000000000000000000000000000';
await implementationDirectory.setImplementation("FundingRequest",registration.fundingRequest.address);
await implementationDirectory.setImplementation("Dictator",registration.dictator.address);
await implementationDirectory.setImplementation("ReputationAdmin",registration.reputationAdmin.address);

await implementationDirectory.setImplementation("TokenTrade",registration.tokenTrade.address);

registration.implementationDirectory = implementationDirectory;

Expand Down
Loading