-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathblockInfosController.js
More file actions
125 lines (101 loc) · 4.22 KB
/
Copy pathblockInfosController.js
File metadata and controls
125 lines (101 loc) · 4.22 KB
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
angular.module('ethExplorer')
.controller('blockInfosCtrl', function ($rootScope, $scope, $location, $routeParams,$q) {
var web3 = $rootScope.web3;
$scope.init = function()
{
$scope.blockId = $routeParams.blockId;
if($scope.blockId!==undefined) {
getBlockInfos()
.then(function(result){
var number = web3.eth.blockNumber;
$scope.result = result;
if(result.hash!==undefined){
$scope.hash = result.hash;
}
else{
$scope.hash ='pending';
}
if(result.miner!==undefined){
$scope.miner = result.miner;
}
else{
$scope.miner ='pending';
}
$scope.gasLimit = result.gasLimit;
$scope.gasUsed = result.gasUsed;
$scope.nonce = result.nonce;
$scope.difficulty = ("" + result.difficulty).replace(/['"]+/g, '');
$scope.gasLimit = result.gasLimit; // that's a string
$scope.nonce = result.nonce;
$scope.number = result.number;
$scope.parentHash = result.parentHash;
$scope.blockNumber = result.number;
$scope.timestamp = result.timestamp;
$scope.extraData = result.extraData;
$scope.dataFromHex = hex2a(result.extraData);
$scope.size = result.size;
if($scope.blockNumber!==undefined){
$scope.conf = number - $scope.blockNumber + " Confirmations";
if($scope.conf===0 + " Confirmations"){
$scope.conf='Unconfirmed';
}
}
if($scope.blockNumber!==undefined){
var info = web3.eth.getBlock($scope.blockNumber);
if(info!==undefined){
var newDate = new Date();
newDate.setTime(info.timestamp*1000);
$scope.time = newDate.toUTCString();
}
}
});
} else {
$location.path("/");
}
function getBlockInfos() {
var deferred = $q.defer();
web3.eth.getBlock($scope.blockId,function(error, result) {
if(!error) {
deferred.resolve(result);
} else {
deferred.reject(error);
}
});
return deferred.promise;
}
};
$scope.init();
// parse transactions
$scope.transactions = []
web3.eth.getBlockTransactionCount($scope.blockId, function(error, result){
var txCount = result
for (var blockIdx = 0; blockIdx < txCount; blockIdx++) {
web3.eth.getTransactionFromBlock($scope.blockId, blockIdx, function(error, result) {
var transaction = {
id: result.hash,
hash: result.hash,
from: result.from,
to: result.to,
gas: result.gas,
input: result.input,
value: result.value
}
web3.eth.getTransactionReceipt(result.hash, function (err2, receipt) {
if(!err2) {
for (var attrname in receipt) { transaction[attrname] = receipt[attrname]; }
}
$scope.$apply(
$scope.transactions.push(transaction)
);
});
});
}
});
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
});