solc

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: MIT Imports: 23 Imported by: 0

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("0.8.21")
    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.21
├── src/
│   └── test.sol
├── contract_test.go
├── go.mod
└── go.sum

generate *.sol and compile it at runtime

[!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

This section is empty.

Functions

func CompileInDirWithCertainFiles

func CompileInDirWithCertainFiles()

func NewConsole

func NewConsole(tb testing.TB) vm.EVMLogger

NewConsole returns a vm.EVMLogger that logs calls of console.sol to the given testing.TB.

Types

type Compiler

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

func New

func New(version string) *Compiler

func (*Compiler) BuildSettings

func (c *Compiler) BuildSettings(opts []Option) *Settings

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) CompileSingleFile

func (c *Compiler) CompileSingleFile(in *Input) (*Output, error)

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 {
	Code       []byte // The bytecode of the contract after deployment.
	DeployCode []byte // The bytecode to deploy the contract.
	Metadata   string
}

Contract represents a compiled contract.

func GetContract

func GetContract(result map[string]map[string]contract, contractName string) (*Contract, bool)

type EVMVersion

type EVMVersion string

EVMVersion represents the EVM version to compile for.

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

type Input

type Input struct {
	Lang     lang           `json:"language"`
	Sources  map[string]Src `json:"sources"`
	Settings *Settings      `json:"settings"`
}

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 WithViaIR

func WithViaIR(enabled bool) Option

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

type Output

type Output struct {
	Errors    []error_                       `json:"errors"`
	Sources   map[string]srcOut              `json:"sources"`
	Contracts map[string]map[string]contract `json:"contracts"`
}

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 Src

type Src struct {
	Keccak256 string   `json:"keccak256,omitempty"`
	Content   string   `json:"content,omitempty"`
	URLS      []string `json:"urls,omitempty"`
}

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