Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed Jun 25, 2019
1 parent 1bca769 commit e8f21cf
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 146 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-use-promise": "^0.2.0",
"use-interval": "^1.1.0",
"victory": "^31.3.0"
},
"devDependencies": {
Expand Down
59 changes: 28 additions & 31 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from "react";

import { AppBar, Card, CardContent, CardHeader, Toolbar, Typography } from "@material-ui/core";
import * as React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Address from "./containers/Address";
import Block from "./containers/Block";
Expand All @@ -18,35 +17,33 @@ const routes = [
{ path: "/address/:address", component: Address, title: "Address Details" },
];

class App extends React.Component {
public render() {
return (
<>
<AppBar position="static" color="default" elevation={0}>
<Toolbar>
<Typography>Jade Block Explorer</Typography>
</Toolbar>
</AppBar>
<Router>
<Switch>
{
routes.map((routeProps, i) => {
const wrapped = (props: any) => (
<Card>
<CardHeader title={routeProps.title} />
<CardContent>
{routeProps.component({ ...props })}
</CardContent>
</Card>
);
return (<Route key={i} path={routeProps.path} component={wrapped} exact={routeProps.exact} />);
})
}
</Switch>
</Router>
</>
);
}
function App(props: any) {
return (
<>
<AppBar position="static" color="default" elevation={0}>
<Toolbar>
<Typography>Jade Block Explorer</Typography>
</Toolbar>
</AppBar>
<Router>
<Switch>
{
routes.map((routeProps, i) => {
const wrapped = (p: any) => (
<Card>
<CardHeader title={routeProps.title} />
<CardContent>
{routeProps.component({ ...p })}
</CardContent>
</Card>
);
return (<Route key={i} path={routeProps.path} component={wrapped} exact={routeProps.exact} />);
})
}
</Switch>
</Router>
</>
);
}

export default App;
9 changes: 0 additions & 9 deletions src/components/Dashboard/Dashboard.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/Dashboard/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/components/Header/Header.css

This file was deleted.

22 changes: 0 additions & 22 deletions src/components/Header/Header.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/Header/index.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/components/validators.ts

This file was deleted.

26 changes: 18 additions & 8 deletions src/containers/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@ import * as React from "react";
import usePromise from "react-use-promise";
import AddressView from "../components/AddressView";
import erpc from "../erpc";
import { useBlockNumber } from "../helpers";
const unit = require("ethjs-unit");

export default function Address(props: any) {
const { address } = props.match.params;
const [{ transactionCount, balance, code }, error, state] = usePromise(
const [blockNumber] = useBlockNumber();
const [result, error, state] = usePromise(
async () => {
const blockNumber = await erpc.eth_blockNumber();
if (blockNumber === undefined) {
return;
}
const hexBlockNumber = `0x${blockNumber.toString(16)}`;
return {
transactionCount: await erpc.eth_getTransactionCount(address, blockNumber),
balance: await erpc.eth_getBalance(address),
code: await erpc.eth_getCode(address),
transactionCount: await erpc.eth_getTransactionCount(address, hexBlockNumber),
balance: await erpc.eth_getBalance(address, hexBlockNumber),
code: await erpc.eth_getCode(address, hexBlockNumber),
};
},
[address],
[address, blockNumber],
);
if (error) {
return (<div>Oops. there was an error. try again later: {error.message}</div>)
}
if (!result) {
return null;
}
const { transactionCount, balance, code } = result;
return (
<>
{state}
{error && <div> Error Displaying Balance for address: {address} </div>}
<AddressView
address={address}
txCount={transactionCount ? parseInt(transactionCount, 10) : 0}
Expand Down
6 changes: 3 additions & 3 deletions src/containers/BlockList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import * as React from "react";
import usePromise from "react-use-promise";
import BlockList from "../components/BlockList";
import getBlocks from "../helpers";
import { CircularProgress } from "@material-ui/core";

interface IProps {
from: number;
to: number;
}


export default function BlockListContainer(props: IProps) {
const { from, to } = props;
const [blocks, error, state] = usePromise(() => getBlocks(from, to), [from, to]);
if (!blocks) {
return null;
if (!blocks && state === "pending") {
return <CircularProgress />;
}
return (
<div>
Expand Down
47 changes: 30 additions & 17 deletions src/containers/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Grid, Typography } from "@material-ui/core";
import { Button, Grid, Typography, CircularProgress } from "@material-ui/core";
import BigNumber from "bignumber.js";
import * as React from "react";
import usePromise from "react-use-promise";
Expand All @@ -7,12 +7,12 @@ import { hashesToGH, weiToGwei } from "../components/formatters";
import HashChart from "../components/HashChart";
import HashRate from "../components/HashRate";
import erpc from "../erpc";
import getBlocks from "../helpers";
import getBlocks, { useBlockNumber } from "../helpers";
import BlockList from "./BlockList";

const config = {
blockTime: 15, // seconds
blockHistoryLength: 100,
blockHistoryLength: 16,
chartHeight: 200,
chartWidth: 400,
};
Expand Down Expand Up @@ -55,31 +55,44 @@ const getStyles = () => {
};
};

export default (props: any) => {
const styles = getStyles();
const [results, error, state] = usePromise(async () => {
const bn = await erpc.eth_blockNumber();
const blockNum: number = parseInt(bn, 16);
const useDashboardInfo = (blockNumber: number): any => {
const [dashboardResults, setDashboardResults] = React.useState();

const [, error, state] = usePromise(async () => {
if (blockNumber === undefined) {
return null;
}
const rangeOfBlocks: any[] = await getBlocks(
Math.max(blockNum - config.blockHistoryLength + 1, 0),
blockNum,
Math.max(blockNumber - config.blockHistoryLength + 1, 0),
blockNumber,
);
const gp: number = parseInt(await erpc.eth_gasPrice(), 16);
return {
blockNumber: blockNum,
const r = {
chainId: parseInt(await erpc.eth_chainId(), 16),
block: await erpc.eth_getBlockByNumber(bn, true),
block: await erpc.eth_getBlockByNumber(`0x${blockNumber.toString(16)}`, true),
blocks: rangeOfBlocks,
gasPrice: gp,
syncing: await erpc.eth_syncing(),
peerCount: parseInt(await erpc.net_peerCount(), 16),
};
}, []);
if (!results) {
return null;
setDashboardResults(r);
return r;
}, [blockNumber]);
return [dashboardResults, error, state];
};

export default (props: any) => {
const styles = getStyles();
const [blockNumber] = useBlockNumber();
const [results, error, state] = useDashboardInfo(blockNumber);
if (error && !results) {
return (<div>Oops. Something went wrong. Please try again. <br /><br /><code>{error.message}</code></div>);
}
if (state && !results) {
return (<CircularProgress />);
}
const { block, blocks, gasPrice, peerCount, blockNumber, chainId, syncing } = results;

const { block, blocks, gasPrice, peerCount, chainId, syncing } = results;
return (
<div>
<Grid container={true} spacing={24}>
Expand Down
2 changes: 1 addition & 1 deletion src/containers/NodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import erpc from "../erpc";
export default function NodeView(props: any) {
const [blockNumber, error, state] = usePromise(() => erpc.eth_blockNumber(), []);
return (
<BlockList key={1} from={Math.max(parseInt(blockNumber, 16) - 15, 0)} to={parseInt(blockNumber, 16)} />
<BlockList from={Math.max(parseInt(blockNumber, 16) - 15, 0)} to={parseInt(blockNumber, 16)} />
);
}
2 changes: 1 addition & 1 deletion src/erpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export default new ERPC({
transport: {
type: "http",
host: "localhost",
port: 58578,
port: 60178,
},
});
14 changes: 13 additions & 1 deletion src/helpers.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import * as React from "react";
import useInterval from "use-interval";
import erpc from "./erpc";

const getBlocks = (from: number, to: number): Promise<any> => {
export const getBlocks = (from: number, to: number): Promise<any> => {
const promises: any[] = [];
for (let i = from; i < to; i++) {
promises.push(erpc.eth_getBlockByNumber(`0x${i.toString(16)}`, true));
}
return Promise.all(promises);
};

export const useBlockNumber = () => {
const [blockNumber, setBlockNumber] = React.useState();
useInterval(() => {
erpc.eth_blockNumber().then((bn: string) => {
setBlockNumber(parseInt(bn, 16));
});
}, 1000, true);
return [blockNumber];
};

export default getBlocks;

0 comments on commit e8f21cf

Please sign in to comment.