Skip to content

Commit

Permalink
Reuse already defined filters throughout all the controllers
Browse files Browse the repository at this point in the history
Reuse the already defined filter module in all controllers, defined
timestamp format filter. The aim is to minimize the repeated code #2
  • Loading branch information
herrBez committed Jan 14, 2018
1 parent 91ac624 commit a3fe39c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
11 changes: 5 additions & 6 deletions app/scripts/controllers/blockInfosController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ angular.module('ethExplorer')
$scope.gasLimit = result.gasLimit;
$scope.gasUsed = result.gasUsed;
$scope.nonce = result.nonce;
var diff = ("" + result.difficulty).replace(/['"]+/g, '') / 1000000000000;
$scope.difficulty = diff.toFixed(3) + " T";
$scope.gasLimit = new BigNumber(result.gasLimit).toFormat(0) + " m/s"; // that's a string
$scope.gasUsed = new BigNumber(result.gasUsed).toFormat(0) + " m/s";
$scope.difficulty = ("" + result.difficulty).replace(/['"]+/g, '');
$scope.gasLimit = result.gasLimit;
$scope.gasUsed = result.gasUsed;
$scope.nonce = result.nonce;
$scope.number = result.number;
$scope.parentHash = result.parentHash;
$scope.uncledata = result.sha3Uncles;
$scope.rootHash = result.stateRoot;
$scope.blockNumber = result.number;
$scope.timestamp = new Date(result.timestamp * 1000).toUTCString();
$scope.timestamp = result.timestamp;
$scope.extraData = result.extraData.slice(2);
$scope.dataFromHex = hex2a(result.extraData.slice(2));
$scope.size = result.size;
Expand Down Expand Up @@ -132,4 +131,4 @@ angular.module('ethExplorer')

return str;
}
});
});
10 changes: 4 additions & 6 deletions app/scripts/controllers/chainInfosController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ angular.module('ethExplorer')

