Coder Social home page Coder Social logo

solcj's Introduction

solcJ

solcJ提供了Java编译solidity合约文件的接口,solidity编译器solc可以参考: https://github.com/ethereum/solidity

支持平台

  • linux
    • x64
    • aarch64
  • mac
  • windows

支持版本

solcJ支持三个版本的solidity编译器版本

  • 0.4.25
  • 0.5.2
  • 0.6.10

使用

  • Gradle

    # 0.4.25 版本
    compile group: 'org.fisco-bcos', name: 'solcJ', version: '0.4.25.0'
    
    # 0.5.2 版本
    compile group: 'org.fisco-bcos', name: 'solcJ', version: '0.5.2.0'
    
    # 0.6.10 版本
    compile group: 'org.fisco-bcos', name: 'solcJ', version: '0.6.10.0'
  • Maven

    # 0.4.25 版本
    <dependency>
          <groupId>org.fisco-bcos</groupId>
          <artifactId>solcJ</artifactId>
          <version>0.4.25.0</version>
      </dependency>
    
    # 0.5.2 版本
    <dependency>
          <groupId>org.fisco-bcos</groupId>
          <artifactId>solcJ</artifactId>
          <version>0.5.2.0</version>
      </dependency>
    
     # 0.6.10 版本
      <dependency>
          <groupId>org.fisco-bcos</groupId>
          <artifactId>solcJ</artifactId>
          <version>0.6.10.0</version>
      </dependency>
    

接口

编译

public static Result SolidityCompiler::compile(File source, boolean sm, boolean combinedJson, Option... options)
        throws IOException;
public static Result SolidityCompiler::compile(byte[] source, boolean sm, boolean combinedJson, Option... options)
        throws IOExceptio;

参数:

  • File source : solidity文件文件路径
  • byte[] resource : solidity文件内容
  • boolean sm: 国密开关
  • combinedJson:
  • Option ... options: solc编译参数
    • Options.ABI: 相当于solc --abi
    • Options.BIN: 相当于solc --bin
    • Options.INTERFACE: 相当于solc --interface
    • Options.METADATA: 相当于solc --metadata
    • Options.ASTJSON: 相当于solc --ast-json

返回:

  • SolidityCompiler::Result对象:
    • boolean success: 是否编译成功
    • String errors: 错误或者警告信息,编译失败时保存错误信息,编译成功时保存编译的警告信息
    • String output: 编译结果,具体格式参考solc的返回格式

返回解析

public static CompilationResult CompilationResult::parse(String rawJson) throws IOException 

参数:

  • String rawJson: Result.getOutput的值

返回:

  • CompilationResult对象
    • Map<String, ContractMetadata> contracts: 合约文件所有合约的编译结果

示例

File socFile = new File("HelloWorld.sol");
SolidityCompiler.Result res = SolidityCompiler.compile(solFile, false, true, ABI, BIN, INTERFACE, METADATA);

if (!res.isFailed())) {
    // 编译成功,解析返回
    CompilationResult result = CompilationResult.parse(res.getOutput());
    // 获取HelloWorld合约的编译结果
    CompilationResult.ContractMetadata meta = result.getContract(“HelloWorld”);
    // abi
    String abi = meta.abi;
    // bin
    String bin = meta.bin;
    // 其他逻辑
} else {
    // 编译失败,res.getError()保存编译失败的原因
}

solcj's People

Contributors

ywy2090 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.