Skip to content

casper-network/casper-java-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java CI GitHub tag (latest SemVer) Project license

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

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

casperService = CasperService.usingPeer("127.0.0.1","7777");

2. Query a block

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--"));

3. Query transfers

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--"));

3. Query state root 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--"));

Get a Deploy from the network

DeployData deployData = casperService.getDeploy("--hash--");

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()

8. Get account info

Returns an Account from the network

AccountData account = casperService.getStateAccountInfo("--publicKey--", new HeightBlockIdentifier(1234));
AccountData account = casperService.getStateAccountInfo("--publicKey--", new HashBlockIdentifier("--hash--"));

9. Get auction info

Returns the Auction info for a given block

AuctionData auction = casperService.getStateAuctionInfo(new HeightBlockIdentifier(1234));
AuctionData auction = casperServiceMainnet.getStateAuctionInfo(new HashBlockIdentifier("--hash--"));

10. Get era info

Returns an EraInfo from the network

EraInfoData eraInfoData = casperService.getEraInfoBySwitchBlock(new HeightBlockIdentifier(1234));
EraInfoData eraInfoData = casperService.getEraInfoBySwitchBlock(new HashBlockIdentifier("--hash--"));

11. Deploy

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...    
        }
    }
});