The indexer program can be used to index Solana addresses into serial lists for fast and efficient off-chain lookups.
```yaml
[dependencies] cronos-indexer = { version = "0.0.1" } ```
create_list
List
account.delete_list
List
account.push_element
Element
account.pop_element
Element
account.List
Element
These examples are for Solana programs that need to create and manage their own on-chain lists. These examples show an Anchor program that has a singleton "authority account" for signing instructions on behalf of the program.
Here is an example instruction create_my_list
that creates an list owned by the program.
```rs // createmylist.rs
use { crate::state::, anchor_lang::{prelude::, solanaprogram::systemprogram}, std::mem::size_of, };
pub struct CreateMyList<'info> { #[account( mut, seeds = [SEED_AUTHORITY], bump = authority.bump, owner = crate::ID )] pub authority: Account<'info, Authority>,
#[account(mut)]
pub list: AccountInfo<'info>,
#[account(address = list_program::ID)]
pub list_program: Program<'info, list_program::program::ListProgram>,
#[account(
init,
payer = signer,
space = 8 + size_of<Namespace>()
)]
pub namespace: Account<'info, Namespace>
#[account(mut)]
pub signer: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
}
pub fn handler(ctx: Context
// Create a list owned by the program authority.
list_program::cpi::create_index(
CpiContext::new_with_signer(
list_program.to_account_info(),
list_program::cpi::accounts::CreateList {
index: index.to_account_info(),
namespace: namespace.to_account_info(),
owner: authority.to_account_info(),
payer: signer.to_account_info(),
system_program: system_program.to_account_info(),
},
&[&[SEED_AUTHORITY, &[authority.bump]]],
),
bump,
)
} ```
Here is an example instruction push_my_element
that adds an element to a list owned by the program.
```rs // createmypointer.rs
use { crate::state::, anchor_lang::{prelude::, solanaprogram::systemprogram}, std::mem::size_of, };
pub struct CreateMyPointer<'info> { #[account( mut, seeds = [SEED_AUTHORITY], bump = authority.bump, owner = crate::ID )] pub authority: Account<'info, Authority>,
#[account()]
pub element: AccountInfo<'info>,
#[account(
mut,
constraint = list.owner == authority.key(),
constraint = list.namespace == namespace.key(),
owner = list_program.key()
)]
pub list: AccountInfo<'info>,
#[account(address = list_program::ID)]
pub list_program: Program<'info, list_program::program::IndexProgram>,
#[account()]
pub namespace: Account<'info, Namespace>
#[account(mut)]
pub signer: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
}
pub fn handler(
ctx: Context
// Add the reference address into the list.
list_program::cpi::push_element(
CpiContext::new_with_signer(
list_program.to_account_info(),
list_program::cpi::accounts::PushElement {
element: element.to_account_info(),
list: list.to_account_info(),
owner: authority.to_account_info(),
payer: signer.to_account_info(),
system_program: system_program.to_account_info(),
},
&[&[SEED_AUTHORITY, &[authority.bump]]],
),
reference,
bump,
)
} ```