Skip to content

Commit

Permalink
Documentation and final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Aug 28, 2023
1 parent be3b603 commit f9c51f5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
73 changes: 71 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Compiler struct {
}

// NewCompiler creates a new Compiler instance with the given context, configuration, and source.
// It returns an error if the provided configuration, solc instance, or source is invalid.
func NewCompiler(ctx context.Context, solc *Solc, config *CompilerConfig, source string) (*Compiler, error) {
if config == nil {
return nil, fmt.Errorf("config must be provided to create new compiler")
Expand Down Expand Up @@ -68,6 +69,7 @@ func (v *Compiler) GetSources() string {
}

// Compile compiles the Solidity sources using the configured compiler version and arguments.
// It returns the compilation results or an error if the compilation fails.
func (v *Compiler) Compile() ([]*CompilerResults, error) {
compilerVersion := v.GetCompilerVersion()
if compilerVersion == "" {
Expand Down Expand Up @@ -131,6 +133,74 @@ func (v *Compiler) Compile() ([]*CompilerResults, error) {
return []*CompilerResults{results}, err
}

if v.config.JsonConfig != nil {
return v.resultsFromJson(compilerVersion, out)
}

return v.resultsFromSimple(compilerVersion, out)
}

// resultsFromSimple parses the output from the solc compiler when the output is in a simple format.
// It extracts the compilation details such as bytecode, ABI, and any errors or warnings.
// The method returns a slice of CompilerResults or an error if the output cannot be parsed.
func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) ([]*CompilerResults, error) {
// Parse the output
var compilationOutput struct {
Contracts map[string]struct {
Bin string `json:"bin"`
Abi interface{} `json:"abi"`
} `json:"contracts"`
Errors []string `json:"errors"`
Version string `json:"version"`
}

if err := json.Unmarshal(out.Bytes(), &compilationOutput); err != nil {
return nil, err
}

// Separate errors and warnings
var errors, warnings []string
for _, msg := range compilationOutput.Errors {
if strings.Contains(msg, "Warning:") {
warnings = append(warnings, msg)
} else {
errors = append(errors, msg)
}
}

var results []*CompilerResults

for key, output := range compilationOutput.Contracts {
isEntryContract := false
if v.config.GetEntrySourceName() != "" && key == "<stdin>:"+v.config.GetEntrySourceName() {
isEntryContract = true
}

abi, err := json.Marshal(output.Abi)
if err != nil {
return nil, err
}

results = append(results, &CompilerResults{
IsEntryContract: isEntryContract,
RequestedVersion: compilerVersion,
CompilerVersion: compilationOutput.Version,
Bytecode: output.Bin,
ABI: string(abi),
ContractName: strings.TrimLeft(key, "<stdin>:"),
Errors: errors,
Warnings: warnings,
})
}

return results, nil
}

// resultsFromJson parses the output from the solc compiler when the output is in a JSON format.
// It extracts detailed compilation information including bytecode, ABI, opcodes, and metadata.
// Additionally, it separates any errors and warnings from the compilation process.
// The method returns a slice of CompilerResults or an error if the output cannot be parsed.
func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]*CompilerResults, error) {
// Parse the output
var compilationOutput struct {
Contracts map[string]map[string]struct {
Expand All @@ -150,8 +220,7 @@ func (v *Compiler) Compile() ([]*CompilerResults, error) {
Version string `json:"version"`
}

err = json.Unmarshal(out.Bytes(), &compilationOutput)
if err != nil {
if err := json.Unmarshal(out.Bytes(), &compilationOutput); err != nil {
return nil, err
}

Expand Down
26 changes: 16 additions & 10 deletions compiler_json_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@ package solc

import "encoding/json"

// Source represents the content of a Solidity source file.
type Source struct {
Content string `json:"content"`
Content string `json:"content"` // The content of the Solidity source file.
}

// Settings defines the configuration settings for the Solidity compiler.
type Settings struct {
Optimizer Optimizer `json:"optimizer"`
EVMVersion string `json:"evmVersion,omitempty"`
Remappings []string `json:"remappings,omitempty"`
OutputSelection map[string]map[string][]string `json:"outputSelection"`
Optimizer Optimizer `json:"optimizer"` // Configuration for the optimizer.
EVMVersion string `json:"evmVersion,omitempty"` // The version of the Ethereum Virtual Machine to target. Optional.
Remappings []string `json:"remappings,omitempty"` // List of remappings for library addresses. Optional.
OutputSelection map[string]map[string][]string `json:"outputSelection"` // Specifies the type of information to output (e.g., ABI, AST).
}

// Optimizer represents the configuration for the Solidity compiler's optimizer.
type Optimizer struct {
Enabled bool `json:"enabled"`
Runs int `json:"runs"`
Enabled bool `json:"enabled"` // Indicates whether the optimizer is enabled.
Runs int `json:"runs"` // Specifies the number of optimization runs.
}

// CompilerJsonConfig represents the JSON configuration for the Solidity compiler.
type CompilerJsonConfig struct {
Language string `json:"language"`
Sources map[string]Source `json:"sources"`
Settings Settings `json:"settings"`
Language string `json:"language"` // Specifies the language version (e.g., "Solidity").
Sources map[string]Source `json:"sources"` // Map of source file names to their content.
Settings Settings `json:"settings"` // Compiler settings.
}

// ToJSON converts the CompilerJsonConfig to its JSON representation.
// It returns the JSON byte array or an error if the conversion fails.
func (c *CompilerJsonConfig) ToJSON() ([]byte, error) {
return json.Marshal(c)
}
2 changes: 1 addition & 1 deletion releases/releases.json
Git LFS file not shown

0 comments on commit f9c51f5

Please sign in to comment.