Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class SolanaRpc

  1. Object
  2. JsonRpc
  3. SolanaRpc

A Solana JSON-RPC client: balances, the recent blockhash a transaction needs, rent, token balances, sending and confirmation.

SolanaRpc sol = new SolanaRpc(new NetworkTransport(), SolanaRpc.DEVNET);
Wallet wallet = Wallet.fromMnemonic(Mnemonic.generate(128), "");
SolanaAddress me = wallet.getSolanaAddress(0);
List<Instruction> instructions = new ArrayList<Instruction>();
instructions.add(SystemProgram.transfer(me, me, 1000000L));
SolanaMessage m = SolanaMessage.compile(me, instructions, sol.getLatestBlockhash());
SolanaTransaction tx = new SolanaTransaction(m);
wallet.signSolanaTransaction(tx, 0);
String signature = sol.sendTransaction(tx);

Reads use the commitment given to the constructor; confirmed by default.

Fields

public static final String MAINNET = "https://api.mainnet-beta.solana.com"The public mainnet-beta endpoint, rate limited.
public static final String DEVNET = "https://api.devnet.solana.com"The public devnet endpoint.
public static final String TESTNET = "https://api.testnet.solana.com"The public testnet endpoint.

Constructors

public SolanaRpc(RpcTransport transport, String url)A client reading at confirmed.
public SolanaRpc(RpcTransport transport, String url, String commitment)A client reading at a commitment: processed, confirmed or finalized.

Methods

public long getBalance(SolanaAddress address) throws IOExceptiongetBalance in lamports.
public String getLatestBlockhash() throws IOExceptiongetLatestBlockhash: the Base58 blockhash for a new message.
public long getLastValidBlockHeight() throws IOExceptionThe block height after which a transaction using the latest blockhash expires.
public long getMinimumBalanceForRentExemption(long space) throws IOExceptiongetMinimumBalanceForRentExemption for an account of space bytes.
public String getTokenAccountBalance(SolanaAddress tokenAccount) throws IOExceptiongetTokenAccountBalance: the raw amount in base units, as the decimal string the node answers (a token amount may exceed a signed long).
public boolean accountExists(SolanaAddress address) throws IOExceptionWhether an account exists, which a sender checks before paying to create the recipient’s token account.
public String sendTransaction(SolanaTransaction signed) throws IOExceptionsendTransaction with preflight at this client’s commitment, answering the signature (the transaction id).
public String getSignatureStatus(String signature) throws IOExceptiongetSignatureStatuses for one signature: null when the cluster has not seen it, otherwise its confirmationStatus (processed, confirmed, finalized), or "failed" when it landed with an error.
public String requestAirdrop(SolanaAddress to, long lamports) throws IOExceptionrequestAirdrop on devnet or testnet, answering the airdrop’s signature.

Inherited methods

Field details

MAINNET

public static final String MAINNET = "https://api.mainnet-beta.solana.com"
The public mainnet-beta endpoint, rate limited.

DEVNET

public static final String DEVNET = "https://api.devnet.solana.com"
The public devnet endpoint.

TESTNET

public static final String TESTNET = "https://api.testnet.solana.com"
The public testnet endpoint.

Constructor details

SolanaRpc

public SolanaRpc(RpcTransport transport, String url)
A client reading at confirmed.

SolanaRpc

public SolanaRpc(RpcTransport transport, String url, String commitment)
A client reading at a commitment: processed, confirmed or finalized.

Method details

getBalance

public long getBalance(SolanaAddress address) throws IOException
getBalance in lamports.

getLatestBlockhash

public String getLatestBlockhash() throws IOException
getLatestBlockhash: the Base58 blockhash for a new message.

getLastValidBlockHeight

public long getLastValidBlockHeight() throws IOException
The block height after which a transaction using the latest blockhash expires.

getMinimumBalanceForRentExemption

public long getMinimumBalanceForRentExemption(long space) throws IOException
getMinimumBalanceForRentExemption for an account of space bytes.

getTokenAccountBalance

public String getTokenAccountBalance(SolanaAddress tokenAccount) throws IOException
getTokenAccountBalance: the raw amount in base units, as the decimal string the node answers (a token amount may exceed a signed long).

accountExists

public boolean accountExists(SolanaAddress address) throws IOException
Whether an account exists, which a sender checks before paying to create the recipient’s token account.

sendTransaction

public String sendTransaction(SolanaTransaction signed) throws IOException
sendTransaction with preflight at this client’s commitment, answering the signature (the transaction id).

getSignatureStatus

public String getSignatureStatus(String signature) throws IOException
getSignatureStatuses for one signature: null when the cluster has not seen it, otherwise its confirmationStatus (processed, confirmed, finalized), or "failed" when it landed with an error.

requestAirdrop

public String requestAirdrop(SolanaAddress to, long lamports) throws IOException
requestAirdrop on devnet or testnet, answering the airdrop’s signature.