From 805f6d48f30d04afc29e792e25eb1ffeb9c215ab Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 17 Jun 2021 02:38:59 +0200 Subject: [PATCH] Referral Program reimplemented, without Token Rewards yet. --- .../UI/Function-Libraries/UserReferrals.js | 165 ++++++++++++------ 1 file changed, 110 insertions(+), 55 deletions(-) diff --git a/Projects/Governance/UI/Function-Libraries/UserReferrals.js b/Projects/Governance/UI/Function-Libraries/UserReferrals.js index dc207c456..c50d80ab4 100644 --- a/Projects/Governance/UI/Function-Libraries/UserReferrals.js +++ b/Projects/Governance/UI/Function-Libraries/UserReferrals.js @@ -9,17 +9,28 @@ function newGovernanceFunctionLibraryUserReferrals() { pools, userProfiles ) { - let tokensReward - let totalReferralsPower = 0 + /* + Here we will store the total amount of tokens that is going to be distributed among all participants + of the Referral Program. This will come from a Pool that is configured wiht a codeName config property + with the value "Referral-Program" + */ + let referralProgramPoolTokenReward + /* + In order to be able to calculate the share of the Referrals Program Pool for each User Profile, + we need to accumulate all the Icomming Referral Power that each User Profile at their Referral Program + node has, because with that Incoming Power is that each Referral Program node gets a share of + the pool. + */ + let accumulatedIncomingReferralPower = 0 /* Scan Pools Until finding the User-Referrlas Pool */ for (let i = 0; i < pools.length; i++) { let poolsNode = pools[i] findPool(poolsNode) } - if (tokensReward === undefined || tokensReward === 0) { return } + if (referralProgramPoolTokenReward === undefined || referralProgramPoolTokenReward === 0) { return } /* - We will first reset all the referral combinedPower, and then distribute it. + We will first reset all the referral outgoingPower, and then distribute it. */ for (let i = 0; i < userProfiles.length; i++) { let userProfile = userProfiles[i] @@ -55,14 +66,14 @@ function newGovernanceFunctionLibraryUserReferrals() { /* When we reach certain node types, we will halt the distribution, because these are targets for - voting combinedPower. + voting outgoingPower. */ if ( node.type === 'Pool' ) { let codeName = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(node.payload, 'codeName') if (codeName === "Referral-Program") { - tokensReward = node.payload.tokens + referralProgramPoolTokenReward = node.payload.tokens return } } @@ -97,11 +108,11 @@ function newGovernanceFunctionLibraryUserReferrals() { function resetUserReferrals(node) { if (node === undefined) { return } if (node.payload === undefined) { return } - node.payload.referring = { + node.payload.referrals = { count: 0, - combinedPower: 0, + outgoingPower: 0, ownPower: 0, - referralsPower: 0, + incomingPower: 0, awarded: { tokens: 0, percentage: 0 @@ -129,7 +140,7 @@ function newGovernanceFunctionLibraryUserReferrals() { return } /* - If there is a reference parent defined, this means that the referral combinedPower is + If there is a reference parent defined, this means that the referral outgoingPower is transfered to it. */ if ( @@ -143,78 +154,122 @@ function newGovernanceFunctionLibraryUserReferrals() { function distributeForReferralProgram(referralProgram) { if (referralProgram === undefined || referralProgram.payload === undefined) { return } - let referringPower = referralProgram.payload.tokenPower - let referringCount = 0 - referralProgram.payload.referring.ownPower = referringPower - distributeUserReferrals(referralProgram, referringPower, referringCount) + /* + Here we will convert Token Power into Referral Power. + As per system rules referringPower = tokensPower / 10 + */ + let referringPower = referralProgram.payload.tokenPower / 10 + /* + We will also reseet the count of referrals here. + */ + let count = 0 + /* + The Own Power is the power generated by the same User Profile tokens, not inherited from others. + */ + referralProgram.payload.referrals.ownPower = referringPower + + distributeReferralPower(referralProgram, referringPower, count) } - function distributeUserReferrals(node, referringPower, referringCount) { + function distributeReferralPower( + node, + referringPower, + count + ) { + if (node === undefined) { return } if (node.payload === undefined) { return } - node.payload.referring.count = node.payload.referring.count + referringCount - node.payload.referring.combinedPower = node.payload.referring.combinedPower + referringPower - /* - If the node is a User Profile, we will check if it has a User Referrer child defined. - */ - totalReferralsPower = totalReferralsPower - node.payload.referring.referralsPower - node.payload.referring.referralsPower = node.payload.referring.combinedPower - node.payload.referring.ownPower - totalReferralsPower = totalReferralsPower + node.payload.referring.referralsPower - if ( - node.type === 'Referral Program' && - node.userReferrer !== undefined - ) { - distributeUserReferrals(node.userReferrer, referringPower / 10, 1, 0) - return - } - /* - If there is a reference parent defined, this means that the referral combinedPower is - transfered to it. - */ - if ( - node.type === 'User Referrer' && - node.payload.referenceParent !== undefined - ) { - drawUserReferrals(node) - distributeUserReferrals(node.payload.referenceParent, referringPower, 1) - return + switch (node.type) { + case 'Referral Program': { + /* + This is the point where we increase to our local count of referrals whatever it comes + at the count parameters. If we are processing the User Profile of this Referral Program + then we will add zero, otherwise, 1. + */ + node.payload.referrals.count = node.payload.referrals.count + count + /* + The outgoingPower of this node will be accumulating all the referringPower flowing + through it, no matter from where it comes. + */ + node.payload.referrals.outgoingPower = node.payload.referrals.outgoingPower + referringPower + /* + We need to adjust the balance that holds the accumulationt of all incomingPower of all Referral Program + nodes. To do this we will substratct the current incomingPower, bacause it is going to be recalculated + inmediatelly after this, and then we will add it again after the recalcualtion. + */ + accumulatedIncomingReferralPower = accumulatedIncomingReferralPower - node.payload.referrals.incomingPower + /* + At any point in time, the incomingPower will be equal to the total of the outgoingPower minus + the ownPower. This is like this because the outgoingPower is the accumulation of all the + power flow that is leaving this node, which includes the ownPower. That means that if we + substract the ownPower, we will have the accumulation of all the incomingPower, which + means all the power coming from other User Profiles referencing this one. + */ + node.payload.referrals.incomingPower = node.payload.referrals.outgoingPower - node.payload.referrals.ownPower + /* + Now that we have the incomingPower calculated again, we can add it again to the balance of all the incomingPower + of all Referral Program nodes. + */ + accumulatedIncomingReferralPower = accumulatedIncomingReferralPower + node.payload.referrals.incomingPower + + if (node.userReferrer !== undefined) { + distributeReferralPower(node.userReferrer, referringPower, 0) + } + break + } + case 'User Referrer': { + drawUserReferrer(node) + if (node.payload.referenceParent !== undefined) { + distributeReferralPower(node.payload.referenceParent, referringPower / 10, 0) + } + break + } + case 'User Profile': { + if (node.tokenSwitch !== undefined) { + distributeReferralPower(node.tokenSwitch, referringPower, 0) + } + break + } + case 'Token Switch': { + if (node.referralProgram !== undefined) { + distributeReferralPower(node.referralProgram, referringPower, 1) + } + break + } } } function calculateForReferralProgram(referralProgram) { if (referralProgram.payload === undefined) { return } - let totalPowerRewardRatio = tokensReward / totalReferralsPower + let totalPowerRewardRatio = referralProgramPoolTokenReward / accumulatedIncomingReferralPower if (totalPowerRewardRatio < 1) { totalPowerRewardRatio = 1 } - referralProgram.payload.referring.awarded.tokens = referralProgram.payload.referring.referralsPower * totalPowerRewardRatio + referralProgram.payload.referrals.awarded.tokens = referralProgram.payload.referrals.incomingPower * totalPowerRewardRatio drawReferralProgram(referralProgram) } - function drawUserReferrals(node) { + function drawUserReferrer(node) { if (node.payload !== undefined) { - const ownPowerText = new Intl.NumberFormat().format(node.payload.referring.ownPower) - const referralsPowerText = new Intl.NumberFormat().format(node.payload.referring.referralsPower) - const combinedPowerText = new Intl.NumberFormat().format(node.payload.referring.combinedPower) + const ownPowerText = new Intl.NumberFormat().format(node.payload.parentNode.payload.referrals.ownPower) + const incomingPowerText = new Intl.NumberFormat().format(node.payload.parentNode.payload.referrals.incomingPower) + const outgoingPowerText = new Intl.NumberFormat().format(node.payload.parentNode.payload.referrals.outgoingPower) node.payload.uiObject.valueAngleOffset = 180 node.payload.uiObject.valueAtAngle = true - node.payload.uiObject.setValue( combinedPowerText + ' ' + 'Referring Power') + node.payload.uiObject.setValue(outgoingPowerText + ' ' + 'Referring Power') node.payload.uiObject.statusAngleOffset = 0 node.payload.uiObject.statusAtAngle = false - node.payload.uiObject.setStatus(ownPowerText + ' own RP + ' + referralsPowerText + ' inherited RP = ' + combinedPowerText + ' ' + ' Total RP') + node.payload.uiObject.setStatus(ownPowerText + ' Own RP + ' + incomingPowerText + ' Incoming RP = ' + outgoingPowerText + ' ' + ' Outgoing RP') } } function drawReferralProgram(node) { if (node.payload !== undefined) { - const powerText = new Intl.NumberFormat().format(node.payload.referring.combinedPower) - const awardedTokens = new Intl.NumberFormat().format(node.payload.referring.awarded.tokens) - const referralPower = new Intl.NumberFormat().format(node.payload.referring.referralsPower) node.payload.uiObject.statusAngleOffset = 0 node.payload.uiObject.statusAtAngle = false @@ -229,18 +284,18 @@ function newGovernanceFunctionLibraryUserReferrals() { node.userReferrer.payload !== undefined && node.userReferrer.payload.referenceParent !== undefined) || - node.payload.referring.combinedPower >= 10000000 + node.payload.referrals.outgoingPower >= 10000000 ) { let status = powerText + ' ' + 'Combined Referring Power' + ' - ' + - node.payload.referring.count + ' Referrals' + ' - ' + + node.payload.referrals.count + ' Referrals' + ' - ' + referralPower + ' Referrals Power' + ' - ' + awardedTokens + ' ' + 'SA Tokens Awarded' node.payload.uiObject.setStatus(status) return } else { - node.payload.referring.awarded.tokens = 0 + node.payload.referrals.awarded.tokens = 0 node.payload.uiObject.setStatus(awardedTokens + ' SA Tokens not Awarded, because you have not defined your referrer yet.') } }