-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBlockToStoreInBlockchain.ts
80 lines (74 loc) · 1.95 KB
/
getBlockToStoreInBlockchain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import getCurrentBlock from './getCurrentBlockchainBlock';
import Web3 from 'web3';
// TODO: Fix this formatting
/**
* @description Returns a block object to store in the database
* @param currentHash - current block hash
*/
const getBlockToStoreInBlockchain = (
currentHash
): {
number: string;
hash: string;
parentHash: string;
nonce: string;
extraData: string;
sha3Uncles: string;
transactionsRoot: string;
receiptsRoot: string;
stateRoot: string;
miner: any;
timestamp: string;
size: string;
gasLimit: string;
gasUsage: string;
logsBloom: string;
totalDifficulty: string;
difficulty: string;
mixHash: string;
transactions: any[];
uncleHeaders: any[];
} => {
let currentBlock = getCurrentBlock();
let {
number,
parentHash,
nonce,
uncleHash,
transactionsTrie,
receiptTrie,
coinbase,
stateRoot,
difficulty,
logsBloom,
gasLimit,
gasUsed,
mixHash,
extraData,
timestamp
} = currentBlock.header;
let blockTemplate = {
number: Web3.utils.toHex(number.toString()),
hash: '0x' + currentHash.toString('hex'),
parentHash: '0x' + parentHash.toString('hex'),
nonce: '0x' + nonce.toString('hex'),
extraData: '0x' + extraData.toString('hex'),
sha3Uncles: '0x' + uncleHash.toString('hex'),
transactionsRoot: '0x' + transactionsTrie.toString('hex'),
receiptsRoot: '0x' + receiptTrie.toString('hex'),
stateRoot: '0x' + stateRoot.toString('hex'),
miner: coinbase.toString(),
timestamp: Web3.utils.toHex(timestamp.toString()),
size: '0x0',
gasLimit: Web3.utils.toHex(gasLimit.toString()),
gasUsage: Web3.utils.toHex(gasUsed.toString()),
logsBloom: '0x' + logsBloom.toString('hex'),
totalDifficulty: '0x0',
difficulty: Web3.utils.toHex(difficulty.toString()),
mixHash: '0x' + mixHash.toString('hex'),
transactions: [],
uncleHeaders: []
};
return blockTemplate;
};
export default getBlockToStoreInBlockchain;