這篇文章的內(nèi)容主要圍繞Java如何使用web3j調(diào)用智能合約進(jìn)行講述,文章內(nèi)容清晰易懂,條理清晰,非常適合新手學(xué)習(xí),值得大家去閱讀。感興趣的朋友可以跟隨小編一起閱讀吧。希望大家通過這篇文章有所收獲!
成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)服務(wù)團(tuán)隊是一支充滿著熱情的團(tuán)隊,執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時竭誠為客戶提供服務(wù)是我們的理念。創(chuàng)新互聯(lián)建站把每個網(wǎng)站當(dāng)做一個產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!
1.Java程序引入相關(guān)依賴,后面用于調(diào)用智能合約中的函數(shù)
<!-- 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不再維護(hù),所以使用了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進(jìn)行編譯
編譯后,復(fù)制abi、Bytecode,放入指定位置,生成abi和bin文件
@Test void generateABIAndBIN() { String abi = "abi復(fù)制放這里"; String bin = "bin復(fù)制過來放這里"; 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("創(chuàng)建失敗"); } } 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("寫入過程出現(xiàn)錯誤"); }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獲取免費節(jié)點
5.java代碼調(diào)用只能合約代碼
@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()); // 調(diào)用合約的函數(shù) awToken.transfer("0x0", value); }
Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
感謝你的閱讀,相信你對“Java如何使用web3j調(diào)用智能合約”這一問題有一定的了解,快去動手實踐吧,如果想了解更多相關(guān)知識點,可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站!小編會繼續(xù)為大家?guī)砀玫奈恼拢?/p>
網(wǎng)站欄目:Java如何使用web3j調(diào)用智能合約
本文來源:http://www.rwnh.cn/article22/jeeojc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、、靜態(tài)網(wǎng)站、App開發(fā)、搜索引擎優(yōu)化、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)