// TODO: put the 2 web3.eth.getBlock into the async function below
// easiest to first do with fastInfosCtrl
var blockNewest = web3.eth.getBlock($scope.blockNum);
const blockNewest = web3.eth.getBlock($scope.blockNum);
const block1 = web3.eth.getBlock(1);
if (blockNewest !== undefined) {

Expand All @@ -37,12 +37,10 @@ angular.module('ethExplorer')
$scope.totalDifficulty_formatted = $scope.totalDifficulty.toFormat(0);

// Gas Limit
$scope.gasLimit = new BigNumber(blockNewest.gasLimit).toFormat(0) + " m/s";
$scope.gasLimit = blockNewest.gasLimit;

// Time
var newDate = new Date();
newDate.setTime(blockNewest.timestamp * 1000);
$scope.time = newDate.toUTCString();
$scope.time = blockNewest.timestamp;

$scope.secondsSinceBlock1 = blockNewest.timestamp - block1.timestamp;
$scope.daysSinceBlock1 = ($scope.secondsSinceBlock1 / 86400).toFixed(2);
Expand Down Expand Up @@ -118,4 +116,4 @@ angular.module('ethExplorer')
$scope.init();
console.log($scope.result);

});
});
19 changes: 13 additions & 6 deletions app/scripts/controllers/mainController.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ angular.module('ethExplorer')
function updateStats() {
$scope.blockNum = web3.eth.blockNumber; // now that was easy

if ($scope.blockNum !== undefined) {
if ($scope.blockNum !== undefined && $scope.blockNum >= 1) {
const block1 = web3.eth.getBlock(1);
// TODO: put the 2 web3.eth.getBlock into the async function below
// easiest to first do with fastInfosCtrl
Expand All @@ -98,12 +98,10 @@ angular.module('ethExplorer')
$scope.totalDifficulty_formatted = $scope.totalDifficulty.toFormat(0);

// Gas Limit
$scope.gasLimit = new BigNumber(blockNewest.gasLimit).toFormat(0) + " m/s";
$scope.gasLimit = blockNewest.gasLimit;

// Time
var newDate = new Date();
newDate.setTime(blockNewest.timestamp * 1000);
$scope.time = newDate.toUTCString();
$scope.time = blockNewest.timestamp;

$scope.secondsSinceBlock1 = blockNewest.timestamp - block1.timestamp;
$scope.daysSinceBlock1 = ($scope.secondsSinceBlock1 / 86400).toFixed(2);
Expand Down Expand Up @@ -171,7 +169,7 @@ angular.module('ethExplorer')
* https://github.com/badmofo/ethereum-mining-calculator/tree/90f4e5aae595a6877f1a26b43c15a7db35149a22
*/
function getHashrate() {
if ($scope.blockNum !== undefined) {
if ($scope.blockNum !== undefined && $scope.blockNum >= 0) {
const blockNewest = web3.eth.getBlock($scope.blockNum);
const blockBefore = web3.eth.getBlock($scope.blockNum - 1);
const blockTime = blockNewest.timestamp - blockBefore.timestamp;
Expand Down Expand Up @@ -319,4 +317,13 @@ filter('sizeFormat', function() {
];
return transformer(FORMAT, size);
};
}).
filter('timestampFormat', function() {
return function(timestamp) {
if (isNaN(timestamp)) return timestamp;
var newDate = new Date();
newDate.setTime(timestamp * 1000);
return newDate.toUTCString();

};
});
10 changes: 5 additions & 5 deletions app/views/blockInfos.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div id="block" class="blockdiv">
<div style="padding-bottom:30px;width:100%;text-align:right;clear:both">
<button class="btn btn-primary">{{conf}}</button>
<button class="btn btn-success cb"><span>{{gasUsed}} Gas Used</span></button>
<button class="btn btn-success cb"><span>{{ gasUsed | gasFormat }} Gas Used</span></button>
<a class="btn btn-primary btn-sm" ng-class="{disabled:firstBlock}" href="./#/block/{{blockNumber-1}}">&lt; Prev</a>
<a class="btn btn-primary btn-sm" ng-class="{disabled: lastBlock}" href="./#/block/{{blockNumber+1}}">Next &gt;</a>
</div>
Expand All @@ -26,7 +26,7 @@ <h2 style="margin-top: 30px;">
<tr>
<td>Received Time</td>
<td>
{{timestamp}}
{{timestamp | timestampFormat}}
</td>
</tr>
<tr>
Expand All @@ -36,7 +36,7 @@ <h2 style="margin-top: 30px;">
<tr>
<td>Difficulty</td>
<td>
{{difficulty}}
{{difficulty | diffFormat}}
</td>
</tr>
<tr>
Expand All @@ -53,11 +53,11 @@ <h2 style="margin-top: 30px;">
</tr>
<tr>
<td>Gas Limit</td>
<td>{{gasLimit}}</td>
<td>{{gasLimit | gasFormat}}</td>
</tr>
<tr>
<td>Gas Used</td>
<td>{{gasUsed}}</td>
<td>{{gasUsed | gasFormat}}</td>
</tr>
<tr>
<td>Uncle Hash</td>
Expand Down
8 changes: 4 additions & 4 deletions app/views/chainInfos.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ <h1><i class="fa fa-server"></i> Ethereum Information
</tr>
<tr>
<td>Current Difficulty</td>
<td>{{ difficulty_formatted }}</td>
<td>{{ difficulty_formatted }} ({{ difficulty | diffFormat }})</td>
</tr>
<tr>
<td>Total Difficulty</td>
<td>{{ totalDifficulty_formatted }}</td>
<td>{{ totalDifficulty_formatted}} ( &#126; {{ totalDifficulty | diffFormat}})</td>
</tr>
<tr>
<td>Gas Limit</td>
<td>{{ gasLimit }}</td>
<td>{{ gasLimit | gasFormat }}</td>
</tr>
<tr>
<td>Time</td>
<td>{{ time }}</td>
<td>{{ time | timestampFormat }}</td>
</tr>
<tr>
<td>Connected to ETH Network</td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="row">
<div class="col-md-2" align="center"><div class="boxi">Current Block:<br />{{ blockNumber }}</div></div>
<div class="col-md-2" align="center"><div class="boxi">ETH/USD Price:<br />{{ ethprice || "Loading" }}</div></div>
<div class="col-md-2" align="center"><div class="boxi">Gas Limit:<br />{{ gasLimit }}</div></div>
<div class="col-md-2" align="center"><div class="boxi">Gas Limit:<br />{{ gasLimit | gasFormat }}</div></div>
<div class="col-md-2" align="center"><div class="boxi">Block Time:<br />{{ blocktime }} second(s)</div></div>
<div class="col-md-2" align="center"><div class="boxi">Current Diff:<br />{{ difficulty | diffFormat }}</div></div>
<div class="col-md-2" align="center"><div class="boxi">Hashrate:<br />{{ hashrate | hashFormat }}</div></div>
Expand Down

0 comments on commit a3fe39c

Please sign in to comment.