diff --git a/lib/core/engine.js b/lib/core/engine.js index 4cba06fb..296586ed 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -246,6 +246,7 @@ class Engine { this.registerModule('blockchain_connector', { isDev: this.isDev, + plugins: this.plugins, web3: options.web3 }); diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 0f6c2b7b..33877e0b 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(); } @@ -179,6 +180,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; } @@ -191,6 +238,14 @@ 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); + } + getCode(address, cb) { this.web3.eth.getCode(address, cb); }