added currency switch with cache

This commit is contained in:
Mario Colque 2014-02-05 15:59:44 -03:00
parent 33b9ad4084
commit 78d4c99337
11 changed files with 128 additions and 39 deletions

View File

@ -1,11 +1,16 @@
'use strict';
var config = require('../../config/config');
// Set the initial vars
var timestamp = +new Date(),
delay = config.currencyRefresh * 60000,
bitstampRate = 0;
exports.index = function(req, res) {
var _xhr = function() {
if (typeof ActiveXObject !== 'undefined' && ActiveXObject !== null) {
return new ActiveXObject('Microsoft.XMLHTTP');
} else if (typeof XMLHttpRequest !== 'undefined' && XMLHttpRequest !== null) {
if (typeof XMLHttpRequest !== 'undefined' && XMLHttpRequest !== null) {
return new XMLHttpRequest();
} else if (typeof require !== 'undefined' && require !== null) {
var XMLhttprequest = require('xmlhttprequest').XMLHttpRequest;
@ -21,33 +26,46 @@ exports.index = function(req, res) {
if (request.readyState === 4) {
if (request.status === 200) {
return cb(false, request.responseText);
} else {
return cb(true, {
status: request.status,
message: 'Request error'
});
}
return cb(true, {
status: request.status,
message: 'Request error'
});
}
};
return request.send(null);
};
_request('https://www.bitstamp.net/api/ticker/', function(err, data) {
if (err) {
return res.jsonp({
status: err.status,
message: err.message
// Init
var currentTime = +new Date();
console.log('-----------------------------------');
console.log(timestamp);
console.log(currentTime);
console.log(currentTime >= (timestamp + delay));
console.log('-----------------------------------');
if (bitstampRate === 0 || currentTime >= (timestamp + delay)) {
timestamp = currentTime;
_request('https://www.bitstamp.net/api/ticker/', function(err, data) {
if (!err) bitstampRate = parseFloat(JSON.parse(data).last);
res.jsonp({
status: 200,
data: {
bitstamp: bitstampRate,
delay: delay
}
});
}
var bitstamp = JSON.parse(data);
});
} else {
res.jsonp({
status: 200,
data: {
bitstamp: parseFloat(bitstamp.last)
bitstamp: bitstampRate,
delay: delay
}
});
});
}
};

View File

@ -30,6 +30,7 @@ script(type='text/javascript', src='/js/services/address.js')
script(type='text/javascript', src='/js/services/transactions.js')
script(type='text/javascript', src='/js/services/blocks.js')
script(type='text/javascript', src='/js/services/socket.js')
script(type='text/javascript', src='/js/services/currency.js')
//Application Controllers
script(type='text/javascript', src='/js/controllers/index.js')

View File

@ -39,4 +39,7 @@ module.exports = {
network: process.env.INSIGHT_NETWORK || 'testnet',
disableP2pSync: false,
disableHistoricSync: false,
// Time to refresh the currency rate. In minutes
currencyRefresh: 10
};

View File

@ -21,6 +21,9 @@ module.exports = function(app, historicSync) {
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');
// Compress JSON outputs
app.set('json spaces', 0);
//Enable jsonp
app.enable('jsonp callback');

View File

@ -14,7 +14,8 @@ angular.module('insight',[
'insight.address',
'insight.search',
'insight.status',
'insight.connection'
'insight.connection',
'insight.currency'
]);
angular.module('insight.system', []);
@ -25,3 +26,4 @@ angular.module('insight.address', []);
angular.module('insight.search', []);
angular.module('insight.status', []);
angular.module('insight.connection', []);
angular.module('insight.currency', []);

View File

@ -1,15 +1,62 @@
'use strict';
angular.module('insight.system').controller('FooterController',
function($scope, Version) {
function($rootScope, $scope, Version, Currency) {
var _getVersion = function() {
Version.get({},
function(res) {
$scope.version = res.version;
});
};
var _roundFloat = function(x, n) {
if(!parseInt(n, 10)) n = 0;
$scope.version = _getVersion();
if(!parseFloat(x)) return false;
return Math.round(x * Math.pow(10, n)) / Math.pow(10, n);
};
$rootScope.currency = {
factor: 1,
symbol: 'BTC',
bitstamp: 0,
getConversion: function(value) {
if (value !== 'undefined' && value !== null) {
var response;
if (this.symbol === 'USD') {
response = _roundFloat((value * this.factor), 2);
} else if (this.symbol === 'mBTC') {
this.factor = 1000;
response = _roundFloat((value * this.factor), 5);
} else {
this.factor = 1;
response = value;
}
return response + ' ' + this.symbol;
}
return null;
}
};
$scope.setCurrency = function(currency) {
if (currency === 'USD') {
Currency.get({}, function(res) {
$rootScope.currency.factor = res.data.bitstamp;
});
} else if (currency === 'mBTC') {
$rootScope.currency.factor = 1000;
} else {
$rootScope.currency.factor = 1;
}
$rootScope.currency.symbol = currency;
};
var _getVersion = function() {
Version.get({},
function(res) {
$scope.version = res.version;
});
};
$scope.version = _getVersion();
});

View File

@ -1,9 +1,11 @@
'use strict';
angular.module('insight.system').controller('HeaderController',
function($scope, $rootScope, getSocket, Global, Block) {
function($scope, $rootScope, getSocket, Global, Block, Currency) {
$scope.global = Global;
Currency.get();
$scope.menu = [
{
'title': 'Blocks',

View File

@ -3,7 +3,7 @@
var ZeroClipboard = window.ZeroClipboard;
angular.module('insight')
.directive('whenScrolled', ['$window', function($window) {
.directive('whenScrolled', function($window) {
return {
restric: 'A',
link: function(scope, elm, attr) {
@ -21,13 +21,14 @@ angular.module('insight')
};
$window.on('scroll', handler);
scope.$on('$destroy', function() {
return $window.off('scroll', handler);
});
}
};
}])
.directive('clipCopy', [function() {
})
.directive('clipCopy', function() {
ZeroClipboard.config({
moviePath: '/lib/zeroclipboard/ZeroClipboard.swf',
trustedDomains: ['*'],
@ -54,4 +55,4 @@ angular.module('insight')
});
}
};
}]);
});

View File

@ -0,0 +1,6 @@
'use strict';
angular.module('insight.currency').factory('Currency',
function($resource) {
return $resource('/api/currency');
});

View File

@ -1,3 +1,9 @@
<div class="container" data-ng-controller="FooterController">
<div class="pull-left m10v">
<small>Currency:</small>
<a href="#" data-ng-click="setCurrency('USD')">USD</a> -
<a href="#" data-ng-click="setCurrency('BTC')">BTC</a> -
<a href="#" data-ng-click="setCurrency('mBTC')">mBTC</a>
</div>
<a class="insight m10v pull-right" href="/">Insight <small>API v{{version}}</small></a>
</div>

View File

@ -8,7 +8,7 @@
<div class="col-md-5">
<div class="row" data-ng-show="tx.isCoinBase">
<div class="col-md-12 transaction-vin-vout" data-ng-repeat="vin in tx.vin">
<div class="text-muted pull-right btc-value"><small>{{vin.reward}} BTC</small></div>
<div class="text-muted pull-right btc-value"><small>{{$root.currency.getConversion(vin.reward)}}</small></div>
<div class="ellipsis">
<span>No Inputs (Newly Generated Coins)</span>
</div>
@ -17,7 +17,7 @@
<div class="row" data-ng-show="!tx.isCoinBase">
<div data-ng-repeat="vin in tx.vinSimple" data-ng-show="!itemsExpanded">
<div class="col-md-12 transaction-vin-vout">
<div class="text-muted pull-right btc-value" data-ng-class="{'text-danger': $root.currentAddr == vin.addr}"><small>{{vin.value}} BTC</small></div>
<div class="text-muted pull-right btc-value" data-ng-class="{'text-danger': $root.currentAddr == vin.addr}"><small>{{$root.currency.getConversion(vin.value)}}</small></div>
<div class="ellipsis">
<span data-ng-show="vin.notAddr">{{vin.addr}}</span>
<span class="text-muted" title="Current Bitcoin Address" data-ng-show="vin.addr == $root.currentAddr">{{vin.addr}}</span>
@ -27,7 +27,7 @@
</div>
<div data-ng-repeat="vin in tx.vin" data-ng-show="itemsExpanded">
<div class="col-md-12 transaction-vin-vout">
<div class="text-muted pull-right btc-value"><small>{{vin.value}} BTC</small></div>
<div class="text-muted pull-right btc-value"><small>{{$root.currency.getConversion(vin.value)}}</small></div>
<div class="ellipsis">
<a class="glyphicon glyphicon-chevron-right" href="/#!/tx/{{vin.txid}}" title="Outpoint: {{vin.txid}},{{vin.vout}}"></a>&nbsp;&nbsp;
<span data-ng-show="vin.notAddr">{{vin.addr}}</span>
@ -57,7 +57,7 @@
<div class="row">
<div data-ng-repeat="vout in tx.voutSimple" data-ng-show="!itemsExpanded">
<div class="col-md-12 transaction-vin-vout">
<div class="text-muted pull-right btc-value" data-ng-class="{'text-success': $root.currentAddr == vout.addr}"><small>{{vout.value}} BTC</small></div>
<div class="text-muted pull-right btc-value" data-ng-class="{'text-success': $root.currentAddr == vout.addr}"><small>{{$root.currency.getConversion(vout.value)}}</small></div>
<div class="ellipsis">
<span data-ng-show="vout.notAddr">{{vout.addr}}</span>
<span class="text-muted" title="Current Bitcoin Address" data-ng-show="address == $root.currentAddr" data-ng-repeat="address in vout.addr.split(',')">{{vout.addr}}</span>
@ -67,7 +67,7 @@
</div>
<div data-ng-repeat="vout in tx.vout" data-ng-show="itemsExpanded">
<div class="col-md-12 transaction-vin-vout">
<div class="text-muted pull-right btc-value"><small>{{vout.value}} BTC</small></div>
<div class="text-muted pull-right btc-value"><small>{{$root.currency.getConversion(vout.value)}}</small></div>
<div class="ellipsis">
<a href="/address/{{address}}" data-ng-repeat="address in vout.scriptPubKey.addresses">{{address}}</a>
</div>
@ -97,6 +97,6 @@
<div class="pull-right">
<button data-ng-show="tx.confirmations" class="btn btn-success">{{tx.confirmations}} Confirmations</button>
<button data-ng-show="!tx.confirmations" class="btn btn-danger">Unconfirmed Transaction!</button>
<button class="btn btn-primary">{{tx.valueOut}} BTC</button>
<button class="btn btn-primary">{{$root.currency.getConversion(tx.valueOut)}}</button>
</div>
</div>