SoLibrary
  • Solana
  • The Meme Coin Problem and Solution
  • Developing on Solana
  • From Rust To Deployment
  • Solana Blockchain Explorer
  • Building a Solana dAPP
  • Deploying a Solana dApp
  • Deploying a Solana Memecoin using CLI
  • Solana Smart Contracts
  • Send Solana via javascript functions
  • Candy Machine
  • Pump fun APIs
  • Metaplex
  • Metaplex Program Library
  • Solana Program Library
  • UMI Framework
  • Umi and Web3js Differences
  • Fetching Accounts
  • UMI Helpers
  • HTTP Requests
  • Umi's Interfaces
  • Interface implementations
  • Kinobi
  • UMI Plugins
  • Registering Programs
  • Public keys and Signers
  • Connecting RPCS
  • Serializer
  • Storage
  • Transactions
  • Web 3.JS Adapters
  • Metaplex Umi Plugins
  • Core JS SDK v1.0
  • Local Validator
  • SolScriptions
  • FAQ
  • Initialize
  • Write Inscription Data
  • Fetch
  • Clear
  • Close
  • Authority
  • Sharding
  • Getting Started using JavaScript
  • Getting started using the Inscriptions CLI
  • Core Candy Machine
  • Getting Started using JavaScript
  • Candy Guard
  • Assets
  • Creating a Core Candy Machine
  • Inserting Items
  • Updating The Core Candy Machine
  • Guard Groups
  • Special Guard Instructions
  • Fetching a Core Candy Machine
  • Minting
  • Withdrawing a Core Candy Machine
  • Address Gate Guard
  • Allocation
  • Allowlist Guard
  • Asset Burn Guard
  • Asset Burn Multi
  • Asset Payment Guard
  • Asset Payment Multi
  • Asset Mint Limit
  • Bot Tax Guard
  • End Date Guard
  • Edition
  • Freeze Sol Payment guard
  • Freeze Token Payment Guard
  • Gatekeeper Guard
  • Mint Limit Guard
  • NFT Burn Guard
  • NFT Gate Guard
  • NFT Mint Limit Guard
  • NFT Payment Guard
  • Program Gate Guard
  • Redeemed Amount Guard
  • Sol Fixed Fee Guard
  • Sol Payment Guard
  • Start Date Guard
  • Third Party Signer Guard
  • Token Burn Guard
  • Token Gate Guard
  • Token Payment Guard
  • Token2022 Payment Guard
  • Generating Custom Guard Client for Core Candy Machine
Powered by GitBook
On this page
  • Getting the RPC's endpoint and cluster
  • Sending transactions
  • Fetching accounts
  • Airdropping SOL on supported clusters
  • Getting the balance of an account
  • Getting the latest blockhash
  • Getting the most recent slot
  • Getting the rent exemption
  • Sending custom RPC requests

Connecting RPCS

Contacting the Solana blockchain via an RPC is an important part of any decentralized application. Umi provides a RpcInterface that helps us do just that.

Configuring the RPC's endpoint

When creating a new Umi instance via the default bundle, you must pass the RPC's endpoint or an instance of @solana/web3.js's Connection class as the first argument. Going forward, this is the endpoint or Connection that will be used every time you call a method on the RPC interface.

import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { Connection } from '@solana/web3.js';

// Pass in your RPC endpoint.
const umi = createUmi("https://api.mainnet-beta.solana.com");

// Or an explicit Connection instance from web3.js.
const umi = createUmi(new Connection("https://api.mainnet-beta.solana.com"));

Alternatively, you may set or update the RPC implementation explicitly by the using the plugin they provide. For instance, the web3JsRpc plugin will set the RPC implementation to use the @solana/web3.js library.

import { web3JsRpc } from '@metaplex-foundation/umi-rpc-web3js';
import { Connection } from '@solana/web3.js';

umi.use(web3JsRpc("https://api.mainnet-beta.solana.com"));
umi.use(web3JsRpc(new Connection("https://api.mainnet-beta.solana.com")));

Getting the RPC's endpoint and cluster

Once an RPC implementation has been set, you may access its endpoint and cluster via the following methods:

const endpoint = umi.rpc.getEndpoint();
const cluster = umi.rpc.getCluster();

Where cluster is one of the following:

type Cluster = "mainnet-beta" | "devnet" | "testnet" | "localnet" | "custom"

Sending transactions

The following methods can be used to send, confirm and fetch transactions:

const signature = await umi.rpc.sendTransaction(myTransaction);
const confirmResult = await umi.rpc.confirmTransaction(signature, { strategy });
const transaction = await umi.rpc.getTransaction(signature);

Fetching accounts

The following methods can be used to fetch accounts or check for their existence:

const accountExists = await umi.rpc.accountExists(myPublicKey);
const maybeAccount = await umi.rpc.getAccount(myPublicKey);
const maybeAccounts = await umi.rpc.getAccounts(myPublicKeys);
const accounts = await umi.rpc.getProgramAccounts(myProgramId, { filters });

Airdropping SOL on supported clusters

If the used cluster supports airdrops, you can use the following method to send SOL to an account and confirm the request.

// Send 1.5 SOL to "myPublicKey" and wait for the transaction to be confirmed.
await umi.rpc.airdrop(myPublicKey, sol(1.5));

Getting the balance of an account

const balance = await umi.rpc.getBalance(myPublicKey);

Getting the latest blockhash

You may get the latest blockhash with its expiry block height via the following method:

const { blockhash, lastValidBlockHeight } = await umi.rpc.getLatestBlockhash();

Getting the most recent slot

You may get the most recent slot as a number via the following method:

const recentSlot = await umi.rpc.getSlot();

Getting the rent exemption

If you need to figure out the storage fees for an account, you may use the getRent method and pass in the amount bytes that the account's data will require. This will return the rent-exemption fee — a.k.a storage fee — as a SolAmount.

const rent = await umi.rpc.getRent(100);

Note that this will automatically take the size of the account header into consideration so you only need to pass in the bytes of the account's data.

Say you now wanted to get the rent-exemption fee for 3 accounts with 100 bytes of data each. Running umi.rpc.getRent(100 * 3) will not provide an accurate response since it will only add the account header for one account and not three. This is why Umi allows you to pass in the account header size explicitly by setting the includesHeaderBytes option to true.

const rent = await umi.rpc.getRent((ACCOUNT_HEADER_SIZE + 100) * 3, {
  includesHeaderBytes: true
});

Sending custom RPC requests

Because each RPC endpoint may provide their own custom methods, Umi allows you to send custom requests to the RPC via the call method. It takes the method name as the first argument and an optional array of parameters as the second argument.

const rpcResult = await umi.rpc.call("myCustomMethod", [myFirstParam, mySecondParam]);
PreviousPublic keys and SignersNextSerializer

Last updated 10 months ago

Since transactions are an important component of Solana clients, we discuss them in more detail on the documentation page.

Since fetching accounts is one of the most common operations, we discuss it in more detail on the documentation page.

You may use the following method to get the SOL balance of any account. This will return a SolAmount object .

Sending transactions
Fetching accounts
as documented here