> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Helius Rust SDK for Agents

> Helius Rust SDK的完整指南，适用于AI代理。DAS API、交易、Helius Sender、webhook、增强型WebSocket、质押、钱包API以及完整的API快速参考的异步Rust绑定。

[Helius Rust SDK](https://github.com/helius-labs/helius-rust-sdk)为所有Helius API提供异步Rust绑定，非常适合高性能代理工作负载。

* **Crate**: `helius` (crates.io)
* **Version**: 1.x (使用 `solana-client` 3.0, `solana-sdk` 3.0)
* **Runtime**: 异步 (`tokio` 1.x)
* **Rust**: 1.85+ (edition 2021)
* **HTTP Client**: `reqwest`
* **License**: MIT

## 安装

```toml theme={"system"}
[dependencies]
helius = "1.0.0"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
solana-sdk = "3.0.0"
```

crate默认使用`native-tls`。对于纯Rust TLS（在OpenSSL不可用时有用），使用：

```toml theme={"system"}
helius = { version = "1.0.0", default-features = false, features = ["rustls"] }
```

## 快速入门

```rust theme={"system"}
use helius::error::Result;
use helius::types::*;
use helius::Helius;

#[tokio::main]
async fn main() -> Result<()> {
    let helius = Helius::new("YOUR_API_KEY", Cluster::MainnetBeta)?;

    // Get all NFTs owned by a wallet
    let assets = helius.rpc().get_assets_by_owner(GetAssetsByOwner {
        owner_address: "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY".to_string(),
        page: 1,
        limit: Some(50),
        ..Default::default()
    }).await?;

    // Get transaction history (with token account activity)
    let txs = helius.rpc().get_transactions_for_address(
        "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY".to_string(),
        GetTransactionsForAddressOptions {
            limit: Some(100),
            transaction_details: Some(TransactionDetails::Full),
            filters: Some(GetTransactionsFilters {
                token_accounts: Some(TokenAccountsFilter::BalanceChanged),
                ..Default::default()
            }),
            ..Default::default()
        },
    ).await?;

    // Send a transaction via Helius Sender (ultra-low latency)
    let sig = helius.send_smart_transaction_with_sender(
        SmartTransactionConfig {
            create_config: CreateSmartTransactionConfig {
                instructions: vec![transfer_instruction],
                signers: vec![wallet_signer],
                ..Default::default()
            },
            ..Default::default()
        },
        SenderSendOptions {
            region: "US_EAST".to_string(),
            ..Default::default()
        },
    ).await?;

    Ok(())
}
```

## 客户端构造器

### `Helius::new` — 基本同步客户端

```rust theme={"system"}
let helius = Helius::new("YOUR_API_KEY", Cluster::MainnetBeta)?;
```

最简单的构造器。不需要`.await`。提供RPC方法、webhook、增强型交易、智能交易和钱包API。不支持异步Solana客户端或WebSocket。

### `Helius::new_async` — 功能齐全的异步客户端

```rust theme={"system"}
let helius = Helius::new_async("YOUR_API_KEY", Cluster::MainnetBeta).await?;
```

推荐用于生产环境。包括异步Solana RPC客户端和增强型WebSocket流。需要`.await`，因为它建立了WebSocket连接。

### `Helius::new_with_url` — 自定义RPC端点

```rust theme={"system"}
let helius = Helius::new_with_url("http://localhost:8899")?;
```

适用于专用RPC节点、代理或本地开发。不需要API密钥。

### `HeliusBuilder` — 高级配置

```rust theme={"system"}
use helius::HeliusBuilder;

let helius = HeliusBuilder::new()
    .with_api_key("YOUR_API_KEY")?
    .with_cluster(Cluster::MainnetBeta)
    .with_async_solana()
    .with_websocket(None, None)
    .with_commitment(CommitmentConfig::confirmed())
    .build()
    .await?;
```

### `HeliusFactory` — 多集群

```rust theme={"system"}
let factory = HeliusFactory::new("YOUR_API_KEY");
let devnet_client = factory.create(Cluster::Devnet)?;
let mainnet_client = factory.create(Cluster::MainnetBeta)?;
```

### 访问嵌入式Solana客户端

```rust theme={"system"}
helius.connection()          // Sync SolanaRpcClient (Arc)
helius.async_connection()?   // Async SolanaRpcClient (requires new_async or HeliusBuilder)
helius.ws()                  // Enhanced WebSocket (Option)
helius.rpc()                 // Helius RpcClient (Arc)
helius.config()              // Config (Arc)
```

## 深入探讨

<CardGroup cols={2}>
  <Card title="最佳实践" icon="lightbulb" href="/zh/agents/rust-sdk/best-practices">
    推荐的模式、分页、常见错误和错误处理
  </Card>

  <Card title="API 参考" icon="book" href="/zh/agents/rust-sdk/api-reference">
    每个类别的完整方法列表
  </Card>
</CardGroup>

## 资源

<CardGroup cols={2}>
  <Card title="GitHub 仓库" icon="github" href="https://github.com/helius-labs/helius-rust-sdk">
    源代码、示例和问题跟踪
  </Card>

  <Card title="docs.rs" icon="book" href="https://docs.rs/helius/latest/helius/">
    docs.rs 上的完整 API 文档
  </Card>

  <Card title="代码示例" icon="code" href="https://github.com/helius-labs/helius-rust-sdk/tree/dev/examples">
    按类别组织的每个功能的工作示例
  </Card>

  <Card title="迁移指南 (0.x 到 1.0)" icon="arrow-right" href="https://github.com/helius-labs/helius-rust-sdk/blob/main/MIGRATION.md">
    从 solana-sdk 1.x 升级到 3.0
  </Card>
</CardGroup>
