solc

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2025 License: MIT Imports: 24 Imported by: 1

README

go-solc: Go Bindings for the Solidity Compiler

Go Reference Go Report Card

go-solc provides an easy way to compile Solidity contracts from Go.

go get github.com/lmittmann/go-solc

Getting Started

go-solc automatically downloads the specified version of the Solidity compiler from https://binaries.soliditylang.org and caches it at .solc/bin/.

Example test:

// contract_test.go
func TestContract(t *testing.T) {
    c := solc.New(solc.VersionLatest)
    contract, err := c.Compile("src", "Test",
        solc.WithOptimizer(&solc.Optimizer{Enabled: true, Runs: 999999}),
    )
    // ...
}

Example directory structure:

workspace/
├── .solc/
│   └── bin/ # cached solc binaries
│       └── solc_v0.8.30
├── src/
│   └── test.sol
├── contract_test.go
├── go.mod
└── go.sum

[!WARNING]

This package is pre-1.0. There might be breaking changes between minor versions.

Documentation

Overview

Package solc provides bindings for the Solidity compiler.

Index

Constants

This section is empty.

Variables

View Source
var Versions []Version

Versions is a list of all available solc versions.

Functions

func NewConsole

func NewConsole(tb testing.TB) *tracing.Hooks

NewConsole returns a [vm.tracing.Hooks] that logs calls of console.sol to the given testing.TB.

To use console logging in your Solidity contract, import "console.sol".

Types

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

func New

func New(version Version) *Compiler

func (*Compiler) Compile

func (c *Compiler) Compile(dir, contract string, opts ...Option) (*Contract, error)

Compile all contracts in the given directory and return the contract code of the contract with the given name.

func (*Compiler) MustCompile

func (c *Compiler) MustCompile(dir, contract string, opts ...Option) *Contract

MustCompile is like Compiler.Compile but panics on error.

type Contract

type Contract struct {
	Runtime     []byte // The runtime bytecode of the contract.
	Constructor []byte // The constructor bytecode of the contract.
	Code        []byte // Deprecated: The bytecode of the contract after deployment.
	DeployCode  []byte // Deprecated: The bytecode to deploy the contract.
}

Contract represents a compiled contract.

type EVMVersion

type EVMVersion string

EVMVersion represents the EVM version to compile for.

const (
	EVMVersionPrague     EVMVersion = "prague"
	EVMVersionCancun     EVMVersion = "cancun"
	EVMVersionShanghai   EVMVersion = "shanghai"
	EVMVersionParis      EVMVersion = "paris"
	EVMVersionLondon     EVMVersion = "london"
	EVMVersionBerlin     EVMVersion = "berlin"
	EVMVersionIstanbul   EVMVersion = "istanbul"
	EVMVersionPetersburg EVMVersion = "petersburg"
	EVMVersionByzantium  EVMVersion = "byzantium"
	EVMVersionOsaka      EVMVersion = "osaka"
)

type Optimizer

type Optimizer struct {
	Enabled bool   `json:"enabled"`
	Runs    uint64 `json:"runs"`
}

type Option

type Option func(*Settings)

An Option configures the compilation Settings.

func WithEVMVersion

func WithEVMVersion(evmVersion EVMVersion) Option

WithEVMVersion configures the compilation Settings to set the given EVM version.

func WithOptimizer

func WithOptimizer(o *Optimizer) Option

WithOptimizer configures the compilation Settings to set the given Optimizer.

func WithRemappings added in v0.3.3

func WithRemappings(remappings []string) Option

WithRemappings configures the compilation Settings to set the remmappings options. Each remapping is in standard format {src}={dst}. Destination path needs to be absolute.

func WithViaIR

func WithViaIR(enabled bool) Option

WithViaIR configures the compilation Settings to set viaIR to the given parameter "enabled".

type Settings

type Settings struct {
	Remappings      []string                       `json:"remappings,omitempty"`
	Optimizer       *Optimizer                     `json:"optimizer"`
	ViaIR           bool                           `json:"viaIR,omitempty"`
	EVMVersion      EVMVersion                     `json:"evmVersion"`
	OutputSelection map[string]map[string][]string `json:"outputSelection"`
	// contains filtered or unexported fields
}

