From 85bc313b89ce6e2dfeb47613c3d95a1ebe383366 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 13 Jun 2021 18:30:19 +0200 Subject: [PATCH] Governance: Referral Calculation Implemented --- Client/httpInterface.js | 1 - .../Function-Libraries/DistributionProcess.js | 4 + .../UI/Function-Libraries/Tokens.js | 2 +- .../UI/Function-Libraries/UserReferrals.js | 119 ++++++++++++++++++ .../Governance/UI/Function-Libraries/Votes.js | 6 +- Projects/ProjectsSchema.json | 5 + 6 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 Projects/Governance/UI/Function-Libraries/UserReferrals.js diff --git a/Client/httpInterface.js b/Client/httpInterface.js index b29a1e469..ed08eb406 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -1100,7 +1100,6 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE getAllFilesInDirectoryAndSubdirectories(folder, onFilesReady) function onFilesReady(files) { - console.log(files) respondWithContent(JSON.stringify(files), httpResponse) } } diff --git a/Projects/Governance/UI/Function-Libraries/DistributionProcess.js b/Projects/Governance/UI/Function-Libraries/DistributionProcess.js index d20957fe3..32abadf3d 100644 --- a/Projects/Governance/UI/Function-Libraries/DistributionProcess.js +++ b/Projects/Governance/UI/Function-Libraries/DistributionProcess.js @@ -31,6 +31,10 @@ function newGovernanceFunctionLibraryDistributionProcess() { UI.projects.governance.functionLibraries.tokens.calculate( pools ) + + UI.projects.governance.functionLibraries.userrReferrals.calculate( + userProfiles + ) UI.projects.governance.functionLibraries.claims.calculate( features, diff --git a/Projects/Governance/UI/Function-Libraries/Tokens.js b/Projects/Governance/UI/Function-Libraries/Tokens.js index 93056af1c..a41f1d8fc 100644 --- a/Projects/Governance/UI/Function-Libraries/Tokens.js +++ b/Projects/Governance/UI/Function-Libraries/Tokens.js @@ -52,7 +52,7 @@ function newGovernanceFunctionLibraryTokens() { return } /* - When we reach certain node types, we will halt the distribution, because thse are targets for + When we reach certain node types, we will halt the distribution, because these are targets for token flow. */ if ( diff --git a/Projects/Governance/UI/Function-Libraries/UserReferrals.js b/Projects/Governance/UI/Function-Libraries/UserReferrals.js new file mode 100644 index 000000000..0fb66ae20 --- /dev/null +++ b/Projects/Governance/UI/Function-Libraries/UserReferrals.js @@ -0,0 +1,119 @@ +function newGovernanceFunctionLibraryUserReferrals() { + let thisObject = { + calculate: calculate + } + + return thisObject + + function calculate( + userProfiles + ) { + /* + We will first reset all the referral power, and then distribute it. + */ + for (let i = 0; i < userProfiles.length; i++) { + let userProfile = userProfiles[i] + resetUserReferrals(userProfile) + } + for (let i = 0; i < userProfiles.length; i++) { + let userProfile = userProfiles[i] + distributeForProfile(userProfile) + } + + function resetUserReferrals(node) { + if (node === undefined) { return } + if (node.payload === undefined) { return } + node.payload.referring = { + count: 0, + power: 0 + } + /* + If the node is a User Profile, we will check if it has a User Referrer child defined. + */ + if ( + node.userReferrer !== undefined && + node.type === 'User Profile' + ) { + resetUserReferrals(node.userReferrer) + return + } + /* + If there is a reference parent defined, this means that the referral power is + transfered to it. + */ + if ( + node.payload.referenceParent !== undefined && + node.type === 'User Referrer' + ) { + resetUserReferrals(node.payload.referenceParent) + return + } + } + + function distributeForProfile(userProfile) { + let referringPower = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'tokens') + let referringCount = 0 + distributeUserReferrals(userProfile, referringPower, referringCount) + } + + function distributeUserReferrals(node, referringPower, referringCount) { + if (node === undefined) { return } + if (node.payload === undefined) { return } + + node.payload.referring = { + count: node.payload.referring.count + referringCount, + power: node.payload.referring.power + referringPower + } + + drawUserReferrals(node, node.payload.referring.count, node.payload.referring.power) + /* + If the node is a User Profile, we will check if it has a User Referrer child defined. + */ + if ( + node.userReferrer !== undefined && + node.type === 'User Profile' + ) { + distributeUserReferrals(node.userReferrer, referringPower, 1, 0) + return + } + /* + If there is a reference parent defined, this means that the referral power is + transfered to it. + */ + if ( + node.payload.referenceParent !== undefined && + node.type === 'User Referrer' + ) { + distributeUserReferrals(node.payload.referenceParent, referringPower / 10, 1) + return + } + } + + function drawUserReferrals(node, count, power) { + const powerText = new Intl.NumberFormat().format(power) + ' ' + 'Referring Power' + if (node.payload !== undefined) { + + node.payload.uiObject.valueAngleOffset = 0 + node.payload.uiObject.valueAtAngle = false + + if (node.type === 'User Referrer') { + node.payload.uiObject.setStatus(powerText) + return + } + if ( + (node.type === 'User Profile' && + node.userReferrer !== undefined && + node.userReferrer.payload !== undefined && + node.userReferrer.payload.referenceParent !== undefined) + || + power >= 10000000 + ) { + node.payload.uiObject.setStatus(powerText + ' - ' + count + ' Referrals') + return + } else { + node.payload.uiObject.setStatus('Define your referrer to collect tokens from your referrals.') + } + } + } + } +} \ No newline at end of file diff --git a/Projects/Governance/UI/Function-Libraries/Votes.js b/Projects/Governance/UI/Function-Libraries/Votes.js index 4f7ca4529..790517151 100644 --- a/Projects/Governance/UI/Function-Libraries/Votes.js +++ b/Projects/Governance/UI/Function-Libraries/Votes.js @@ -25,7 +25,7 @@ function newGovernanceFunctionLibraryVotes() { resetVotes(featuresNode) } - /* Reset Votes at Features */ + /* Reset Votes at Assets */ for (let i = 0; i < assets.length; i++) { let assetsNode = assets[i] resetVotes(assetsNode) @@ -53,7 +53,7 @@ function newGovernanceFunctionLibraryVotes() { if (node.payload === undefined) { return } node.payload.votes = 0 /* - When we reach certain node types, we will halt the distribution, because thse are targets for + When we reach certain node types, we will halt the distribution, because these are targets for voting power. */ if ( @@ -125,7 +125,7 @@ function newGovernanceFunctionLibraryVotes() { node.payload.votes = node.payload.votes + votes drawVotes(node, node.payload.votes, switchPercentage) /* - When we reach certain node types, we will halt the distribution, because thse are targets for + When we reach certain node types, we will halt the distribution, because these are targets for voting power. */ if ( diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 6f0c75711..7ba6c94d5 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -992,6 +992,11 @@ "name": "Plugins Functions", "propertyName": "pluginsFunctions", "functionName": "newGovernanceFunctionLibraryPluginsFunctions" + }, + { + "name": "User Referrals", + "propertyName": "userrReferrals", + "functionName": "newGovernanceFunctionLibraryUserReferrals" } ], "utilities": [],