Add blockchain account endpoint
This commit is contained in:
parent
418d9bb12a
commit
3735fbde9c
|
@ -231,6 +231,7 @@ class Engine {
|
|||
|
||||
this.registerModule('blockchain_connector', {
|
||||
isDev: this.isDev,
|
||||
plugins: this.plugins,
|
||||
web3: options.web3
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue