Java

Java developers can easily operate on the Zetrix blockchain via the Zetrix Java SDK!

Overview

The Zetrix Java SDK empowers developers to seamlessly interact with the Zetrix blockchain. This robust toolkit provides a high-level abstraction over complex blockchain interactions, allowing you to focus on building innovative applications. With its intuitive API and comprehensive features, the SDK simplifies account management, smart contract deployment, transaction creation, block info and more.

Target Audience

The Zetrix Java SDK is designed for developers with a solid understanding of Java and intends to build an API service to interact with Zetrix. Whether you're a seasoned blockchain developer or new to the space, the SDK offers a user-friendly onramp to building on Zetrix.

Quick Start Guide

Installation

Getting started with the Zetrix Java SDK is straightforward. In this tutorial we will be using Maven for dependency management. Follow these steps to include the package in your pom.xml.

The repositories required:

<repositories>
	<repository>
		<id>bubi-release</id>
		<name>releases</name>
		<url>http://maven.bubidev.cn/repository/releases</url>
	</repository>
	<repository>
		<id>bubi-snapshot</id>
		<name>snapshots</name>
		<url>http://maven.bubidev.cn/repository/snapshots</url>
	</repository>
	<repository>
		<id>bubi-thirdparty</id>
		<name>thirdparty</name>
		<url>http://maven.bubidev.cn/repository/thirdparty</url>
	</repository>
	<repository>
		<id>danubetech-maven-public</id>
		<url>https://repo.danubetech.com/repository/maven-public/</url>
	</repository>
</repositories>

The dependencies required:

<dependencies>
	<dependency>
		<groupId>org.zetrix.sdk</groupId>
		<artifactId>zetrix-sdk</artifactId>
		<version>1.0.5.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.zetrix.encryption</groupId>
		<artifactId>zetrix-encryption</artifactId>
		<version>1.0.0.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>2.0.21</version>
	</dependency>
	<dependency>
		<groupId>net.i2p.crypto</groupId>
		<artifactId>eddsa</artifactId>
		<version>0.1.0</version>
	</dependency>
</dependencies>

Do note that the individual environments may differ. Please create an issue here if you face any problems setting up your environment for development.

Prerequisites

  • Zetrix Wallet installed.

  • A Zetrix account with sufficient balance. Claim your faucet here.

Basic Usages

Included in this section are some sample code snippets to help you get started with the SDK.

Create a Zetrix Java SDK instance to begin using the SDK.

If you're using the Zetrix testnet, initialize with this URL:

// Testnet node URL
SDK sdk = SDK.getInstance("https://test-node.zetrix.com");

If you're using the Zetrix mainnet, initialize with this URL:

// Mainnet node URL
SDK sdk = SDK.getInstance("https://node.zetrix.com");

Let's look at some examples:

Getting the nonce for a particular address:

AccountGetNonceRequest request = new AccountGetNonceRequest();
request.setAddress(address);
AccountGetNonceResponse response = sdk.getAccountService().getNonce(request);
return response.getResult().getNonce();

Submitting a transaction:

TransactionSubmitRequest transactionSubmitRequest = new TransactionSubmitRequest();
transactionSubmitRequest.setTransactionBlob(submitTxReq.getBlob());
transactionSubmitRequest.setSignatures(signatures);
return sdk.getTransactionService().submit(transactionSubmitRequest);

Getting the balance of a particular address:

AccountGetBalanceResponse response = sdk.getAccountService().getBalance(request);
return response.getResult().getBalance();

Getting transaction info:

TransactionGetInfoRequest txInfo = new TransactionGetInfoRequest();
txInfo.setHash(txHash);
TransactionGetInfoResponse respInfo = sdk.getTransactionService().getInfo(txInfo);
TransactionHistory txHistory = respInfo.getResult().getTransactions()[0];

API References

Account APIs

sdk.getAccountService().getNonce(AccountGetNonceRequest request);
  • Purpose: Retrieves the nonce of an account.

  • Parameters: AccountGetNonceRequest - Contains the address of the account to retrieve the nonce for.

  • Return: AccountGetNonceResponse - The response object containing the account's nonce.

