dgf-prototype/ethereum/contracts/DAO.sol

244 lines
9.1 KiB
Solidity
Raw Normal View History

2024-03-04 19:33:06 -06:00
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.24;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "./ReputationHolder.sol";
2024-03-04 19:54:48 -06:00
struct Stake {
bool inFavor;
uint256 amount;
2024-03-05 10:15:31 -06:00
address sender;
2024-03-05 13:30:36 -06:00
uint256 tokenId;
2024-03-04 19:54:48 -06:00
}
struct ValidationPool {
mapping(uint => Stake) stakes;
uint stakeCount;
2024-03-05 13:30:36 -06:00
address author;
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-05 13:30:36 -06:00
uint256 tokenIdFor;
uint256 tokenIdAgainst;
2024-03-05 10:15:31 -06:00
}
struct StakeData {
2024-03-05 12:25:07 -06:00
uint poolIndex;
2024-03-05 10:15:31 -06:00
bool inFavor;
2024-03-04 19:54:48 -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.
contract DAO is ERC721("Reputation", "REP"), ReputationHolder {
2024-03-04 19:54:48 -06:00
mapping(uint256 tokenId => uint256) tokenValues;
uint256 nextTokenId;
2024-03-05 21:21:27 -06:00
uint256 public totalValue;
mapping(uint => ValidationPool) public validationPools;
uint public validationPoolCount;
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
event ValidationPoolInitiated(uint poolIndex);
2024-03-05 10:15:31 -06:00
2024-03-04 19:33:06 -06:00
/// Inspect the value of a given reputation NFT
function valueOf(uint256 tokenId) public view returns (uint256 value) {
2024-03-04 19:54:48 -06:00
value = tokenValues[tokenId];
2024-03-04 19:33:06 -06:00
}
2024-03-05 10:15:31 -06:00
/// Confirm ownership of a token and return its value.
/// This should be used when receiving an NFT transfer, because otherwise
/// someone could send any NFT with a tokenId matching one of ours.
function verifiedValueOf(
address owner,
uint256 tokenId
) public view returns (uint256 value) {
require(ownerOf(tokenId) == owner);
value = valueOf(tokenId);
}
2024-03-04 19:33:06 -06:00
/// Internal function to mint a new reputation NFT
2024-03-05 12:25:07 -06:00
function mint(uint256 value) internal returns (uint256 tokenId) {
2024-03-04 19:33:06 -06:00
// Generate a new (sequential) ID for the token
2024-03-04 19:54:48 -06:00
tokenId = nextTokenId++;
2024-03-04 19:33:06 -06:00
// Mint the token, initially to be owned by the current contract.
_mint(address(this), tokenId);
2024-03-05 12:25:07 -06:00
tokenValues[tokenId] = value;
// Keep track of total value minted
// TODO: More sophisticated logic can compute total _available_, _active_ reputation
totalValue += value;
2024-03-04 19:33:06 -06:00
}
/// Internal function to transfer value from one reputation token to another
function transferValueFrom(
uint256 fromTokenId,
uint256 toTokenId,
uint256 amount
) internal {
require(amount >= 0);
require(valueOf(fromTokenId) >= amount);
2024-03-04 19:54:48 -06:00
tokenValues[fromTokenId] -= amount;
tokenValues[toTokenId] += amount;
}
/// Accept fee to initiate a validation pool
2024-03-05 13:30:36 -06:00
/// TODO: Rather than accept author as a parameter, accept a reference to a forum post
function initiateValidationPool(
address author,
uint duration
2024-03-05 21:21:27 -06:00
) public payable returns (uint poolIndex) {
poolIndex = validationPoolCount++;
2024-03-05 12:25:07 -06:00
ValidationPool storage pool = validationPools[poolIndex];
2024-03-05 13:30:36 -06:00
pool.author = author;
pool.fee = msg.value;
2024-03-05 10:15:31 -06:00
pool.duration = duration;
pool.endTime = block.timestamp + duration;
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-05 13:30:36 -06:00
pool.tokenIdFor = mint(msg.value / 2);
pool.tokenIdAgainst = mint(msg.value / 2);
stake(pool, address(this), true, pool.tokenIdFor);
stake(pool, address(this), false, pool.tokenIdAgainst);
2024-03-05 12:25:07 -06:00
emit ValidationPoolInitiated(poolIndex);
}
/// Internal function to register a stake for/against a validation pool
function stake(
ValidationPool storage pool,
address sender,
bool inFavor,
uint256 tokenId
) internal {
require(block.timestamp < pool.endTime);
Stake storage _stake = pool.stakes[pool.stakeCount++];
_stake.sender = sender;
_stake.inFavor = inFavor;
_stake.amount = verifiedValueOf(sender, tokenId);
2024-03-05 13:30:36 -06:00
_stake.tokenId = tokenId;
2024-03-05 10:15:31 -06:00
}
2024-03-04 19:54:48 -06:00
/// Accept reputation stakes toward a validation pool
function onERC721Received(
address,
address from,
uint256 tokenId,
2024-03-05 10:15:31 -06:00
bytes calldata data
2024-03-04 19:54:48 -06:00
) public override returns (bytes4) {
2024-03-05 10:15:31 -06:00
// `data` needs to encode the target validation pool, and the for/again boolean
StakeData memory stakeParameters = abi.decode(data, (StakeData));
ValidationPool storage pool = validationPools[
2024-03-05 12:25:07 -06:00
stakeParameters.poolIndex
2024-03-05 10:15:31 -06:00
];
2024-03-05 12:25:07 -06:00
stake(pool, from, stakeParameters.inFavor, tokenId);
2024-03-04 19:54:48 -06:00
return super.onERC721Received.selector;
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];
require(block.timestamp >= pool.endTime);
require(pool.resolved == false);
uint256 amountFor;
uint256 amountAgainst;
2024-03-05 13:30:36 -06:00
Stake storage _stake;
2024-03-05 12:25:07 -06:00
for (uint i = 0; i < pool.stakeCount; i++) {
_stake = pool.stakes[i];
if (_stake.inFavor) {
amountFor += _stake.amount;
} else {
amountAgainst += _stake.amount;
}
}
// 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-05 13:30:36 -06:00
votePasses = amountFor >= amountAgainst;
2024-03-05 12:25:07 -06:00
pool.resolved = true;
2024-03-05 13:30:36 -06:00
// If the outcome is true, value of all stakes against the pool should be distributed among the stakes in favor.
// If the outcome is false, value of all stakes for the pool should be distributed among the stakes against.
uint256 amountFromWinners;
uint256 amountFromLosers;
// Collect the reputation from the losing stakes
for (uint i = 0; i < pool.stakeCount; i++) {
_stake = pool.stakes[i];
if (votePasses && !_stake.inFavor) {
// Transfer value to the token that was minted in favor
amountFromLosers += _stake.amount;
transferValueFrom(
_stake.tokenId,
pool.tokenIdFor,
_stake.amount
);
} else if (!votePasses && _stake.inFavor) {
// Transfer value to the token that was minted against
amountFromLosers += _stake.amount;
transferValueFrom(
_stake.tokenId,
pool.tokenIdAgainst,
_stake.amount
);
} else if (
votePasses &&
_stake.inFavor &&
_stake.tokenId != pool.tokenIdFor
) {
// Tally the total value of winning stakes
amountFromWinners += _stake.amount;
} else if (
!votePasses &&
!_stake.inFavor &&
_stake.tokenId != pool.tokenIdAgainst
) {
// Tally the total value of winning stakes
amountFromWinners += _stake.amount;
}
}
// Distribute reputation from losing stakes to winning stakes
for (uint i = 0; i < pool.stakeCount; i++) {
_stake = pool.stakes[i];
if (
votePasses &&
_stake.inFavor &&
_stake.tokenId != pool.tokenIdFor
) {
uint256 reward = (amountFromLosers * _stake.amount) /
amountFromWinners;
transferValueFrom(pool.tokenIdAgainst, _stake.tokenId, reward);
} else if (
!votePasses &&
!_stake.inFavor &&
_stake.tokenId != pool.tokenIdAgainst
) {
uint256 reward = (amountFromLosers * _stake.amount) /
amountFromWinners;
transferValueFrom(pool.tokenIdFor, _stake.tokenId, reward);
}
}
2024-03-05 13:38:10 -06:00
// Transfer minted reputation to the author
// TODO: Handle multiple authors
if (votePasses) {
_transfer(address(this), pool.author, pool.tokenIdFor);
}
2024-03-05 13:30:36 -06:00
// Distribute fee proportionatly among all reputation holders
for (uint tokenId = 0; tokenId < nextTokenId; tokenId++) {
2024-03-05 21:21:27 -06:00
uint256 share = (pool.fee * valueOf(tokenId)) / totalValue;
2024-03-05 13:30:36 -06:00
address recipient = ownerOf(tokenId);
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-05 13:30:36 -06:00
payable(recipient).transfer(share);
}
2024-03-05 12:25:07 -06:00
}
2024-03-04 19:33:06 -06:00
}