Get Accounts
The getAccountsFactory
function creates a function (called get(Something)Accounts
) that gets all program accounts of a particular type. Accounts will be decoded, so you can see the actual values for each of the fields inside. Useful for getting all offers, auctions, or users from a program.
This is the only factory function you need to use in Kite. That’s because it makes functions to get the accounts of a particular type from your Anchor programs. Have structs for Offer
or User
or Election
in your Anchor program? Use this function to make a getOffers
or getUsers
or getElections
function. Whatever struct you need to fetch from your Anchor program, use this function to make a function to get it.
Usage
// Create a function to get all offers
const getOffers = connection.getAccountsFactory(
programClient.ESCROW_PROGRAM_ADDRESS,
OFFER_DISCRIMINATOR,
getOfferDecoder(),
);
// Use it to get all offers
const offers = await getOffers();
Parameters
programAddress
:Address
- The program address to query accounts fromdiscriminator
:Uint8Array
- The discriminator to filter accounts by (usually the first 8 bytes of the account data)decoder
:Decoder<T>
- The decoder to use for parsing account data into your custom type
Returns
Returns: () => Promise<Array<T>>
- A function that returns an array of decoded accounts
Examples
This example shows how to get all offers from an escrow program. The programClient
, OFFER_DISCRIMINATOR
and getOfferDecoder
all come from a generated client made by Codama.
import * as programClient from "../dist/js-client";
import { getOfferDecoder, OFFER_DISCRIMINATOR } from "../dist/js-client";
// Create a function to get all offers
const getOffers = connection.getAccountsFactory(
programClient.ESCROW_PROGRAM_ADDRESS,
OFFER_DISCRIMINATOR,
getOfferDecoder(),
);
// Use it to get all offers
const offers = await getOffers();
Create a function to get all auctions:
const getAuctions = connection.getAccountsFactory(
programClient.AUCTION_PROGRAM_ADDRESS,
AUCTION_DISCRIMINATOR,
getAuctionDecoder()
);
// Use it to get all auctions
const auctions = await getAuctions();
Create a function to get all users:
const getUsers = connection.getAccountsFactory(
programClient.USER_PROGRAM_ADDRESS,
USER_DISCRIMINATOR,
getUserDecoder()
);
// Use it to get all users
const users = await getUsers();
Understanding Account Factories
Account factories are useful when you need to:
- Get all accounts of a particular type from a program
- Decode account data into a structured format
- Work with multiple accounts of the same type
Common uses include:
- Getting all offers in an escrow program
- Getting all auctions in an auction program
- Getting all users in a user management program
See also: Get PDA and Bump