139 lines
5.3 KiB
JavaScript
139 lines
5.3 KiB
JavaScript
var express = require('express');
|
|
var router = express.Router();
|
|
var Web3 = require('web3');
|
|
|
|
/* GET home page. */
|
|
router.get('/', function(req, res, next) {
|
|
//Connexion au noeud de la blockchain pour récupérer les infos
|
|
var web3 = new Web3();
|
|
|
|
var blockNb = 1;
|
|
|
|
|
|
// On détermine un objet qui contiendra les infos à utiliser dans la vue
|
|
function Blockchain(account, blockNb) {
|
|
this.wallets = web3.eth.getAccounts();
|
|
this.nbBlocks = web3.eth.getBlockNumber();
|
|
this.montant = web3.eth.getBalance(account);
|
|
this.sync = web3.eth.isSyncing();
|
|
}
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
|
var node1 = new Blockchain("0x5421c79d465a288c28e10aa43f9b7dff1b313c8e", blockNb);
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1402"));
|
|
var node2 = new Blockchain("0xef816528949bda3b87e19b86848fb28767156232", blockNb);
|
|
web3.setProvider(new web3.providers.HttpProvider("http://5.51.59.70:1403"));
|
|
var node3 = new Blockchain("0x869abc2DadD7E23c8B38F054276813A67D8131A7");
|
|
|
|
|
|
//On fait un Promise all pour récupérer toutes les valeurs et les envoyer dans la vue... y a surement plus propre mais pour l'instant j'ai pas la bonne logique.
|
|
Promise.all([node1.wallets, node1.nbBlocks, node1.montant, node1.sync, node2.wallets, node2.nbBlocks, node2.montant, node2.sync,node3.wallets, node3.nbBlocks, node3.montant, node3.sync]).then(values=>{
|
|
res.render('index', { title: 'Moniteur de Blockchain', infos: values, block: blockNb });
|
|
});
|
|
//TODO : Trouver un moyen de récuéprer les valeurs des Promises pour les traiter et ensuite els envoyer à la vue -> permettra de lister transactions ?
|
|
//TODO : Actualiser valeurs sur la page sans refresh
|
|
})
|
|
|
|
.get('/blockslist/:page?', function(req, res, next) {
|
|
//Connexion au noeud de la blockchain pour récupérer les infos
|
|
var web3 = new Web3();
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
|
|
|
//On récupère la page donnée en URL
|
|
if (parseInt(req.params.page) > 0) {
|
|
var page = parseInt(req.params.page);
|
|
}
|
|
else {
|
|
page = 0;
|
|
}
|
|
//Nombre d'entrées ?
|
|
var max = page+100;
|
|
|
|
|
|
// On détermine un objet qui contiendra les infos à utiliser dans la vue
|
|
function Blockchain(account) {
|
|
this.amounts = [];
|
|
this.blockTransactions = [];
|
|
|
|
//Pour chauqe blocs
|
|
for (var b = page; b < max ; b++) {
|
|
//Total du montant du compte
|
|
this.amounts.push(
|
|
web3.eth.getBalance(account, b)
|
|
.then(data=>{return data;})
|
|
.catch(err=>{return err;})
|
|
);
|
|
|
|
// total des transactions
|
|
this.blockTransactions.push(
|
|
web3.eth.getBlockTransactionCount(b)
|
|
.then(data=>{return data;})
|
|
.catch(err=>{return err;})
|
|
);
|
|
}
|
|
|
|
}
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
|
var node1 = new Blockchain("0x5421c79d465a288c28e10aa43f9b7dff1b313c8e")
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1402"));
|
|
var node2 = new Blockchain("0xef816528949bda3b87e19b86848fb28767156232");
|
|
web3.setProvider(new web3.providers.HttpProvider("http://5.51.59.70:1403"));
|
|
var node3 = new Blockchain("0x869abc2DadD7E23c8B38F054276813A67D8131A7");
|
|
|
|
//On fait un Promise all pour récupérer toutes les valeurs et les envoyer dans la vue... y a surement plus propre mais pour l'instant j'ai pas la bonne logique.
|
|
Promise.all(node3.amounts)
|
|
.then(amounts=>{
|
|
Promise.all(node3.blockTransactions)
|
|
.then(blockTrans=>{
|
|
res.render('blockslist', { title: 'Liste de blocs', page: page, max: max, amounts: amounts, trans: blockTrans})
|
|
})
|
|
})
|
|
})
|
|
/****
|
|
** Page infos bloc seul *
|
|
****/
|
|
.get('/block/:numBloc', function(req, res, next) {
|
|
//Connexion au noeud de la blockchain pour récupérer les infos
|
|
var web3 = new Web3();
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
|
|
|
//On récupère le num de blocs donné en URL
|
|
if (parseInt(req.params.numBloc) > 0) {
|
|
var numBloc = parseInt(req.params.numBloc);
|
|
}
|
|
else {
|
|
numBloc = 0;
|
|
}
|
|
|
|
|
|
// On détermine un objet qui contiendra les infos à utiliser dans la vue
|
|
function Blockchain() {
|
|
this.transactions = [];
|
|
}
|
|
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1401"));
|
|
var node1 = new Blockchain()
|
|
web3.setProvider(new web3.providers.HttpProvider("http://93.30.148.59:1402"));
|
|
var node2 = new Blockchain();
|
|
web3.setProvider(new web3.providers.HttpProvider("http://5.51.59.70:1403"));
|
|
var node3 = new Blockchain();
|
|
|
|
//On fait un Promise all pour récupérer toutes les valeurs et les envoyer dans la vue... y a surement plus propre mais pour l'instant j'ai pas la bonne logique.
|
|
web3.eth.getBlockTransactionCount(numBloc)
|
|
.then(count=>{
|
|
for (var i = 0; i < count; i++) {
|
|
node3.transactions.push(web3.eth.getTransactionFromBlock(numBloc, i)
|
|
.then(data=>{return data;})
|
|
.catch(err=>{return err;})
|
|
);
|
|
}
|
|
Promise.all(node3.transactions)
|
|
.then(trans=>{
|
|
res.render('block', { title: 'Détails du bloc', num: numBloc, transactions: trans})
|
|
})
|
|
.catch(err=>{return err;})
|
|
})
|
|
.catch(err=>{return err;})
|
|
});
|
|
|
|
module.exports = router;
|