Java implementation of the Nimiq RPC client specs.
Here's a simple example that reports the info about the current head block and its miner account:
NimiqClientFactory clientFactory = new NimiqClientFactory(new URL("http://localhost:8648/"));
NimiqClient client = clientFactory.getClient();
// Wait for consensus to establish
ConsensusState consensusState;
while ((consensusState = client.getConsensusState()) != ConsensusState.ESTABLISHED) {
if (consensusState == ConsensusState.CONNECTING) {
System.out.println("Connecting to the network");
} else if (consensusState == ConsensusState.SYNCING) {
SyncingState syncingState = client.getSyncingState();
System.out.printf("Syncing at block #%d\n", syncingState.getCurrentBlock());
}
Thread.sleep(1000);
}
System.out.println("Consensus established");
// Display the head block
int blockNumber = client.getBlockNumber();
Block block = client.getBlockByNumber(blockNumber, false);
long balance = client.getBalance(block.getMinerAddress());
System.out.printf("Head block #%d, %s mined by %s who has %.5f NIM.\n", block.getNumber(), block.getHash(),
block.getMinerAddress(), NimiqUtils.lunasToCoins(balance));
See /docs or GitHub Pages.
You can also refer to the original specs.
Add the dependency to your pom.xml
:
<dependency>
<groupId>com.nimiq</groupId>
<artifactId>nimiq-rpc-client</artifactId>
<version>1.0</version>
</dependency>
mvn -DskipTests source:jar javadoc:jar install
You need a test Nimiq node started like this:
nodejs index.js --protocol=dumb --type=full --network=test --rpc
There must be a wallet funded with a few hundred NIM. To run the test:
mvn test
This implementation was originally contributed by Mat (a.k.a. Tomkha).
Please send your contributions as pull requests. Refer to the issue tracker for ideas.