java開發區塊鏈4__區塊鏈

來源:互聯網
上載者:User
使用java開發區塊鏈

這裡需要使用的環境 web3j,nodejs 安裝編譯sol工具

$ npm install -g solc
儲存為hello.sol檔案到本地
pragma solidity 0.4.19;  contract hello {  function main(uint a) constant returns (uint b)       {          uint result = a * 8;          return result;      }  }  
編譯sol檔案
$ solcjs <sol檔案目錄>   --optimize  --bin --abi --output-dir <輸出目錄>//demo$ solcjs F:\\hello.sol   --optimize  --bin --abi --output-dir F:\\

出現這種錯誤

Failed to write F:\F_\hello_sol_hello.bin: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.bin'Failed to write F:\F_\hello_sol_hello.abi: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.abi'

這裡有個坑,就是使用solcjs 編譯智能合約檔案輸出到目錄會有一個檔案夾,這個需要手動建立,我這裡輸出目錄到F:\\ 但是它還是要輸出到F:\\F_\ 下,這裡的F_檔案夾需要我們建立。 編譯成功,記錄下bin 和 abi尾碼的檔案 使用web3j工具產生java版本的智能合約 web3j 命令列工具下載

$ web3j solidity generate <編譯的bin檔案地址> <編譯的abi檔案地址> -o <輸出目錄> -p <java包名>//demo$ web3j solidity generate F:\F_\hello_sol_hello.bin F:\F_\hello_sol_hello.abi -o E:\etheth\src\main\java -p xyz.lihang.demo.eth.sol
產生成功
package xyz.lihang.demo.eth.sol;import java.math.BigInteger;import java.util.Arrays;import org.web3j.abi.TypeReference;import org.web3j.abi.datatypes.Function;import org.web3j.abi.datatypes.Type;import org.web3j.abi.datatypes.generated.Uint256;import org.web3j.crypto.Credentials;import org.web3j.protocol.Web3j;import org.web3j.protocol.core.RemoteCall;import org.web3j.tx.Contract;import org.web3j.tx.TransactionManager;/** * <p>Auto generated code. * <p><strong>Do not modify!</strong> * <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>, * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the  * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update. * * <p>Generated with web3j version 3.2.0. */public class Hello_sol_hello extends Contract {    private static final String BINARY = "60606040523415600e57600080fd5b609a8061001c6000396000f300606060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663ab3ae25581146043575b600080fd5b3415604d57600080fd5b60566004356068565b60405190815260200160405180910390f35b600802905600a165627a7a723058200cc51f5dad45190b24189d9f8ff836d704bcebc9862cfd669e054b8c8f19f66c0029";    protected Hello_sol_hello(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {        super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);    }    protected Hello_sol_hello(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {        super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);    }    public RemoteCall<BigInteger> main(BigInteger a) {        Function function = new Function("main",                 Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(a)),                 Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));        return executeRemoteCallSingleValueReturn(function, BigInteger.class);    }    public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {        return deployRemoteCall(Hello_sol_hello.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");    }    public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {        return deployRemoteCall(Hello_sol_hello.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");    }    public static Hello_sol_hello load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {        return new Hello_sol_hello(contractAddress, web3j, credentials, gasPrice, gasLimit);    }    public static Hello_sol_hello load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {        return new Hello_sol_hello(contractAddress, web3j, transactionManager, gasPrice, gasLimit);    }}

一定要注意合約調用後進行挖礦,這樣才能網路確認,才能使用合約

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.