Skip to content

Commit

Permalink
Added Solidity as an output option. -f sol
Browse files Browse the repository at this point in the history
Bumped version to 1.1.28
  • Loading branch information
naddison36 committed Aug 5, 2021
1 parent 2701f69 commit 50f1295
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ If an Ethereum address with a 0x prefix is passed, the verified source code from
Options:
-b, --baseContractNames <value> only output contracts connected to these comma separated base contract names
-f, --outputFormat <value> output file format: svg, png, dot or all (default: "svg")
-f, --outputFormat <value> output file format: svg, png, sol, dot or all (default: "svg")
-o, --outputFileName <value> output file name
-d, --depthLimit <depth> number of sub folders that will be recursively searched for Solidity files. Default -1 is unlimited (default: -1)
-i, --ignoreFilesOrFolders <filesOrFolders> comma separated list of files or folders to ignore
Expand Down
1 change: 1 addition & 0 deletions lib/converter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export declare const convertUmlClassesToSvg: (umlClasses: UmlClass[], clusterFol
export declare function convertUmlClasses2Dot(umlClasses: UmlClass[], clusterFolders?: boolean, classOptions?: ClassOptions): string;
export declare function addAssociationsToDot(umlClasses: UmlClass[], classOptions?: ClassOptions): string;
export declare function convertDot2Svg(dot: string): any;
export declare function writeSolidity(code: string, filename?: string): void;
export declare function writeDot(dot: string, dotFilename?: string): void;
export declare function writeSVG(svg: any, svgFilename?: string, outputFormats?: OutputFormats): Promise<void>;
export declare function writePng(svg: any, filename: string): Promise<void>;
15 changes: 14 additions & 1 deletion lib/converter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/etherscanParser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export declare class EtherscanParser {
* @return Promise with an array of UmlClass objects
*/
getUmlClasses(contractAddress: string): Promise<UmlClass[]>;
/**
* Get Solidity code from Etherscan for a contract and merges all files
* into one long string of Solidity code.
* @param contractAddress Ethereum contract address with a 0x prefix
* @return Promise string of Solidity code
*/
getSolidityCode(contractAddress: string): Promise<string>;
/**
* Parses Solidity source code into an ASTNode object
* @param sourceCode Solidity source code
Expand Down
14 changes: 14 additions & 0 deletions lib/etherscanParser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion lib/sol2uml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sol2uml",
"version": "1.1.27",
"version": "1.1.28",
"description": "Unified Modeling Language (UML) class diagram generator for Solidity contracts",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
16 changes: 16 additions & 0 deletions src/ts/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ export function convertDot2Svg(dot: string): any {
}
}

export function writeSolidity(code: string, filename = 'solidity') {
const outputFile = filename + '.sol'
debug(`About to write Solidity to file ${outputFile}`)

writeFile(outputFile, code, (err) => {
if (err) {
throw new VError(
err,
`Failed to write Solidity to file ${outputFile}`
)
} else {
console.log(`Solidity written to ${outputFile}`)
}
})
}

export function writeDot(dot: string, dotFilename = 'classDiagram.dot') {
debug(`About to write Dot file to ${dotFilename}`)

Expand Down
16 changes: 16 additions & 0 deletions src/ts/etherscanParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ export class EtherscanParser {
return umlClasses
}

/**
* Get Solidity code from Etherscan for a contract and merges all files
* into one long string of Solidity code.
* @param contractAddress Ethereum contract address with a 0x prefix
* @return Promise string of Solidity code
*/
async getSolidityCode(contractAddress: string): Promise<string> {
const sourceFiles = await this.getSourceCode(contractAddress)

let solidityCode = ''
sourceFiles.forEach((sourceFile) => {
solidityCode += sourceFile.code
})
return solidityCode
}

/**
* Parses Solidity source code into an ASTNode object
* @param sourceCode Solidity source code
Expand Down
14 changes: 12 additions & 2 deletions src/ts/sol2uml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ If an Ethereum address with a 0x prefix is passed, the verified source code from
)
.option(
'-f, --outputFormat <value>',
'output file format: svg, png, dot or all',
'output file format: svg, png, sol, dot or all',
'svg'
)
.option('-o, --outputFileName <value>', 'output file name')
Expand Down Expand Up @@ -74,7 +74,7 @@ if (options.verbose) {
}

// This function needs to be loaded after the DEBUG env variable has been set
import { generateFilesFromUmlClasses } from './converter'
import { generateFilesFromUmlClasses, writeSolidity } from './converter'

async function sol2uml() {
let fileFolderAddress: string
Expand All @@ -97,6 +97,16 @@ async function sol2uml() {
options.network
)

// If output is Solidity code
if (options.outputFormat === 'sol') {
const solidityCode = await etherscanParser.getSolidityCode(
fileFolderAddress
)

// Write Solidity to the contract address
writeSolidity(solidityCode, fileFolderAddress)
return
}
umlClasses = await etherscanParser.getUmlClasses(fileFolderAddress)
} else {
const depthLimit = parseInt(options.depthLimit)
Expand Down

0 comments on commit 50f1295

Please sign in to comment.