solc

package module
v0.0.0-...-46126dc Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: MIT Imports: 11 Imported by: 0

README

solc-go

golang binding for solidity compiler

usage

go get github.com/imxyb/solc-go
package main

import (
	"encoding/json"
	"fmt"

	"github.com/imxyb/solc-go"
)

func main() {
	// get build list
	buildList, err := solc.GetBuildList()
	if err != nil {
		panic(err)
	}
	fmt.Println(buildList)

	// get 0.6.0 complier
	compiler, err := solc.GetCompiler("0.8.13")
	if err != nil {
		panic(err)
	}

	// refer to https://docs.soliditylang.org/en/v0.8.17/using-the-compiler.html#input-description
	input := &solc.Input{
		Language: "Solidity",
		Sources: map[string]solc.SourceIn{
			"Token.sol": solc.SourceIn{
				Content: `
// SPDX-License-Identifier: MIT
// compiler version must be greater than or equal to 0.8.13 and less than 0.9.0
pragma solidity ^0.8.13;

contract HelloWorld {
    string public greet;

    constructor(string memory g) {
        greet = g;
    }
}
`,
			},
		},
		Settings: solc.Settings{
			Optimizer: solc.Optimizer{
				Enabled: false,
				Runs:    200,
			},
			OutputSelection: map[string]map[string][]string{
				"*": map[string][]string{
					"*": []string{"*"},
				},
			},
		},
	}

	// refer to https://docs.soliditylang.org/en/v0.8.17/using-the-compiler.html#output-description
	output, err := compiler.Compile(input)
	if err != nil {
		panic(err)
	}

	// get deployed code and metadata
	metadata := output.Contracts["Token.sol"]["HelloWorld"].Metadata
	m := make(map[string]interface{})
	if err := json.Unmarshal([]byte(metadata), &m); err != nil {
		panic(err)
	}
	compiledByteCode := output.Contracts["Token.sol"]["HelloWorld"].EVM.DeployedBytecode.Object

	// verify code
	remoteByteCode := "608060..."
	verify, err := solc.Verify(compiledByteCode, remoteByteCode, m["bytecodeHash"].(string))
	if err != nil {
		panic(err)
	}
	fmt.Println(verify)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Verify

func Verify(compiledByteCode, byteCode, byteCodeHash string) (bool, error)

Verify verify bytecode is equal refer to https://docs.soliditylang.org/en/v0.8.17/metadata.html

Types

type BuildList

type BuildList struct {
	Path        string `json:"path"`
	Version     string `json:"version"`
	Build       string `json:"build"`
	LongVersion string `json:"longVersion"`
	Keccak256   string `json:"keccak256"`
	Sha256      string `json:"sha256"`
}

func GetBuildList

func GetBuildList() ([]*BuildList, error)

GetBuildList get build list

type Bytecode

type Bytecode struct {
	Object         string                                `json:"object,omitempty"`
	Opcodes        string                                `json:"opcodes,omitempty"`
	SourceMap      string                                `json:"sourceMap,omitempty"`
	LinkReferences map[string]map[string][]LinkReference `json:"linkReferences,omitempty"`
}

type Compiler

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

func GetCompiler

func GetCompiler(ver string) (*Compiler, error)

func NewCompiler

func NewCompiler(wasmScript, ver string) (*Compiler, error)

func (*Compiler) Close

func (c *Compiler) Close()

func (*Compiler) Compile

func (c *Compiler) Compile(input *Input) (*Output, error)

type Contract

type Contract struct {
	ABI      []json.RawMessage `json:"abi,omitempty"`
	Metadata string            `json:"metadata,omitempty"`
	UserDoc  json.RawMessage   `json:"userdoc,omitempty"`
	DevDoc   json.RawMessage   `json:"devdoc,omitempty"`
	IR       string            `json:"ir,omitempty"`
	// StorageLayout StorageLayout     `json:"storageLayout,omitempty"`
	EVM   EVM   `json:"evm,omitempty"`
	EWASM EWASM `json:"ewasm,omitempty"`
}

type EVM

type EVM struct {
	Assembly          string                       `json:"assembly,omitempty"`
	LegacyAssembly    json.RawMessage              `json:"legacyAssembly,omitempty"`
	Bytecode          Bytecode                     `json:"bytecode,omitempty"`
	DeployedBytecode  Bytecode                     `json:"deployedBytecode,omitempty"`
	MethodIdentifiers map[string]string            `json:"methodIdentifiers,omitempty"`
	GasEstimates      map[string]map[string]string `json:"gasEstimates,omitempty"`
}

type EWASM

type EWASM struct {
	Wast string `json:"wast,omitempty"`
	Wasm string `json:"wasm,omitempty"`
}

type Error

type Error struct {
	SourceLocation   SourceLocation `json:"sourceLocation,omitempty"`
	Type             string         `json:"type,omitempty"`
	Component        string         `json:"component,omitempty"`
	Severity         string         `json:"severity,omitempty"`
	Message          string         `json:"message,omitempty"`
	FormattedMessage string         `json:"formattedMessage,omitempty"`
}

type Input

type Input struct {
	Language string              `json:"language,omitempty"`
	Sources  map[string]SourceIn `json:"sources,omitempty"`
	Settings Settings            `json:"settings,omitempty"`
}

type LinkReference

type LinkReference struct {
	Start int `json:"start,omitempty"`
	End   int `json:"end,omitempty"`
}

type Optimizer

type Optimizer struct {
	Enabled bool `json:"enabled,omitempty"`
	Runs    int  `json:"runs,omitempty"`
}

type Output

type Output struct {
	Errors    []Error                        `json:"errors,omitempty"`
	Sources   map[string]SourceOut           `json:"sources,omitempty"`
	Contracts map[string]map[string]Contract `json:"contracts,omitempty"`
}

type Settings

type Settings struct {
	Remappings      []string                       `json:"remappings,omitempty"`
	Optimizer       Optimizer                      `json:"optimizer,omitempty"`
	EVMVersion      string                         `json:"evmVersion,omitempty"`
	OutputSelection map[string]map[string][]string `json:"outputSelection,omitempty"`
}

type SourceIn

type SourceIn struct {
	Keccak256 string `json:"keccak256,omitempty"`
	Content   string `json:"content,omitempty"`
}

type SourceLocation

type SourceLocation struct {
	File  string `json:"file,omitempty"`
	Start int    `json:"start,omitempty"`
	End   int    `json:"end,omitempty"`
}

type SourceOut

type SourceOut struct {
	ID        int             `json:"id,omitempty"`
	AST       json.RawMessage `json:"ast,omitempty"`
	LegacyAST json.RawMessage `json:"legacyAST,omitempty"`
}

Jump to

Keyboard shortcuts

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