ETH转账

一:最快,最方便的请求 这种方式是web3j自己封装好的方法,但是无法指定矿工费,如果没有矿工费这方面需求,直接用这个是最方便的。

//默认转账,不设置矿工费 public static void transfer(String toAddress,String fromKey,double value){ //通过提供的Transfer进行转账 TransactionReceipt transactionReceipt = null; //转出账户 Credentials credentials1= Credentials.create(fromKey); try { transactionReceipt = Transfer.sendFunds( web3j, credentials1, toAddress, BigDecimal.valueOf(value), Convert.Unit.ETHER).send(); } catch (Exception e) { e.printStackTrace(); } }

其中web3j对象通过
Web3jFactory.build(new HttpService("https://rinkeby.infura.io/v3/appkey"))

appkey可通过https://infura.io/注册获取。

二:需要指定矿工费的转账 矿工费=gas Price × gas Limit;
通常 gas limit =21000;gas price =2 GWEI;
public static void transfer(String toAddress,String fromKey,double value,double gasValue,String desc){ //转出账户 Credentials credentials1= Credentials.create(fromKey); //获取当前账户的下一个有效随机数 此处的address为当前钱包的地址 try { //这个方法就是自动加的,不需要自己存了。。 EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount( credentials1.getAddress(), DefaultBlockParameterName.LATEST).send(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); BigInteger bigAmount=Convert.toWei(value+"", Convert.Unit.ETHER).toBigInteger(); BigInteger bigGas=Convert.toWei(gasValue+"", Convert.Unit.GWEI).toBigInteger(); //创建交易交易序号,gas ,gaslimit ,address,value; RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, bigGas,Convert.toWei("21000", Convert.Unit.WEI).toBigInteger(), toAddress,bigAmount); // 签名交易 并转换为16进制 byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials1); String hexValue = https://www.it610.com/article/Numeric.toHexString(signedMessage); //发送交易 发送完了或获取一个交易的hash值,这个值可以在区块链浏览器上查询当前交易的结果 EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send(); } catch (IOException e) { e.printStackTrace(); }}

三:需要添加备注的转账 如果需要添加备注,就无法使用RawTransaction.createEtherTransaction创建,因为这个方法默认传的空串;
【ETH转账】使用RawTransaction.createTransaction创建。
public static void transfer(String toAddress,String fromKey,double value,double gasValue,String desc){ //转出账户 Credentials credentials1= Credentials.create(fromKey); //获取当前账户的下一个有效随机数 此处的address为当前钱包的地址 try { //这个方法就是自动加的,不需要自己存了。。 EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount( credentials1.getAddress(), DefaultBlockParameterName.LATEST).send(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); BigInteger bigAmount=Convert.toWei(value+"", Convert.Unit.ETHER).toBigInteger(); BigInteger bigGas=Convert.toWei(gasValue+"", Convert.Unit.GWEI).toBigInteger(); //创建交易交易序号,gas ,gaslimit ,address,value; //desc 需要进行16进制转换 RawTransaction rawTransaction =RawTransaction.createTransaction(nonce,bigGas,Convert.toWei("21000", Convert.Unit.WEI).toBigInteger(), toAddress,bigAmount, FormatUtils.str2HexStr(desc)); // 签名交易 并转换为16进制 byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials1); String hexValue = https://www.it610.com/article/Numeric.toHexString(signedMessage); //发送交易 发送完了或获取一个交易的hash值,这个值可以在区块链浏览器上查询当前交易的结果 EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send(); } catch (IOException e) { e.printStackTrace(); }}


    推荐阅读