Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways of web3j transfer

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about web3j transfer, which are detailed in content and clear in logic. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article.

Web3 transfer function

In order to complete the ethernet place transaction, there must be several prerequisites

1. The address of the other party's Ethernet Square

2. Determine the amount to be transferred

3. Transfer authority of your own address

4. If the etheric currency is larger than the amount transferred, the etheric currency transfer is actually a transaction, and the miner will calculate it for you, and the transaction will be concluded immediately after the calculation is completed. However, miner calculation needs to pay a certain fee (GAS). If the payment is too little, the transfer may be slow or unsuccessful.

Transfer method 1:

The code is as follows

1 import org.web3j.crypto.Credentials; 2 import org.web3j.crypto.RawTransaction; 3 import org.web3j.crypto.TransactionEncoder; 4 import org.web3j.protocol.Web3j; 5 import org.web3j.protocol.core.DefaultBlockParameterName; 6 import org.web3j.protocol.core.methods.response.EthGetTransactionCount; 7 import org.web3j.protocol.core.methods.response.EthSendTransaction; 8 import org.web3j.protocol.http.HttpService; 9 import org.web3j.utils.Convert;10 import org.web3j.utils.Numeric;11 12 import java.math.BigInteger 13 14 15 public class TransactionTest {16 public static void main (String [] args) throws Exception {17 / / the miner fee required for setting up 18 BigInteger GAS_PRICE = BigInteger.valueOf (22,000000L); 19 BigInteger GAS_LIMIT = BigInteger.valueOf (4000000000) 20 21 / / the kovan test environment is called, and the client 22 Web3j web3j = Web3j.build is used here ("https://kovan.infura.io/"));23 / / transferor account address 24 String ownAddress =" 0xD1c82c71cC567d63Fd53D5B91dcAC6156E5B96B3 "; 25 / / transferee account address 26 String toAddress =" 0x6e27727bbb9f0140024a62822f013385f4194999 ") 27 / / the transferor's private key 28 Credentials credentials = Credentials.create ("xxxxxxxxxxxxx"); 29 / / Credentials credentials = WalletUtils.loadCredentials (30 / / "123", 31 / / "src/main/resources/UTC--2018-03-01T05-53-37.043Z--d1c82c71cc567d63fd53d5b91dcac6156e5b96b3") 32 33 / / getNonce (I don't quite understand the Nonce here, probably the number of transactions) 34 EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount (35 ownAddress, DefaultBlockParameterName.LATEST). SendAsync (). Get (); 36 BigInteger nonce = ethGetTransactionCount.getTransactionCount () 37 38 / / create a transaction, here is to transfer 0.5 etheric currency 39 BigInteger value = Convert.toWei ("0.5", Convert.Unit.ETHER). ToBigInteger (); 40 RawTransaction rawTransaction = RawTransaction.createEtherTransaction (41 nonce, GAS_PRICE, GAS_LIMIT, toAddress, value); 42 43 / signature Transaction, here to sign the transaction 44 byte [] signedMessage = TransactionEncoder.signMessage (rawTransaction, credentials) 45 String hexValue = Numeric.toHexString (signedMessage); 46 47 / send transaction 48 EthSendTransaction ethSendTransaction = 49 web3j.ethSendRawTransaction (hexValue). SendAsync (). Get (); 50 String transactionHash = ethSendTransaction.getTransactionHash (); 51 52 / / once you get the transactionHash, you can check the status of the transaction on the website of Ethernet Square. 53 System.out.println (transactionHash); 54} 55}

Note:

The above transaction code is offline transaction, first assemble the transaction, and then send it to the chain. Web3j provides online transaction, but this kind of transaction requires parity wallet. Download the complete blockchain wallet and bind the account into it.

1. Lines 27-31, you can obtain the trust credentials of the address in two ways, one is to use the private key directly, and the other is to use the keystore file.

2. Https://kovan.etherscan.io/tx/0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304

Your transaction can be found at this address.

0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304 is transactionHash.

This address is the test address. If you need to query the address on the main network, delete kovan.

Transfer method 2:

Import org.web3j.crypto.Credentials;import org.web3j.protocol.Web3j;import org.web3j.protocol.core.methods.response.TransactionReceipt;import org.web3j.protocol.http.HttpService;import org.web3j.tx.Transfer;import org.web3j.utils.Convert;import java.math.BigDecimal;public class TransactionTest2 {public static void main (String [] args) throws Exception {Web3j web3j = Web3j.build (new HttpService ("https://kovan.infura.io/")); String ownAddress =" 0xD1c82c71cC567d63Fd53D5B91dcAC6156E5B96B3 ") String toAddress = "0x6e27727bbb9f0140024a62822f013385f4194999"; Credentials credentials = Credentials.create ("xxxxxxxx"); TransactionReceipt transactionReceipt = Transfer.sendFunds (web3j, credentials, toAddress, BigDecimal.valueOf (0.2), Convert.Unit.ETHER). Send (); System.out.println (transactionReceipt.getTransactionHash ());}}

Be careful

This is also offline, but the amount of code is relatively small.

Note:

If there is no Ethernet currency in the kovan environment, you can ask for it from https://gitter.im/kovan-testnet/faucet. After registering the account directly, send your account address to the group. You will transfer money in a few minutes. The account address of the main network is the same as kovan, but the currency in it is not the same.

These are all the contents of the article "what are the ways of web3j transfer?" Thank you for your reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report