Skip to content

Commit 47e70ef

Browse files
authored
Merge pull request #5 from mu373/render-each-variable-on-load
Render each variable on load
2 parents 35e1360 + 3376de7 commit 47e70ef

File tree

3 files changed

+243
-161
lines changed

3 files changed

+243
-161
lines changed

components/DashboardPanels.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import React from 'react';
33
import { capitalizeFirstLetter, decimalFloor } from '../lib/utils';
44

55
const formatValue = (value) => {
6-
return value !== undefined ? value : '-';
6+
if (value === undefined || value === null) {
7+
return '-'
8+
} else {
9+
return value
10+
}
711
};
812

913
export const BlockchainPanel: React.FC<{ data: any }> = ({ data }) => {
@@ -31,7 +35,7 @@ export const BlockchainPanel: React.FC<{ data: any }> = ({ data }) => {
3135
{formatValue(data.verificationProgress && decimalFloor(data.verificationProgress * 100, 10))} %
3236
</dd>
3337
<dt>Size on disk</dt>
34-
<dd>{formatValue(data.chainSize && (data.chainSize / (1024 ** 3)).toFixed(1))} GB</dd>
38+
<dd>{formatValue(data.chainSize && data.chainSize.toFixed(1))} GB</dd>
3539
<dt>Blockchain size</dt>
3640
<dd>{formatValue(data.currentChainSize && data.currentChainSize.toFixed(1))} GB</dd>
3741
<dt>Difficulty</dt>
@@ -88,7 +92,7 @@ return (
8892
<dd>{formatValue(data.electrumBlockHeight && data.electrumBlockHeight.toLocaleString())}</dd>
8993
<dt>Indexed</dt>
9094
<dd>
91-
{formatValue(data.electrumBlockHeight && data.blockInfo?.blocks && decimalFloor(data.electrumBlockHeight / data.blockInfo.blocks * 100, 10))} %
95+
{formatValue(data.electrumBlockHeight && data.blocks && decimalFloor(data.electrumBlockHeight / data.blocks * 100, 10))} %
9296
</dd>
9397
</dl>
9498
</div>

lib/data.ts

Lines changed: 118 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ async function getAPI(cfg: BitcoindConfig, method: string) {
3434
return data;
3535
}
3636

37-
const net = require('net')
3837
const ElectrumClient = require("@lily-technologies/electrum-client");
3938
const ELECTRUM_HOST = process.env.ELECTRUM_HOST || "electrs";
4039
const ELECTRUM_PORT = process.env.ELECTRUM_PORT || 50001;
@@ -43,77 +42,150 @@ const electrumConfig = {
4342
version: "1.4",
4443
}
4544

45+
const electrumPersistencePolicy = {
46+
retryPeriod: 10000,
47+
maxRetry: 3,
48+
pingPeriod: 120000,
49+
callback: null
50+
}
51+
4652
export async function getElectrumVersion() {
47-
let electrumClient = new ElectrumClient(ELECTRUM_PORT, ELECTRUM_HOST, "tcp");
48-
const initClient = await electrumClient.initElectrum(electrumConfig);
49-
const version = initClient.versionInfo[0]
50-
return version;
53+
try {
54+
let electrumClient = new ElectrumClient(ELECTRUM_PORT, ELECTRUM_HOST, "tcp");
55+
const initClient = await electrumClient.initElectrum(electrumConfig, electrumPersistencePolicy);
56+
const version = initClient.versionInfo[0]
57+
return version;
58+
} catch (error) {
59+
console.error("Error loading data from Electrum server:", error);
60+
return null;
61+
}
5162
}
5263

5364
export async function getElectrumBlockHeight() {
54-
const electrumClient = new ElectrumClient(ELECTRUM_PORT, ELECTRUM_HOST, "tcp");
55-
const initClient = await electrumClient.initElectrum(electrumConfig);
56-
const headers = await initClient.blockchainHeaders_subscribe();
57-
const height = headers.height
58-
console.log(height)
59-
return height;
65+
try {
66+
const electrumClient = new ElectrumClient(ELECTRUM_PORT, ELECTRUM_HOST, "tcp");
67+
const initClient = await electrumClient.initElectrum(electrumConfig);
68+
const headers = await initClient.blockchainHeaders_subscribe();
69+
const height = headers.height
70+
return height;
71+
} catch (error) {
72+
console.error("Error loading data from Electrum server:", error);
73+
return null;
74+
}
6075
}
6176

6277
export async function getBlockInfo() {
63-
const json = await getAPI(cfg, 'getblockchaininfo');
64-
const res = json.result;
65-
return {
66-
chain: res.chain,
67-
blocks: res.blocks,
68-
chainSize: res.size_on_disk,
69-
verificationProgress: res.verificationprogress,
70-
pruned: res.pruned,
71-
difficulty: res.difficulty,
78+
try {
79+
const json = await getAPI(cfg, 'getblockchaininfo');
80+
const res = json.result;
81+
return {
82+
chain: res.chain,
83+
blocks: res.blocks,
84+
chainSize: res.size_on_disk,
85+
verificationProgress: res.verificationprogress,
86+
pruned: res.pruned,
87+
difficulty: res.difficulty,
88+
}
89+
} catch (error) {
90+
console.error("Error loading data from bitcoind", error);
91+
return {
92+
chain: null,
93+
blocks: null,
94+
chainSize: null,
95+
verificationProgress: null,
96+
pruned: null,
97+
difficulty: null,
98+
}
7299
}
73100
}
74101

75102
export async function getConnectionCount() {
76-
const json = await getAPI(cfg, 'getconnectioncount');
77-
return ( json.result.result )
103+
try {
104+
const json = await getAPI(cfg, 'getconnectioncount');
105+
return ( json.result.result )
106+
} catch (error) {
107+
console.error("Error loading data from bitcoind", error);
108+
return null;
109+
}
78110
}
79111

80112
export async function getNetworkInfo() {
81-
const json = await getAPI(cfg, 'getnetworkinfo');
82-
return ( json.result )
113+
try {
114+
const json = await getAPI(cfg, 'getnetworkinfo');
115+
return ( json.result )
116+
} catch (error) {
117+
console.error("Error loading data from bitcoind", error);
118+
return null;
119+
}
83120
}
84121

85122
export async function getMiningInfo() {
86-
const json = await getAPI(cfg, 'getmininginfo');
87-
const res = json.result;
88-
return {
89-
hashrate: res.networkhashps,
123+
try {
124+
const json = await getAPI(cfg, 'getmininginfo');
125+
const res = json.result;
126+
return {
127+
hashrate: res.networkhashps
128+
}
129+
} catch (error) {
130+
console.error("Error loading data from bitcoind", error);
131+
return {
132+
hashrate: null
133+
}
90134
}
91135
}
92136

93137
export async function getMempoolInfo() {
94-
const json = await getAPI(cfg, 'getmempoolinfo');
95-
const res = json.result;
96-
return {
97-
transactions: res.size,
98-
usage: res.usage,
99-
totalFee: res.total_fee,
100-
minFee: res.mempoolminfee,
138+
try {
139+
const json = await getAPI(cfg, 'getmempoolinfo');
140+
const res = json.result;
141+
return {
142+
transactions: res.size,
143+
usage: res.usage,
144+
totalFee: res.total_fee,
145+
minFee: res.mempoolminfee,
146+
}
147+
} catch (error) {
148+
console.error("Error loading data from bitcoind", error);
149+
return {
150+
transactions: null,
151+
usage: null,
152+
totalFee: null,
153+
minFee: null
154+
}
101155
}
102156
}
103157

104158
export async function getCurrentBlock() {
105-
const url = 'https://blockchain.info/latestblock';
106-
const response = await fetch(url);
107-
const data = await response.json();
108-
return ( data.height );
159+
try {
160+
const url = 'https://blockchain.info/latestblock';
161+
const response = await fetch(url);
162+
const data = await response.json();
163+
return ( data.height );
164+
} catch (error) {
165+
console.error("Error loading data from blockchain.info API", error);
166+
return null
167+
}
109168
}
110169

111170
export async function getCurrentChainSize() {
112-
const url = 'https://api.blockchain.info/charts/blocks-size?timespan=1days'
113-
const response = await fetch(url);
114-
const dataJson = await response.json();
115-
const data = dataJson.values[0];
116-
const date = data.x
117-
const size = data.y
118-
return ( {date: date, chainSize: size } )
171+
try {
172+
const url = 'https://api.blockchain.info/charts/blocks-size?timespan=1days'
173+
const response = await fetch(url);
174+
const dataJson = await response.json();
175+
const data = dataJson.values[0];
176+
const date = data.x
177+
const size = data.y
178+
return (
179+
{
180+
date: date,
181+
chainSize: size
182+
}
183+
)
184+
} catch (error) {
185+
console.error("Error loading data from blockchain.info API", error);
186+
return {
187+
date: null,
188+
chainSize: null
189+
}
190+
}
119191
}

0 commit comments

Comments
 (0)