WEB3 transfer function
In order to complete the Ethereum transaction, there must be a few prerequisites
1, the other's ethereum address
2. Determine the amount to transfer
3, the transfer of their own address rights
4, more than the transfer amount of the etheric currency, the ether transfer is actually a transaction, the miners will help you calculate, calculate the completion of the transaction, but the miners calculate the need to pay a certain amount (gas), pay too little, calculate the transfer can be slow or unsuccessful.
Transfer Method 1:
The code is as follows
1 Importorg.web3j.crypto.Credentials;2 Importorg.web3j.crypto.RawTransaction;3 ImportOrg.web3j.crypto.TransactionEncoder;4 Importorg.web3j.protocol.Web3j;5 ImportOrg.web3j.protocol.core.DefaultBlockParameterName;6 ImportOrg.web3j.protocol.core.methods.response.EthGetTransactionCount;7 Importorg.web3j.protocol.core.methods.response.EthSendTransaction;8 ImportOrg.web3j.protocol.http.HttpService;9 ImportOrg.web3j.utils.Convert;Ten ImportOrg.web3j.utils.Numeric; One A ImportJava.math.BigInteger; - - the Public classTransactiontest { - Public Static voidMain (string[] args)throwsException { - //set up the required miner's fee -BigInteger Gas_price =biginteger.valueof (22_000_000_000l); +BigInteger Gas_limit =biginteger.valueof (4_300_000); - + //The Kovan test environment is called, and the Infura client is used here. Aweb3j web3j = Web3j.build (NewHttpservice ("https://kovan.infura.io/<your-token>")); at //account address of the transfer person -String ownaddress = "0xd1c82c71cc567d63fd53d5b91dcac6156e5b96b3"; - //account address of the person to be transferred -String toaddress = "0x6e27727bbb9f0140024a62822f013385f4194999"; - //Transfer Person private key -Credentials Credentials = credentials.create ("xxxxxxxxxxxxx"); in //Credentials Credentials = walletutils.loadcredentials ( - //"123", to //"Src/main/resources/utc--2018-03-01t05-53-37.043z--d1c82c71cc567d63fd53d5b91dcac6156e5b96b3"); + - //Getnonce (The nonce here I do not quite understand, it is probably the number of transactions) theEthgettransactioncount Ethgettransactioncount =Web3j.ethgettransactioncount ( * ownaddress, Defaultblockparametername.latest). SendAsync (). get (); $BigInteger nonce =Ethgettransactioncount.gettransactioncount ();Panax Notoginseng - //Create a transaction, here is a transfer of 0.5 etheric coins theBigInteger value = Convert.towei ("0.5", Convert.Unit.ETHER). Tobiginteger (); +Rawtransaction rawtransaction =Rawtransaction.createethertransaction ( A nonce, Gas_price, Gas_limit, toaddress, value); the + //signature transaction, here to sign the deal. - byte[] Signedmessage =Transactionencoder.signmessage (rawtransaction, credentials); $String Hexvalue =numeric.tohexstring (signedmessage); $ - //Send a transaction -Ethsendtransaction ethsendtransaction = the web3j.ethsendrawtransaction (hexvalue). SendAsync (). get (); -String Transactionhash =Ethsendtransaction.gettransactionhash ();Wuyi the //once you get to Transactionhash, you can check the status of the deal on the Ethereum website . - System.out.println (transactionhash); Wu } -}
Attention:
The above transaction code is an offline transaction, the first assembly of the transaction, then sent to the chain, web3j to provide online trading, but this transaction requires parity wallet, the full blockchain wallet download, and then bind the account in.
1, chapters 27–31 line, you can use two ways to obtain the address of the trust credential, one is to use the private key directly, one is to use the KeyStore file
2, https://kovan.etherscan.io/tx/0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304
This address is for you to find this deal.
0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304 is Transactionhash.
This address is the test address, if you need to query the main network, delete Kovan
Transfer Method 2:
Importorg.web3j.crypto.Credentials;Importorg.web3j.protocol.Web3j;ImportOrg.web3j.protocol.core.methods.response.TransactionReceipt;ImportOrg.web3j.protocol.http.HttpService;ImportOrg.web3j.tx.Transfer;ImportOrg.web3j.utils.Convert;ImportJava.math.BigDecimal; Public classTransactionTest2 { Public Static voidMain (string[] args)throwsException {web3j web3j= Web3j.build (NewHttpservice ("https://kovan.infura.io/<your-token>")); 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 ()); }}
Attention
This is also offline, but the amount of code is smaller.
Note:
If there is no etheric currency in the Kovan environment, you can go to https://gitter.im/kovan-testnet/faucet here to want, directly registered account, your account address sent to the group on the line, in a few minutes will be transferred to you money, The account address of the main network is the same as the Kovan, but the coin inside is not the same.
"WEB3" Transaction transfer