Skip to content

Commit ee96cf3

Browse files
authored
Merge pull request #96 from ConsenSys/example-improvements
Update and extend example programs:
2 parents 404d6cc + 1b0be8a commit ee96cf3

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

example/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Input parameter handling is minimal and command options are lacking so
77
we can focus on how to set up and call the client.
88

99

10-
* [mythx-analysis](https://github.com/ConsenSys/armlet/blob/master/example/mythx-analysis): Submit a JSON with Solidity source and EVM bytecode information to the MythX and retrieve results. For fancier clients see [sabre](https://github.com/b-mueller/sabre) or [mythos](https://github.com/cleanunicorn/mythos).
10+
* [mythx-analysis](https://github.com/ConsenSys/armlet/blob/master/example/mythx-analysis): Submit a JSON with Solidity source and EVM bytecode information to the MythX and retrieve results. For fancier clients see [mythos](https://github.com/cleanunicorn/mythos).
11+
* [mythx-tool-use](https://github.com/ConsenSys/armlet/blob/master/example/mythx-tool-use): Show use counts for MythX tools.
12+
* [mythx-api-version](https://github.com/ConsenSys/armlet/blob/master/example/api-version): Retrieve MythX API version information. JSON is output.
1113
* [analysis-status](https://github.com/ConsenSys/armlet/blob/master/example/analysis-status): Get status of a prior MythX analysis request.
1214
* [analysis-issues](https://github.com/ConsenSys/armlet/blob/master/example/analysis-issues): Get issues reported from a prior MythX analysis.
1315
* [list-analyses](https://github.com/ConsenSys/armlet/blob/master/example/list-analyses): Get issues reported from a prior MythX analysis.
14-
* [api-version](https://github.com/ConsenSys/armlet/blob/master/example/api-version): Retrieve MythX API version information. JSON is output.
1516
* [openapi-spec](https://github.com/ConsenSys/armlet/blob/master/example/openapi-spec): Retrieve the current MythX openapi specification. YAML is output.
16-
1717
See [folder `typescript`](https://github.com/ConsenSys/armlet/tree/master/example/typescript) for some of these examples written in [typescript](https://www.typescriptlang.org/).

example/mythx-tool-use

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
"use strict"
3+
const util = require('util');
4+
5+
function usage() {
6+
console.log(`usage: ${process.argv[1]} *tool-name1* [*tool-name2* ...]
7+
8+
Show the total MythX requests issued by each of the tools given.
9+
`)
10+
process.exit(1)
11+
}
12+
13+
/**********************************
14+
Minimal option processing
15+
***********************************/
16+
17+
const argLen = process.argv.length
18+
if (argLen === 3 &&
19+
process.argv[2].match(/^[-]{0,2}h(?:elp)?$/)) {
20+
usage()
21+
}
22+
23+
/**********************************
24+
Example code starts here ...
25+
***********************************/
26+
27+
const armlet = require('../index') // if not installed
28+
// const armlet = require('armlet') // if installed
29+
30+
const promises = armlet.mythXToolUse(process.argv.slice(2))
31+
for (const promise of promises) {
32+
promise.then(result => {
33+
console.log(result)
34+
}).catch(err => {
35+
console.log(`error: ${util.inspect(err)}`)
36+
})
37+
}

index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,49 @@ class Client {
277277
}
278278
}
279279

280+
/*
281+
Return a promise of MythX API information. For example,
282+
after the promise is resolved we get:
283+
284+
{
285+
api: 'v1.4.3-45-g3a7a71f',
286+
harvey: '0.0.13',
287+
maestro: '1.2.3-2-g2d54a08',
288+
maru: '0.4.1',
289+
mythril: '0.20.0',
290+
hash: 'd40ef2cdd30ccf134c0ae0fe2e90b247'
291+
url: 'https://api.mythx.io'
292+
}
293+
*/
280294
module.exports.ApiVersion = (inputApiUrl = defaultApiUrl) => {
281295
const url = libUtil.joinUrl(inputApiUrl, `${defaultApiVersion}/version`)
282-
return simpleRequester.do({ url, json: true })
296+
return simpleRequester.do({ url, json: true }).then(result => {
297+
result.url = inputApiUrl
298+
return result
299+
})
283300
}
284301

302+
// Return a promise for the MythX openAPI spec in YAML format.
285303
module.exports.OpenApiSpec = (inputApiUrl = defaultApiUrl) => {
286304
const url = libUtil.joinUrl(inputApiUrl, `${defaultApiVersion}/openapi.yaml`)
287305
return simpleRequester.do({ url })
288306
}
289307

308+
// Return an array of promises for the MythX tool use counts
309+
// The actual result is an object.
310+
module.exports.mythXToolUse = (toolNames, inputApiUrl = defaultApiUrl) => {
311+
let promises = []
312+
for (const toolName of toolNames) {
313+
const url = libUtil.joinUrl(inputApiUrl, `${defaultApiVersion}/client-tool-stats/${toolName}`)
314+
promises.push(simpleRequester.do({ url }).then(result => {
315+
const jsonObj = JSON.parse(result)
316+
jsonObj.name = toolName
317+
return jsonObj
318+
}))
319+
}
320+
return promises
321+
}
322+
290323
module.exports.Client = Client
291324
module.exports.defaultApiUrl = new url.URL(defaultApiUrl)
292325
module.exports.defaultApiHost = defaultApiUrl

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
22
"name": "armlet",
3-
"version": "2.2.0",
3+
"version": "2.3.0-git",
44
"description": [
55
"Armlet is a thin wrapper around the MythX API written in Javascript.",
66
"It simplifies interaction with MythX. For example, the library",
77
"wraps API analysis requests into a promise. A MythX API client.",
88
"",
9-
"A simple command-line tool, mythx-analysis, is provided to show how to",
10-
"use the API. It can be used to run MythX analyses on a single Solidity",
11-
"smart-contract text file."
9+
"Simple command-line tools, mythx-analysis, mythx-api-version, and mythx-tool-use",
10+
"are provided to show how to",
11+
"use the API. mythx-analysis can be used to run MythX analyses on a single Solidity",
12+
"smart-contract text file or the underlying JSON that is sent."
1213
],
1314
"main": "index.js",
1415
"bin": {
15-
"mythx-analysis": "./example/mythx-analysis"
16+
"mythx-analysis": "./example/mythx-analysis",
17+
"mythx-api-version": "./example/mythx-api-version",
18+
"mythx-tool-use": "./example/mythx-tool-use"
1619
},
1720
"directories": {
1821
"example": "example",

0 commit comments

Comments
 (0)