Skip to content

A Go package that compiles Solidity smart contracts using soljson.js via the embedded V8 engine (v8go). It reads .sol files from a contracts directory, handles imports and dependencies, and outputs ABI and bytecode as JSON files for Ethereum development.

Notifications You must be signed in to change notification settings

0xsharma/gosolc

Repository files navigation

gosolc

Table of Contents

About

A Go package that compiles Solidity smart contracts using soljson.js via the embedded V8 engine (v8go). It reads .sol files from a contracts directory, handles imports and dependencies, and outputs ABI and bytecode as JSON files for Ethereum development.

Features

  • Compiles multiple Solidity files from the contracts directory.
  • Supports standard JSON input/output format for solc.
  • Extracts and saves ABI, bytecode, and deployed bytecode for each contract.
  • Handles imports (e.g., import "./dummy_ERC20.sol").

Prerequisites

What things you need to install the software and how to install them.

Go: Version 1.18 or higher.

Installing

go get -u "github.com/0xsharma/gosolc"

Usage

New Compiler

import (
	"github.com/0xsharma/gosolc"
)

c, err := gosolc.NewDefaultCompiler("./contracts")
if err != nil {
    //handle error
}

or

New compiler with config (evm version, optimization, optimization runs and custom soljson)

import (
	"github.com/0xsharma/gosolc"
)

cfg := gosolc.NewCompilerConfig("cancun", false, 0)

c, err := gosolc.NewCompiler("./contracts", cfg,"solc-bin/soljson-v0.8.29.js")
if err != nil {
    //handle error
}

Compile contracts

compiled, err := c.Compile()
if err != nil {
    //handle error
}

Compile and write ABI and Bytecode to JSON files

err = c.CompileAndWriteOutput()
if err != nil {
    panic(err)
}

Get Bytecodes from compiler output

bytecodes, err := compiled.GetContractByteCodes()
if err != nil {
    //handle error
}

deployedBytecodes, err := compiled.GetDeployedContractByteCodes()
if err != nil {
    //handle error
}

// Print the bytecodes
for contractName, bytecode := range bytecodes {
    fmt.Printf("ContractName: %s\n\nBytecode: %s\n\n", contractName, bytecode)
}

for contractName, bytecode := range deployedBytecodes {
    fmt.Printf("ContractName: %s\n\nDeployed Bytecode: %s\n\n", contractName, bytecode)
}

Contributing

Contributions are welcome! Currently the project is using solc version 0.8.29 by default. If you want to add support for a new version, please create a new branch and submit a pull request. Please make sure to update the README.md file with any new features or changes you make.

About

A Go package that compiles Solidity smart contracts using soljson.js via the embedded V8 engine (v8go). It reads .sol files from a contracts directory, handles imports and dependencies, and outputs ABI and bytecode as JSON files for Ethereum development.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages