Skip to content

Commit da633a9

Browse files
committed
fix
1 parent 3d6310e commit da633a9

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/types/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export interface BlockTransaction {
2+
hash: string;
3+
size: number;
4+
fee: number;
5+
time: number;
6+
inputs: any[];
7+
out: any[];
8+
}
9+
10+
export interface BlockInfo {
11+
hash: string;
12+
height: number;
13+
time: number;
14+
size: number;
15+
txCount: number;
16+
prevBlockHash: string;
17+
nextBlockHash: string;
18+
merkleRoot: string;
19+
version: number;
20+
bits: number;
21+
nonce: number;
22+
weight: number;
23+
difficulty: number;
24+
transactions: BlockTransaction[];
25+
reward: number;
26+
miner: string;
27+
}

src/utils/api.ts

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

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

374374
// Process the block data according to blockchain.info API format
375375
const processedData: BlockInfo = {
@@ -386,10 +386,23 @@ export const fetchBlockDetails = async (hashOrHeight: string): Promise<BlockInfo
386386
nonce: data.nonce || 0,
387387
weight: data.weight || 0,
388388
difficulty: data.difficulty || 0,
389-
transactions: Array.isArray(data.tx) ? data.tx : [],
390-
reward: calculateBlockReward(data.height || 0)
389+
transactions: Array.isArray(data.tx) ? data.tx.map((tx: any) => ({
390+
hash: tx.hash || '',
391+
size: tx.size || 0,
392+
fee: tx.fee || 0,
393+
time: tx.time || data.time || Math.floor(Date.now() / 1000),
394+
inputs: tx.inputs || [],
395+
out: tx.out || []
396+
})) : [],
397+
reward: calculateBlockReward(data.height || 0),
398+
miner: extractMinerFromCoinbase(data.tx?.[0]?.inputs?.[0]?.script || '')
391399
};
392400

401+
// Validate the processed data
402+
if (!isBlockHash(processedData.hash)) {
403+
throw new Error('Invalid block data: hash is not in correct format');
404+
}
405+
393406
return processedData;
394407
} catch (error) {
395408
console.error('Error fetching block details:', error);

0 commit comments

Comments
 (0)