ETH Basic JAVA web3j Intelligent Contract __java

Source: Internet
Author: User

Schema Springboot

This is illustrated using the integrated JDK currently recommended by WEB3J ETH. Of course, you can also use its most low-level RPC scheme to write (website also has instructions).
Here's a summary.

Step one: First introduce JDK [maven]

<dependency>
	<groupId>org.web3j</groupId>
	<artifactId>core</artifactId>
	<version>3.2.0</version>
</dependency>
Connection Wallet Node "all subsequent operations will require the wallet node to broadcast out."

web3j web3 = web3j.build (New Httpservice ("http://localhost:5201314/"));
Very simple, the test node is linked successfully

Web3clientversion web3clientversion;
try {
	web3clientversion = web3.web3clientversion (). Send ();
	String clientversion = Web3clientversion.getweb3clientversion ();
	System.out.println (clientversion);
} catch (IOException e) {
	e.printstacktrace ();
}
Use wallet The key is to create the wallet address and key

String FilePath = "E:/pictures";
String FileName;
Create Wallet Address
eth-password needs their own management, set up their own oh.
fileName = Walletutils.generatenewwalletfile ("Set Your password", new File (FilePath), false);
System.out.println (fileName);//Save your Encrypted file information
System.out.println (alice.getaddress ());//Wallet Address
System.out.println (Alice.geteckeypair (). Getprivatekey ());//private key
System.out.println (Alice.geteckeypair (). Getpublickey ());//Public key
The underlying operation. It's very simple. -

Now, of course, the most important thing is the deal.

Generating transactions requires the loading of the private key and wallet address. This is the way to load the wallet address by referencing the file.

String path= ' Wallet encrypted file address ';
Credentials ALICE = walletutils.loadcredentials ("Your password", path);
This will request a wallet information object.

BigInteger nonce = getnonce ("Send wallet address");
private static BigInteger getnonce (String address) throws Exception {
Web3.ethgettransactioncount (address,defaultblockparametername.latest). SendAsync (
). return Ethgettransactioncount.gettransactioncount ();
}
Send Amount
Rawtransaction rawtransaction = createethertransaction (nonce, "Mubia wallet Address");
private static Rawtransaction createethertransaction (BigInteger nonce, String toaddress) {
BigInteger value = Convert.towei ("Quantity", Convert.Unit.ETHER). Tobiginteger ();
Transaction fees are determined by Price*limit, you can customize all of these values, or you can use system parameters to get the current two values
The effect of the result is that the custom fees will affect the account time, the process fee is too low mine opportunity to deal with your last. Using the system, the fees can be very high, the system is to obtain the current transaction of a procedure to calculate. May require a sum of hundreds of RMB return rawtransaction.createethertransaction (nonce, Price,limit, toaddress, value);}
Return object
byte[] Signedmessage = Transactionencoder.signmessage (rawtransaction, ALICE);
Transaction order number
String hexvalue = numeric.tohexstring (signedmessage);
All right, finally, check the account balance.
Get balance
ethgetbalance EthGetBalance1 = web3.ethgetbalance ("0xb86d57174bf8c53f1084be7f565f9fd9dabd87d0", Defaultblockparameter.valueof ("latest")). Send ();
ETH default will be 18 0 here to deal with more casual, we can casually deal with ha
BigDecimal balance = new BigDecimal (Ethgetbalance1.getbalance (). Divide (new BigInteger ("10000000000000")). ToString ());
BigDecimal nbalance = balance.divide (New BigDecimal ("100000"), 8, bigdecimal.round_down);
System.out.println (nbalance);
I'll make it up for the next day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.