Casper Java SDK
This project implements the SDK to interact with a Casper Node. It wraps the Json-RPC requests and maps the results to Java objects.
Dependencies
- Java 8
- Gradle
- crypto-keys
Build instructions
./gradlew build
Including the library
Using gradle:
implementation 'network.casper:casper-java-sdk:2.4.1'
Using maven:
<dependency>
<groupId>network.casper</groupId>
<artifactId>casper-java-sdk</artifactId>
<version>2.4.1</version>
</dependency>
How to
Set-up a connection
1.casperService = CasperService.usingPeer("127.0.0.1","7777");
2. Query a block
Retrieve block info by a block identifier
Last block
JsonBlockData result = casperService.getBlock();
By height
JsonBlockData result = casperService.getBlock(new HeightBlockIdentifier(1234));
By hash
JsonBlockData blockData = casperService.getBlock(new HashBlockIdentifier("--hash--"));
3. Query transfers
Retrieve block transfers by a block identifier
Last block
TransferData transferData = casperService.getBlockTransfers();
By block height
TransferData transferData = casperService.getBlockTransfers(new HeightBlockIdentifier(1234));
By block hash
TransferData transferData = casperService.getBlockTransfers(new HashBlockIdentifier("--hash--"));
3. Query state root hash
Retrieve the state root hash given the BlockIdentifier
Last block
StateRootHashData stateRootData = casperService.getStateRootHash();
By block height
StateRootHashData stateRootData = casperService.getStateRootHash(new HeightBlockIdentifier(1234));
By block hash
StateRootHashData stateRootData = casperService.getStateRootHash(new HashBlockIdentifier("--hash--"));
Query deploy
4.Get a Deploy from the network
DeployData deployData = casperService.getDeploy("--hash--");
Query peers
5.Get network peers data
PeerData peerData = casperService.getPeerData();
Query stored value
6.Retrieve a stored value from the network
StoredValueData result = casperService.getStateItem("--stateRootHash--", "key", Arrays.asList("The path components starting from the key as base"));
Get node status
7.Return the current status of the node
StatusData status = casperService.getStatus()
8. Get account info
Returns an Account from the network
By block height
AccountData account = casperService.getStateAccountInfo("--publicKey--", new HeightBlockIdentifier(1234));
By block hash
AccountData account = casperService.getStateAccountInfo("--publicKey--", new HashBlockIdentifier("--hash--"));
9. Get auction info
Returns the Auction info for a given block
By block height
AuctionData auction = casperService.getStateAuctionInfo(new HeightBlockIdentifier(1234));
By block hash
AuctionData auction = casperServiceMainnet.getStateAuctionInfo(new HashBlockIdentifier("--hash--"));
10. Get era info
Returns an EraInfo from the network
By block height
EraInfoData eraInfoData = casperService.getEraInfoBySwitchBlock(new HeightBlockIdentifier(1234));
By block hash
EraInfoData eraInfoData = casperService.getEraInfoBySwitchBlock(new HashBlockIdentifier("--hash--"));
11. Deploy
Transfering CSPR
Deploy deploy = CasperDeployService.buildTransferDeploy(from, to,
BigInteger.valueOf(2500000000L), "casper-test",
id, BigInteger.valueOf(100000000L), 1L, ttl, new Date(),
new ArrayList<>());
DeployResult deployResult = casperServiceTestnet.putDeploy(deploy);
12. Consuming Events
The Java SDK supports the consumption of casper events using the event service API. This API allows the consumer to choose the events to be provided as Pojos or as raw JSON via the EventTarget enum values. Each event stream is consumed individually by providing the required stream (main, sigs, and deploys) using the EventType parameter to the consumeEvents method.
For more information on events see: Monitoring and Consuming Events.
Consuming Raw JSON Event Strings
// Construct an events service
final EventService eventService = EventService.usingPeer(new URI("http://localhost:28101"));
// Consume the main events as raw JSON
eventService.consumeEvents(EventType.MAIN, EventTarget.RAW, 0L, new EventConsumer<String>(){
@Override
public void accept(final Event<String> event) {
// Obtain the raw JSON event as a String
final String json = event.getData();
// Obtain the optional event ID
final long id = event.getId().orElse(0L);
}
});
Consuming Pojo Events
// Construct an events service
final EventService eventService = EventService.usingPeer(new URI("http://localhost:28101"));
// Consume the main events as Casper Java SDK Pojos
eventService.consumeEvents(EventType.MAIN, EventTarget.POJO, 0L, new EventConsumer<EventData>() {
@Override
public void accept(final Event<EventData> event) {
switch (event.getDataType()) {
case BLOCK_ADDED:
handleBlockAdded(event.getId().get(), ((BlockAdded) event.getData()));
break;
// And so on...
}
}
});