Skip to content

Code Draft 1

Andreas E edited this page Jan 27, 2020 · 4 revisions

Background

After going through some initial brainstorming and having considered:

with the help of some additional background information from our Wiki, we'll get our hands dirty.

Code Draft 1

pragma solidity >=0.5.0 <0.7.0;

/// @author 71ae and The Coding Army.
/// @title Split and Distribute Mining Rewards between a costcenter and a beneficiary.
contract SplitMiningRewards {

    // DATA

    address payable owner;
    mapping(address => bool) delegates;

    address payable costcenter;
    address payable beneficiary;

    // EVENTS

    event PaymentReceived(address from, uint value);
    event DelegationChanged(address from, address delegate, bytes32 txt);

    // MODIFIERS

    modifier onlyDelegates() {
        require(delegates[msg.sender], "Caller is not an allowed delegate.");
        _;
    }

    // CONSTRUCTOR

    /// Input adresses for costcenter and beneficiary.
    constructor(address _costcenter, address _beneficiary) public {
        owner = msg.sender;
        costcenter = _costcenter;
        beneficiary = _beneficiary;
        delegates[owner] = true;
    }

    // MAIN CONTRACT FUNCTIONS

    // Receiving pure Ether.
    receive() external payable {
        emit PaymentReceived(msg.sender, msg.value);
    }

    // Our main business logic goes in here.
    function splitAndPay(uint cost) public onlyDelegates() {
        uint _balance = this.balance;
        require(_balance > 21000 gwei, "Not enough funds.");
        // Here the main function of this program shall be coded in.
        // All the rest you can actually see here is just "overhead". Boo.
    }

    // POTENTIALLY, SUPPORTING FUNCTIONS

    // SAFE-GUARD

    // Make fallback payable to receive funds even when people don't know what they're doing.
    // We're not polite to send them back.
    fallback () external payable { }

    // Safe-guard: withdraw all remaining balance on the contract address to the owner.
    function withdrawAll() public {
        require(msg.sender == owner, "No access if you're not the owner.");
        uint _balance = this.balance;
        require(_balance > 21000 gwei, "Not enough funds.");
        // TO-DO: emit an event
        payable(owner).transfer(_balance);
        }
    }

    // MAINTENANCE

    // Maintenance: add delegate 'newDelegate'
    function addDelegate(address _newDelegate) public onlyDelegates() {
        delegates[_newDelegate] = true;
        emit DelegationChanged(msg.sender, _newDelegate, "added as new delegate.");
    }

    // Maintenance: delete delegate 'removeDelegate'
    function delDelegate(address _removeDelegate) public onlyDelegates() {
        // TO-DO: add additional safe-guard here not to delete the last remaining delegate!
        // Within this code, delegates[] is a hash table, which does not allow counting its members.
        // We'll have to think about different options. Otherwise we may lose access to the main functionality.
        // For now: The owner of the contract is always the first delegate, and we'll ensure here that the owner cannot be removed.
        require(_removeDelegate != owner, "Removal of the owner as delegate is not provided.");
        if (delegates[_removeDelegate]) {
            delegates[_removeDelegate] = false;
            emit DelegationChanged(msg.sender, _removeDelegate, "removed as delegate.");        }
    }

    // Maintenance: Change payee addresses
    // TO-DO: Write some more functions.

}

Code written by-hand without support of an IDE, without any syntax checks, and without any functional tests.

I have absolutely no idea if this actually can work, makes sense, is a rational architecture, or whatever doubts you can think of. My "experience" with Solidity and smart contracts so far is limited to reading some documentation. And maybe having a generally good grasp and a god-given gift for learning new programming languages pretty quickly.

You are very encouraged to comment on this, and to edit and change this code stub for the better. I hope we can learn from each other, while progressing working on a mutual goal.

Do not use. Provided without any warranty!

Clone this wiki locally