From 7df9ae66f9bb80d10d500c409585d2a8f1e27ba8 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 31 Jul 2018 15:46:19 +0100 Subject: [PATCH] Add blockchain account endpoint --- lib/core/engine.js | 1 + lib/modules/blockchain_connector/index.js | 51 +++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/core/engine.js b/lib/core/engine.js index 10bb240f8..c6c2284d3 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -245,6 +245,7 @@ class Engine { this.registerModule('blockchain_connector', { isDev: this.isDev, locale: this.locale, + plugins: this.plugins, web3: options.web3, wait: options.wait }); diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index b3ced1bf3..e23473da4 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -35,6 +35,7 @@ class BlockchainConnector { this.registerServiceCheck(); this.registerRequests(); + this.registerAPIRequests(); this.registerWeb3Object(); this.registerEvents(); } @@ -283,6 +284,52 @@ class BlockchainConnector { }); } + registerAPIRequests() { + const self = this; + + let plugin = self.plugins.createPlugin('blockchain', {}); + plugin.registerAPICall( + 'get', + '/embark/blockchain/accounts', + (req, res) => { + let results = []; + self.getAccounts((err, addresses) => { + async.eachOf(addresses, (address, index, eachCb) => { + let result = {address, index}; + results.push(result); + async.waterfall([ + function(callback) { + self.getTransactionCount(address, (err, count) => { + if (err) { + result.transactionCount = 0; + } else { + result.transactionCount = count; + } + callback(); + }); + }, + function(callback) { + self.getBalance(address, (err, balance) => { + if (err) { + result.balance = 0; + } else { + result.balance = self.web3.utils.fromWei(balance); + } + callback(); + }); + } + ], function () { + eachCb(); + }); + }, function () { + res.send(results); + }); + }); + } + ); + } + + defaultAccount() { return this.web3.eth.defaultAccount; } @@ -295,6 +342,10 @@ class BlockchainConnector { this.web3.eth.getAccounts(cb); } + getTransactionCount(address, cb) { + this.web3.eth.getTransactionCount(address, cb); + } + getBalance(address, cb) { this.web3.eth.getBalance(address, cb); }