In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to package the Ethernet intelligent contract through java". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to package the etheric intelligent contract through java".
Introduction to web3j
Web3j is a lightweight, highly modular, responsive, type-safe Java and Android class library that provides rich API for handling Ethernet Square smart contracts and integrating with clients (nodes) on the Ethernet Fong network.
You can use it to develop the ethernet block chain without writing integrated code for your application platform.
You can quickly start the dmeo sample
For a quick start, there is a Web3j demo sample project available that demonstrates many of the core features of developing Ethernet Square through Web3j, including:
Connect to a node on an Ethernet network
Load an Ethernet Square wallet file
Send etheric coins from one address to another
Deploy smart contracts to the network
Read values from deployed smart contracts
Update the values in the deployed smart contract
View events logged by smart contracts
Introduction to web3j development
First install the latest version of web3j into the project.
Maven
Java 8:
Org.web3j core 3.4.0
Android:
Org.web3j core 3.3.1-androidGradle
Java 8:
Compile ('org.web3j:core:3.4.0')
Android:
Compile ('org.web3j:core:3.3.1-android') starts the client
You need to start an Ethernet Fong client, but of course you don't need to start it again if you've already started it.
If it's geth, it starts like this:
$geth-rpcapi personal,db,eth,net,web3-rpc-rinkeby
If it is started by Parity:
$parity-chain testnet
If you use the free cloud service provided by the Infura client, start it like this:
Web3j web3 = Web3j.build (new HttpService ("https://morden.infura.io/your-token"));
If you want to learn more about infura, see Using Infura with web3j.
The documentation on how to get Ethernet coins on the network can be found in this: testnet section of the docs.
When a Web3j instance is not needed, you need to call the shutdown method to release the resources it uses.
Web3.shutdown () sends the request
Send synchronization request
Web3j web3 = Web3j.build (new HttpService ()); / / defaults to http://localhost:8545/Web3ClientVersion web3ClientVersion = web3.web3ClientVersion () .send (); String clientVersion = web3ClientVersion.getWeb3ClientVersion ()
* * use CompletableFuture (Future on Android) to send asynchronous requests * *
Web3j web3 = Web3j.build (new HttpService ()); / / defaults to http://localhost:8545/Web3ClientVersion web3ClientVersion = web3.web3ClientVersion (). SendAsync (). Get (); String clientVersion = web3ClientVersion.getWeb3ClientVersion ()
* Observable using RxJava
Web3j web3 = Web3j.build (new HttpService ()); / / defaults to http://localhost:8545/web3.web3ClientVersion().observable().subscribe(x-> {String clientVersion = x.getWeb3ClientVersion ();...})
Pay attention to the use of Android
Web3j web3 = Web3jFactory.build (new HttpService ()); / / defaults to http://localhost:8545/...IPC
Web3j also supports fast inter-process communication (IPC) over file sockets, allowing clients to run Web3j on the same host at the same time. When creating a service, it can be done using the relevant IPCService without going through the HTTPService.
/ / OS X/Linux/Unix:Web3j web3 = Web3j.build (new UnixIpcService ("/ path/to/socketfile")); / / WindowsWeb3j web3 = Web3j.build (new WindowsIpcService ("/ path/to/namedpipefile"));
It should be noted that IPC communication is not available in web3j-android.
Package the ethernet smart contract through java
Web3j can automatically package smart contract code to deploy and interact with ethernet smart contracts without leaving JVM.
To package the code, you need to compile the smart contract:
$solc .sol-- bin-- abi-- optimize-o /
Then package the code with web3j's command-line tool:
Web3j solidity generate / path/to/.bin / path/to/.abi-o / path/to/src/main/java-p com.your.organisation.name
Next, you can create and deploy smart contracts:
Web3j web3 = Web3j.build (new HttpService ()); / / defaults to http://localhost:8545/Credentials credentials = WalletUtils.loadCredentials ("password", "/ path/to/walletfile"); YourSmartContract contract = YourSmartContract.deploy (, GAS_PRICE, GAS_LIMIT,...,) .send (); / / constructor params
Or use an existing smart contract:
YourSmartContract contract = YourSmartContract.load ("0x |", GAS_PRICE, GAS_LIMIT)
Then you can interact with smart contracts:
TransactionReceipt transactionReceipt = contract.someMethod (,...) .send ()
Invoke the smart contract:
Type result = contract.someMethod (,...) .send ()
More information on packaging can be found here: Solidity smart contract wrappers
Filters
The responsive function of web3j makes it easy for the observer to notify the message subscriber through events and can be recorded in the block chain. Receive all new blocks and add them to the block chain:
Subscription subscription = web3j.blockObservable (false) .subscribe (block-> {...})
Receive all new transactions and add them to the blockchain:
Subscription subscription = web3j.transactionObservable () .subscribe (tx-> {...})
Receive all transactions that have been submitted to the network for processing. Before they are uniformly assigned to a block.)
Subscription subscription = web3j.pendingTransactionObservable () .subscribe (tx-> {...})
Or if you reset all blocks to the latest location, you will be notified when a new block is created.
Subscription subscription = catchUpToLatestAndSubscribeToNewBlocksObservable (,) .subscribe (block-> {...})
Topic filtering is also supported:
EthFilter filter = new EthFilter (DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST,) .addSingleTopic (...) | .addOptionalTopics (...,...) |; web3j.ethLogObservable (filter) .subscribe (log-> {...})
Subscriptions should also be cancelled when they are no longer needed:
Subscription.unsubscribe ()
* * Note: filters is not supported in Infura. **
For more information about filters and events, check out the interfaces between Filters and Events and Web3jRx.
Trade
Web3j supports the use of Ethernet Square wallet files (recommended) and Ethernet Square client management commands for sending transactions.
Use an etheric wallet file to send ethercoins to others:
Web3j web3 = Web3j.build (new HttpService ()); / / defaults to http://localhost:8545/Credentials credentials = WalletUtils.loadCredentials ("password", "/ path/to/walletfile"); TransactionReceipt transactionReceipt = Transfer.sendFunds (web3, credentials, "0x |", BigDecimal.valueOf (1.0), Convert.Unit.ETHER) .send ()
Or you want to build your own custom deal:
Web3j web3 = Web3j.build (new HttpService ()); / / defaults to http://localhost:8545/Credentials credentials = WalletUtils.loadCredentials ("password", "/ path/to/walletfile"); / / get the next available nonceEthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount (address, DefaultBlockParameterName.LATEST). Send (); BigInteger nonce = ethGetTransactionCount.getTransactionCount (); / / create our transactionRawTransaction rawTransaction = RawTransaction.createEtherTransaction (nonce,) / / sign & send our transactionbyte [] signedMessage = TransactionEncoder.signMessage (rawTransaction, credentials); String hexValue = Numeric.toHexString (signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction (hexValue). Send (); / /.
It is much easier to trade etheric currencies using Web3j's Transfer.
Use the administrative commands of the Ethernet Square client (if your wallet key is already stored on the client):
Admin web3j = Admin.build (new HttpService ()); / / defaults to http://localhost:8545/PersonalUnlockAccount personalUnlockAccount = web3j.personalUnlockAccount ("0x000...", "a password"). SendAsync (). Get (); if (personalUnlockAccount.accountUnlocked ()) {/ / send a transaction}
If you want to use the Parity's Personal or Trace function, or the Geth's Personal client APIs, you can use the org.web3j:parity and org.web3j:geth modules.
Command line tool
Web3j's jar package provides command-line tools for each version. Command line tools allow you to use some of the features of web3j directly through some commands:
Wallet creation
Wallet password management
Transfer of money from wallet to another
Intelligent contract function package written by solidity
Refer to the documentation for further information about the command line.
Other details.
Java8 bulid:
Web3j provides secure access to all response types. Optional or null response java 8 is supported.
The asynchronous request packet is in a java 8 CompletableFutures. Web3j provides packaging tools around all asynchronous requests to ensure that any exception can be caught during execution, not just discarded. Because there are many unsupported exceptions in a full inspection, these exceptions are usually identified as undetected exceptions, resulting in problems in the detection process. For more information, see Async.run () and its associated test.
In the Android version of java 8:
The number of packets is returned as BigIntegers. For simple results, you can get the quantitative results of the string type through Response.getResult ().
You can also place the native JSON package in the response through the includeRawResponse parameter that exists in the HttpService and IpcService classes.
Thank you for your reading, the above is the content of "how to package Ethernet Square Intelligent contract through java". After the study of this article, I believe you have a deeper understanding of how to package Ethernet Square Intelligent contract through java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.