Skip to content

Instantly share code, notes, and snippets.

@trutheum
Last active Jul 12, 2017
Embed
What would you like to do?
This is a preliminary test of Trutheum (trutheum.com)
pragma solidity ^0.4.13;
// Trutheum Contract v0.01
contract Trutheum {
// Contributer is any person who transfers funds
// to this contract. They have a right to vote for
// the truth that they believe answers the RFT.
struct Contributor {
uint weight;
bool voted;
uint vote;
bool exists;
}
// Truth is from a person revealing what they believe
// is the truth for the RFT. They must supply proof
// and an address to transfer ethereum to.
struct Truth {
string info;
uint voteCount;
address payAddress;
}
// This will eventually be community powered, but for now
// Trutheum's only power is to initiate the funds transfer.
address public owner;
// The RFT description
string public description;
// All contributors
mapping(address => Contributor) public contributors;
// All truths
Truth[] public truths;
function Trutheum(string desc) {
description = desc;
owner = msg.sender;
}
// Submit a truth to be considered.
function submit(string info, address payAddress) {
require(bytes(info).length < 500);
truths.push(Truth({
info: info,
voteCount: 0,
payAddress: payAddress
}));
}
// Fund this RFT and get the right to vote.
function() payable {
require(msg.value > 0);
contributors[msg.sender] = Contributor({
voted: false,
exists: true,
weight: msg.value,
vote: 0
});
}
// Helper to see how much has been raised for this RFT.
function raised() constant
returns (uint raised) {
raised = this.balance;
}
// Cast your vote for the correct truth.
function vote(uint truth) {
Contributor contrib = contributors[msg.sender];
require(contrib.exists);
require(!contrib.voted);
contrib.voted = true;
contrib.vote = truth;
// out of range with throw and revert
truths[truth].voteCount += contrib.weight;
}
// Which truth has the most votes.
function mostVotes() constant
returns (uint mostIndex) {
uint voteCount = 0;
for (uint x = 0; x < truths.length; x++) {
if (truths[x].voteCount > voteCount) {
voteCount = truths[x].voteCount;
mostIndex = x;
}
}
}
// This will transfer the reward to the truth with the
// most votes. Please note that Trutheum has no power
// to redirect funds. Trutheum can only initiate the
// transfer.
function transferReward() {
require(msg.sender == owner);
address transferTo = truths[mostVotes()].payAddress;
transferTo.transfer(this.balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment