My First SmartContract Project!


#1

Hello everybody,

Today i wanted to share my first and simple smart contract project idea in order to get feedback and maybe get some help from this awesome community.

Let me explain, i have cryptocurrency mines in venezuela, but i am currently in Madrid, Spain. Naturally, i have hosts that help me with taking care of the mining rigs, i pay them 10% of the total mining rewards and the other 90% i send to another address of mine. What i would like is a smart contract that receives the payment sent by the pool and divides the amount and sends the correspondent eth automatically to my host’s address (10%) and to my address (90%).

I have the following code.

pragma solidity ^0.4.0;

contract splitEth {
    //array of addresses to which the eths will be distributed
    address[] hosts = [0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c];
    //eths that have been received.
    uint totalReceived = 0;
    mapping (address => uint) withdrawnAmounts;
    
    //constructor function, will be called when contract is created
    function splitEth() payable{
        updateTotalReceived();
        
    }
    
    //fallback function, executes whenver somebody sends eth to the contract
    function () payable{
        updateTotalReceived();
        
    }
    
    //function to update the totalReceived variable, will be triggered when somebody pays into the contract
    //uses msg.value which is the amount of ether that the contract receives
    function updateTotalReceived() internal {
        totalReceived += msg.value;
    }
    
    //method to check if the address msg.sender is inside our declared array
    modifier canWithdraw() {
        bool contains = false;
        for (uint i=0; i<hosts.length; i++) {
            if (hosts[i] == msg.sender) {
                contains = true;
            }
        }
        require(contains);
        _;
    }
    
    //Host claims their portion of the ether received, by calling this function
    //Uses the canWithdraw method to check if the withdrawer is infact inside out array
    function withdraw() canWithdraw{
        uint amountAllocated = (totalReceived / 10);
        uint amountWithdrawn = withdrawnAmounts[msg.sender];
        uint amount = amountAllocated - amountWithdrawn;
        withdrawnAmounts[msg.sender] = amountWithdrawn + amount;
        
        if (amount > 0) {
            msg.sender.transfer(amount);
        }
    }
}

i must give almost all of the credit for this code to kevin healy for his youtube tutorial, https://www.youtube.com/watch?v=TC-bDQZbXd0&t=3s
all though i understand and have tweaked this code, most of it is his!.

As you can see, this code doesnt allocate the 90% to one of the addresses which is supposed to be mine and it asks for the withdraw function to be activated by the user and not each time new payments arrive to the contract address automaticaly.

I’m working on this problem and will update whenever i have new solutions, feel free to share with me too.
Have a good one!.


#2

Just to disclaim I have not done any solidity yet however this looks like a general type of problem. I think if you are trying to automatically send eth then maybe try calling withdraw(); function inside payable() function.

Try changing payable() function like this (just add withdraw();):

//fallback function, executes whenver somebody sends eth to the contract
    function () payable{
        updateTotalReceived();
        withdraw();
    }

Let me know what happened :thinking:

Have a great day in Madrid!
:v:


#3

hey donald, thank you very much for your interest, as the matter of fact, i just found its unefficient to do this type of contract because, when you mine in a pool such as ethermine, they send the rewards with little gas making it complicated to send to a smartcontract address which will require a minimum amount of gas to run the code. i could have the pool send the rewards to 1 account and from that account set my own gas and after that send it to the smartcontract’s address, but now i would be making too many jumps. im saddened to know that my idea will not work out, but i very much appreciate your interest.


#4

Hey Rick, what can you do right. Well interesting micro contract to do. For practice sake I will test this on testnet when I have some time to play around.