Skip to content

Commit

Permalink
support configuration and make readme pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Mar 9, 2023
1 parent 3da89b9 commit f59474e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 20 deletions.
53 changes: 41 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,53 @@

Deno CLI and API to compile Solidity smart contracts.

## Install
## Getting Started

### CLI
Deno >=v1.25 is required.

To install `sol_build` globally run:

```sh
deno install --allow-read --allow-write --allow-net -f https://deno.land/x/sol_build/cli.ts
```

### API
Initialize a new project with a sample `hello.sol` contract:

```sh
sol_build init helloworld
# Initializing a new project
# Fetching releases...
# Downloading soljson from https://solc-bin.ethereum.org/bin/soljson-v0.8.19+commit.7dd6d404.js...
cd helloworld
```

Compile all Solidity files and produce [Hardhat](https://github.com/NomicFoundation/hardhat)-compatible artifacts with ABIs, EVM bytecode and more:

```sh
sol_build compile
```

If you only need ABIs, pass the `--abi` option.
To run optimizations, pass the `--optimizer <number of runs>` flag.

## Configuration

You can pass a custom config as a file with `sol_build -c sol.config.ts` if you need more flexible settings:

```ts
import type { Config } from 'https://deno.land/x/sol_build/config.ts'

export const config = {
abi: true, // only produce ABIs
// all solidity settings go here
}
```

CLI arguments have a higher priority over config except for `outputSelection` setting.

## Programmatic use

`sol_build` exports functions for finding, linking and compiling Solidity files.

```ts
import { compileToFs } from 'https://deno.land/x/sol_build/mod.ts'
Expand All @@ -28,15 +66,6 @@ const solc = solc = wrapper(require('./.solc.js'))
await compileToFs(solc, { optimizer: { enabled: true, runs: 200 }})
```

## CLI Reference

### `init`

Initializes a basic project with a `Hello World` contract and a compiler binary.

### `compile`

Compile Solidity files in a current directory and save outputs to the `artifacts` folder.

[code-quality-img]: https://img.shields.io/codefactor/grade/github/deno-web3/sol_build?style=for-the-badge&color=626890&
[code-quality]: https://www.codefactor.io/repository/github/deno-web3/sol_build
Expand Down
51 changes: 46 additions & 5 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { colors, Command, createRequire, Wrapper, wrapper } from './deps.ts'
import { Config } from './config.ts'
import {
colors,
Command,
createRequire,
InputSettings,
Wrapper,
wrapper,
} from './deps.ts'
import * as api from './mod.ts'
const require = createRequire(import.meta.url)

const cwd = Deno.cwd()

await new Command()
.name('sol_build')
.description('Deno CLI for compiling Solidity smart contracts.')
Expand All @@ -22,7 +32,9 @@ await new Command()
'--optimizer [runs:number]',
'Enable optimizer (optionally with custom number of runs)',
)
.action(async ({ optimizer }) => {
.option('--config, -c <file:string>', 'Config file')
.option('--abi', 'Generate only ABIs')
.action(async ({ optimizer, abi, config }) => {
let solc: Wrapper
try {
solc = wrapper(require('./.solc.js'))
Expand All @@ -34,12 +46,37 @@ await new Command()
)
}
let count = 0
let configAbi: boolean | undefined = false
let configModule: InputSettings = {}
if (config) {
const { abi, ...cfg }: Config = await import(
`${cwd}/${config}`
).then((m) => m.config)
configAbi = abi
configModule = cfg
}

try {
count = await api.compileToFs(solc, {
...configModule,
optimizer: {
enabled: !!optimizer,
runs: typeof optimizer === 'number' ? optimizer : 200,
enabled: typeof optimizer === 'undefined'
? configModule.optimizer?.enabled
: !!optimizer,
runs: typeof optimizer === 'undefined'
? configModule.optimizer?.runs
: typeof optimizer === 'number'
? optimizer
: 200,
},
outputSelection: {
'*': {
// @ts-ignore types bug
'*': configAbi || abi
? ['abi']
: ['evm.bytecode', 'evm.deployedBytecode', 'abi'],
},
...configModule.outputSelection,
},
})
} catch (e) {
Expand All @@ -48,6 +85,10 @@ await new Command()
e as Error,
)
}
console.log(colors.green(`Compiled ${count} Solidity file${count > 1 ? 's' : ''} successfully`))
console.log(
colors.green(
`Compiled ${count} Solidity file${count > 1 ? 's' : ''} successfully`,
),
)
})
.parse(Deno.args)
5 changes: 5 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { InputSettings } from './deps.ts'

export type Config = {
abi?: boolean
} & InputSettings
11 changes: 8 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const compile = async (
'*': {
'*': ['evm.bytecode', 'evm.deployedBytecode', 'abi'],
},
...settings.outputSelection, // to be able to override outputs
},
},
} as Input),
Expand Down Expand Up @@ -124,13 +125,17 @@ export const compileToFs = async (
return {
sourceName,
contracts: Object.entries(c).map(([contractName, contract]) => {
const linkReferences = contract.evm?.bytecode?.linkReferences
const deployedLinkReferences = contract.evm?.deployedBytecode
?.linkReferences
const bytecode = contract.evm?.bytecode?.object
return ({
contractName,
sourceName,
bytecode: `0x${contract.evm.bytecode.object}`,
bytecode: bytecode ? `0x${bytecode}` : undefined,
abi: contract.abi,
linkReferences: contract.evm.bytecode.linkReferences,
deployedLinkReferences: contract.evm.deployedBytecode.linkReferences,
linkReferences,
deployedLinkReferences,
})
}),
}
Expand Down

0 comments on commit f59474e

Please sign in to comment.