This crate provides a transaction indexing framework for smart contracts. In order to be able to use this client, you will need to use Postgres database. Framework will be imported into an empty indexer project and contract logic for instructions will be defined by the framework consumer.
Add this to your Cargo.toml:
toml
[dependencies]
solana-indexer = "0.5.0"
It is required to set indexer configuration before start. Define config file in INDEXER_CFG env variable. Configuration file must contain fields: ```toml
[indexersettings] # Configuration parameters for indexing engine programid # The public key of the account containing a program connectionstr # An HTTP URL of working environment timestampinterval # An interval between indexer calls
[fetchingsettings] # Configuration of the fetching process (OPTIONAL) rpcrequesttimeout # Maximum allowed duration of a RPC call in milliseconds (default - 100) retrylimit # Maximum allowed number of retries (default - 10) transactionbatchsize # Amount of transaction that can be fetched in one time (default - 20)
[dbsettings] # Database configuration pameters host port username password databasename require_ssl
```
Code example: ```rust pub use { solana_indexer::{Indexer, IndexerEngine, CallbackResult, CallbackError, Instruction}, std::fmt::Display, std::process, std::sync::Arc, thiserror::Error, };
pub enum CustomError { #[error("Custom")] Custom, }
fn unwraporexit
fn simplecallback(instruction: &Instruction) -> CallbackResult<()> { /* Callback body */ if instruction.programid == "" { // You can use your custom error as cb result return Err(CallbackError::fromerr(CustomError::Custom)); } else if instruction.programid == "123" { // Also you can generate error from string to use it as cb result return Err(CallbackError::from("Callback error")); } Ok(()) }
async fn run(config: &str) { let mut solanaindexer = Indexer::build(config).await.unwrap(); solanaindexer.setcallback(Arc::new(simplecallback));
unwrap_or_exit(
solana_indexer
.start_indexing()
.await,
);
}
Solana indexer is distributed under the terms of the MIT license.