Comprehensive API reference for Apex SDK Protocol v0.1.4 - Your guide to building cross-chain blockchain applications with Rust, Substrate, and EVM.
ApexSDKThe main SDK instance providing unified access to blockchain operations.
pub struct ApexSDK {
substrate_adapter: Option<SubstrateAdapter>,
evm_adapter: Option<EvmAdapter>,
}
builder() -> ApexSDKBuilderCreates a new builder for configuring the SDK.
let sdk = ApexSDK::builder()
.with_substrate_endpoint("wss://...")
.with_evm_endpoint("https://...")
.build()
.await?;
substrate(&self) -> Result<&SubstrateAdapter>Returns a reference to the Substrate adapter if configured.
let substrate = sdk.substrate()?;
evm(&self) -> Result<&EvmAdapter>Returns a reference to the EVM adapter if configured.
let evm = sdk.evm()?;
is_chain_supported(&self, chain: &Chain) -> boolChecks if a specific chain is supported based on configured adapters.
if sdk.is_chain_supported(&Chain::Ethereum) {
println!("Ethereum is supported!");
}
get_transaction_status(&self, chain: &Chain, tx_hash: &str) -> Result<TransactionStatus>Queries the status of a transaction on a specific chain.
let status = sdk.get_transaction_status(
&Chain::Ethereum,
"0x123..."
).await?;
transaction(&self) -> TransactionBuilderCreates a new transaction builder.
let tx = sdk.transaction()
.from_evm_address("0x...")
.to_evm_address("0x...")
.amount(1000)
.build()?;
execute(&self, transaction: Transaction) -> Result<TransactionResult>Executes a transaction.
let result = sdk.execute(tx).await?;
Builder for constructing an ApexSDK instance with custom configuration.
pub struct ApexSDKBuilder {
substrate_endpoint: Option<String>,
evm_endpoint: Option<String>,
timeout_seconds: Option<u64>,
}
new() -> SelfCreates a new builder instance.
let builder = ApexSDKBuilder::new();
with_substrate_endpoint(self, url: impl Into<String>) -> SelfSets the Substrate WebSocket endpoint.
let builder = builder.with_substrate_endpoint("wss://polkadot.api.onfinality.io/public-ws");
with_evm_endpoint(self, url: impl Into<String>) -> SelfSets the EVM HTTP/WebSocket endpoint.
let builder = builder.with_evm_endpoint("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY");
with_timeout(self, seconds: u64) -> SelfSets the connection timeout in seconds.
let builder = builder.with_timeout(30);
build(self) -> Result<ApexSDK>Builds the SDK instance. Returns an error if no adapters are configured.
let sdk = builder.build().await?;
Builder for creating blockchain transactions.
pub struct TransactionBuilder {
from: Option<Address>,
to: Option<Address>,
amount: Option<u128>,
source_chain: Option<Chain>,
data: Option<Vec<u8>>,
gas_limit: Option<u64>,
}
new() -> SelfCreates a new transaction builder.
let builder = TransactionBuilder::new();
from(self, address: Address) -> SelfSets the sender address.
let builder = builder.from(Address::evm("0x..."));
from_substrate_account(self, address: impl Into<String>) -> SelfSets the sender as a Substrate address.
let builder = builder.from_substrate_account("5GrwvaEF...");
from_evm_address(self, address: impl Into<String>) -> SelfSets the sender as an EVM address.
let builder = builder.from_evm_address("0x742d35Cc...");
to(self, address: Address) -> SelfSets the recipient address.
let builder = builder.to(Address::evm("0x..."));
to_substrate_account(self, address: impl Into<String>) -> SelfSets the recipient as a Substrate address.
let builder = builder.to_substrate_account("5GrwvaEF...");
to_evm_address(self, address: impl Into<String>) -> SelfSets the recipient as an EVM address.
let builder = builder.to_evm_address("0x742d35Cc...");
amount(self, amount: u128) -> SelfSets the transfer amount.
let builder = builder.amount(1_000_000_000_000_000_000u128); // 1 ETH
on_chain(self, chain: Chain) -> SelfExplicitly sets the source chain.
let builder = builder.on_chain(Chain::Ethereum);
with_data(self, data: Vec<u8>) -> SelfSets transaction data/payload.
let builder = builder.with_data(b"Hello, World!".to_vec());
with_gas_limit(self, limit: u64) -> SelfSets the gas limit (for EVM transactions).
let builder = builder.with_gas_limit(21000);
build(self) -> Result<Transaction>Builds the transaction. Returns an error if required fields are missing.
let tx = builder.build()?;
Represents a blockchain transaction.
pub struct Transaction {
pub from: Address,
pub to: Address,
pub amount: u128,
pub source_chain: Chain,
pub destination_chain: Chain,
pub data: Option<Vec<u8>>,
pub gas_limit: Option<u64>,
}
is_cross_chain(&self) -> boolChecks if this is a cross-chain transaction.
if tx.is_cross_chain() {
println!("This is a cross-chain transaction");
}
hash(&self) -> StringReturns a hash of the transaction.
let hash = tx.hash();
Result of a transaction execution.
pub struct TransactionResult {
pub source_tx_hash: String,
pub destination_tx_hash: Option<String>,
pub status: TransactionStatus,
pub block_number: Option<u64>,
pub gas_used: Option<u64>,
}
AddressRepresents a blockchain address.
pub enum Address {
Substrate(String),
Evm(String),
}
substrate(addr: impl Into<String>) -> SelfCreates a Substrate address.
let addr = Address::substrate("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
evm(addr: impl Into<String>) -> SelfCreates an EVM address.
let addr = Address::evm("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7");
as_str(&self) -> &strReturns the address as a string.
let addr_str = address.as_str();
ChainSupported blockchain networks.
pub enum Chain {
Polkadot,
Kusama,
Ethereum,
BinanceSmartChain,
Polygon,
Avalanche,
Moonbeam,
Astar,
}
chain_type(&self) -> ChainTypeReturns the chain type.
let chain_type = Chain::Ethereum.chain_type();
// Returns ChainType::Evm
name(&self) -> &strReturns the chain name.
let name = Chain::Polkadot.name();
// Returns "Polkadot"
ChainTypeType of blockchain.
pub enum ChainType {
Substrate,
Evm,
Hybrid,
}
TransactionStatusStatus of a blockchain transaction.
pub enum TransactionStatus {
Pending,
Confirmed {
block_number: u64,
confirmations: u32,
},
Failed {
error: String,
},
Unknown,
}
ErrorMain error type for Apex SDK.
pub enum Error {
Config(String),
Connection(String),
Transaction(String),
UnsupportedChain(String),
InvalidAddress(String),
Substrate(apex_sdk_substrate::Error),
Evm(apex_sdk_evm::Error),
Serialization(String),
Other(String),
}
Config: Configuration errors (e.g., no adapters configured)Connection: Network connection errorsTransaction: Transaction building or execution errorsUnsupportedChain: Requested chain is not supportedInvalidAddress: Invalid address formatSubstrate: Substrate adapter errorEvm: EVM adapter errorSerialization: Serialization/deserialization errorOther: Generic errorResult<T>Type alias for std::result::Result<T, Error>.
pub type Result<T> = std::result::Result<T, Error>;
Low-level adapter for Substrate chains.
pub struct SubstrateAdapter {
endpoint: String,
connected: bool,
}
connect(endpoint: &str) -> Result<Self>Connects to a Substrate node.
let adapter = SubstrateAdapter::connect("wss://polkadot.api.onfinality.io/public-ws").await?;
get_transaction_status(&self, tx_hash: &str) -> Result<TransactionStatus>Gets transaction status.
let status = adapter.get_transaction_status("0x123...").await?;
validate_address(&self, address: &Address) -> boolValidates a Substrate address.
let valid = adapter.validate_address(&address);
pallet(&self, name: &str) -> Result<PalletInfo>Gets information about a pallet.
let balances = adapter.pallet("Balances")?;
Low-level adapter for EVM chains.
pub struct EvmAdapter {
endpoint: String,
connected: bool,
}
connect(endpoint: &str) -> Result<Self>Connects to an EVM node.
let adapter = EvmAdapter::connect("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY").await?;
get_transaction_status(&self, tx_hash: &str) -> Result<TransactionStatus>Gets transaction status.
let status = adapter.get_transaction_status("0x123...").await?;
validate_address(&self, address: &Address) -> boolValidates an EVM address.
let valid = adapter.validate_address(&address);
contract(&self, address: &str) -> Result<ContractInfo>Gets information about a contract.
let contract = adapter.contract("0x...")?;
use apex_sdk::prelude::*;
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?;
let tx = sdk
.transaction()
.from_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7")
.to_evm_address("0x1234567890123456789012345678901234567890")
.amount(1_000_000_000_000_000_000u128)
.with_gas_limit(21000)
.build()?;
let result = sdk.execute(tx).await?;
println!("TX Hash: {}", result.source_tx_hash);
let status = sdk
.get_transaction_status(&Chain::Ethereum, "0x123...")
.await?;
match status {
TransactionStatus::Confirmed { block_number, confirmations } => {
println!("Confirmed at block {} ({} confirmations)",
block_number, confirmations);
}
_ => println!("Not confirmed yet"),
}
let tx = sdk
.transaction()
.from_substrate_account("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
.to_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7")
.amount(5_000_000_000_000)
.build()?;
let result = sdk.execute(tx).await?;
println!("Source TX: {}", result.source_tx_hash);
if let Some(dest_tx) = result.destination_tx_hash {
println!("Destination TX: {}", dest_tx);
}
Apex SDK provides optional feature flags to enable specific functionality:
apex-sdk-coremocks
[dependencies]
apex-sdk-core = { version = "0.1.4", features = ["mocks"] }
apex-sdk-substratetyped
typed-polkadot
typed feature automaticallytyped-kusama
typed feature automaticallytyped-westend
typed feature automatically[dependencies]
# Enable typed metadata for Polkadot
apex-sdk-substrate = { version = "0.1.4", features = ["typed-polkadot"] }
# Enable multiple chains
apex-sdk-substrate = { version = "0.1.4", features = ["typed-polkadot", "typed-kusama"] }
By default, all crates use minimal dependencies. Enable features as needed for your use case:
[dependencies]
# Minimal configuration (default)
apex-sdk = "0.1.4"
# With testing support
apex-sdk-core = { version = "0.1.4", features = ["mocks"] }
# With typed Substrate metadata
apex-sdk-substrate = { version = "0.1.4", features = ["typed-polkadot"] }
#[cfg(feature = "mocks")]
use apex_sdk_core::mocks::MockAdapter;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_with_mock() {
let mock_adapter = MockAdapter::new();
// Test your code with mock adapter
}
}
#[cfg(feature = "typed-polkadot")]
use apex_sdk_substrate::polkadot;
async fn polkadot_example() -> Result<()> {
let adapter = SubstrateAdapter::connect("wss://polkadot.api.onfinality.io/public-ws").await?;
// Use typed metadata for compile-time safety
#[cfg(feature = "typed-polkadot")]
{
let balance = adapter.typed_balance::<polkadot::Runtime>(&address).await?;
}
Ok(())
}
Cargo.toml for specific versions