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!.