您好,登錄后才能下訂單哦!
這篇文章的內容主要圍繞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進行編譯
編譯后,復制abi、Bytecode,放入指定位置,生成abi和bin文件
@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獲取免費節點
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是一門面向對象編程語言,可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序。
感謝你的閱讀,相信你對“Java如何使用web3j調用智能合約”這一問題有一定的了解,快去動手實踐吧,如果想了解更多相關知識點,可以關注億速云網站!小編會繼續為大家帶來更好的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。