Skip to content

Commit

Permalink
Merge pull request #20 from energicryptocurrency/develop
Browse files Browse the repository at this point in the history
proposal-creator v1.0.0 release
  • Loading branch information
RyanLucchese authored Apr 30, 2018
2 parents 7599b7d + 99dd55c commit 5ed5108
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 56 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-scripts": "1.0.17"
},
"scripts": {
Expand All @@ -13,4 +13,4 @@
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
}
130 changes: 91 additions & 39 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,83 @@ class App extends Component
this.waitForConfirmations = this.waitForConfirmations.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.getGovernanceInfo = this.getGovernanceInfo.bind(this);
this.getSuperblockInfo = this.getSuperblockInfo.bind(this);
this.getBestBlock = this.getBestBlock.bind(this);
this.updateNetwork = this.updateNetwork.bind(this);
this.initializeEpochs = this.initializeEpochs.bind(this);
}

getGovernanceInfo()
getSuperblockInfo()
{
return fetch(this.explorerAPI + 'getgovernanceinfo')
.then((resp) => {
if (resp.ok) {
var getGovernanceInfo = function()
{
return fetch(this.explorerAPI + 'getgovernanceinfo')
.then((resp) => {
if (resp.ok) {
return resp.json()
.then((responseData) => {
this.setState({governanceInfo: responseData},
function()
{
getSuperblockBudget();
}
);
return responseData;
});
}
return resp.json()
.then((responseData) => {
this.setState({governanceInfo: responseData},
function()
{
this.apiSyncState++;
if (this.apiSyncState == 2) this.initializeEpochs();
}
);
return responseData;
.then((error) => {
return Promise.reject(error);
});
}
return resp.json()
.then((error) => {
return Promise.reject(error);
});
})
.catch(err => {this.setError("Unable to fetch current blockchain information. Please try again later. " + err.toString())});
})
.catch(err => {this.setError("Unable to fetch current blockchain information. Please try again later. " + err.toString())});
}

var getSuperblockBudget = function(depth = 0)
{
const maxDepth = 52;
const superblockNumber = this.state.governanceInfo.nextsuperblock + (depth * this.state.governanceInfo.superblockcycle);
return fetch(this.explorerAPI + 'getsuperblockbudget/' + superblockNumber)
.then((resp) => {
if (resp.ok) {
return resp.json()
.then((responseData) => {
var superblockBudgets = this.state.superblockBudgets || [];
superblockBudgets[depth] = responseData;
this.setState({superblockBudgets: superblockBudgets},
function()
{
if (depth === (maxDepth - 1))
{
this.apiSyncState++;
if (this.apiSyncState === 2) this.initializeEpochs();
}
else
{
getSuperblockBudget(depth + 1);
}
}
);
return responseData;
});
}
return resp.json()
.then((error) => {
return Promise.reject(error);
});
})
.catch(err => {this.setError("Unable to fetch current blockchain information. Please try again later. " + err.toString())});
}

getGovernanceInfo = getGovernanceInfo.bind(this);
getSuperblockBudget = getSuperblockBudget.bind(this);

getGovernanceInfo();
}

getBestBlock()
{
function getBlock(hash)
var getBlock = function(hash)
{
return fetch(this.explorerAPI + 'getblock?hash=' + hash)
.then((resp) => {
Expand All @@ -108,7 +151,7 @@ class App extends Component
function()
{
this.apiSyncState++;
if (this.apiSyncState == 2) this.initializeEpochs();
if (this.apiSyncState === 2) this.initializeEpochs();
}
);
return responseData;
Expand All @@ -122,7 +165,7 @@ class App extends Component
.catch(err => {this.setError("Unable to fetch current blockchain information. Please try again later. " + err.toString())});
}

function getHash(height)
var getHash = function(height)
{
return fetch(this.explorerAPI + 'getblockhash?index=' + Number(height).toString())
.then((resp) => {
Expand All @@ -141,7 +184,7 @@ class App extends Component
.catch(err => {this.setError("Unable to fetch current blockchain information. Please try again later. " + err.toString())});
}

function getHeight()
var getHeight = function()
{
return fetch(this.explorerAPI + 'getblockcount')
.then((resp) => {
Expand All @@ -167,9 +210,9 @@ class App extends Component

updateNetwork(networkName)
{
function fetchBlockchainInfo()
var fetchBlockchainInfo = function()
{
this.getGovernanceInfo();
this.getSuperblockInfo();
this.getBestBlock();
}

Expand All @@ -178,8 +221,8 @@ class App extends Component
this.setState({network: networkName}, function()
{
if (this.state.network === 'main') this.explorerAPI = 'https://explore.energi.network/api/';
else if (this.state.network == 'test') this.explorerAPI = 'http://explore.test.energi.network/api/';
else if (this.state.network === 'test60x') this.explorerAPI = 'http://explore.test60x.energi.network/api/';
else if (this.state.network === 'test') this.explorerAPI = 'https://explore.test.energi.network/api/';
//else if (this.state.network === 'test60x') this.explorerAPI = 'https://explore.test60x.energi.network/api/';
else this.setError("Invalid network");

fetchBlockchainInfo();
Expand All @@ -193,9 +236,9 @@ class App extends Component
return new Promise(resolve => setTimeout(resolve, ms));
}

async function waitForSync()
var waitForSync = async function()
{
while (this.apiSyncState != 2) await sleep(100);
while (this.apiSyncState !== 2) await sleep(100);
}

waitForSync = waitForSync.bind(this);
Expand All @@ -207,14 +250,14 @@ class App extends Component
let new_gobj = this.state.gobj;
new_gobj[0][1].start_epoch = initial_epoch;
new_gobj[0][1].end_epoch = initial_epoch + (gov.superblockcycle * 60);
this.setState({gobj: new_gobj}, this.validateNewState());
this.setState({gobj: new_gobj}, this.validateNewState);
}

componentDidMount()
{
document.title = "Energi Proposal Creator";

this.updateNetwork('test60x');
this.updateNetwork('main');
}

setError(errStr)
Expand Down Expand Up @@ -315,11 +358,15 @@ class App extends Component

function validateProposalAmount(setError, state)
{
const maximumBudgetAmount = state.governanceInfo.lastsuperblock === 0 ? 4000000 : 184000;
const nextSuperblockTime = ((state.governanceInfo.nextsuperblock - state.bestBlock.height) * 60) + state.bestBlock.time;
const superblockIndex = (state.gobj[0][1].start_epoch - nextSuperblockTime) / 60 / state.governanceInfo.superblockcycle;
let needed_budgets = state.superblockBudgets.slice(superblockIndex, superblockIndex + state.payment_cycles);
const maximumBudgetAmount = Math.min(...needed_budgets);

const payment_amount = state.gobj[0][1].payment_amount;
if (payment_amount > maximumBudgetAmount)
{
setError("Payment amount exceeds maximum budget of " + maximumBudgetAmount.toString() + " EGI");
setError("Payment amount exceeds maximum budget of " + maximumBudgetAmount.toString() + " NRG");
return false;
}
if (payment_amount <= 0)
Expand Down Expand Up @@ -350,11 +397,12 @@ class App extends Component
result = result && validateProposalAddress(this.setError, this.state);
result = result && validateProposalAmount(this.setError, this.state);
result = result && validateProposalType(this.setError, this.state);
return result;
}

waitForConfirmations()
{
function fetchConfirmations()
var fetchConfirmations = function()
{
return fetch(this.explorerAPI + 'getrawtransaction?txid=' + this.state.collateral_txhash + '&decrypt=1')
.then((resp) => {
Expand All @@ -379,7 +427,7 @@ class App extends Component
return new Promise(resolve => setTimeout(resolve, ms));
}

async function checkConfirmations()
var checkConfirmations = async function()
{
while (this.state.confirmations < 6)
{
Expand All @@ -404,6 +452,10 @@ class App extends Component
{
this.setState({collateral_txhash: value}, this.waitForConfirmations);
}
else if (name === 'networkSelector')
{
this.updateNetwork(target.value);
}
else
{
// make sure numbers are numbers
Expand All @@ -428,15 +480,15 @@ class App extends Component
new_state.gobj[0][1].end_epoch = new_state.gobj[0][1].start_epoch + (new_state.payment_cycles * this.state.governanceInfo.superblockcycle * 60);
}

this.setState(new_state, this.validateNewState());
this.setState(new_state, this.validateNewState);
}
}

handleSubmit(/*event*/)
{
const dateTime = new Date().getTime();
const timestamp = Math.floor(dateTime / 1000);
this.setState({submitted: true, proposalTime: timestamp}, this.validateNewState());
this.setState({submitted: true, proposalTime: timestamp}, this.validateNewState);
}

render()
Expand Down
6 changes: 2 additions & 4 deletions src/DisplayTotal.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DisplayTotal extends Component
const start_epoch = this.props.gobj[0][1].start_epoch;
const end_epoch = this.props.gobj[0][1].end_epoch;

if (this.props.payment_cycles == 1)
if (this.props.payment_cycles === 1)
{
return "at " + format_date(start_epoch);
}
Expand All @@ -59,13 +59,11 @@ class DisplayTotal extends Component

render()
{
let props = this.props;

if (this.props.gobj[0][1].payment_amount <= 0) return null;

return (
<div class="App-displayTotalDiv">
Total: {this.get_total()} EGI {this.get_date_str()}
Total: {this.get_total()} NRG {this.get_date_str()}
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/PrepareForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import React, { Component } from 'react';
import TextInputField from './TextInputField.js';
import SelectFirstPayment from './SelectFirstPayment.js';
import SelectPaymentCycles from './SelectPaymentCycles.js';
import SelectNetwork from './SelectNetwork.js';
import DisplayTotal from './DisplayTotal.js';
import ValidationError from './ValidationError.js';
import PreparedProposal from './PreparedProposal.js';
Expand Down Expand Up @@ -59,6 +60,7 @@ class PrepareForm extends Component
<TextInputField fieldLabel="Proposal Name" fieldName="name" onChange={this.props.onChange} />
<TextInputField fieldLabel="Proposal Description URL" fieldName="url" onChange={this.props.onChange} />
<SelectFirstPayment {...firstPayment_props} onChange={this.props.onChange} />
<SelectNetwork onChange={this.props.onChange} />
<SelectPaymentCycles onChange={this.props.onChange} />
<TextInputField fieldLabel="Payment Address" fieldName="payment_address" onChange={this.props.onChange} />
<TextInputField fieldLabel="Payment Amount" fieldName="payment_amount" onChange={this.props.onChange} />
Expand Down
2 changes: 0 additions & 2 deletions src/PreparedProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class PreparedProposal extends Component

render()
{
let props = this.props;

if (!this.props.submitted || (this.props.validationError !== '')) return null;

return (
Expand Down
33 changes: 33 additions & 0 deletions src/SelectNetwork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Energi Proposal Creator
*/

import React, { Component } from 'react';

class SelectNetwork extends Component
{
getNetworks()
{
let htmlOptionTags = [];
htmlOptionTags.push(<option value="main"> main </option>);
//htmlOptionTags.push(<option value="test60x"> test60x </option>);
htmlOptionTags.push(<option value="test"> test </option>);
return htmlOptionTags;
}

render()
{
let props = this.props;
return (
<div>
<label>
Network:
<select name="networkSelector" onChange={props.onChange}>
{ this.getNetworks() }
</select>
</label>
</div>
);
}
}

export default SelectNetwork;
2 changes: 0 additions & 2 deletions src/SubmitProposalForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class SubmitProposalForm extends Component

render()
{
let props = this.props;

if (this.props.confirmations < 6) return null;

return (
Expand Down

0 comments on commit 5ed5108

Please sign in to comment.