-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathEthInfo.java
More file actions
67 lines (60 loc) · 2.13 KB
/
EthInfo.java
File metadata and controls
67 lines (60 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.ethjava;
import com.ethjava.utils.Environment;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
public class EthInfo {
private static Web3j web3j;
public static void main(String[] args) {
web3j = Web3j.build(new HttpService(Environment.RPC_URL));
getEthInfo();
}
/**
* 请求区块链的信息
*/
private static void getEthInfo() {
Web3ClientVersion web3ClientVersion = null;
try {
//客户端版本
web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println("clientVersion " + clientVersion);
//区块数量
EthBlockNumber ethBlockNumber = web3j.ethBlockNumber().send();
BigInteger blockNumber = ethBlockNumber.getBlockNumber();
System.out.println(blockNumber);
//挖矿奖励账户
EthCoinbase ethCoinbase = web3j.ethCoinbase().send();
String coinbaseAddress = ethCoinbase.getAddress();
System.out.println(coinbaseAddress);
//是否在同步区块
EthSyncing ethSyncing = web3j.ethSyncing().send();
boolean isSyncing = ethSyncing.isSyncing();
System.out.println(isSyncing);
//是否在挖矿
EthMining ethMining = web3j.ethMining().send();
boolean isMining = ethMining.isMining();
System.out.println(isMining);
//当前gas price
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
BigInteger gasPrice = ethGasPrice.getGasPrice();
System.out.println(gasPrice);
//挖矿速度
EthHashrate ethHashrate = web3j.ethHashrate().send();
BigInteger hashRate = ethHashrate.getHashrate();
System.out.println(hashRate);
//协议版本
EthProtocolVersion ethProtocolVersion = web3j.ethProtocolVersion().send();
String protocolVersion = ethProtocolVersion.getProtocolVersion();
System.out.println(protocolVersion);
//连接的节点数
NetPeerCount netPeerCount = web3j.netPeerCount().send();
BigInteger peerCount = netPeerCount.getQuantity();
System.out.println(peerCount);
} catch (IOException e) {
e.printStackTrace();
}
}
}