The Conflux Java SDK allows any Java client to interact with a local or remote Conflux node based on JSON-RPC 2.0 protocol. With Conflux Java SDK, user can easily manage accounts, send transactions, deploy smart contracts and query blockchain information.
Use AccountManager
to manage accounts at local machine.
- Create/Import/Update/Delete an account.
- List all accounts.
- Unlock/Lock an account.
- Sign a transaction.
Use Cfx
interface to query Conflux blockchain information, such as block, epoch, transaction, receipt. Following is an example to query the current epoch number:
package conflux.sdk.examples;
import java.math.BigInteger;
import conflux.web3j.Cfx;
public class App {
public static void main(String[] args) throws Exception {
Cfx cfx = Cfx.create("http://mainnet-jsonrpc.conflux-chain.org:12537", 3, 1000);
BigInteger epoch = cfx.getEpochNumber().sendAndGet();
System.out.println("Current epoch: " + epoch);
}
}
Cfx
interface provides a factory method to create an instance, and allow client to enable auto-retry mechanism in case of temporary IO errors.
To send a transaction, you need to sign the transaction at local machine, and send the signed transaction to local or remote Conflux node.
- Sign a transaction with unlocked account:
AccountManager.signTransaction(RawTransaction tx, String address)
- Sign a transaction with passphrase for locked account:
AccountManager.signTransaction(RawTransaction tx, String address, String password)
- Send a signed transaction
Cfx.sendRawTransaction(String signedTx)
There are three types of address in Conflux:
- User address: starts with
0x1
. - Contract address: starts with
0x8
. - Internal contract: starts with
0x0
.
Conflux Java SDK provides some utilities to validate or format address, please refer to Address
and AddressType
for more details.
The underlying library Web3j
also provides API to convert address to a checksumed format.
Keys.toChecksumAddress(String address)
The conflux-rust
fullnode support PubSub through websocket, the default port is 12535, you need open it manually.
Now the SDK provide three methods subscribeNewHeads
, subscribeLogs
, subscribeEpochs
you can use to sub respect events.
// initiate a WebSocketService and connect, then use it to create a Cfx
WebSocketService wsService = new WebSocketService("ws://localhost:12535/", false);
wsService.connect();
Cfx cfx = Cfx.create(wsService);
// Invoke cfx method
BigInteger epoch = cfx.getEpochNumber().sendAndGet();
System.out.println("Current epoch: " + epoch);
// PubSub Subscribe to incoming events and process incoming events
final Flowable<NewHeadsNotification> events = cfx.subscribeNewHeads();
final Disposable disposable = events.subscribe(event -> {
// You can get the detail through getters
System.out.println(event.getParams().getResult());
});
// close
disposable.dispose();
Conflux Java SDK also provides some helpful tools:
Account
: used for a single account to send multiple transactions and managenonce
automatically.CfxUnit
: provides utilities for unit conversion.ContractCall
: query contract data without ABI file.DecodeUtil
andTupleDecoder
: provides utilities for ABI decode.Recall
: diagnose failed transactions.