Last updated
syntax = "proto3";
package getInfo;
service getInfoService {
rpc getAccountBalance (account) returns (payload) {}
rpc getLastestBlock (empty) returns (payload) {}
}
message account {
string account = 1;
}
message payload {
string payload = 1;
}
message empty {}const PROTO_PATH = './protos/getInfo.proto';
const grpc = require('grpc');
const https = require('https');
const getInfo_proto = grpc.load(PROTO_PATH).getInfo;
/**
* Implements the SayHello RPC method.
*/
function getAccountBalance(call, callback) {
https.get({
host: 'blockchain.info',
path: `/balance?active=${call.request.account}`
}, function (response) {
var body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
callback(null, { payload: 'Account ' + body });
});
});
}
function getLastestBlock(call, callback) {
https.get({
host: 'blockchain.info',
path: '/latestblock'
}, function (response) {
var body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
console.log(body)
callback(null, { payload: 'Latestblock ' + body });
});
});
}
function main() {
var server = new grpc.Server();
server.addService(getInfo_proto.getInfoService.service, { getAccountBalance, getLastestBlock });
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();
}
main();var PROTO_PATH = './protos/getInfo.proto';
var grpc = require('grpc');
var getInfo_proto = grpc.load(PROTO_PATH).getInfo;
var client = new getInfo_proto.getInfoService('localhost:50051',
grpc.credentials.createInsecure());
//Read command Line
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you want to know? ', (answer) => {
switch (answer) {
case 'getAccountBalance':
getAccountBalance();
break;
case 'getLastestBlock':
getLastestBlock();
break;
default:
break;
}
rl.close();
});
function getAccountBalance() {
var account;
if (process.argv.length >= 3) {
account = process.argv[2];
} else {
account = '12Vji8DJLgPEowfcEaGqjopueTCH9EsFim'; //default sample account
}
client.getAccountBalance({ account }, function (err, response) {
console.log('Balance:', response.payload);
});
}
function getLastestBlock() {
client.getLastestBlock({}, function (err, response) {
console.log('LastestBlock:', response.payload);
});
}client.getLastestBlock({}, function(err, response) {
console.log('Balance:', response);
});