Skip to content

Commit

Permalink
Merge pull request #11 from Adamant-im/rpc-server
Browse files Browse the repository at this point in the history
Rpc server
  • Loading branch information
zyuhel authored Dec 6, 2018
2 parents 43a6b1a + df981d8 commit 2c92e8e
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 6 deletions.
17 changes: 17 additions & 0 deletions ops/client_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
*/
const package = require('../package.json')
module.exports = function (vorpal) {
return vorpal.command('client version').description('Shows adamant-console version').action(function (args, callback) {
answer = {
success: true,
version: package.version
}
this.log(JSON.stringify(answer, null, 4))
if (callback)
callback()
else
return answer
});
};
53 changes: 53 additions & 0 deletions ops/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*/
const config = require('../helpers/configReader.js')
const popsicle = require('popsicle')
var possibleTypes=['height', 'version']
module.exports=function (vorpal) {
return vorpal.command('node <type>').description('Fetch information about node. Available types: height, version').autocomplete(possibleTypes).action(function(args, callback) {
if (!possibleTypes.includes(args.type)) {
this.log('Not valid type')
if (callback)
callback()
return {success: false, error:'Not valid type'}
}
else {
var endpoint=''
var multipleSelect = false
if (args.input=='?') {
var multipleSelect = true
}
if (!multipleSelect) {
switch (args.type) {
case 'height':
endpoint = '/api/blocks/getHeight'
break
case 'version':
endpoint = '/api/peers/version'
break
default:
this.log('Not implemented yet')
if (callback)
callback()
return {success: false, error:'Not implemented yet'}
}
} else {
this.log('Not implemented yet')
if (callback)
callback()
return {success: false, error:'Not implemented yet'}
}
var self = this
return popsicle.request({
method: 'GET',
url: config.getNodeConnectString() + endpoint
}).then(function (res) {
var answer = JSON.parse(res.body)
self.log(JSON.stringify(answer,null,4))
if (callback)
callback()
return answer
})
}
})
}
69 changes: 64 additions & 5 deletions ops/rpc_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ module.exports = function (vorpal) {
} else resolve(data.account);
})
},
clientVersion: function (args) {
return new Promise(function (resolve, reject) {
var cmd = 'client version '
var err = null
var data = {success: false}
data = vorpal.execSync(cmd)
resolve(data.version)
})
},
getAddress: function (args) {
return new Promise(function (resolve, reject) {
var cmd = 'get address '
var err = null
var data = {success: false}
if (!args.length || args.length>1) {
if (!args.length || args.length > 1) {
err = this.error(-32602)
reject(err)
} else {
Expand Down Expand Up @@ -52,7 +61,7 @@ module.exports = function (vorpal) {
var cmd = 'get block '
var err = null
var data = {success: false}
if (!args.length || args.length>1) {
if (!args.length || args.length > 1) {
err = this.error(-32602)
reject(err)
} else {
Expand Down Expand Up @@ -82,7 +91,7 @@ module.exports = function (vorpal) {
var cmd = 'get delegate '
var err = null
var data = {success: false}
if (!args.length || args.length>1) {
if (!args.length || args.length > 1) {
err = this.error(-32602)
reject(err)
} else {
Expand Down Expand Up @@ -112,7 +121,7 @@ module.exports = function (vorpal) {
var cmd = 'get transaction '
var err = null
var data = {success: false}
if (!args.length || args.length>1) {
if (!args.length || args.length > 1) {
err = this.error(-32602)
reject(err)
} else {
Expand Down Expand Up @@ -141,7 +150,7 @@ module.exports = function (vorpal) {
var cmd = 'get transactions '
var err = null
var data = {success: false}
if (!args.length || args.length>1) {
if (!args.length || args.length > 1) {
err = this.error(-32602)
reject(err)
} else {
Expand Down Expand Up @@ -194,6 +203,56 @@ module.exports = function (vorpal) {
}
})
},
nodeHeight: function (args) {
return new Promise(function (resolve, reject) {
var cmd = 'node height '
var err = null
var data = {success: false}

data = vorpal.execSync(cmd)
data.then(function (data) {
if (data) {
if (data.success === false) {
err = server.error(1, data.error)
} else {
data = data.height
}
}
if (err)
reject(err)
else
resolve(data)
}).catch(function (err) {
reject(err)
})

})
},
nodeVersion: function (args) {
return new Promise(function (resolve, reject) {
var cmd = 'node version '
var err = null
var data = {success: false}

data = vorpal.execSync(cmd)
data.then(function (data) {
if (data) {
if (data.success === false) {
err = server.error(1, data.error)
} else {
data = {commit: data.commit, version: data.version}
}
}
if (err)
reject(err)
else
resolve(data)
}).catch(function (err) {
reject(err)
})

})
},
sendTokens: function (args) {
return new Promise(function (resolve, reject) {
var cmd = 'send tokens '
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant-console",
"version": "1.4.0",
"version": "1.4.1",
"description": "console tools for ADAMANT.im",
"main": "index.js",
"dependencies": {
Expand Down

0 comments on commit 2c92e8e

Please sign in to comment.