2019-02-02 23:04:55 +07:00
pragma solidity ^ 0 . 5 . 0 ;
/*
Future Goals :
- remove admins necessity
- encourage contributors to allocate
2019-02-02 23:07:28 +07:00
DApp :
2019-02-02 23:04:55 +07:00
- show tokens to allocate
- allocate token to person with praise
- leaderboard , showing amount totalReceived and totalForfeited and amount , praises https : //codepen.io/lewismcarey/pen/GJZVoG
- allows you to send SNT to meritocracy
- add / remove contributor
- add / remove adminstrator
2019-02-10 13:43:35 +07:00
Extension :
- Command :
- above command = display allocation , received , withdraw button , allocate button ? ( might be better in dapp )
- / kudos 500 " <person> " " <praise> "
2019-02-02 23:04:55 +07:00
* /
import " token/ERC20Token.sol " ;
contract Meritocracy {
struct Status {
address author ;
string praise ;
uint256 amount ;
uint256 time ; // block.timestamp
}
struct Contributor {
address addr ;
uint256 allocation ; // Amount they can send to other contributors, and amount they forfeit, when forfeit just zero this out and leave Token in contract, Owner can use escape to receive it back
uint256 totalForfeited ; // Allocations they've burnt, can be used to show non-active players.
uint256 totalReceived ;
uint256 received ; // Ignore amounts in Status struct, and use this as source of truth, can withdraw at any time
2019-02-02 23:10:51 +07:00
// bool inPot; // Require Contributor WARN: commented because there's some edge cases not dealt with
2019-02-02 23:04:55 +07:00
Status [ ] status ;
}
2019-02-10 13:43:35 +07:00
ERC20Token public token ; // token contract
2019-02-02 23:04:55 +07:00
address payable public owner ; // contract owner
uint256 public lastForfeit ; // timestamp to block admins calling forfeitAllocations too quickly
address [ ] public registry ; // array of contributor addresses
uint256 public maxContributors ; // Dynamic finite limit on registry.
2019-02-08 23:08:02 +07:00
mapping ( address => bool ) public admins ;
mapping ( address => Contributor ) public contributors ;
2019-02-02 23:04:55 +07:00
2019-02-10 14:01:03 +07:00
// Modifiers --------------------------------------------------------------------------------------------
2019-02-10 13:43:35 +07:00
// Functions only Owner can call
modifier onlyOwner ( ) {
require ( msg . sender == owner ) ;
_ ;
}
// Functions only Admin can call
modifier onlyAdmin ( ) {
require ( admins [ msg . sender ] ) ;
_ ;
}
2019-02-10 14:01:03 +07:00
// Open Functions --------------------------------------------------------------------------------------
2019-02-02 23:04:55 +07:00
2019-02-10 13:43:35 +07:00
// Split amount over each contributor in registry, any contributor can allocate? TODO maybe relax this restriction, so anyone can allocate tokens
2019-02-02 23:04:55 +07:00
function allocate ( uint256 _amount ) external {
// Locals
uint256 individualAmount ;
2019-02-10 13:43:35 +07:00
// Contributor memory cAllocator = contributors[msg.sender];
2019-02-02 23:04:55 +07:00
// Requirements
2019-02-10 13:43:35 +07:00
// require(cAllocator.addr != address(0)); // is sender a Contributor? TODO maybe relax this restriction.
require ( token . transferFrom ( msg . sender , address ( this ) , _amount ) ) ;
2019-02-02 23:04:55 +07:00
// Body
// cAllocator.inPot = true;
individualAmount = _amount / registry . length ;
for ( uint256 i = 0 ; i < registry . length ; i ++ ) {
contributors [ registry [ i ] ] . allocation += individualAmount ;
}
}
2019-02-08 23:08:02 +07:00
// Getter for Dynamic Array Length
2019-02-10 13:43:35 +07:00
function registryLength ( ) public view returns ( uint256 ) {
2019-02-08 23:08:02 +07:00
return registry . length ;
}
2019-02-10 14:01:03 +07:00
// Contributor Functions --------------------------------------------------------------------------------
2019-02-02 23:04:55 +07:00
// Allows a contributor to withdraw their received Token, when their allocation is 0
function withdraw ( ) external {
// Locals
Contributor storage cReceiver = contributors [ msg . sender ] ;
// Requirements
require ( cReceiver . addr == msg . sender ) ; //is sender a Contributor?
require ( cReceiver . received > 0 ) ; // Contributor has received some tokens
require ( cReceiver . allocation == 0 ) ; // Contributor must allocate all Token (or have Token burnt) before they can withdraw.
// require(cReceiver.inPot); // Contributor has put some tokens into the pot
// Body
uint256 r = cReceiver . received ;
cReceiver . received = 0 ;
// cReceiver.inPot = false;
2019-02-10 13:43:35 +07:00
token . transferFrom ( address ( this ) , cReceiver . addr , r ) ;
2019-02-02 23:04:55 +07:00
}
// Allow Contributors to award allocated tokens to other Contributors
function award ( address _contributor , uint256 _amount , string calldata _praise ) external {
// Locals
Contributor storage cSender = contributors [ msg . sender ] ;
Contributor storage cReceiver = contributors [ _contributor ] ;
// Requirements
require ( cSender . addr == msg . sender ) ; // Ensure Contributors both exist, and isn't the same address
require ( cReceiver . addr == _contributor ) ;
require ( cSender . addr != cReceiver . addr ) ; // cannot send to self
require ( cSender . allocation >= _amount ) ; // Ensure Sender has enough tokens to allocate
// Body
cSender . allocation -= _amount ; // burn is not adjusted, which is done only in forfeitAllocations
cReceiver . received += _amount ;
cReceiver . totalReceived += _amount ;
Status memory s = Status ( {
author : cSender . addr ,
praise : _praise ,
amount : _amount ,
time : block . timestamp
} ) ;
cReceiver . status . push ( s ) ; // Record the history
}
// Admin Functions -------------------------------------------------------------------------------------
// Add Contributor to Registry
2019-02-10 13:43:35 +07:00
function addContributor ( address _contributor ) public onlyAdmin ( ) {
2019-02-02 23:04:55 +07:00
// Requirements
2019-02-10 13:43:35 +07:00
require ( registry . length + 1 <= maxContributors ) ; // Don't go out of bounds
require ( contributors [ _contributor ] . addr == address ( 0 ) ) ; // Contributor doesn't exist
2019-02-02 23:04:55 +07:00
// Body
Contributor storage c = contributors [ _contributor ] ;
c . addr = _contributor ;
registry . push ( _contributor ) ;
}
2019-02-03 14:33:49 +07:00
// Add Multiple Contributors to the Registry in one tx
2019-02-10 13:43:35 +07:00
function addContributors ( address [ ] calldata _newContributors ) external onlyAdmin ( ) {
2019-02-10 14:01:03 +07:00
// Locals
uint256 newContributorLength = _newContributors . length ;
2019-02-02 23:04:55 +07:00
// Requirements
2019-02-10 14:01:03 +07:00
require ( registry . length + newContributorLength <= maxContributors ) ; // Don't go out of bounds
2019-02-02 23:04:55 +07:00
// Body
2019-02-10 14:01:03 +07:00
for ( uint256 i = 0 ; i < newContributorLength ; i ++ ) {
2019-02-02 23:04:55 +07:00
addContributor ( _newContributors [ i ] ) ;
}
}
// Remove Contributor from Registry
// Note: Should not be easy to remove multiple contributors in one tx
2019-02-10 13:43:35 +07:00
// WARN: Changed to idx, client can do loop by enumerating registry
function removeContributor ( uint256 idx ) external onlyAdmin ( ) { // address _contributor
2019-02-10 14:01:03 +07:00
// Locals
uint256 registryLen = registry . length - 1 ;
2019-02-02 23:04:55 +07:00
// Requirements
2019-02-10 14:01:03 +07:00
require ( idx < registryLen ) ; // idx needs to be smaller than registry.length - 1 OR maxContributors
2019-02-02 23:04:55 +07:00
// Body
address c = registry [ idx ] ;
// Swap & Pop!
2019-02-10 14:01:03 +07:00
registry [ idx ] = registry [ registryLen ] ;
2019-02-02 23:04:55 +07:00
registry . pop ( ) ;
delete contributors [ c ] ; // TODO check if this works
}
// Implictly sets a finite limit to registry length
2019-02-10 13:43:35 +07:00
function setMaxContributors ( uint256 _maxContributors ) external onlyAdmin ( ) {
2019-02-02 23:04:55 +07:00
require ( _maxContributors > registry . length ) ; // have to removeContributor first
// Body
maxContributors = _maxContributors ;
}
// Zero-out allocations for contributors, minimum once a week, if allocation still exists, add to burn
2019-02-10 13:43:35 +07:00
function forfeitAllocations ( ) public onlyAdmin ( ) {
2019-02-10 14:01:03 +07:00
// Locals
uint256 registryLen = registry . length ;
// Requirements
2019-02-02 23:04:55 +07:00
require ( block . timestamp >= lastForfeit + 1 weeks ) ; // prevents multiple admins accidently calling too quickly.
// Body
lastForfeit = block . timestamp ;
2019-02-10 14:01:03 +07:00
for ( uint256 i = 0 ; i < registryLen ; i ++ ) { // should never be longer than maxContributors, see addContributor
2019-02-02 23:04:55 +07:00
Contributor storage c = contributors [ registry [ i ] ] ;
c . totalForfeited += c . allocation ; // Shaaaaame!
c . allocation = 0 ;
// cReceiver.inPot = false; // Contributor has to put tokens into next round
}
}
// Owner Functions -------------------------------------------------------------------------------------
// Set Admin flag for address to true
2019-02-10 13:43:35 +07:00
function addAdmin ( address _admin ) public onlyOwner ( ) {
2019-02-02 23:04:55 +07:00
admins [ _admin ] = true ;
}
// Set Admin flag for address to false
2019-02-10 13:43:35 +07:00
function removeAdmin ( address _admin ) public onlyOwner ( ) {
2019-02-02 23:04:55 +07:00
delete admins [ _admin ] ;
}
// Change owner address, ideally to a management contract or multisig
2019-02-10 13:43:35 +07:00
function changeOwner ( address payable _owner ) external onlyOwner ( ) {
2019-02-02 23:04:55 +07:00
// Body
removeAdmin ( owner ) ;
addAdmin ( _owner ) ;
owner = _owner ;
}
// Change Token address
// WARN: call escape first, or escape(token);
2019-02-10 13:43:35 +07:00
function changeToken ( address _token ) external onlyOwner ( ) {
2019-02-02 23:04:55 +07:00
// Body
// Zero-out allocation and received, send out received tokens before token switch.
for ( uint256 i = 0 ; i < registry . length ; i ++ ) {
Contributor storage c = contributors [ registry [ i ] ] ;
2019-02-10 13:43:35 +07:00
uint256 r = c . received ;
2019-02-02 23:04:55 +07:00
c . received = 0 ;
c . allocation = 0 ;
// WARN: Should totalReceived and totalForfeited be zeroed-out?
2019-02-10 13:43:35 +07:00
token . transferFrom ( address ( this ) , c . addr , r ) ; // Transfer any owed tokens to contributor
2019-02-02 23:04:55 +07:00
}
lastForfeit = block . timestamp ;
2019-02-10 13:43:35 +07:00
token = ERC20Token ( _token ) ;
2019-02-02 23:04:55 +07:00
}
// Failsafe, Owner can escape hatch all Tokens and ETH from Contract.
2019-02-10 13:43:35 +07:00
function escape ( ) public onlyOwner ( ) {
2019-02-02 23:04:55 +07:00
// Body
2019-02-10 13:43:35 +07:00
token . transferFrom ( address ( this ) , owner , token . balanceOf ( address ( this ) ) ) ;
owner . transfer ( address ( this ) . balance ) ;
2019-02-02 23:04:55 +07:00
}
// Overloaded failsafe function, recourse incase changeToken is called before escape and funds are in a different token
// Don't want to require in changeToken incase bad behaviour of ERC20 token
2019-02-10 13:43:35 +07:00
function escape ( address _token ) external onlyOwner ( ) {
2019-02-02 23:04:55 +07:00
// Body
2019-02-10 13:43:35 +07:00
ERC20Token t = ERC20Token ( _token ) ;
t . transferFrom ( address ( this ) , owner , t . balanceOf ( address ( this ) ) ) ;
2019-02-02 23:04:55 +07:00
escape ( ) ;
}
2019-02-10 14:01:03 +07:00
// Constructor ------------------------------------------------------------------------------------------
// Set Owner, Token address and initial maxContributors
2019-02-02 23:04:55 +07:00
constructor ( address _token , uint256 _maxContributors ) public {
// Body
owner = msg . sender ;
addAdmin ( owner ) ;
lastForfeit = block . timestamp ;
2019-02-10 13:43:35 +07:00
token = ERC20Token ( _token ) ;
2019-02-02 23:04:55 +07:00
maxContributors = _maxContributors ;
}
}