Merge pull request #10 from status-im/accounts_end_point_cherry_pick

Add blockchain account endpoint
This commit is contained in:
Iuri Matias 2018-07-31 17:45:44 -04:00 committed by GitHub
commit acab1f5040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -231,6 +231,7 @@ class Engine {
this.registerModule('blockchain_connector', {
isDev: this.isDev,
plugins: this.plugins,
web3: options.web3
});

View File

@ -35,6 +35,7 @@ class BlockchainConnector {
}
this.registerServiceCheck();
this.registerRequests();
this.registerAPIRequests();
this.registerWeb3Object();
this.registerEvents();
}
@ -167,6 +168,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;
}
@ -179,6 +226,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);
}