Skip to content

Instantly share code, notes, and snippets.

@kbahr
Created January 6, 2020 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kbahr/3100c3a88bfab87ab47d1fd49d132664 to your computer and use it in GitHub Desktop.
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
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