Welcome to Apex SDK! This guide will help you get started with building cross-chain applications in under 10 minutes.
Add Apex SDK to your Cargo.toml:
[dependencies]
apex-sdk = "0.1.4"
tokio = { version = "1.35", features = ["full"] }
anyhow = "1.0"
# Install the CLI
cargo install apex-sdk-cli
# Create a new project
apex new my-project
# Navigate to your project
cd my-project
# Build and run
cargo build
cargo run
Create a new file src/main.rs:
use apex_sdk::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Initialize SDK with both Substrate and EVM support
let sdk = ApexSDK::builder()
.with_substrate_endpoint("wss://polkadot.api.onfinality.io/public-ws")
.with_evm_endpoint("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")
.build()
.await?;
// Build a cross-chain transaction
let tx = sdk
.transaction()
.from_substrate_account("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
.to_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7")
.amount(1_000_000_000_000) // 1 DOT in Planck
.build()?;
// Execute the transaction
let result = sdk.execute(tx).await?;
println!("Transaction successful!");
println!("Source TX: {}", result.source_tx_hash);
if let Some(dest_tx) = result.destination_tx_hash {
println!("Destination TX: {}", dest_tx);
}
Ok(())
}
The SDK supports three modes of operation:
// Substrate-only
let sdk = ApexSDK::builder()
.with_substrate_endpoint("wss://polkadot.api.onfinality.io/public-ws")
.build()
.await?;
// EVM-only
let sdk = ApexSDK::builder()
.with_evm_endpoint("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")
.build()
.await?;
// Multi-chain
let sdk = ApexSDK::builder()
.with_substrate_endpoint("wss://polkadot.api.onfinality.io/public-ws")
.with_evm_endpoint("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")
.build()
.await?;
The Transaction Builder provides a fluent API:
let tx = sdk
.transaction()
.from_evm_address("0x...") // Set sender
.to_evm_address("0x...") // Set recipient
.amount(1_000_000_000) // Set amount
.with_gas_limit(21000) // Optional: set gas limit
.with_data(vec![1, 2, 3]) // Optional: add data
.build()?;
Apex SDK uses type-safe addresses:
// Substrate address (SS58 format)
let substrate_addr = Address::substrate("5GrwvaEF5z...");
// EVM address (hex format)
let evm_addr = Address::evm("0x742d35Cc663...");
Check which chains are supported:
if sdk.is_chain_supported(&Chain::Ethereum) {
println!("Ethereum is supported!");
}
// Supported chains:
// - Chain::Polkadot
// - Chain::Kusama
// - Chain::Ethereum
// - Chain::BinanceSmartChain
// - Chain::Polygon
// - Chain::Avalanche
// - Chain::Moonbeam (Hybrid)
// - Chain::Astar (Hybrid)
Query transaction status after execution:
let status = sdk
.get_transaction_status(&Chain::Ethereum, "0x123...")
.await?;
match status {
TransactionStatus::Pending => println!("Transaction pending"),
TransactionStatus::Confirmed { block_number, confirmations } => {
println!("Confirmed at block {} with {} confirmations",
block_number, confirmations);
}
TransactionStatus::Failed { error } => {
println!("Transaction failed: {}", error);
}
TransactionStatus::Unknown => println!("Status unknown"),
}
let tx = sdk
.transaction()
.from_evm_address("0x...")
.to_evm_address("0x...")
.amount(1_000_000_000_000_000_000u128) // 1 ETH
.with_gas_limit(21000)
.build()?;
let result = sdk.execute(tx).await?;
let tx = sdk
.transaction()
.from_substrate_account("5GrwvaEF...")
.to_evm_address("0x...")
.amount(5_000_000_000_000) // 5 DOT
.build()?;
let result = sdk.execute(tx).await?;
// result.destination_tx_hash will contain the EVM transaction hash
let tx = sdk
.transaction()
.from_evm_address("0x...")
.to_evm_address("0x...")
.amount(0) // Can be zero for data-only transactions
.with_data(b"Hello, Apex SDK!".to_vec())
.build()?;
Apex SDK includes comprehensive examples:
cd examples/basic-transfer
cargo run
Demonstrates:
cd examples/defi-aggregator
cargo run
Demonstrates:
cd examples/nft-bridge
cargo run
Demonstrates:
cd examples/dao-governance
cargo run
Demonstrates:
Apex SDK uses Rust’s Result type for error handling:
use apex_sdk::prelude::*;
async fn transfer() -> Result<()> {
let sdk = ApexSDK::builder()
.with_evm_endpoint("https://...")
.build()
.await?; // Returns Error::Config if failed
let tx = sdk
.transaction()
.from_evm_address("0x...")
.to_evm_address("0x...")
.amount(1000)
.build()?; // Returns Error::Transaction if invalid
let result = sdk.execute(tx).await?; // Returns Error::Connection if failed
Ok(())
}
Common error types:
Error::Config: Configuration errorsError::Connection: Network connection errorsError::Transaction: Transaction building errorsError::InvalidAddress: Invalid address formatError::UnsupportedChain: Chain not supportedRun the test suite:
# Run all tests
cargo test
# Run tests for a specific package
cargo test --package apex-sdk
# Run with verbose output
cargo test --verbose
# Run a specific test
cargo test test_transaction_builder
examples/ directory for more use casescargo doc --open to view API documentationBefore deploying to production:
with_substrate_endpoint() or with_evm_endpoint() before build()5GrwvaEF...)0x prefix (e.g., 0x742d35Cc...)cargo doc --openexamples/ directory