LayerHop for developers · Rust SDK
The Rust SDK for cross-chain swaps
The layerhop crate is the async Rust client for the LayerHop API, built on reqwest with rustls (no OpenSSL), tokio and serde. Create a client with LayerHop::builder(), call quote(), build_tx() and report_submitted(), and drive watch() as a Stream; the crate returns unsigned transactions and your wallet library signs them.
[dependencies]
layerhop = "0.9"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
futures-util = "0.3" # StreamExt for watch()How to use the SDK
- 1
Add the crate and create a client
Add layerhop to Cargo.toml, then LayerHop::builder().build(), with .api_key(..) and .base_url(..) when you need them.
- 2
Get a quote
lh.quote(&QuoteParams { .. }).await returns the response with best and quotes. Amounts are base-unit strings.
- 3
Build the unsigned transaction
lh.build_tx(&q.best.quote_id).await returns a BuiltTx: Evm, Svm or Tvm (Tron).
- 4
Have the user's wallet sign and send it
For an EVM chain, send tx.approval first when it is present, then the main transaction (to, data, value) on tx.chainId. For Solana, sign the base64 VersionedTransaction; when submitViaApi is true, send the signed bytes through LayerHop instead of an RPC node.
- 5
Report and track
lh.report_submitted(..).await, then poll lh.watch(provider, tx_hash, WatchOptions::default()) as a Stream until the order is final.
Code
Copy, then replace the addresses and the key.
use futures_util::StreamExt;
use layerhop::{BuiltTx, LayerHop, QuoteParams, WatchOptions};
let lh = LayerHop::builder().build(); // .api_key(..) when you have a key
// 1. Quote: 100 USDC on Base -> SOL on Solana (amounts are base-unit strings)
let q = lh.quote(&QuoteParams {
src_chain: "base".into(), src_token: "USDC".into(), amount: "100000000".into(),
dst_chain: "solana".into(), dst_token: "SOL".into(),
sender: "0xYourEvmAddress".into(), recipient: "YourSolanaAddress".into(),
..Default::default()
}).await?;
// 2. Build the unsigned transaction
let tx = lh.build_tx(&q.best.quote_id).await?;
// 3. Sign and send with YOUR wallet library (sign_evm / sign_svm / sign_tron are yours)
let tx_hash = match tx {
BuiltTx::Evm(evm) => sign_evm(evm).await?, // send evm.approval first when Some
BuiltTx::Svm(svm) => sign_svm(svm).await?, // submit_via_api == Some(true) -> lh.submit(..)
BuiltTx::Tvm(tron) => sign_tron(tron).await?,
};
// 4. Report the broadcast, then watch it
lh.report_submitted(&q.best.quote_id, &tx_hash).await?;
let mut updates = Box::pin(lh.watch(&q.best.provider, &tx_hash, WatchOptions::default()));
while let Some(status) = updates.next().await {
println!("{}", status?.state);
}What works today, and what doesn't
So you know before you build.
Works today
- serde models and a typed error enum for the public API: quotes, exact-output quotes, quote streams, orders, tracking, checkout
- watch() as a Stream that ends when the order is final, after a request error, or on a timeout
- rustls, no OpenSSL; webhook signature verification
Good to know
- It never signs: use alloy, solana-sdk or a KMS for that
- Quotes expire within about 30 to 60 seconds
- Providers are mainnet only; use an lh_test_ key to develop without real funds
Questions
Do I need an API key?
Not to try it: requests without a key work under a per-IP rate limit. For production, sign in to the dashboard with a wallet and create a key (lh_live_…, sent in the x-api-key header). A key gets its own rate limit, shows your usage and logs, and lets your orders trigger webhooks. lh_test_ keys give sandbox quotes and simulated orders.
What does it cost?
LayerHop's fee on a trade made through the API channel is 0.10% of the input amount, reported on every quote as fees.ourFeeBps next to the provider's fees and gas. Partners can add their own fee on top.
Does it hold keys or sign transactions?
No. It returns unsigned transactions and your own wallet library (viem, ethers, web3.py, go-ethereum, alloy, a Solana wallet, a KMS) signs and broadcasts them. LayerHop never holds funds or keys.
Which Rust version do I need?
The crate declares a minimum Rust version of 1.75 (edition 2021).
Is it async only?
Yes: it is built on tokio and reqwest, so use it from an async runtime.