Official Rust SDK for ChainBridge — a trustless cross-chain atomic swap protocol on Stellar.
[dependencies]
chainbridge-sdk = "0.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }Optional features:
ws— enablesChainBridgeWebSocketfor real-time event streaming.native-tls— use the system TLS stack (defaults torustls).
use chainbridge_sdk::{
crypto::{derive_hash_lock, generate_secret_default},
ChainBridgeClient, ClientConfig, CreateOrderInput,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ChainBridgeClient::new(ClientConfig {
base_url: "https://api.chainbridge.io".into(),
api_key: Some(std::env::var("CHAINBRIDGE_API_KEY")?),
..Default::default()
})?;
let secret = generate_secret_default();
let hash_lock = derive_hash_lock(&secret)?;
let order = client
.orders()
.create(&CreateOrderInput {
from_chain: "stellar".into(),
to_chain: "bitcoin".into(),
from_asset: "XLM".into(),
to_asset: "BTC".into(),
from_amount: "1000000000".into(),
to_amount: "10000".into(),
sender_address: "GA...".into(),
expiry: 86_400,
})
.await?;
println!("order_id={} hash_lock={hash_lock}", order.order_id);
Ok(())
}| Method | Endpoint family |
|---|---|
client.orders() |
/api/v1/orders |
client.htlcs() |
/api/v1/htlcs |
client.swaps() |
/api/v1/swaps |
client.proofs() |
/api/v1/proofs |
client.market() |
/api/v1/market |
client.assets() |
/api/v1/assets |
client.analytics() |
/api/v1/analytics |
client.auth() |
/api/v1/auth |
All resource methods return Result<_, chainbridge_sdk::Error>. The variants
match the API's error model (Authentication, NotFound, Validation,
RateLimit, Api, Network, Decode, Config).
The HTTP client retries 5xx, 429, and transport errors with exponential backoff.
use chainbridge_sdk::websocket::ChainBridgeWebSocket;
let mut ws = ChainBridgeWebSocket::new("wss://api.chainbridge.io/ws")
.with_api_key(std::env::var("CHAINBRIDGE_API_KEY")?);
ws.subscribe("orders");
ws.run(|event| println!("{}: {}", event.event_type, event.data)).await?;chainbridge_sdk::wallets defines WalletAdapter and HtlcWalletAdapter
async traits plus a StubWallet implementation for tests. Pair these with
your chosen on-chain SDK (e.g. stellar-sdk, bitcoin, web3).
cargo run --example create_swapcargo run --example track_orders --features ws
MIT