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
  • Account definitions
  • Fetching RPC accounts
  • Deserializing accounts

Fetching Accounts

Let's see how we can fetch account data from the Solana blockchain using Umi. For that, we will need the RpcInterface to fetch accounts with serialized data and serializers to help deserialize them.

Account definitions

Umi defines an account with serialized data as an RpcAccount. It contains information from the account header — i.e. the SOL on the account, the program owner, etc. — and the account's public key and serialized data.

type RpcAccount = AccountHeader & {
  publicKey: PublicKey;
  data: Uint8Array;
};

It also defines a MaybeRpcAccount type that represents an RpcAccount that may or may exist. When the account does not exist, it keeps track of its public key so that, in a list of accounts, we know which public key was not found.

type MaybeRpcAccount =
  | ({ exists: true } & RpcAccount)
  | { exists: false; publicKey: PublicKey };

When dealing with MaybeRpcAccounts, you may use the assertAccountExists helper method to assert that an account exists and fail otherwise.

assertAccountExists(myMaybeAccount);
// From now on, we know myMaybeAccount is an RpcAccount.

Last but not least, it provides a generic Account type that directly exposes the deserialized data — represented as a generic type T — with two extra attributes: publicKey and header. This allows us to directly access the deserialized data without nested data attributes.

type Account<T extends object> = T & {
  publicKey: PublicKey;
  header: AccountHeader;
};

Fetching RPC accounts

Now that we know how accounts are represented in Umi, let's see how we can fetch them.

First of all, we can fetch a single account using the getAccount method of the RpcInterface. This will return a MaybeRpcAccount instance since the account may or may not exist. As mentioned above, you may use the assertAccountExists function to ensure it does.

const myAccount = await umi.rpc.getAccount(myPublicKey);
assertAccountExists(myAccount);

Note that if you are only interested to know if an account exists at the given address, you may use the accountExists method instead.

const accountExists = await umi.rpc.accountExists(myPublicKey);

If you need to fetch multiple accounts at once, you may use the getAccounts method instead. This will return a list of MaybeRpcAccounts, one for each public key you passed in.

const myAccounts = await umi.rpc.getAccounts(myPublicKeys);
// Fetch all accounts from a program.
const allProgramAccounts = await umi.rpc.getProgramAccounts(myProgramId);

// Fetch a slice of all accounts from a program.
const slicedProgramAccounts = await umi.rpc.getProgramAccounts(myProgramId, {
  dataSlice: { offset: 32, length: 8 },
});

// Fetch some accounts from a program that matches a given set of filters.
const filteredProgramAccounts = await umi.rpc.getProgramAccounts(myProgramId, {
  filters: [
    { dataSize: 42 },
    { memcmp: { offset: 0, bytes: new Uint8Array([1, 2, 3]) } },
  ],
});

Deserializing accounts

import { assertAccountExists, deserializeAccount } from '@metaplex-foundation/umi';
import { struct, publicKey, u64 } from '@metaplex-foundation/umi/serializers';

// Given an existing RPC account.
const myRpcAccount = await umi.rpc.getAccount(myPublicKey);
assertAccountExists(myRpcAccount);

// And an account data serializer.
const myDataSerializer = struct([
  ['source', publicKey()],
  ['destination', publicKey()],
  ['amount', u64()],
]);

// We can deserialize the account like so.
const myAccount = deserializeAccount(rawAccount, myDataSerializer);
// myAccount.source -> PublicKey
// myAccount.destination -> PublicKey
// myAccount.amount -> bigint
// myAccount.publicKey -> PublicKey
// myAccount.header -> AccountHeader
import { Metadata, deserializeMetadata, fetchMetadata, safeFetchMetadata } from '@metaplex-foundation/mpl-token-metadata';

// Deserializes a metadata account.
const metadata: Metadata = deserializeMetadata(umi, unparsedMetadataAccount);

// Fetch and deserialize a metadata account, fail if the account does not exist.
const metadata: Metadata = await fetchMetadata(umi, metadataPublicKey);

// Fetch and deserialize a metadata account, return null if the account does not exist.
const metadata: Metadata | null = await safeFetchMetadata(umi, metadataPublicKey);
PreviousUmi and Web3js DifferencesNextUMI Helpers

Last updated 10 months ago

Finally, the getProgramAccounts method can be used to fetch all accounts from a given program that match a given set of filters. This method returns a list of RpcAccount directly since it will only return accounts that exist. Refer to the following to learn more about filters and data slicing.

Note that when fetching program accounts, you might be interested in .

In order to turn a RpcAccount into a deserialized Account<T>, we simply need the deserializeAccount function and a Serializer that knows how to deserialize the account's data. You can read more about Serializers in the but here's a quick example assuming the data is composed of two public keys and one u64 number.

Note that, in practice, program libraries should provide account data serializers and helpers for you. Here's an example using a .

Get Program Account documentation
GpaBuilders
Serializers page
Kinobi-generated library