move getContributor function to Meritocracy service
This commit is contained in:
parent
b766031cd7
commit
21b2b6cb43
|
@ -6,7 +6,7 @@ import Select from 'react-select';
|
||||||
|
|
||||||
import Meritocracy from 'Embark/contracts/Meritocracy';
|
import Meritocracy from 'Embark/contracts/Meritocracy';
|
||||||
|
|
||||||
import {getContributorList} from '../services/Meritocracy';
|
import {getFormattedContributorList, getCurrentContributorData} from '../services/Meritocracy';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
TODO:
|
TODO:
|
||||||
|
@ -44,14 +44,11 @@ class Home extends React.Component {
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
try {
|
try {
|
||||||
this.contibutorList = await getContributorList();
|
const contributorList = await getFormattedContributorList();
|
||||||
this.contibutorList = this.contibutorList.map(prepareOptions);
|
|
||||||
|
|
||||||
await this.getContributors();
|
const currentContributor = await getCurrentContributorData();
|
||||||
|
|
||||||
this.getCurrentContributorData();
|
this.setState({busy: false, currentContributor, contributorList});
|
||||||
|
|
||||||
this.setState({busy: false});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.setState({errorMsg: e.message || 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) {
|
async awardTokens(e) {
|
||||||
const {award, selectedContributors, praise} = this.state;
|
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 estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
|
||||||
const receipt = await toSend.send({from: web3.eth.defaultAccount, gas: estimatedGas + 1000});
|
const receipt = await toSend.send({from: web3.eth.defaultAccount, gas: estimatedGas + 1000});
|
||||||
this.resetUIFields();
|
this.resetUIFields();
|
||||||
this.getCurrentContributorData();
|
const currentContributor = await getCurrentContributorData();
|
||||||
|
this.setState({currentContributor});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
this.setState({errorMsg: 'tx failed? got enough tokens to award?'});
|
this.setState({errorMsg: 'tx failed? got enough tokens to award?'});
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
@ -177,7 +143,8 @@ class Home extends React.Component {
|
||||||
const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
|
const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
|
||||||
const receipt = await toSend.send({from: web3.eth.defaultAccount, gas: estimatedGas + 1000});
|
const receipt = await toSend.send({from: web3.eth.defaultAccount, gas: estimatedGas + 1000});
|
||||||
|
|
||||||
this.getCurrentContributorData();
|
const currentContributor = await getCurrentContributorData();
|
||||||
|
this.setState({currentContributor});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
this.setState({errorMsg: 'tx failed? Did you allocate all your tokens first?'});
|
this.setState({errorMsg: 'tx failed? Did you allocate all your tokens first?'});
|
||||||
console.error(e);
|
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;
|
export default Home;
|
||||||
|
|
|
@ -2,10 +2,12 @@
|
||||||
import Meritocracy from 'Embark/contracts/Meritocracy';
|
import Meritocracy from 'Embark/contracts/Meritocracy';
|
||||||
import EmbarkJS from 'Embark/EmbarkJS';
|
import EmbarkJS from 'Embark/EmbarkJS';
|
||||||
|
|
||||||
const mainAccount = web3.eth.defaultAccount;
|
|
||||||
|
let contributorList;
|
||||||
|
|
||||||
export function addContributor(name, address) {
|
export function addContributor(name, address) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
const mainAccount = web3.eth.defaultAccount;
|
||||||
try {
|
try {
|
||||||
const list = await getContributorList();
|
const list = await getContributorList();
|
||||||
list.push({label: name, value: address});
|
list.push({label: name, value: address});
|
||||||
|
@ -34,8 +36,8 @@ export function getContributorList(hash) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = await EmbarkJS.Storage.get(hash);
|
const content = await EmbarkJS.Storage.get(hash);
|
||||||
const data = JSON.parse(content);
|
contributorList = JSON.parse(content);
|
||||||
resolve(data);
|
resolve(contributorList);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const message = 'Error getting contributor file on IPFS';
|
const message = 'Error getting contributor file on IPFS';
|
||||||
console.error(message);
|
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) {
|
export function saveContributorList(list) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue