This project implements the SDK to interact with a Casper Node. It wraps the Json-RPC requests and maps the results to Java objects.
- Java 8
- Gradle
- crypto-keys
./gradlew build
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>
casperService = CasperService.usingPeer("127.0.0.1","7777");
Retrieve block info by a block identifier
JsonBlockData result = casperService.getBlock();
JsonBlockData result = casperService.getBlock(new HeightBlockIdentifier(1234));
JsonBlockData blockData = casperService.getBlock(new HashBlockIdentifier("--hash--"));
Retrieve block transfers by a block identifier
TransferData transferData = casperService.getBlockTransfers();
TransferData transferData = casperService.getBlockTransfers(new HeightBlockIdentifier(1234));
TransferData transferData = casperService.getBlockTransfers(new HashBlockIdentifier("--hash--"));
Retrieve the state root hash given the BlockIdentifier
StateRootHashData stateRootData = casperService.getStateRootHash();
StateRootHashData stateRootData = casperService.getStateRootHash(new HeightBlockIdentifier(1234));
StateRootHashData stateRootData = casperService.getStateRootHash(new HashBlockIdentifier("--hash--"));
4. Query deploy
Get a Deploy from the network
DeployData deployData = casperService.getDeploy("--hash--");
5. Query peers
Get network peers data
PeerData peerData = casperService.getPeerData();
Retrieve a stored value from the network
StoredValueData result = casperService.getStateItem("--stateRootHash--", "key", Arrays.asList("The path components starting from the key as base"));
Return the current status of the node
StatusData status = casperService.getStatus()
Returns an Account from the network
AccountData account = casperService.getStateAccountInfo("--publicKey--", new HeightBlockIdentifier(1234));
AccountData account = casperService.getStateAccountInfo("--publicKey--", new HashBlockIdentifier("--hash--"));
Returns the Auction info for a given block
AuctionData auction = casperService.getStateAuctionInfo(new HeightBlockIdentifier(1234));
AuctionData auction = casperServiceMainnet.getStateAuctionInfo(new HashBlockIdentifier("--hash--"));
Returns an EraInfo from the network
EraInfoData eraInfoData = casperService.getEraInfoBySwitchBlock(new HeightBlockIdentifier(1234));
EraInfoData eraInfoData = casperService.getEraInfoBySwitchBlock(new HashBlockIdentifier("--hash--"));
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);
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.
// 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);
}
});
// 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...
}
}
});