Skip to content

Commit 3d6310e

Browse files
committed
fix
1 parent 3d325cc commit 3d6310e

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/pages/BlockDetails.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ export const BlockDetails: React.FC = () => {
3535

3636
try {
3737
const blockData = await fetchBlockDetails(hashOrHeight);
38-
if (!blockData || !blockData.height) {
38+
console.log('Block data:', blockData); // Debug log
39+
40+
if (!blockData || !blockData.hash) {
3941
throw new Error('Invalid block data received');
4042
}
43+
4144
setBlock(blockData);
4245
} catch (err) {
4346
console.error('Error fetching block details:', err);
44-
setError('Failed to load block details');
47+
setError(err instanceof Error ? err.message : 'Failed to load block details');
4548
} finally {
4649
setIsLoading(false);
4750
}

src/utils/api.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,25 @@ export const fetchBlockDetails = async (hashOrHeight: string): Promise<BlockInfo
369369
}
370370

371371
const data = response.data;
372+
console.log('Block data received:', data); // Debug log
372373

373374
// Process the block data according to blockchain.info API format
374375
const processedData: BlockInfo = {
375-
hash: data.hash,
376+
hash: data.hash || '',
376377
height: data.height || 0,
377378
time: data.time || Math.floor(Date.now() / 1000),
378379
size: data.size || 0,
379380
txCount: data.n_tx || 0,
380-
prevBlockHash: data.prev_block,
381-
nextBlockHash: data.next_block,
382-
merkleRoot: data.mrkl_root,
383-
version: data.ver,
384-
bits: data.bits,
385-
nonce: data.nonce,
386-
weight: data.weight,
387-
difficulty: data.difficulty,
388-
transactions: data.tx || [],
389-
reward: data.reward || 0
381+
prevBlockHash: data.prev_block || '',
382+
nextBlockHash: data.next_block || '',
383+
merkleRoot: data.mrkl_root || '',
384+
version: data.ver || 0,
385+
bits: data.bits || 0,
386+
nonce: data.nonce || 0,
387+
weight: data.weight || 0,
388+
difficulty: data.difficulty || 0,
389+
transactions: Array.isArray(data.tx) ? data.tx : [],
390+
reward: calculateBlockReward(data.height || 0)
390391
};
391392

392393
return processedData;
@@ -400,6 +401,15 @@ export const fetchBlockDetails = async (hashOrHeight: string): Promise<BlockInfo
400401
}
401402
};
402403

404+
// Helper function to calculate block reward based on height
405+
const calculateBlockReward = (height: number): number => {
406+
const INITIAL_REWARD = 50;
407+
const HALVING_INTERVAL = 210000;
408+
const halvings = Math.floor(height / HALVING_INTERVAL);
409+
if (halvings >= 64) return 0; // After 64 halvings, reward becomes 0
410+
return INITIAL_REWARD / Math.pow(2, halvings);
411+
};
412+
403413
export const fetchNetworkStats = blockApi.fetchNetworkStats;
404414
export const fetchMempoolInfo = blockApi.fetchMempoolInfo;
405415
export const generateNetworkChartData = blockApi.generateNetworkChartData;

0 commit comments

Comments
 (0)