Governance: Reading token balance from the blockchain.

This commit is contained in:
Luis Fernando Molina committed 2021-06-14 16:20:31 +02:00
1 parent c8dd0cacff
commit 37ea7f735e
4 files changed
+52 -3

No files matched your search

@@ -1,7 +1,7 @@
{
"type": "User Profile",
"name": "Luis-Fernando-Molina",
"config": "{\n \"tokens\": 10000000\n}",
"config": "{\n \"tokens\": 10000000,\n\"blockchainAccount\": \"0x9546eAA9380A224a56d7067D712296E9D40d40e1\"\n}",
"project": "Governance",
"contributionClaims": {
"type": "Contribution Claims",
@@ -117,7 +117,13 @@ function newGovernanceFunctionLibraryUserReferrals() {
function distributeForProfile(userProfile) {
if (userProfile === undefined || userProfile.payload === undefined) { return }
let referringPower = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'tokens')
let referringPower
if (userProfile.payload.blockchainTokens === undefined) {
referringPower = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'tokens')
} else {
referringPower = userProfile.payload.blockchainTokens
}
let referringCount = 0
userProfile.payload.referring.ownPower = referringPower
distributeUserReferrals(userProfile, referringPower, referringCount)
@@ -157,6 +163,8 @@ function newGovernanceFunctionLibraryUserReferrals() {
}
function calculateAwardedForProfile(userProfile) {
if (userProfile.payload === undefined) { return }
let totalPowerRewardRation = tokensReward / totalReferralsPower
if (totalPowerRewardRation < 1) { totalPowerRewardRation = 1 }
@@ -115,7 +115,14 @@ function newGovernanceFunctionLibraryVotes() {
}
function distributeForProfile(userProfile) {
let votes = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'tokens')
if (userProfile.payload === undefined) { return }
let votes
if (userProfile.payload.blockchainTokens === undefined) {
votes = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'tokens')
} else {
votes = userProfile.payload.blockchainTokens
}
distributeVotes(userProfile, votes)
}
@@ -39,6 +39,40 @@ function newGovernanceUserProfileSpace() {
function physics() {
if (UI.projects.foundations.spaces.designSpace.workspace === undefined) { return }
let userProfiles = UI.projects.foundations.spaces.designSpace.workspace.getHierarchyHeadsByNodeType('User Profile')
/*
We will get all the user Profiles tokens from the blockchain, making a call
every 5 seconds so as not to exceed the rate limit.
*/
let timer = 0
for (let i = 0; i < userProfiles.length; i++) {
let userProfile = userProfiles[i]
if (userProfile.payload === undefined) { continue }
if (userProfile.payload.blockchainTokens === undefined) {
let blockchainAccount = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'blockchainAccount')
if (blockchainAccount !== undefined && blockchainAccount !== "") {
setTimeout(getBlockchainTokens, timer, userProfile)
timer = timer + 5000
}
}
}
function getBlockchainTokens(userProfile) {
let blockchainAccount = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(userProfile.payload, 'blockchainAccount')
const url = "https://api.bscscan.com/api?module=account&action=tokenbalance&contractaddress=0xfb981ed9a92377ca4d75d924b9ca06df163924fd&address=" + blockchainAccount + "&tag=latest&apikey=YourApiKeyToken"
fetch(url).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data)
userProfile.payload.uiObject.setInfoMessage(data)
userProfile.payload.blockchainTokens = Number(data.result) / 1000000000000000000
}).catch(function (err) {
userProfile.payload.uiObject.setErrorMessage(err.message)
});
}
}
function draw() {