2024-03-04 19:33:06 -06:00
|
|
|
// SPDX-License-Identifier: Unlicense
|
|
|
|
pragma solidity ^0.8.24;
|
|
|
|
|
2024-03-10 12:57:30 -05:00
|
|
|
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
2024-03-10 22:29:51 -05:00
|
|
|
import "./IAcceptAvailability.sol";
|
2024-03-17 21:00:31 -05:00
|
|
|
import "./IOnValidate.sol";
|
2024-03-04 19:33:06 -06:00
|
|
|
|
2024-03-11 13:39:56 -05:00
|
|
|
import "hardhat/console.sol";
|
|
|
|
|
2024-03-12 14:10:06 -05:00
|
|
|
struct Post {
|
2024-03-12 17:53:04 -05:00
|
|
|
uint id;
|
2024-03-12 14:10:06 -05:00
|
|
|
address sender;
|
|
|
|
address author;
|
|
|
|
}
|
|
|
|
|
2024-03-04 19:54:48 -06:00
|
|
|
struct Stake {
|
2024-03-12 17:53:04 -05:00
|
|
|
uint id;
|
2024-03-04 19:54:48 -06:00
|
|
|
bool inFavor;
|
|
|
|
uint256 amount;
|
2024-03-05 10:15:31 -06:00
|
|
|
address sender;
|
2024-03-04 19:54:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ValidationPool {
|
2024-03-12 17:53:04 -05:00
|
|
|
uint id;
|
|
|
|
uint postIndex;
|
2024-03-17 12:30:12 -05:00
|
|
|
address sender;
|
2024-03-04 19:54:48 -06:00
|
|
|
mapping(uint => Stake) stakes;
|
|
|
|
uint stakeCount;
|
2024-03-05 13:30:36 -06:00
|
|
|
uint256 fee;
|
2024-03-05 10:15:31 -06:00
|
|
|
uint duration;
|
|
|
|
uint endTime;
|
2024-03-05 12:25:07 -06:00
|
|
|
bool resolved;
|
|
|
|
bool outcome;
|
2024-03-17 21:00:31 -05:00
|
|
|
bool callbackOnValidate;
|
|
|
|
bytes callbackData;
|
2024-03-05 10:15:31 -06:00
|
|
|
}
|
|
|
|
|
2024-03-04 19:33:06 -06:00
|
|
|
/// This contract must manage validation pools and reputation,
|
|
|
|
/// because otherwise there's no way to enforce appropriate permissions on
|
|
|
|
/// transfer of value between reputation NFTs.
|
2024-03-10 12:57:30 -05:00
|
|
|
contract DAO is ERC20("Reputation", "REP") {
|
|
|
|
mapping(uint => address) public members;
|
|
|
|
uint public memberCount;
|
|
|
|
mapping(address => bool) public isMember;
|
2024-03-05 21:21:27 -06:00
|
|
|
mapping(uint => ValidationPool) public validationPools;
|
|
|
|
uint public validationPoolCount;
|
2024-03-12 14:10:06 -05:00
|
|
|
mapping(uint => Post) public posts;
|
|
|
|
uint public postCount;
|
2024-03-04 19:33:06 -06:00
|
|
|
|
2024-03-05 12:25:07 -06:00
|
|
|
// ufixed8x1 constant mintingRatio = 1;
|
|
|
|
// ufixed8x1 constant quorum = 0;
|
|
|
|
// ufixed8x1 constant stakeForAuthor = 0.5;
|
|
|
|
// ufixed8x1 constant winningRatio = 0.5;
|
|
|
|
// TODO: Make parameters adjustable
|
|
|
|
// TODO: Add forum parameters
|
|
|
|
|
2024-03-13 12:02:08 -05:00
|
|
|
uint public constant minDuration = 60; // 1 minute
|
|
|
|
uint public constant maxDuration = 86400; // 1 day
|
|
|
|
|
2024-03-12 18:02:07 -05:00
|
|
|
event PostAdded(uint postIndex);
|
2024-03-05 12:25:07 -06:00
|
|
|
event ValidationPoolInitiated(uint poolIndex);
|
2024-03-11 13:39:56 -05:00
|
|
|
event ValidationPoolResolved(uint poolIndex, bool votePasses);
|
2024-03-04 19:54:48 -06:00
|
|
|
|
2024-03-12 14:10:06 -05:00
|
|
|
function addPost(address author) public returns (uint postIndex) {
|
|
|
|
postIndex = postCount++;
|
|
|
|
Post storage post = posts[postIndex];
|
|
|
|
post.author = author;
|
|
|
|
post.sender = msg.sender;
|
2024-03-12 17:53:04 -05:00
|
|
|
post.id = postIndex;
|
2024-03-12 18:02:07 -05:00
|
|
|
emit PostAdded(postIndex);
|
2024-03-12 14:10:06 -05:00
|
|
|
}
|
|
|
|
|
2024-03-04 19:54:48 -06:00
|
|
|
/// Accept fee to initiate a validation pool
|
2024-03-10 12:57:30 -05:00
|
|
|
/// TODO: Handle multiple authors
|
2024-03-07 21:27:37 -06:00
|
|
|
/// TODO: Constrain duration to allowable range
|
2024-03-05 13:30:36 -06:00
|
|
|
function initiateValidationPool(
|
2024-03-12 14:10:06 -05:00
|
|
|
uint postIndex,
|
2024-03-17 21:00:31 -05:00
|
|
|
uint duration,
|
|
|
|
bool callbackOnValidate,
|
|
|
|
bytes calldata callbackData
|
2024-03-05 21:21:27 -06:00
|
|
|
) public payable returns (uint poolIndex) {
|
2024-03-10 11:55:59 -05:00
|
|
|
require(msg.value > 0, "Fee is required to initiate validation pool");
|
2024-03-13 12:02:08 -05:00
|
|
|
require(duration >= minDuration, "Duration is too short");
|
|
|
|
require(duration <= maxDuration, "Duration is too long");
|
2024-03-12 17:53:04 -05:00
|
|
|
Post storage post = posts[postIndex];
|
|
|
|
require(post.author != address(0), "Target post not found");
|
2024-03-05 21:21:27 -06:00
|
|
|
poolIndex = validationPoolCount++;
|
2024-03-05 12:25:07 -06:00
|
|
|
ValidationPool storage pool = validationPools[poolIndex];
|
2024-03-17 12:30:12 -05:00
|
|
|
pool.sender = msg.sender;
|
2024-03-12 17:53:04 -05:00
|
|
|
pool.postIndex = postIndex;
|
2024-03-05 13:30:36 -06:00
|
|
|
pool.fee = msg.value;
|
2024-03-05 10:15:31 -06:00
|
|
|
pool.duration = duration;
|
|
|
|
pool.endTime = block.timestamp + duration;
|
2024-03-12 17:53:04 -05:00
|
|
|
pool.id = poolIndex;
|
2024-03-17 21:00:31 -05:00
|
|
|
pool.callbackOnValidate = callbackOnValidate;
|
|
|
|
pool.callbackData = callbackData;
|
|
|
|
|
2024-03-05 12:25:07 -06:00
|
|
|
// Because we need to stake part of the mited value for the pool an part against,
|
|
|
|
// we mint two new tokens.
|
|
|
|
// Here we assume a minting ratio of 1, and a stakeForAuthor ratio of 0.5
|
|
|
|
// Implementing this with adjustable parameters will require more advanced fixed point math.
|
|
|
|
// TODO: Make minting ratio an adjustable parameter
|
|
|
|
// TODO: Make stakeForAuthor an adjustable parameter
|
2024-03-12 17:53:04 -05:00
|
|
|
_mint(post.author, msg.value);
|
2024-03-10 22:29:51 -05:00
|
|
|
// TODO: We need a way to exclude this pending reputation from the total supply when computing fee distribution
|
2024-03-12 17:53:04 -05:00
|
|
|
_stake(pool, post.author, msg.value / 2, true);
|
|
|
|
_stake(pool, post.author, msg.value / 2, false);
|
2024-03-05 12:25:07 -06:00
|
|
|
emit ValidationPoolInitiated(poolIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal function to register a stake for/against a validation pool
|
2024-03-10 12:57:30 -05:00
|
|
|
function _stake(
|
2024-03-05 12:25:07 -06:00
|
|
|
ValidationPool storage pool,
|
|
|
|
address sender,
|
2024-03-10 12:57:30 -05:00
|
|
|
uint256 amount,
|
|
|
|
bool inFavor
|
2024-03-05 12:25:07 -06:00
|
|
|
) internal {
|
2024-03-07 21:27:37 -06:00
|
|
|
require(block.timestamp <= pool.endTime, "Pool end time has passed");
|
2024-03-11 13:39:56 -05:00
|
|
|
_transfer(sender, address(this), amount);
|
2024-03-12 17:53:04 -05:00
|
|
|
uint stakeIndex = pool.stakeCount++;
|
|
|
|
Stake storage s = pool.stakes[stakeIndex];
|
2024-03-10 12:57:30 -05:00
|
|
|
s.sender = sender;
|
|
|
|
s.inFavor = inFavor;
|
|
|
|
s.amount = amount;
|
2024-03-12 17:53:04 -05:00
|
|
|
s.id = stakeIndex;
|
2024-03-05 10:15:31 -06:00
|
|
|
}
|
2024-03-04 19:54:48 -06:00
|
|
|
|
|
|
|
/// Accept reputation stakes toward a validation pool
|
2024-03-10 12:57:30 -05:00
|
|
|
function stake(uint poolIndex, uint256 amount, bool inFavor) public {
|
|
|
|
ValidationPool storage pool = validationPools[poolIndex];
|
|
|
|
_stake(pool, msg.sender, amount, inFavor);
|
2024-03-04 19:33:06 -06:00
|
|
|
}
|
2024-03-05 10:15:31 -06:00
|
|
|
|
|
|
|
/// Evaluate outcome of a validation pool
|
2024-03-05 13:30:36 -06:00
|
|
|
function evaluateOutcome(uint poolIndex) public returns (bool votePasses) {
|
2024-03-05 12:25:07 -06:00
|
|
|
ValidationPool storage pool = validationPools[poolIndex];
|
2024-03-12 17:53:04 -05:00
|
|
|
Post storage post = posts[pool.postIndex];
|
2024-03-07 21:27:37 -06:00
|
|
|
require(
|
|
|
|
block.timestamp > pool.endTime,
|
|
|
|
"Pool end time has not yet arrived"
|
|
|
|
);
|
|
|
|
require(pool.resolved == false, "Pool is already resolved");
|
2024-03-10 12:57:30 -05:00
|
|
|
uint256 stakedFor;
|
|
|
|
uint256 stakedAgainst;
|
|
|
|
Stake storage s;
|
2024-03-05 12:25:07 -06:00
|
|
|
for (uint i = 0; i < pool.stakeCount; i++) {
|
2024-03-10 12:57:30 -05:00
|
|
|
s = pool.stakes[i];
|
|
|
|
if (s.inFavor) {
|
|
|
|
stakedFor += s.amount;
|
2024-03-05 12:25:07 -06:00
|
|
|
} else {
|
2024-03-10 12:57:30 -05:00
|
|
|
stakedAgainst += s.amount;
|
2024-03-05 12:25:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Here we assume a quorum of 0
|
|
|
|
// TODO: Make quorum an adjustable parameter
|
|
|
|
// A tie is resolved in favor of the validation pool.
|
|
|
|
// This is especially important so that the DAO's first pool can pass,
|
|
|
|
// when no reputation has yet been minted.
|
2024-03-10 12:57:30 -05:00
|
|
|
votePasses = stakedFor >= stakedAgainst;
|
2024-03-12 17:53:04 -05:00
|
|
|
if (votePasses && !isMember[post.author]) {
|
|
|
|
members[memberCount++] = post.author;
|
|
|
|
isMember[post.author] = true;
|
2024-03-05 13:30:36 -06:00
|
|
|
}
|
2024-03-10 12:57:30 -05:00
|
|
|
pool.resolved = true;
|
2024-03-12 17:53:04 -05:00
|
|
|
pool.outcome = votePasses;
|
2024-03-11 13:39:56 -05:00
|
|
|
emit ValidationPoolResolved(poolIndex, votePasses);
|
|
|
|
// Value of losing stakes should be di stributed among winners, in proportion to their stakes
|
2024-03-10 12:57:30 -05:00
|
|
|
uint256 amountFromWinners = votePasses ? stakedFor : stakedAgainst;
|
|
|
|
uint256 amountFromLosers = votePasses ? stakedAgainst : stakedFor;
|
2024-03-11 13:39:56 -05:00
|
|
|
uint256 totalRewards;
|
2024-03-05 13:30:36 -06:00
|
|
|
for (uint i = 0; i < pool.stakeCount; i++) {
|
2024-03-10 12:57:30 -05:00
|
|
|
s = pool.stakes[i];
|
|
|
|
if (votePasses == s.inFavor) {
|
|
|
|
uint256 reward = (amountFromLosers * s.amount) /
|
2024-03-05 13:30:36 -06:00
|
|
|
amountFromWinners;
|
2024-03-10 12:57:30 -05:00
|
|
|
_transfer(address(this), s.sender, s.amount + reward);
|
2024-03-11 13:39:56 -05:00
|
|
|
totalRewards += reward;
|
2024-03-05 13:30:36 -06:00
|
|
|
}
|
|
|
|
}
|
2024-03-11 13:39:56 -05:00
|
|
|
// Due to rounding, there may be some reward left over. Include this as a reward to the author.
|
|
|
|
uint256 remainder = amountFromLosers - totalRewards;
|
|
|
|
if (remainder > 0) {
|
2024-03-12 17:53:04 -05:00
|
|
|
_transfer(address(this), post.author, remainder);
|
2024-03-11 13:39:56 -05:00
|
|
|
}
|
2024-03-05 13:30:36 -06:00
|
|
|
// Distribute fee proportionatly among all reputation holders
|
2024-03-10 12:57:30 -05:00
|
|
|
for (uint i = 0; i < memberCount; i++) {
|
|
|
|
address member = members[i];
|
|
|
|
uint256 share = (pool.fee * balanceOf(member)) / totalSupply();
|
2024-03-05 21:21:27 -06:00
|
|
|
// TODO: For efficiency this could be modified to hold the funds for recipients to withdraw
|
2024-03-10 12:57:30 -05:00
|
|
|
payable(member).transfer(share);
|
2024-03-05 13:30:36 -06:00
|
|
|
}
|
2024-03-17 21:00:31 -05:00
|
|
|
// Callback if requested
|
|
|
|
if (pool.callbackOnValidate) {
|
|
|
|
IOnValidate(pool.sender).onValidate(votePasses, pool.callbackData);
|
|
|
|
}
|
2024-03-05 12:25:07 -06:00
|
|
|
}
|
2024-03-10 22:29:51 -05:00
|
|
|
|
|
|
|
/// Transfer REP to a contract, and call that contract's receiveTransfer method
|
|
|
|
function stakeAvailability(
|
|
|
|
address to,
|
|
|
|
uint256 value,
|
|
|
|
uint duration
|
|
|
|
) external returns (bool transferred) {
|
|
|
|
transferred = super.transfer(to, value);
|
|
|
|
if (transferred)
|
|
|
|
IAcceptAvailability(to).acceptAvailability(
|
|
|
|
msg.sender,
|
|
|
|
value,
|
|
|
|
duration
|
|
|
|
);
|
|
|
|
}
|
2024-03-04 19:33:06 -06:00
|
|
|
}
|