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
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
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 IOExceptiongetBalance in lamports.Throws
getLatestBlockhash
public String getLatestBlockhash()
throws IOExceptiongetLatestBlockhash: the Base58 blockhash for a new message.Throws
getLastValidBlockHeight
public long getLastValidBlockHeight()
throws IOExceptionThe block height after which a transaction using the latest blockhash expires.
Throws
getMinimumBalanceForRentExemption
public long getMinimumBalanceForRentExemption(long space)
throws IOExceptiongetMinimumBalanceForRentExemption for an account of space bytes.Throws
getTokenAccountBalance
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).Throws
accountExists
public boolean accountExists(SolanaAddress address)
throws IOExceptionWhether an account exists, which a sender checks before paying to create the
recipient’s token account.
Throws
sendTransaction
public String sendTransaction(SolanaTransaction signed)
throws IOExceptionsendTransaction with preflight at this client’s commitment, answering the signature
(the transaction id).Throws
getSignatureStatus
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.Throws
requestAirdrop
public String requestAirdrop(SolanaAddress to, long lamports)
throws IOExceptionrequestAirdrop on devnet or testnet, answering the airdrop’s signature.