sdk.getAccountService().getMetadata(AccountGetMetadataRequest request);
  • Purpose: Retrieves metadata for an account.

  • Parameters: AccountGetMetadataRequest - Contains the address and key of the metadata to retrieve.

  • Return: AccountGetMetadataResponse - The response object containing the account's metadata.

sdk.getAccountService().getBalance(AccountGetBalanceRequest request);
  • Purpose: Retrieves the balance of an account.

  • Parameters: AccountGetBalanceRequest - Contains the address of the account to retrieve the balance for.

  • Return: AccountGetBalanceResponse - The response object containing the account's balance.

sdk.getAccountService().getInfo(AccountGetInfoRequest request);
  • Purpose: Retrieves information about an account.

  • Parameters: AccountGetInfoRequest - Contains the address of the account to retrieve information for.

  • Return: AccountGetInfoResponse - The response object containing the account information.

sdk.getAccountService().checkValid(AccountCheckValidRequest request);
  • Purpose: Checks if an account address is valid.

  • Parameters: AccountCheckValidRequest - Contains the address to be validated.

  • Return: AccountCheckValidResponse - The response object indicating whether the address is valid.

sdk.getAccountService().create()
  • Purpose: Creates a new account by generating a new private key, public key, and address.

  • Parameters: None.

  • Return: AccountCreateResponse - The response object containing the new account's private key, public key, and address.

sdk.getAccountService().checkActivated(AccountCheckActivatedRequest request);
  • Purpose: Checks if an account is activated.

  • Parameters: AccountCheckActivatedRequst - Contains the address of the account to check.

  • Return: AccountCheckActivatedResponse - The response object indicating whether the account is activated.

Block APIs

sdk.getBlockService().getNumber();
  • Purpose: Retrieves the current block number from the blockchain.

  • Parameters: None.

  • Return: BlockGetNumberResponse - Contains the block number and any error information if applicable.

sdk.getBlockService().getInfo(BlockGetInfoRequest request);
  • Purpose: Retrieves information about a specific block.

  • Parameters: BlockGetInfoRequest - Contains the block number for which information is requested.

  • Return: BlockGetInfoResponse - Contains block information and any error information.

sdk.getBlockService().getTransactions(BlockGetTransactionsRequest request);
  • Purpose: Retrieves transactions for a specific block number.

  • Parameters: BlockGetTransactionsRequest - Contains the block number for which transactions are requested.

  • Return: BlockGetTransactionsResponse - Contains transaction details and any error information.

sdk.getBlockService().checkStatus();
  • Purpose: Checks the synchronization status of the blockchain.

  • Parameters: None.

  • Return: BlockCheckStatusResponse - Indicates whether the blockchain is synchronized and includes any error information.

sdk.getBlockService().getLatestInfo();
  • Purpose: Retrieves information about the latest block.

  • Parameters: None.

  • Return: BlockGetLatestInfoResponse - Contains the latest block information and any error information.

sdk.getBlockService().getValidators(BlockGetValidatorsRequest request);
  • Purpose: Retrieves validator information for a specific block.

  • Parameters: BlockGetValidatorsRequest - Contains the block number for which validator information is requested.

  • Return: BlockGetValidatorsResponse - Contains validator details and any error information.

sdk.getBlockService().getLatestValidators();
  • Purpose: Retrieves information about the latest validators.

  • Parameters: None.

  • Return: BlockGetLatestValidatorsResponse - Contains the latest validator information and any error information.

sdk.getBlockService().getReward(BlockGetRewardRequest request);
  • Purpose: Retrieves reward distribution information for a specific block.

  • Parameters: BlockGetRewardRequest - Contains the block number for which reward information is requested.

  • Return: BlockGetRewardResponse - Contains reward distribution details and any error information.

sdk.getBlockService().getLatestReward();
  • Purpose: Retrieves the latest reward distribution information.

  • Parameters: None.

  • Return: BlockGetLatestRewardResponse - Contains the latest reward distribution details and any error information.

