Last active
July 12, 2017 06:27
-
-
Save trutheum/85b8fb03094378d7bfe7c131e31731ed to your computer and use it in GitHub Desktop.
This is a preliminary test of Trutheum (trutheum.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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