In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article mainly focuses on how Java uses web3j to call smart contracts. The content of the article is clear and well-organized. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
The 1.Java program introduces related dependencies, which are later used to call functions in intelligent contracts.
Org.web3j core 5.0.0 org.web3j codegen 5.0.0 org.fisco-bcos solcJ 0.5.2.0 commons-io commons-io 2.4 com.squareup.okhttp3 okhttp 4.4.0 com.squareup.okhttp3 okhttp-ws 3.4.2
two。 Compile the contract using remix
After compilation, copy abi and Bytecode, put them in the specified location, and generate abi and bin files
@ Test void generateABIAndBIN () {String abi = "abi copy here"; String bin = "bin copy here"; String abiFileName = "abi file name .abi"; String binFileName = "bin file name .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 ("creation failed");} 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 ("write process error");} finally {if (abiBos! = null) {try {abiBos.close ();} catch (IOException e) {e.printStackTrace () } if (binBos! = null) {try {binBos.close ();} catch (IOException e) {e.printStackTrace ();}}
3. Generate Java code using codegen (see https://github.com/maohuihua123/solidity-wrapper-generator)
Reasons for using this method (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. Register for infura to get free nodes
5.java code calls can only contract code
@ Test void deployContract () throws Exception {Web3j web3 = Web3j.build (new HttpService ("https://ropsten.infura.io/v3/xxxx")); String ownAddress =" 0x119Eb8E686423E56b7cfc6F211C8CD4a9F71E3Cc "; / / Credentials c = WalletUtils.loadCredentials (" password "," keystore file address "); Credentials credentials = Credentials.create (" private key ") AWToken awToken = AWToken.deploy (web3, credentials, web3.ethGasPrice (). Send (). GetGasPrice (), Contract.GAS_LIMIT). Send (); System.out.println (awToken.getContractAddress ()); / / call contract function awToken.transfer ("0x0", value) What is Java? Java is an object-oriented programming language that can write desktop applications, Web applications, distributed systems, and embedded system applications.
Thank you for your reading. I believe you have some understanding of "how Java uses web3j to call smart contracts". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.