Expand description
§mrls - Moralis Web3 API Client
A comprehensive Rust client for the Moralis Web3 API.
§Features
- Wallet API - Native balances, token balances, transactions, approvals, net worth, profitability
- Token API - Metadata, prices, transfers, swaps, pairs, holders, stats, trending
- NFT API - NFT metadata, transfers, owners, trades, floor prices, collections
DeFiAPI - Pair prices, reserves, positions, protocol summaries- Block API - Block data, timestamps, date-to-block lookups
- Transaction API - Transaction details, decoded calls, internal transactions
- Resolve API - ENS, Unstoppable Domains, domain resolution
- Market Data API - Top tokens, movers, NFT collections, global stats
- Discovery API - Token discovery, trending, analytics, scores
- Entities API - Wallet/protocol/exchange labels and categories
§Quick Start
use mrls::Client;
#[tokio::main]
async fn main() -> Result<(), mrls::Error> {
// Create client from MORALIS_API_KEY env var
let client = Client::from_env()?;
// Get native balance
let balance = client.wallet().get_native_balance(
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
Some("eth"),
).await?;
println!("Balance: {} wei", balance.balance);
// Get token price
let price = client.token().get_price(
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
Some("eth"),
).await?;
println!("WETH Price: ${:?}", price.usd_price);
Ok(())
}§Error Handling
The client provides specific error types for common API errors:
use mrls::{Client, Error};
use mrls::error::DomainError;
#[tokio::main]
async fn main() {
let client = Client::from_env().unwrap();
// Premium endpoints require Starter or Pro plan
match client.discovery().get_token_score("0x...", Some("eth")).await {
Ok(score) => println!("Token score: {:?}", score),
Err(Error::Domain(DomainError::PlanRequired { required_plan, message })) => {
println!("Upgrade to {} plan: {}", required_plan, message);
}
Err(Error::RateLimited { retry_after, .. }) => {
println!("Rate limited, retry after {:?}", retry_after);
}
Err(Error::Domain(DomainError::Unauthorized)) => {
println!("Invalid API key");
}
Err(e) => println!("Other error: {}", e),
}
}§Plan Tiers
Some endpoints require specific plan tiers:
| Tier | Endpoints |
|---|---|
| Free | Most basic endpoints |
| Starter | get_token_score |
| Pro | Volume stats, token discovery, analytics, search |
§Automatic Retries
Use the retry utilities for resilient API calls:
use mrls::{Client, with_retry, RetryConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
let config = RetryConfig::default(); // 3 retries with exponential backoff
let result = with_retry(&config, || async {
client.wallet().get_native_balance(
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
Some("eth"),
).await
}).await?;
println!("Balance: {}", result.balance);
Ok(())
}Preset configurations:
RetryConfig::default()- 3 retries, 100ms initial delayRetryConfig::quick()- 2 retries, 50ms initial delay (interactive)RetryConfig::batch()- 5 retries, 200ms initial delay (batch jobs)RetryConfig::none()- No retries
Re-exports§
pub use error::Error;pub use error::PlanTier;pub use analytics::AnalyticsApi;pub use analytics::AnalyticsQuery;pub use block::BlockApi;pub use block::BlockQuery;pub use defi::DefiApi;pub use defi::DefiQuery;pub use discovery::DiscoveryApi;pub use discovery::DiscoveryQuery;pub use entities::EntitiesApi;pub use entities::EntityQuery;pub use market::MarketApi;pub use market::MarketQuery;pub use nft::NftApi;pub use nft::NftQuery;pub use resolve::ResolveApi;pub use token::TokenApi;pub use transaction::TransactionApi;pub use transaction::TransactionQuery;pub use utils::UtilsApi;pub use utils::UtilsQuery;pub use volume::VolumeApi;pub use volume::VolumeQuery;pub use wallet::WalletApi;pub use wallet::WalletQuery;
Modules§
- analytics
- Token Analytics API
- block
- Block API - block data, timestamps, lookups
- defi
DeFiAPI - pair prices, reserves, positions, protocols- discovery
- Discovery API - token discovery, trending, analytics, scores
- entities
- Entities API - wallets, protocols, exchanges, labels
- error
- Error types for the Moralis API client
- market
- Market Data API - top tokens, movers, NFT collections, global stats
- nft
- NFT API - NFT metadata, transfers, owners, trades, floor prices
- resolve
- Resolve API - ENS, Unstoppable Domains, domain resolution
- token
- Token API - ERC20 tokens, prices, metadata
- transaction
- Transaction API - transaction data, decoded calls, internal transactions
- utils
- Utils and Contract API
- volume
- Volume Analytics API
- wallet
- Wallet API - balances, transactions,
DeFipositions
Structs§
- Client
- Client for the Moralis Web3 API
- Config
- Configuration for the Moralis client
- Http
Client Config - HTTP client configuration
- Retry
Config - Configuration for retry behavior
- Retry
Error - Error wrapper that includes retry information
Constants§
- DEFAULT_
BASE_ URL - Default base URL for the Moralis API
Traits§
- Retryable
Error - Determines if an error should be retried
Functions§
- config_
with_ api_ key - Create a config with an API key
- with_
retry - Execute an async operation with retries
- with_
simple_ retry - Simple retry wrapper for operations that return Result with any error type
Type Aliases§
- Result
- Result type alias for this crate