In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how Java invokes Ethernet Square Intelligent contract". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. What is web3j
Web3j is a development package that you can use to quickly access ethernet / Quorum nodes, and you can also use it to generate a corresponding java wrapper class for an intelligent contract file to be invoked in Java applications.
Systematically study web3j development, recommend Huizhi.com 's online interactive course: "detailed explanation of the Development of Web3j Yi Tai Fang"
2. Node installation and intelligent contract deployment
First of all, we need to build an Ethernet Fong network and deploy the smart contract. You can refer to this repo and follow the README instructions to build and deploy the smart contract.
To invoke a contract on the ethernet chain, we need two things:
Wallet private key
Contract address
In the blockchain world, accounts are often called wallets, but this is a confusing term, sometimes referring to a single address and sometimes to a container containing multiple addresses. In this article, a wallet refers to a single ethernet address
Ganache lists 10 wallet addresses and their corresponding private keys at startup. For example:
(0) 11af9e9f87c53beedfe7eb3f1e9b6e2592b382ab3ecd83a92a6c20cb0c885f63 (1) 7cb8f9f70eac9aca70b514a9a8ebd36a3c2c4a9d28dc9534d6042914ed814161 (…)
In this tutorial, we will use the upload address, that is:
11af9e9f87c53beedfe7eb3f1e9b6e2592b382ab3ecd83a92a6c20cb0c885f63
Deployed contracts are similar:
2_deploy_contracts.js=Deploying 'SimpleStorage'- > transaction hash: 0x18f4dffba426a2cd63bddf5b8741f0708729515d178d59df0b2a43c5aa85e646 > Blocks: 0 Seconds: 0 > contract address: 0x3C4c39bd5a928bc19A981c85A00543EEB9f7C795 > account: 0x169C8C361e1CC394C3FFefa52FcaB91704cde2b2 > balance: 99.99147618 (…)
In the output above, notice the contract address section, which indicates the deployment address of the contract, that is:
Contract-address: installation of 0x3C4c39bd5a928bc19A981c85A00543EEB9f7C7953, smart contract related tools
We first need to install the solidity intelligent contract compiler:
$npm install-g solc
Then download the web3j command line tool from here, extract it, and set the PATH environment variable so that it can be called from any directory.
4. Java encapsulation class of the contract
First compile the contract and generate the java wrapper class:
$solc .sol-bin-abi-optimize-o / $web3j solidity generate-b / path/to/.bin\-a / path/to/.abi\-o / path/to/src/main/java\-p com.your.organisation.name5, call the contract in the Java code
Now we can invoke the contract:
Public class App {/ / change contract-address private static String contractAddress = contract-address; / / main method public static void main (String [] args) {/ / instantiate web3j Web3j web3 = Web3j.build (new HttpService ("http://localhost:9545/")); Credentials credentials = Credentials.create (wallet-address)) / / instantiate the contract SimpleStorage contract = SimpleStorage.load (contractAddress, web3, credentials, new DefaultGasProvider ()); / / say hello System.out.println ("Welcome" + credentials.getAddress ()); / / interact with the contract try {BigInteger currentValue; / / get value currentValue = contract.get () .send () System.out.println (currentValue.toString ()); / / change value contract.set (new BigInteger ("1")). Send (); / / get value currentValue = contract.get (). Send (); System.out.println (currentValue.toString ());} catch (Exception e) {e.printStackTrace ();}
The above code first initializes the Web3j instance with the node URL, then creates an account identity credential object with the wallet address private key, initializes the contract with the contract address, and finally calls the contract method.
This is the end of the content of "how Java invokes the Ethernet Square Smart contract". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.