Created
January 6, 2020 05:39
-
-
Save kbahr/3100c3a88bfab87ab47d1fd49d132664 to your computer and use it in GitHub Desktop.
A HEX/ETH "referral" contract that takes an erc20 token address (made for HEX, but any should work), a set of splitter addresses and their percentages. It will split ETH and ERC20 tokens by the addresses' percentages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.12; | |
import "./HEX.sol"; // TODO: Windows file separator | |
contract ReferralSplitter { | |
event DistributedShares( | |
uint40 timestamp, | |
address indexed memberAddress, | |
uint256 amount | |
); | |
HEX internal hx; | |
address payable[] internal splitAddresses; | |
uint8[] internal splitPercentages; | |
constructor (address hexContract, address payable[] memory addresses, uint8[] memory percentages) | |
public | |
{ | |
require(hexContract != address(0), "HEX contract address required"); | |
require(addresses.length > 0 && percentages.length == addresses.length, | |
"Addresses and percentages must have the same length > 0"); | |
uint hundred = 100; | |
for(uint i = 0; i < percentages.length; i++){ | |
hundred -= percentages[i]; | |
if(hundred < 0){ | |
require(false, "Percentages exceed 100"); | |
} | |
} | |
if(hundred != 0){ | |
require(false, "Percentages must equal 100"); | |
} | |
hx = HEX(hexContract); | |
splitAddresses = addresses; | |
splitPercentages = percentages; | |
} | |
function distribute () | |
public | |
{ | |
// eth part | |
uint256 balance = address(this).balance; | |
uint256 onePercent; | |
uint256 share; | |
uint len = splitAddresses.length; | |
if(balance > 99){ | |
onePercent = balance / 100; | |
for(uint i = 0; i < len; i++){ | |
if(i == len - 1){ | |
share = balance; | |
} else { | |
share = splitPercentages[i] * onePercent; | |
balance -= share; | |
} | |
splitAddresses[i].transfer(share); | |
} | |
} | |
// hex part | |
balance = hx.balanceOf(address(this)); | |
if(balance > 99){ | |
onePercent = balance / 100; | |
for(uint i = 0; i < len; i++){ | |
if(i == len - 1){ | |
share = balance; | |
} else { | |
share = splitPercentages[i] * onePercent; | |
balance -= share; | |
} | |
hx.transfer(splitAddresses[i], share); | |
} | |
} | |
} | |
// for testing | |
function donate() | |
external | |
payable | |
{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment