From d13f9ce3bdeeea450c7f580c14a9c856b4d996ef Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jan 2014 12:58:45 -0300 Subject: [PATCH 1/6] add reward to block api --- app/controllers/addresses.js | 4 +++ app/models/Address.js | 48 +++++++++++++++++++++++------------- app/models/Block.js | 6 ++++- test/model/addr.js | 2 ++ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/app/controllers/addresses.js b/app/controllers/addresses.js index e13000c2..09dd17c7 100644 --- a/app/controllers/addresses.js +++ b/app/controllers/addresses.js @@ -31,6 +31,10 @@ exports.address = function(req, res, next, addr) { */ exports.show = function(req, res) { if (req.address) { + +console.log(req.address); +console.log(req.address.totalSent); +console.log(JSON.stringify(req.address)); res.jsonp(req.address); } }; diff --git a/app/models/Address.js b/app/models/Address.js index b01cc2d2..dd6da83a 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -24,25 +24,39 @@ function spec() { this.addrStr = addrStr; } catch(e){ } + + + Object.defineProperty(this, "totalSent", { + get: function() { + return parseFloat(this.totalSentSat) / parseFloat(BitcoreUtil.COIN); + }, + set: function(i) { + totalSentSat = i * BitcoreUtil.COIN; + }, + enumerable: 1, + }); + + Object.defineProperty(this, "balance", { + get: function() { + return parseFloat(this.balanceSat) / parseFloat(BitcoreUtil.COIN); + }, + set: function(i) { + balance = i * BitcoreUtil.COIN; + }, + enumerable: 1, + }); + + Object.defineProperty(this, "totalReceived", { + get: function() { + return parseFloat(this.totalReceivedSat) / parseFloat(BitcoreUtil.COIN); + }, + set: function(i) { + totalReceived = i * BitcoreUtil.COIN; + }, + enumerable: 1, + }); } - - Address.prototype.__defineGetter__('balance', function(){ - return parseFloat(this.balanceSat) / parseFloat(BitcoreUtil.COIN); - }); - - - Address.prototype.__defineGetter__('totalReceived', function(){ - return parseFloat(this.totalReceivedSat) / parseFloat(BitcoreUtil.COIN); - }); - - - Address.prototype.__defineGetter__('totalSent', function(){ - return parseFloat(this.totalSentSat) / parseFloat(BitcoreUtil.COIN); - }); - - - Address.prototype.update = function(next) { if (! this.addrStr) { diff --git a/app/models/Block.js b/app/models/Block.js index 19e6d53b..46af5907 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -5,7 +5,10 @@ */ var mongoose = require('mongoose'), Schema = mongoose.Schema, + bignum = require('bignum'), RpcClient = require('bitcore/RpcClient').class(), + util = require('bitcore/util/util'), + BitcoreBlock= require('bitcore/Block').class(), config = require('../../config/config') ; @@ -26,7 +29,6 @@ var BlockSchema = new Schema({ fromP2P: Boolean, }); - /** * Validations */ @@ -82,6 +84,8 @@ BlockSchema.methods.getInfo = function (next) { that.info = blockInfo.result; + that.info.reward = BitcoreBlock.getBlockValue(that.info.height) / util.COIN ; + //console.log("THAT", that); return next(null, that.info); }); diff --git a/test/model/addr.js b/test/model/addr.js index 1a300f1e..dfeea716 100644 --- a/test/model/addr.js +++ b/test/model/addr.js @@ -40,6 +40,8 @@ describe('Address balances', function(){ if (v.balance) assert.equal(v.balance, a.balance); if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived); if (v.totalSent) assert.equal(v.totalSent, a.totalSent); + + if (v.transactions) { v.transactions.forEach( function(tx) { From 6ebb282b900cff711fa6661e5ce92964983c0f0d Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jan 2014 13:03:29 -0300 Subject: [PATCH 2/6] add reward to coinbase txs --- app/models/Transaction.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/Transaction.js b/app/models/Transaction.js index 29b04998..57d2fc29 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -10,6 +10,7 @@ var mongoose = require('mongoose'), RpcClient = require('bitcore/RpcClient').class(), Transaction = require('bitcore/Transaction').class(), Address = require('bitcore/Address').class(), + BitcoreBlock= require('bitcore/Block').class(), networks = require('bitcore/networks'), util = require('bitcore/util/util'), bignum = require('bignum'), @@ -296,9 +297,17 @@ TransactionSchema.methods.queryInfo = function (next) { that.info.valueIn = valueIn / util.COIN; that.info.feeds = (valueIn - valueOut) / util.COIN; } + else { + var reward = BitcoreBlock.getBlockValue(that.info.height) / util.COIN; + that.info.vin[0].reward = reward; + that.info.valueIn = reward; + } + that.info.size = b.length; + + return next(err, that.info); }); }); From 843e62eb572faf65d8a8abdb4ca3e40e0fa3f6d5 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jan 2014 14:24:42 -0300 Subject: [PATCH 3/6] rm console.logs --- app/controllers/addresses.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/controllers/addresses.js b/app/controllers/addresses.js index 09dd17c7..e13000c2 100644 --- a/app/controllers/addresses.js +++ b/app/controllers/addresses.js @@ -31,10 +31,6 @@ exports.address = function(req, res, next, addr) { */ exports.show = function(req, res) { if (req.address) { - -console.log(req.address); -console.log(req.address.totalSent); -console.log(JSON.stringify(req.address)); res.jsonp(req.address); } }; From ed6f429c300de360dfd62386a9fc527ac51a2819 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Tue, 14 Jan 2014 14:46:26 -0300 Subject: [PATCH 4/6] added new service search --- app/views/includes/foot.jade | 1 + public/js/app.js | 3 ++- public/js/controllers/search.js | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 public/js/controllers/search.js diff --git a/app/views/includes/foot.jade b/app/views/includes/foot.jade index ab3e4936..52b6e0f1 100755 --- a/app/views/includes/foot.jade +++ b/app/views/includes/foot.jade @@ -37,4 +37,5 @@ script(type='text/javascript', src='/js/controllers/header.js') script(type='text/javascript', src='/js/controllers/blocks.js') script(type='text/javascript', src='/js/controllers/transactions.js') script(type='text/javascript', src='/js/controllers/address.js') +script(type='text/javascript', src='/js/controllers/search.js') script(type='text/javascript', src='/js/init.js') diff --git a/public/js/app.js b/public/js/app.js index cfc1f8cf..cea93635 100755 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,9 +1,10 @@ 'use strict'; -angular.module('mystery', ['ngCookies', 'ngResource', 'ngRoute', 'ui.bootstrap', 'ui.route', 'mystery.system', 'mystery.index', 'mystery.blocks', 'mystery.transactions', 'monospaced.qrcode', 'mystery.address']); +angular.module('mystery', ['ngCookies', 'ngResource', 'ngRoute', 'ui.bootstrap', 'ui.route', 'mystery.system', 'mystery.index', 'mystery.blocks', 'mystery.transactions', 'monospaced.qrcode', 'mystery.address', 'mystery.search']); angular.module('mystery.system', []); angular.module('mystery.index', []); angular.module('mystery.blocks', []); angular.module('mystery.transactions', []); angular.module('mystery.address', []); +angular.module('mystery.search', []); diff --git a/public/js/controllers/search.js b/public/js/controllers/search.js new file mode 100644 index 00000000..878e713a --- /dev/null +++ b/public/js/controllers/search.js @@ -0,0 +1,35 @@ +'use strict'; + +angular.module('mystery.search').controller('SearchController', ['$scope', '$routeParams', '$location', 'Global', 'Block', 'Transaction', 'Address', function ($scope, $routeParams, $location, Global, Block, Transaction, Address) { + $scope.global = Global; + + $scope.search = function() { + var q = $scope.q; + var path; + + $scope.badQuery = false; + $scope.q = ''; + + Block.get({ + blockHash: q + }, function() { + $location.path('block/' + q); + }, function () { //block not found, search on TX + Transaction.get({ + txId: q + }, function() { + $location.path('tx/' + q); + }, function () { //tx not found, search on Address + Address.get({ + addrStr: q + }, function() { + $location.path('address/' + q); + }, function () { //address not found, fail :( + $scope.badQuery = true; + $scope.q = q; + }); + }); + }); + }; + +}]); From ce1fde9aa7c6935a6b9e67483c150ba7060b8d30 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Tue, 14 Jan 2014 14:47:13 -0300 Subject: [PATCH 5/6] look&feel for search --- public/css/common.css | 1 + public/views/header.html | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/public/css/common.css b/public/css/common.css index 396601a7..73068675 100644 --- a/public/css/common.css +++ b/public/css/common.css @@ -48,3 +48,4 @@ body { font-size: 10px; } +#search { width: 400px; } diff --git a/public/views/header.html b/public/views/header.html index fb034edb..5e4f49b7 100755 --- a/public/views/header.html +++ b/public/views/header.html @@ -14,5 +14,13 @@ {{item.title}} +
+ +
From c3a6c0387cfb15e019bd51f116a22591d8467f4e Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Tue, 14 Jan 2014 14:48:25 -0300 Subject: [PATCH 6/6] added a interceptor in ther services to handle the server request error --- public/js/services/address.js | 14 ++++++++++++++ public/js/services/blocks.js | 14 ++++++++++++++ public/js/services/transactions.js | 14 ++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/public/js/services/address.js b/public/js/services/address.js index 820f5370..1151fb78 100644 --- a/public/js/services/address.js +++ b/public/js/services/address.js @@ -3,6 +3,20 @@ angular.module('mystery.address').factory('Address', ['$resource', function($resource) { return $resource('/api/addr/:addrStr', { addrStr: '@addStr' + }, { + get: { + method: 'GET', + interceptor: { + response: function (res) { + return res.data; + }, + responseError: function (res) { + if (res.status === 404) { + return res; + } + } + } + } }); }]); diff --git a/public/js/services/blocks.js b/public/js/services/blocks.js index 7747ff32..6bdd578e 100644 --- a/public/js/services/blocks.js +++ b/public/js/services/blocks.js @@ -3,6 +3,20 @@ angular.module('mystery.blocks').factory('Block', ['$resource', function($resource) { return $resource('/api/block/:blockHash', { blockHash: '@blockHash' + }, { + get: { + method: 'GET', + interceptor: { + response: function (res) { + return res.data; + }, + responseError: function (res) { + if (res.status === 404) { + return res; + } + } + } + } }); }]); diff --git a/public/js/services/transactions.js b/public/js/services/transactions.js index 9edfacaf..bc7ee29d 100644 --- a/public/js/services/transactions.js +++ b/public/js/services/transactions.js @@ -3,6 +3,20 @@ angular.module('mystery.transactions').factory('Transaction', ['$resource', function($resource) { return $resource('/api/tx/:txId', { txId: '@txId' + }, { + get: { + method: 'GET', + interceptor: { + response: function (res) { + return res.data; + }, + responseError: function (res) { + if (res.status === 404) { + return res; + } + } + } + } }); }]);