-
Notifications
You must be signed in to change notification settings - Fork 69
/
blockchain-explorer.html
187 lines (166 loc) · 7.48 KB
/
blockchain-explorer.html
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.2.3/milligram.min.css">
</head>
<body class="container" style="padding-top: 3%;">
<h2><a href="index.html">ethjs</a></h2>
<h4>Blockchain Explorer</h4>
<hr />
<div class="row">
<div class="column">
<label>Last Block #</label>
<p id="lastBlockNumber">Loading...</p>
</div>
<div class="column">
<label>Hash</label>
<p id="lastBlockHash">Loading...</p>
</div>
<div class="column">
<label>Transactions</label>
<p id="lastBlockTransactions">Loading...</p>
</div>
<div class="column">
<label>Created</label>
<p><span id="lastBlockCreated"></span> seconds ago</p>
</div>
<div class="column">
<label>Network</label>
<p id="currentNetwork">Ethereum Mainnet</p>
</div>
</div>
<hr />
<div class="row">
<div class="column">
<label>Block # or Hash</label>
<input type="text" id="blockSearchValue" placeholder="45028923" />
</div>
<div class="column">
<br />
<button id="searchBlock">Search Block</button>
</div>
<div class="column">
<label>Transaction Hash</label>
<input type="text" id="transactionSearchValue" placeholder="0x365b070c8cbac9..." />
</div>
<div class="column">
<br />
<button id="searchTransaction">Search Transaction</button>
</div>
</div>
<br />
<div id="blockSearch" style="display: none;">
<h3>Block Information</h3>
<div style="padding: 20px; border: 1px solid #F1F1F1;">
TimeStamp: <span id="searchBlockTimeAgo">0</span><br />
Transactions: <span id="searchBlockTransactions">0</span> transactions in this block<br />
Hash: <span id="searchBlockHash">0x</span><br />
Parent Hash: <span id="searchBlockParentHash">0x</span><br />
Mined By: <span id="searchBlockMiner">0x</span><br />
Difficulty: <span id="searchBlockDifficulty">0</span><br />
Size: <span id="searchBlockSize">0</span> bytes<br />
Gas Limit: <span id="searchBlockGasLimit">0</span><br />
Gas Used: <span id="searchBlockGasUsed">0</span><br />
Nonce: <span id="searchBlockNonce">0x</span><br />
Uncles: <span id="searchBlockUncles">0</span><br />
</div>
</div>
<div id="transactionSearch" style="display: none;">
<h3>Transaction Information</h3>
<div style="padding: 20px; border: 1px solid #F1F1F1;">
TxHash: <span id="txHash"></span><br />
From: <span id="txFrom"></span><br />
To: <span id="txTo"></span><br />
Value: <span id="txValue"></span> Ether<br />
Gas: <span id="txGas"></span><br />
Gas Price: <span id="txGasPrice"></span> Ether<br />
Gas Used By Transaction: <span id="txGasUsed"></span><br />
Actual Tx Cost/Fee: <span id="txCost"></span> Ether<br />
Cumulative Gas Used:<span id="txCumulativeGasUsed"></span><br />
Nonce: <span id="txNonce"></span><br />
</div>
</div>
<div id="response" style="padding: 20px; border: 1px solid #F1F1F1;"></div>
<script type="text/javascript" src="ethjs.js"></script>
<script type="text/javascript">
var eth = new Eth(new Eth.HttpProvider('https://mainnet.infura.io/'));
var el = function(id){ return document.querySelector(id); };
/*
// uncomment to enable MetaMask support:
if (typeof window.web3 !== 'undefined' && typeof window.web3.currentProvider !== 'undefined') {
eth.setProvider(window.web3.currentProvider);
} else {
eth.setProvider(provider); // set to TestRPC if not available
}
*/
function updateLastBlock() {
eth.getBlockByNumber('latest', false)
.then(function(block){
el('#lastBlockNumber').innerHTML = block.number.toString(10);
el('#lastBlockHash').innerHTML = block.hash.substr(0, 12) + '..';
el('#lastBlockTransactions').innerHTML = block.transactions.length;
el('#lastBlockCreated').innerHTML = new Eth.BN((new Date()).getTime() / 1000).sub(block.timestamp).toString(10);
})
.catch(function(blockError){
el('#response').innerHTML = 'Hmm.. there was an error: ' + String(blockError);
});
}
el('#searchBlock').addEventListener('click', function(){
var searchValue = el('#blockSearchValue').value;
var searchMethod = 'getBlockByNumber';
el('#response').style.display = 'block';
el('#transactionSearch').style.display = 'none';
el('#blockSearch').style.display = 'block';
el('#response').innerHTML = 'Search for block...';
if(Eth.isHexString(searchValue)) {
searchMethod = 'getBlockByHash'
}
eth[searchMethod](searchValue, false)
.then(function(searchBlock){
el('#response').style.display = 'none';
el('#searchBlockTimeAgo').innerHTML = new Date(searchBlock.timestamp.mul(new Eth.BN(1000)).toNumber(10)).toISOString();
el('#searchBlockTransactions').innerHTML = searchBlock.transactions.length;
el('#searchBlockHash').innerHTML = searchBlock.hash;
el('#searchBlockParentHash').innerHTML = searchBlock.parentHash;
el('#searchBlockMiner').innerHTML = searchBlock.miner;
el('#searchBlockDifficulty').innerHTML = searchBlock.difficulty.toString(10);
el('#searchBlockSize').innerHTML = searchBlock.size.toString(10);
el('#searchBlockGasLimit').innerHTML = searchBlock.gasLimit.toString(10);
el('#searchBlockGasUsed').innerHTML = searchBlock.gasUsed.toString(10);
el('#searchBlockNonce').innerHTML = searchBlock.nonce;
el('#searchBlockUncles').innerHTML = searchBlock.uncles.length;
})
.catch(function(blockError){
el('#response').innerHTML = 'Hmm.. there was an error: ' + String(blockError);
});
});
el('#searchTransaction').addEventListener('click', function(){
el('#response').style.display = 'block';
el('#blockSearch').style.display = 'none';
el('#response').innerHTML = 'Search for transaction...';
eth.getTransactionByHash(el('#transactionSearchValue').value)
.then(function(transaction){
eth.getTransactionReceipt(transaction.hash)
.then(function(receipt){
el('#transactionSearch').style.display = 'block';
el('#response').style.display = 'none';
el('#txHash').innerHTML = transaction.hash;
el('#txFrom').innerHTML = transaction.from;
el('#txTo').innerHTML = transaction.to;
el('#txValue').innerHTML = Eth.fromWei(transaction.value, 'ether');
el('#txGas').innerHTML = transaction.gas.toString(10);
el('#txGasPrice').innerHTML = Eth.fromWei(transaction.gasPrice, 'ether');
el('#txGasUsed').innerHTML = Eth.fromWei(receipt.gasUsed, 'ether');
el('#txCost').innerHTML = Eth.fromWei(receipt.cumulativeGasUsed, 'ether');
el('#txCumulativeGasUsed').innerHTML = receipt.cumulativeGasUsed.toString(10);
el('#txNonce').innerHTML = transaction.nonce.toString(10);
});
})
.catch(function(txError){
el('#response').innerHTML = 'Hmm.. there was an error: ' + String(txError);
});
});
setInterval(updateLastBlock, 6000);
updateLastBlock();
</script>
</body>
</html>