From 521a0d6a44d29ca9a5a1bbe70e20ed700d4e8cec Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Tue, 14 Jan 2014 20:34:01 -0300 Subject: [PATCH 1/5] front-end: Completed block info. Getting aync transaction in BlockPage and AddrPage --- app/controllers/transactions.js | 46 ++++++++++++++++++++++++ config/routes.js | 9 ++++- public/js/controllers/address.js | 17 +-------- public/js/controllers/blocks.js | 1 + public/js/controllers/transactions.js | 20 ++++++++++- public/js/services/transactions.js | 12 +++++++ public/views/address.html | 41 ++++++++++----------- public/views/block.html | 51 ++++++++++++--------------- public/views/transaction.html | 6 ++-- 9 files changed, 134 insertions(+), 69 deletions(-) diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index caa66f1..7d32e83 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -2,6 +2,9 @@ var Transaction = require('../models/Transaction'); +var Block = require('../models/Block'); +var Address = require('../models/Address'); +var async = require('async'); //, _ = require('lodash'); @@ -38,3 +41,46 @@ exports.show = function(req, res) { } }; +var getTransaction = function(txid, cb) { + Transaction.fromIdWithInfo(txid, function(err, tx) { + if (err) { + console.log(err); + return cb(err); + } + return cb(null, tx.info); + }); +}; + +exports.getTransactionsByBlock = function(req, res, next, bId) { + Block.fromHashWithInfo(bId, function(err, block) { + if (err && !block) { + console.log(err); + res.status(404).send('Not found'); + return next(); + } + + async.mapSeries(block.info.tx, getTransaction, + function(err, results) { + res.jsonp(results); + }); + }); +}; + +exports.getTransactionsByAddress = function(req, res, next, aId) { + + var a = Address.new(aId); + + a.update(function(err) { + if (err && !a.totalReceivedSat) { + console.log(err); + res.status(404).send('Invalid address'); + return next(); + } + + async.mapSeries(a.transactions, getTransaction, + function(err, results) { + res.jsonp(results); + }); + }); +}; + diff --git a/config/routes.js b/config/routes.js index 45bc4ad..7ced7c7 100644 --- a/config/routes.js +++ b/config/routes.js @@ -14,11 +14,18 @@ module.exports = function(app) { app.get('/api/block/:blockHash', blocks.show); app.param('blockHash', blocks.block); + // Transaction routes var transactions = require('../app/controllers/transactions'); app.get('/api/tx/:txid', transactions.show); - app.param('txid', transactions.transaction); + app.get('/api/txb/:bId', transactions.getTransactionsByBlock); + app.param('bId', transactions.getTransactionsByBlock); + + app.get('/api/txa/:aId', transactions.getTransactionsByAddress); + app.param('aId', transactions.getTransactionsByAddress); + + var addresses = require('../app/controllers/addresses'); app.get('/api/addr/:addr', addresses.show); app.param('addr', addresses.address); diff --git a/public/js/controllers/address.js b/public/js/controllers/address.js index 7c859db..bc894a3 100644 --- a/public/js/controllers/address.js +++ b/public/js/controllers/address.js @@ -1,22 +1,6 @@ 'use strict'; angular.module('mystery.address').controller('AddressController', ['$scope', '$routeParams', '$location', 'Global', 'Address', function ($scope, $routeParams, $location, Global, Address) { -/* - $scope.address = '1JmTTDcksW7A6GN7JnxuXkMAXsVN9zmgm1'; - $scope.hash160 = '77ad7d08aaa9cf489ea4e468eaeb892b85f71e27'; - $scope.transactions = [ - { - hash: '49a1d01759690476dbeec4a8efd969c09c6d4269ea2d88f4d9d4f098f021413c', - time: 1234123445, - amount: 0.3 - }, - { - hash: 'cce948b422a4d485900fb82e64458720eb89f545af3f07ddf7d18660f9f881e9', - time: 1234123445, - amount: 0.1 - } - ]; -*/ $scope.global = Global; $scope.findOne = function() { @@ -27,4 +11,5 @@ angular.module('mystery.address').controller('AddressController', ['$scope', '$r }); }; + $scope.params = $routeParams; }]); diff --git a/public/js/controllers/blocks.js b/public/js/controllers/blocks.js index d9dd958..d9277eb 100644 --- a/public/js/controllers/blocks.js +++ b/public/js/controllers/blocks.js @@ -20,4 +20,5 @@ angular.module('mystery.blocks').controller('BlocksController', ['$scope', '$rou }); }; + $scope.params = $routeParams; }]); diff --git a/public/js/controllers/transactions.js b/public/js/controllers/transactions.js index 53aaf92..ca1ae7e 100644 --- a/public/js/controllers/transactions.js +++ b/public/js/controllers/transactions.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('mystery.transactions').controller('transactionsController', ['$scope', '$routeParams', '$location', 'Global', 'Transaction', function ($scope, $routeParams, $location, Global, Transaction) { +angular.module('mystery.transactions').controller('transactionsController', ['$scope', '$routeParams', '$location', 'Global', 'Transaction', 'TransactionsByBlock', 'TransactionsByAddress', function ($scope, $routeParams, $location, Global, Transaction, TransactionsByBlock, TransactionsByAddress) { $scope.global = Global; $scope.findOne = function() { @@ -10,5 +10,23 @@ angular.module('mystery.transactions').controller('transactionsController', ['$s $scope.tx = tx; }); }; + + $scope.byBlock = function(bId) { + TransactionsByBlock.query({ + bId: bId + }, function(txs) { + $scope.txs = txs; + }); + }; + + $scope.byAddress = function(aId) { + TransactionsByAddress.query({ + aId: aId + }, function(txs) { + $scope.txs = txs; + }); + }; + + }]); diff --git a/public/js/services/transactions.js b/public/js/services/transactions.js index 9edfaca..a449f52 100644 --- a/public/js/services/transactions.js +++ b/public/js/services/transactions.js @@ -6,3 +6,15 @@ angular.module('mystery.transactions').factory('Transaction', ['$resource', func }); }]); +angular.module('mystery.transactions').factory('TransactionsByBlock', ['$resource', function($resource) { + return $resource('/api/txb/:bId', { + bId: '@bId' + }); +}]); + +angular.module('mystery.transactions').factory('TransactionsByAddress', ['$resource', function($resource) { + return $resource('/api/txa/:aId', { + aId: '@aId' + }); +}]); + diff --git a/public/views/address.html b/public/views/address.html index f6bb95b..028eec7 100644 --- a/public/views/address.html +++ b/public/views/address.html @@ -5,12 +5,13 @@ Addresses are identifiers which you use to send bitcoins to another person. -
- - +
+
+
+ - + @@ -29,32 +30,32 @@ - -
Address{{address.addrStr}}{{address.addrStr}}
Total Received{{address.txApperances}}
+ + +
+
+ +
-
- -
-
-

