pub struct ApplicationContext {
pub config: Config,
pub rate_limiter: Arc<RateLimiter>,
/* private fields */
}Expand description
Chain-agnostic context for dependency injection and resource management.
The ApplicationContext serves as a dependency injection container for the riglr ecosystem, enabling tools and workers to access shared resources (RPC clients, signers, database connections) without creating circular dependencies.
§Chain-Agnostic Design
The context uses type erasure (Arc<dyn Any>) to store blockchain clients
without depending on their concrete types. This allows riglr-core to remain
independent of blockchain SDKs while still providing access to them at runtime.
§Extension System
Resources are stored as “extensions” - type-erased objects that can be retrieved by their original type. This enables clean separation between riglr-core (which defines the interfaces) and chain-specific crates (which provide the implementations).
§Accessing Chain-Specific Clients
To maintain its chain-agnostic nature, riglr-core does not have direct methods
like solana_client() on the ApplicationContext. Instead, these ergonomic
accessors are provided via extension traits in chain-specific crates.
For example, riglr-solana-tools provides the SolanaAppContextProvider trait,
which adds the .solana_client() method to ApplicationContext.
See the [riglr_core::provider_extensions] module for more details on this pattern.
§Examples
use riglr_core::provider::ApplicationContext;
use riglr_config::ConfigBuilder;
use std::sync::Arc;
// Application layer creates context and injects dependencies
let config = ConfigBuilder::default().build().unwrap();
let context = ApplicationContext::from_config(&config);
// Inject Solana RPC client (in real code, from riglr-solana-tools)
// let solana_client = Arc::new(solana_client::rpc_client::RpcClient::new(...));
// context.set_extension(solana_client.clone());
// Inject EVM provider (in real code, from riglr-evm-tools)
// let evm_provider = Arc::new(alloy::providers::Provider::new(...));
// context.set_extension(evm_provider.clone());
// Tools retrieve clients by type
// let client: Option<Arc<RpcClient>> = context.get_extension();Fields§
§config: ConfigConfiguration for the application
rate_limiter: Arc<RateLimiter>Rate limiter for controlling request rates per client/user
Implementations§
Source§impl ApplicationContext
impl ApplicationContext
Sourcepub fn from_config(config: &Config) -> Self
pub fn from_config(config: &Config) -> Self
Create a new ApplicationContext from configuration
Sourcepub fn from_env() -> Self
👎Deprecated since 0.3.0: Use Config::from_env() followed by ApplicationContext::from_config() instead. This ensures proper separation of concerns.
pub fn from_env() -> Self
Create a new ApplicationContext from environment variables
DEPRECATED: Configuration should be loaded in the application binary using
riglr_config::Config::from_env() and passed to ApplicationContext::from_config().
This ensures proper separation of concerns where riglr-core consumes configuration
but does not load it, reinforcing the unidirectional dependency flow.
§Migration Guide
Instead of:
use riglr_core::provider::ApplicationContext;
let context = ApplicationContext::from_env();Use:
use riglr_core::provider::ApplicationContext;
use riglr_config::Config;
let config = Config::from_env();
let context = ApplicationContext::from_config(&config);Sourcepub fn set_extension<T: Send + Sync + 'static>(&self, extension: Arc<T>)
pub fn set_extension<T: Send + Sync + 'static>(&self, extension: Arc<T>)
Add an extension to the context
Extensions are stored by their type, allowing type-safe retrieval later. This is the recommended pattern for injecting RPC clients and other resources.
§Examples
use riglr_core::provider::ApplicationContext;
use riglr_config::ConfigBuilder;
use std::sync::Arc;
let config = ConfigBuilder::default().build().unwrap();
let context = ApplicationContext::from_config(&config);
// Add blockchain RPC clients as extensions
// Example: Add Solana RPC client
// let solana_client = Arc::new(solana_client::rpc_client::RpcClient::new(...));
// context.set_extension(solana_client);
// Example: Add EVM provider
// let evm_provider = Arc::new(alloy::Provider::new(...));
// context.set_extension(evm_provider);Sourcepub fn get_extension<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn get_extension<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Get an extension by type
Returns None if no extension of the given type has been set.
§Examples
use riglr_core::provider::ApplicationContext;
use riglr_config::ConfigBuilder;
use std::sync::Arc;
let config = ConfigBuilder::default().build().unwrap();
let context = ApplicationContext::from_config(&config);
// Add a typed extension (e.g., an RPC client)
// let client = Arc::new(MyRpcClient::new(...));
// context.set_extension(client.clone());
// Retrieve the client later by type
// let retrieved: Arc<MyRpcClient> = context.get_extension()
// .expect("RPC client not found");Sourcepub fn has_extension<T: Send + Sync + 'static>(&self) -> bool
pub fn has_extension<T: Send + Sync + 'static>(&self) -> bool
Check if an extension of the given type exists
Sourcepub fn remove_extension<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn remove_extension<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Remove an extension by type
Returns the removed extension if it existed.
Sourcepub fn clear_extensions(&self)
pub fn clear_extensions(&self)
Clear all extensions
Sourcepub fn extension_count(&self) -> usize
pub fn extension_count(&self) -> usize
Get the number of extensions
Trait Implementations§
Source§impl Clone for ApplicationContext
impl Clone for ApplicationContext
Source§fn clone(&self) -> ApplicationContext
fn clone(&self) -> ApplicationContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more