Skip to content

Reward Splitting Code

homo-cogitabundus edited this page Feb 15, 2020 · 4 revisions

Here is a first draft of the code that calculates how much coin to send to the beneficiary. The function SplitAndPay() is called by Edwin, who gives it the electricity costs as input. The function then calculates whether this account has enough money in it to cover all the electricity costs and have enough money left over to make a worthwhile donation to the reward address. If there is enough money in the account to cover the electricity costs but the amount left over after paying them would be very small and insignificant, the function does not transfer any coin to the beneficiary because it would be a waste of coin in transaction fees. So the little remaining coin remains in the account until the next month, when more coin has hopefully been accumulated to make transferring coin to the beneficiary worth the transaction fees.

The function also leaves a small amount of emergency money in the account, currently set to 10 % of the electricity costs: uint coin_to_leave_behind = 0.1*electricity_cost;

The values of the constants transaction_fee and minimum_donation only have temporary values and still need to be set to realistic values.

    address payable costcenter; // the wallet which we send the money to cover the electricity costs to
    address payable beneficiary; // the address to send the donations to

    // Constants:
    uint transaction_fee = 1000 wei;   // estimated fee for sending rewards to the beneficiary address
    uint minimum_donation = 1000 wei;  // a threshold value of the least amount of rewards to donate (because the transaction costs may not be worth it if the donation is very small)


    function SplitAndPay(uint electricity_cost) public 
    {
        uint _balance = address(this).balance; // getting the amount of coin in this account
        uint coin_to_leave_behind = 0.1*electricity_cost;   // a quantity of coin to be left over after the electricity costs have been paid and the rest of the rewards sent to the beneficiary, just in case e.g. electricity costs or coin conversion turn out more expensive than expected
        
        // sending rewards to the beneficiary if enough coin has been mined
        if(_balance > electricity_cost + 2*transaction_fee + coin_to_leave_behind + minimum_donation)    // if there is enough ether on this wallet to cover the electricity costs and transaction fees, and if there would be enough ether left to make a donation to the reward address worthwhile
            require(beneficiary.send((_balance - electricity_cost - transaction_fee))); // sending the donation to the beneficiary address
        
        _balance = address(this).balance; // getting the amount of coin left in this account
        
        // Sending money to the costcenter to cover the electicity costs:
        if(_balance > electricity_cost + transaction_fee)   // if there is enough money in the account to send the costcenter the electricity costs
            require(costcenter.send(electricity_cost)); // sending the costcenter the money to cover the electricity costs
        else // there is not enough money in this wallet to cover all the electricity costs
            require(costcenter.send(_balance - transaction_fee));   // sending the costcenter all the money in this account
    }
Clone this wiki locally