Settings for the compilation.

type Version added in v0.5.0

type Version string

Version represents a solc version.

const (
	Version0_5_0  Version = "0.5.0"
	Version0_5_1  Version = "0.5.1"
	Version0_5_2  Version = "0.5.2"
	Version0_5_3  Version = "0.5.3"
	Version0_5_4  Version = "0.5.4"
	Version0_5_5  Version = "0.5.5"
	Version0_5_6  Version = "0.5.6"
	Version0_5_7  Version = "0.5.7"
	Version0_5_8  Version = "0.5.8"
	Version0_5_9  Version = "0.5.9"
	Version0_5_10 Version = "0.5.10"
	Version0_5_11 Version = "0.5.11"
	Version0_5_12 Version = "0.5.12"
	Version0_5_13 Version = "0.5.13"
	Version0_5_14 Version = "0.5.14"
	Version0_5_15 Version = "0.5.15"
	Version0_5_16 Version = "0.5.16"
	Version0_5_17 Version = "0.5.17"
	Version0_6_0  Version = "0.6.0"
	Version0_6_1  Version = "0.6.1"
	Version0_6_2  Version = "0.6.2"
	Version0_6_3  Version = "0.6.3"
	Version0_6_4  Version = "0.6.4"
	Version0_6_5  Version = "0.6.5"
	Version0_6_6  Version = "0.6.6"
	Version0_6_7  Version = "0.6.7"
	Version0_6_8  Version = "0.6.8"
	Version0_6_9  Version = "0.6.9"
	Version0_6_10 Version = "0.6.10"
	Version0_6_11 Version = "0.6.11"
	Version0_6_12 Version = "0.6.12"
	Version0_7_0  Version = "0.7.0"
	Version0_7_1  Version = "0.7.1"
	Version0_7_2  Version = "0.7.2"
	Version0_7_3  Version = "0.7.3"
	Version0_7_4  Version = "0.7.4"
	Version0_7_5  Version = "0.7.5"
	Version0_7_6  Version = "0.7.6"
	Version0_8_0  Version = "0.8.0"
	Version0_8_1  Version = "0.8.1"
	Version0_8_2  Version = "0.8.2"
	Version0_8_3  Version = "0.8.3"
	Version0_8_4  Version = "0.8.4"
	Version0_8_5  Version = "0.8.5"
	Version0_8_6  Version = "0.8.6"
	Version0_8_7  Version = "0.8.7"
	Version0_8_8  Version = "0.8.8"
	Version0_8_9  Version = "0.8.9"
	Version0_8_10 Version = "0.8.10"
	Version0_8_11 Version = "0.8.11"
	Version0_8_12 Version = "0.8.12"
	Version0_8_13 Version = "0.8.13"
	Version0_8_14 Version = "0.8.14"
	Version0_8_15 Version = "0.8.15"
	Version0_8_16 Version = "0.8.16"
	Version0_8_17 Version = "0.8.17"
	Version0_8_18 Version = "0.8.18"
	Version0_8_19 Version = "0.8.19"
	Version0_8_20 Version = "0.8.20"
	Version0_8_21 Version = "0.8.21"
	Version0_8_22 Version = "0.8.22"
	Version0_8_23 Version = "0.8.23"
	Version0_8_24 Version = "0.8.24"
	Version0_8_25 Version = "0.8.25"
	Version0_8_26 Version = "0.8.26"
	Version0_8_27 Version = "0.8.27"
	Version0_8_28 Version = "0.8.28"
	Version0_8_29 Version = "0.8.29"
	Version0_8_30 Version = "0.8.30"

	// Latest version of solc.
	VersionLatest = Version0_8_30
)

func (Version) Cmp added in v0.5.0

func (v Version) Cmp(other Version) int

Compare returns -1, 0, or +1 depending on whether v < other, v == other, or v > other

func (Version) String added in v0.5.0

func (v Version) String() string

Directories

Path Synopsis
internal
console
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
mod

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL