Loading...
We will create a referral smart contract which is quite famous in multi level marketing applications.
First need to add solidity compiler version
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
Then creating Refer contract ,
contract refer{
...
}
Now let's create variables and objects.
We wil create a user struct which will have that user's referrer and a flag if the user is referral or not.
This referral smart contract will be of four levels and we need to assign a struct for those levels for his referral levels and we will map those structs with addresses.
struct User {
bool referred;
address referred_by;
}
struct Referal_levels {
uint256 level_1;
uint256 level_2;
uint256 level_3;
uint256 level_4;
}
mapping(address => Referal_levels) public refer_info;
mapping(address => User) public user_info;
Function will take an address as an argument and will have a public visibility. We will increment each level of reference with the help of referred_by variable from User struct whernever this function is called until user has no referral.
function referee(address ref_add) public {
require(user_info[msg.sender].referred == false, " Already referred ");
require(ref_add != msg.sender, " You cannot refer yourself ");
user_info[msg.sender].referred_by = ref_add;
user_info[msg.sender].referred = true;
address level1 = user_info[msg.sender].referred_by;
address level2 = user_info[level1].referred_by;
address level3 = user_info[level2].referred_by;
address level4 = user_info[level3].referred_by;
if ((level1 != msg.sender) && (level1 != address(0))) {
refer_info[level1].level_1 += 1;
}
if ((level2 != msg.sender) && (level2 != address(0))) {
refer_info[level2].level_2 += 1;
}
if ((level3 != msg.sender) && (level3 != address(0))) {
refer_info[level3].level_3 += 1;
}
if ((level4 != msg.sender) && (level4 != address(0))) {
refer_info[level4].level_4 += 1;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract refer{
struct User {
bool referred;
address referred_by;
}
struct Referal_levels {
uint256 level_1;
uint256 level_2;
uint256 level_3;
uint256 level_4;
}
mapping(address => Referal_levels) public refer_info;
mapping(address => User) public user_info;
function referee(address ref_add) public {
require(user_info[msg.sender].referred == false, " Already referred ");
require(ref_add != msg.sender, " You cannot refer yourself ");
user_info[msg.sender].referred_by = ref_add;
user_info[msg.sender].referred = true;
address level1 = user_info[msg.sender].referred_by;
address level2 = user_info[level1].referred_by;
address level3 = user_info[level2].referred_by;
address level4 = user_info[level3].referred_by;
if ((level1 != msg.sender) && (level1 != address(0))) {
refer_info[level1].level_1 += 1;
}
if ((level2 != msg.sender) && (level2 != address(0))) {
refer_info[level2].level_2 += 1;
}
if ((level3 != msg.sender) && (level3 != address(0))) {
refer_info[level3].level_3 += 1;
}
if ((level4 != msg.sender) && (level4 != address(0))) {
refer_info[level4].level_4 += 1;
}
}
}