diff --git a/app/js/index.js b/app/js/index.js index c9ce676..f4f82a0 100644 --- a/app/js/index.js +++ b/app/js/index.js @@ -18,6 +18,9 @@ TODO: - listen to events to update UI, (initially on page load but within function calls) */ +const MAINNET = 1; +const TESTNET = 3; + // Todo Resolve ENS entries const options = [ { 'label' : 'Jarrad (Test)', 'value' : '0x061b0227116e76025D5573cFbb1Ac854916286Fe' }, @@ -82,7 +85,8 @@ class App extends React.Component { status: [] }, award: 0, - praise: '' + praise: '', + networkName: '' } constructor(props) { @@ -97,11 +101,19 @@ class App extends React.Component { componentDidMount() { EmbarkJS.onReady(async (err) => { - // TODO: check for chain if (err) { return this.setState({error: err.message || err}); } + const netId = await web3.eth.net.getId(); + if (EmbarkJS.environment === 'testnet' && netId !== TESTNET) { + this.setState({ error: 'Please connect to Ropsten' }); + return; + } else if (EmbarkJS.environment === 'livenet' && netId !== MAINNET) { + this.setState({ error: 'Please connect to Mainnet' }); + return; + } + this.setState({busy: false}); this.getCurrentContributorData(); @@ -133,16 +145,31 @@ class App extends React.Component { error: '', errorMsg: '', award: 0 - }) + }); } 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()); + } + + console.log(currentContributor); + 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) { - return await Meritocracy.methods.contributors(_address).call(); + const contributor = await Meritocracy.methods.contributors(_address).call(); + contributor.praiseNum = await Meritocracy.methods.getStatusLength(_address).call(); + return contributor; } async getContributors() { @@ -162,16 +189,18 @@ class App extends React.Component { let addresses = selectedContributors.map(a => a.value); + const sntAmount = web3.utils.toWei(award.toString(), "ether"); + let toSend; switch(addresses.length) { case 0: this.setState({errorMsg: 'No Contributor Selected'}); return; case 1: - toSend = Meritocracy.methods.award(addresses[0], award, praise); + toSend = Meritocracy.methods.award(addresses[0], sntAmount, praise); break; default: - toSend = Meritocracy.methods.awardContributors(addresses, award, praise); + toSend = Meritocracy.methods.awardContributors(addresses, sntAmount, praise); break; } @@ -180,9 +209,8 @@ class App 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(); this.resetUIFields(); + this.getCurrentContributorData(); } catch(e) { this.setState({errorMsg: 'tx failed? got enough tokens to award?'}); console.error(e); @@ -224,7 +252,7 @@ class App extends React.Component { render() { const { selectedContributors, contributorList, award, currentContributor, busy, error, errorMsg } = this.state; - + if (error) { return (
Something went wrong connecting to ethereum. Please make sure you have a node running or are using metamask to connect to the ethereum network:
@@ -266,7 +294,7 @@ class App extends React.Component { Your Received Kudos: { currentContributor.received } SNT
- 0x00 has sent you 500 SNT "keep up the good work" + {currentContributor.praises && currentContributor.praises.map((item, i) => {item.author} has sent you {web3.utils.fromWei(item.amount, "ether")} SNT {item.praise && "\"" + item.praise + "\""})} diff --git a/config/contracts.js b/config/contracts.js index 07ee867..9405b06 100644 --- a/config/contracts.js +++ b/config/contracts.js @@ -154,12 +154,12 @@ module.exports = { }, "afterDeploy": [ // Give Tokens to Meritocracy Owner - "SNT.methods.generateTokens('$accounts[0]', '100000000000000000000').send()", + "SNT.methods.generateTokens('$accounts[0]', '1000000000000000000000').send()", // Add All Contributors "Meritocracy.methods.addContributors([" + getContributors().toString() + "]).send()", // Allocate Owner Tokens - "SNT.methods.approve('$Meritocracy', 10000).send()", - "Meritocracy.methods.allocate(10000).send()", + "SNT.methods.approve('$Meritocracy', '1000000000000000000000').send()", + "Meritocracy.methods.allocate('1000000000000000000000').send()", ] }, diff --git a/contracts/Meritocracy.sol b/contracts/Meritocracy.sol index 7b63aa8..a009332 100644 --- a/contracts/Meritocracy.sol +++ b/contracts/Meritocracy.sol @@ -151,6 +151,22 @@ contract Meritocracy { emit ContributorTransaction(cSender.addr, cReceiver.addr); } + function getStatusLength(address _contributor) public view returns (uint) { + return contributors[_contributor].status.length; + } + + function getStatus(address _contributor, uint _index) public view returns ( + address author, + string memory praise, + uint256 amount, + uint256 time + ) { + author = contributors[_contributor].status[_index].author; + praise = contributors[_contributor].status[_index].praise; + amount = contributors[_contributor].status[_index].amount; + time = contributors[_contributor].status[_index].time; + } + // Allow Contributor to award multiple Contributors function awardContributors(address[] calldata _contributors, uint256 _amountEach, string calldata _praise) external { // Locals