From 21b2b6cb437462afd1d94fbeef1530fe27692a52 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 10 Apr 2019 14:35:33 -0400 Subject: [PATCH] move getContributor function to Meritocracy service --- app/js/components/Home.js | 64 ++++-------------------------- app/js/services/Meritocracy.js | 71 ++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 59 deletions(-) diff --git a/app/js/components/Home.js b/app/js/components/Home.js index 85937dd..0a26a11 100644 --- a/app/js/components/Home.js +++ b/app/js/components/Home.js @@ -6,7 +6,7 @@ import Select from 'react-select'; import Meritocracy from 'Embark/contracts/Meritocracy'; -import {getContributorList} from '../services/Meritocracy'; +import {getFormattedContributorList, getCurrentContributorData} from '../services/Meritocracy'; /* TODO: @@ -44,14 +44,11 @@ class Home extends React.Component { async componentDidMount() { try { - this.contibutorList = await getContributorList(); - this.contibutorList = this.contibutorList.map(prepareOptions); + const contributorList = await getFormattedContributorList(); - await this.getContributors(); + const currentContributor = await getCurrentContributorData(); - this.getCurrentContributorData(); - - this.setState({busy: false}); + this.setState({busy: false, currentContributor, contributorList}); } catch (e) { this.setState({errorMsg: e.message || e}); } @@ -82,38 +79,6 @@ class Home extends React.Component { }); } - async getCurrentContributorData(){ - const currentContributor = await this.getContributor(web3.eth.defaultAccount); - - let praises = []; - for(let i = 0; i < currentContributor.praiseNum; i++){ - praises.push(Meritocracy.methods.getStatus(web3.eth.defaultAccount, i).call()); - } - - const contribData = this.contibutorList.find(x => x.value === web3.eth.defaultAccount); - if(contribData) currentContributor.name = contribData.label; - - currentContributor.praises = await Promise.all(praises); - currentContributor.allocation = web3.utils.fromWei(currentContributor.allocation, "ether"); - currentContributor.totalForfeited = web3.utils.fromWei(currentContributor.totalForfeited, "ether"); - currentContributor.totalReceived = web3.utils.fromWei(currentContributor.totalReceived, "ether"); - currentContributor.received = web3.utils.fromWei(currentContributor.received, "ether"); - - this.setState({currentContributor}); - } - - async getContributor(_address) { - const contributor = await Meritocracy.methods.contributors(_address).call(); - contributor.praiseNum = await Meritocracy.methods.getStatusLength(_address).call(); - return contributor; - } - - async getContributors() { - const registry = await Meritocracy.methods.getRegistry().call({from: web3.eth.defaultAccount}); - const contributorList = this.contibutorList.filter(x => registry.includes(x.value) && x.value !== web3.eth.defaultAccount); - this.setState({contributorList}); - } - async awardTokens(e) { const {award, selectedContributors, praise} = this.state; @@ -146,7 +111,8 @@ class Home extends React.Component { const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount}); const receipt = await toSend.send({from: web3.eth.defaultAccount, gas: estimatedGas + 1000}); this.resetUIFields(); - this.getCurrentContributorData(); + const currentContributor = await getCurrentContributorData(); + this.setState({currentContributor}); } catch(e) { this.setState({errorMsg: 'tx failed? got enough tokens to award?'}); console.error(e); @@ -177,7 +143,8 @@ class Home extends React.Component { const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount}); const receipt = await toSend.send({from: web3.eth.defaultAccount, gas: estimatedGas + 1000}); - this.getCurrentContributorData(); + const currentContributor = await getCurrentContributorData(); + this.setState({currentContributor}); } catch(e) { this.setState({errorMsg: 'tx failed? Did you allocate all your tokens first?'}); console.error(e); @@ -238,19 +205,4 @@ class Home extends React.Component { } } - -// === Utils =============================================== - -const prepareOptions = option => { - if(option.value.match(/^0x[0-9A-Za-z]{40}$/)){ // Address - option.value = web3.utils.toChecksumAddress(option.value); - } else { // ENS Name - // TODO: resolve ENS names - // EmbarkJS.Names.resolve("ethereum.eth").then(address => { - // console.log("the address for ethereum.eth is: " + address); - // - } - return option; -}; - export default Home; diff --git a/app/js/services/Meritocracy.js b/app/js/services/Meritocracy.js index 89d8971..d053042 100644 --- a/app/js/services/Meritocracy.js +++ b/app/js/services/Meritocracy.js @@ -2,10 +2,12 @@ import Meritocracy from 'Embark/contracts/Meritocracy'; import EmbarkJS from 'Embark/EmbarkJS'; -const mainAccount = web3.eth.defaultAccount; + +let contributorList; export function addContributor(name, address) { return new Promise(async (resolve, reject) => { + const mainAccount = web3.eth.defaultAccount; try { const list = await getContributorList(); list.push({label: name, value: address}); @@ -34,8 +36,8 @@ export function getContributorList(hash) { } const content = await EmbarkJS.Storage.get(hash); - const data = JSON.parse(content); - resolve(data); + contributorList = JSON.parse(content); + resolve(contributorList); } catch (e) { const message = 'Error getting contributor file on IPFS'; console.error(message); @@ -45,6 +47,69 @@ export function getContributorList(hash) { }); } +export async function getFormattedContributorList(hash) { + return new Promise(async (resolve, reject) => { + const mainAccount = web3.eth.defaultAccount; + try { + let list = await getContributorList(hash); + list = list.map(prepareOptions); + + const registry = await Meritocracy.methods.getRegistry().call({from: mainAccount}); + list = list.filter(contributorData => registry.includes(contributorData.value) && contributorData.value !== mainAccount); + + resolve(list); + } catch (e) { + const message = 'Error getting formatted contributor file on IPFS'; + console.error(message); + console.error(e); + reject(message); + } + }); +} + +const prepareOptions = option => { + if(option.value.match(/^0x[0-9A-Za-z]{40}$/)){ // Address + option.value = web3.utils.toChecksumAddress(option.value); + } else { // ENS Name + // TODO: resolve ENS names + // EmbarkJS.Names.resolve("ethereum.eth").then(address => { + // console.log("the address for ethereum.eth is: " + address); + // + } + return option; +}; + +export async function getCurrentContributorData(){ + const mainAccount = web3.eth.defaultAccount; + const currentContributor = await getContributor(mainAccount); + + let praises = []; + for(let i = 0; i < currentContributor.praiseNum; i++){ + praises.push(Meritocracy.methods.getStatus(mainAccount, i).call()); + } + + if (!contributorList) { + await getContributorList(); + } + + const contribData = contributorList.find(x => x.value === mainAccount); + if(contribData) currentContributor.name = contribData.label; + + currentContributor.praises = await Promise.all(praises); + currentContributor.allocation = web3.utils.fromWei(currentContributor.allocation, "ether"); + currentContributor.totalForfeited = web3.utils.fromWei(currentContributor.totalForfeited, "ether"); + currentContributor.totalReceived = web3.utils.fromWei(currentContributor.totalReceived, "ether"); + currentContributor.received = web3.utils.fromWei(currentContributor.received, "ether"); + + return currentContributor; +} + +export async function getContributor(_address) { + const contributor = await Meritocracy.methods.contributors(_address).call(); + contributor.praiseNum = await Meritocracy.methods.getStatusLength(_address).call(); + return contributor; +} + export function saveContributorList(list) { return new Promise(async (resolve, reject) => { try {