The Index Program creates lookup tables for indexing Solana account addresses on-chain. Developers can use these tables to lookup and verify account data relevant to their programs' instructions and state.
```yaml
[dependencies] index-program = { version = "0.1.0" } ```
create_index
Index
account.create_pointer
Pointer
account.Proof
account.delete_index
Index
account.delete_pointer
Pointer
account.Proof
account.Index
Pointer
Proof
These examples are for Solana programs that need to create and manage their own on-chain indices. These examples show a program built with Anchor that has a singleton "authority account" for signing instructions on behalf of the program.
Here is an example instruction create_my_index
that creates an index owned by the program.
```rs // createmyindex.rs
use { crate::state::, anchor_lang::{prelude::, solanaprogram::systemprogram}, std::mem::size_of, };
pub struct CreateMyIndex<'info> { #[account(mut, seeds = [SEED_AUTHORITY], bump = authority.bump, owner = crate::ID)] pub authority: Account<'info, Authority>,
#[account(mut)]
pub index: AccountInfo<'info>,
#[account(address = index_program::ID)]
pub index_program: Program<'info, index_program::program::IndexProgram>,
#[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 an index owned by the program authority.
index_program::cpi::create_index(
CpiContext::new_with_signer(
index_program.to_account_info(),
index_program::cpi::accounts::CreateIndex {
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]]],
),
index_bump,
)
} ```
Here is an example instruction create_my_pointer
that adds a pointer to an index 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(
mut,
constraint = index.owner == authority.key(),
constraint = index.namespace == namespace.key(),
owner = index_program.key()
)]
pub index: AccountInfo<'info>,
#[account(address = index_program::ID)]
pub index_program: Program<'info, index_program::program::IndexProgram>,
#[account()]
pub namespace: Account<'info, Namespace>
#[account()]
pub pointer: AccountInfo<'info>,
#[account()]
pub proof: AccountInfo<'info>,
#[account(mut)]
pub signer: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
}
pub fn handler(
ctx: Context
// Create an index owned by the program authority.
index_program::cpi::create_pointer(
CpiContext::new_with_signer(
index_program.to_account_info(),
index_program::cpi::accounts::CreatePointer {
index: index.to_account_info(),
pointer: pointer.to_account_info(),
proof: proof.to_account_info(),
owner: authority.to_account_info(),
payer: signer.to_account_info(),
system_program: system_program.to_account_info(),
},
&[&[SEED_AUTHORITY, &[authority.bump]]],
),
index.count.to_string(),
reference,
pointer_bump,
proof_bump,
)
} ```