Skip to content

Latest commit

 

History

History
86 lines (63 loc) · 3.17 KB

getting-started.md

File metadata and controls

86 lines (63 loc) · 3.17 KB

Getting started

The java-conflux-sdk can be used to interact with ConfluxNetwork and is very easy to use.

Invoke RPC methods

Conflux node(or client) provide JSON-RPC to enable developer get chain info and send transactions. The java-conflux-sdk provide methods to talk with Conflux node JSON-RPC method.

Init client

To interact with Conflux node, an RPC endpoint is needed. You can run your own a node with RPC opened, or use the public RPC endpoint provide by Conflux fund:

Then create a java Cfx client, which can be used to invoke RPC methods.

import conflux.web3j.Cfx;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws Exception {
        try {
            System.out.println("Hello conflux-web3j");
            Cfx cfx = Cfx.create("https://test.confluxrpc.com");
            BigInteger currentEpochNumber = cfx.getEpochNumber().sendAndGet();
            System.out.printf("Current epoch number %d \n", currentEpochNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Common RPC methods

The Cfx client can be used to get a lot of blockchain info from node, including the latest epochNumber, block, account balance and nonce, transaction, receipt etc.

Get account balance and nonce

import conflux.web3j.types.Address;

Address addr = new Address("cfxtest:aak2rra2njvd77ezwjvx04kkds9fzagfe6d5r8e957");
BigInteger balance = cfx.getBalance(addr).sendAndGet();
// nonce will be used when sending transaction 
BigInteger nonce = cfx.getNonce(addr).sendAndGet();

Get block

import conflux.web3j.request.Epoch;
import conflux.web3j.response.Block;
import conflux.web3j.response.BlockSummary;

Epoch epoch = Epoch.numberOf(1000000);
Optional<BlockSummary> b1 = cfx.getBlockSummaryByEpoch(epoch).sendAndGet();
// block may get failed
boolean blockRetriveSuccess = b1.isPresent();
Optional<Block> blockWithTxDetail = cfx.getBlockByEpoch(epoch).sendAndGet();

String blockHash = "0xe27f5f566d3f450855e0455ae84c6723ebb477891ffa3ee68af9be518d5b150c";
Optional<BlockSummary> b3 = cfx.getBlockSummaryByHash(blockHash).sendAndGet();

Get transaction and receipt

import conflux.web3j.response.Receipt;
import conflux.web3j.response.Transaction;

String txhash = "0x1aed92e97aa70dbc629ae37879915340f47b936a15529bd1e3952783a2efbfcd";
Optional<Transaction> tx = cfx.getTransactionByHash(txhash).sendAndGet();
Optional<Receipt> receipt = cfx.getTransactionReceipt(txhash).sendAndGet();

For complete methods check fullnode JSONRPC doc and Cfx API doc

Base32 address

Conflux use base32 encoded address like cfxtest:aak2rra2njvd77ezwjvx04kkds9fzagfe6d5r8e957. The java SDK provide a class conflux.web3j.types.Address which can be used to construct, encode, decode address. For detail usage check here