sdk.getBlockService().getFees(BlockGetFeesRequest request);
  • Purpose: Retrieves fee information for a specific block.

  • Parameters: BlockGetFeesRequest - Contains the block number for which fee information is requested.

  • Return: BlockGetFeesResponse - Contains fee details and any error information.

sdk.getBlockService().getLatestFees();
  • Purpose: Retrieves the latest fee information.

  • Parameters: None.

  • Return: BlockGetLatestFeesResponse - Contains the latest fee details and any error information.

Transaction APIs

sdk.getTransactionService().buildBlob(TransactionBuildBlobRequest request);
  • Purpose: Constructs a transaction blob from the given request, which includes validating the request parameters and building the transaction.

  • Parameters: TransactionBuildBlobRequest - Contains details like source address, nonce, gas price, fee limit, metadata, and operations.

  • Return: TransactionBuildBlobResponse - Contains the transaction blob and its hash if successful, or error details if an exception occurs.

sdk.getTransactionService().parseBlob(TransactionParseBlobRequest request);
  • Purpose: Parses a transaction blob into a readable format, converting it to JSON.

  • Parameters: TransactionParseBlobRequest - Contains the transaction blob to be parsed.

  • Return: TransactionParseBlobResponse - Contains the parsed transaction details in JSON format or error details if an exception occurs.

sdk.getTransactionService().sign(TransactionSignRequest request);
  • Purpose: Signs a transaction blob using the provided private keys.

  • Parameters: TransactionSignRequest - Contains the transaction blob and an array of private keys for signing.

  • Return: TransactionSignResponse - Contains the signatures and public keys if successful, or error details if an exception occurs.

sdk.getTransactionService().submit(TransactionSubmitRequest request);
  • Purpose: Submits a signed transaction to the blockchain network.

  • Parameters: TransactionSubmitRequest - Contains the transaction blob and signatures.

  • Return: TransactionSubmitResponse - Contains the transaction hash if successful, or error details if an exception occurs.

sdk.getTransactionService().getInfo(TransactionGetInfoRequest request);
  • Purpose: Retrieves information about a transaction using its hash.

  • Parameters: TransactionGetInfoRequest - Contains the hash of the transaction to be queried.

  • Return: TransactionGetInfoResponse - Contains transaction details or error information if an exception occurs.

sdk.getTransactionService().evaluateFee(TransactionEvaluateFeeRequest request);
  • Purpose: Evaluates the fee required for a transaction based on the provided request details.

  • Parameters: TransactionEvaluateFeeRequest - Includes source address, nonce, signature number, metadata, and operations.

  • Return: TransactionEvaluateFeeResponse - Contains the evaluated fee details or error information if an exception occurs.

Contract APIs

sdk.getContractService().call(ContractCallRequest request);
  • Purpose: Calls a contract with specified parameters.

  • Parameters: ContractCallRequest - An object containing details like source address, contract address, code, input, contract balance, gas price, and fee limit.

  • Return: ContractCallResponse - The result of the contract call or error details if the call fails.

sdk.getContractService().getInfo(ContractGetInfoRequest request);
  • Purpose: Retrieves information about a specific contract using its address.

  • Parameters: ContractGetInfoRequest - An object containing the contract address.

  • Return: ContractGetInfoResponse - Contains the contract information or error details if the request fails.

sdk.getContractService().checkValid(ContractCheckValidRequest request);
  • Purpose: Checks if a given contract address is valid.

  • Parameters: ContractCheckValidRequest - An object containing the contract address to be validated.

  • Return: ContractCheckValidResponse - Indicates whether the contract address is valid or not.

sdk.getContractService().getAddress(ContractGetAddressRequest request);
  • Purpose: Retrieves the contract address associated with a given transaction hash.

  • Parameters: ContractGetAddressRequest - An object containing the transaction hash.

  • Return: ContractGetAddressResponse - Containing the contract address information or error details if the retrieval fails.

For code references on Zetrix's Java SDK, please visit our GitHub repository.

Last updated