Create a new affiliate contract that inherits the lottery contract created in the previous recipe:
pragma solidity^0.4.23; import "./InvestableDAL.sol"; contract AffiliateDAL is InvestableDAL { //... }
Create a mapping to store the amount earned by each person with the help of the affiliate program:
mapping(address => uint) affiliate;
Create a function that can be used by someone to claim their affiliate earnings:
function getAffiliatePay() public { //... }
Transfer the amount to the address and empty the affiliate mapping for each payout:
uint amount = affiliate[msg.sender]; require(amount > 0); msg.senser.transfer(amount); affiliate[msg.sender] = 0;
Modify the play function to accept the affiliate address:
function play(uint _hash, address _affiliate) payable public { //... }
Calculate the affiliate amount for each valid address:
uint ticketValue = msg.value; if(_affiliate != address(0)) { uint affiliateAmount = msg.value * (1/100); affiliate[msg.sender] += affiliateAmount; ticketValue = msg.value - affiliateAmount; } //...
The final contract with affiliate functionality will look like this:
pragma solidity^0.4.23; import "./InvestableDAL.sol"; contract AffiliateDAL is InvestableDAL { mapping(address => uint) affiliate; /** * @dev Function to get affiliate payout */ function getAffiliatePay() public { uint amount = affiliate[msg.sender]; require(amount > 0); msg.senser.transfer(amount); affiliate[msg.sender] = 0; } /** * @dev Modified play function with affiliate * @param _hash Guessed number * @param _affiliate Address of the affiliate */ function play(uint _hash, address _affiliate) payable public { uint ticketValue = msg.value; if(_affiliate != address(0)) { uint affiliateAmount = msg.value * (1/100); affiliate[msg.sender] += affiliateAmount; ticketValue = msg.value - affiliateAmount; } //... } }
..................Content has been hidden....................
You can't read the all page of ebook, please click
here login for view all page.