- Transactions - transactions this address relates to -

+
+

Transactions Transactions contained within this block

- + + - - - - - + + + + + +
Transaction Hash DatetimeTransacted amountFeeTransacted Amount
{{transaction}}----
{{tx.txid}}{{tx.time * 1000 | date:'medium'}}{{tx.feeds}}{{tx.valueOut}}
diff --git a/public/views/block.html b/public/views/block.html index 68fb1dd..d44a57d 100644 --- a/public/views/block.html +++ b/public/views/block.html @@ -16,18 +16,6 @@ Number Of Transactions {{block.tx.length}} - - Output Total - -- - - - Estimated Transaction Volume - -- - - - Transaction Fees - -- - Height {{block.height}} @@ -36,10 +24,6 @@ Timestamp {{block.time * 1000 | date:'medium'}} - - Relayed By - -- - Difficulty {{block.difficulty}} @@ -96,17 +80,28 @@
-

Transactions Transactions contained within this block

- - - - +
+

Transactions Transactions contained within this block

+
+ Loading... +
+
Hash
+ + + + + + + + + + + + + + - - - - - - -
Transaction HashDatetimeFeeTransacted Amount
{{tx.txid}}{{tx.time * 1000 | date:'medium'}}{{tx.feeds}}{{tx.valueOut}}
{{tx}}
+ + + diff --git a/public/views/transaction.html b/public/views/transaction.html index cadce85..313411f 100644 --- a/public/views/transaction.html +++ b/public/views/transaction.html @@ -23,7 +23,7 @@