免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

Java如何使用web3j調用智能合約

這篇文章的內容主要圍繞Java如何使用web3j調用智能合約進行講述,文章內容清晰易懂,條理清晰,非常適合新手學習,值得大家去閱讀。感興趣的朋友可以跟隨小編一起閱讀吧。希望大家通過這篇文章有所收獲!

成都做網站、成都網站建設、成都外貿網站建設服務團隊是一支充滿著熱情的團隊,執著、敏銳、追求更好,是創新互聯的標準與要求,同時竭誠為客戶提供服務是我們的理念。創新互聯建站把每個網站當做一個產品來開發,精雕細琢,追求一名工匠心中的細致,我們更用心!

1.Java程序引入相關依賴,后面用于調用智能合約中的函數

        <!-- https://mvnrepository.com/artifact/org.web3j/core -->
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>codegen</artifactId>
            <version>5.0.0</version>
        </dependency>
        <!-- 由于ether-camp/solcJ不再維護,所以使用了FISCO-BCOS/solcj -->
        <dependency>
            <groupId>org.fisco-bcos</groupId>
            <artifactId>solcJ</artifactId>
            <version>0.5.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.4.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp-ws -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp-ws</artifactId>
            <version>3.4.2</version>
        </dependency>

2.將合約使用remix進行編譯

Java如何使用web3j調用智能合約

編譯后,復制abi、Bytecode,放入指定位置,生成abi和bin文件

Java如何使用web3j調用智能合約

    @Test
    void generateABIAndBIN() {
        String abi = "abi復制放這里";
        String bin = "bin復制過來放這里";
        String abiFileName = "abi文件名.abi";
        String binFileName = "bin文件名.bin";
        File abiFile = new File("E:\\solidity\\xx\\xx\\"+ abiFileName);
        File binFile = new File("E:\\solidity\\xx\\xx\\"+ binFileName);
        if (!abiFile.getParentFile().exists()) {
            boolean result = abiFile.getParentFile().mkdirs();
            if (!result) {
                System.out.println("創建失敗");
            }
        }
        BufferedOutputStream abiBos = null;
        BufferedOutputStream binBos = null;
        try {
            FileOutputStream abiFos = new FileOutputStream(abiFile);
            FileOutputStream binFos = new FileOutputStream(binFile);
            abiBos = new BufferedOutputStream(abiFos);
            binBos = new BufferedOutputStream(binFos);
            abiBos.write(abi.getBytes());
            abiBos.flush();
            binBos.write(bin.getBytes());
            binBos.flush();
        }catch (Exception e) {
            throw new RuntimeException("寫入過程出現錯誤");
        }finally {
            if(abiBos != null) {
                try {
                    abiBos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(binBos != null) {
                try {
                    binBos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

3.使用codegen生成Java代碼(參考https://github.com/maohuihua123/solidity-wrapper-generator)
使用該方法原因(ABI in solidity 0.6.0 does not have constant property which is causing UI libraries to think it's a non-constant method)

  @Test
  public void generateClass() throws IOException, ClassNotFoundException {
    String[] args = Arrays.asList(
        "-a", "D:/solidity/xx/xx.abi",
        "-b", "D:/solidity/xx/xx.bin",
        "-p", "top.rhynie.xx.contract",
        "-o", "D:/IDEA_Project/xx/src/main/java"
    ).toArray(new String[0]);
    Stream.of(args).forEach(System.out::println);
    SolidityFunctionWrapperGenerator.main(args);
  }

4.注冊infura獲取免費節點

Java如何使用web3j調用智能合約

5.java代碼調用只能合約代碼

    @Test
    void deployContract() throws Exception {
        Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/v3/xxxx"));
        String ownAddress = "0x119Eb8E686423E56b7cfc6F211C8CD4a9F71E3Cc";
//        Credentials c = WalletUtils.loadCredentials("密碼","keystore文件地址");
        Credentials credentials = Credentials.create("私鑰");
        AWToken awToken = AWToken.deploy(web3, credentials, web3.ethGasPrice().send().getGasPrice(), Contract.GAS_LIMIT).send();
        System.out.println(awToken.getContractAddress());

         // 調用合約的函數
         awToken.transfer("0x0", value);
    }

Java是什么

Java是一門面向對象編程語言,可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序。

感謝你的閱讀,相信你對“Java如何使用web3j調用智能合約”這一問題有一定的了解,快去動手實踐吧,如果想了解更多相關知識點,可以關注創新互聯網站!小編會繼續為大家帶來更好的文章!

網站欄目:Java如何使用web3j調用智能合約
本文來源:http://m.newbst.com/article22/jeeojc.html

成都網站建設公司_創新互聯,為您提供云服務器靜態網站App開發搜索引擎優化域名注冊

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

小程序開發