The AnchorAdapter trait provides a consistent API for integrating with multiple anchor services, regardless of their underlying protocol (SEP-24, SEP-31, custom implementations).
pub trait AnchorAdapter {
fn authenticate(&self, env: &Env, anchor: &Address, credentials: &Bytes) -> AuthResult;
fn deposit(&self, env: &Env, auth: &AuthResult, request: &DepositRequest) -> DepositResponse;
fn withdraw(&self, env: &Env, auth: &AuthResult, request: &WithdrawRequest) -> WithdrawResponse;
fn get_info(&self, env: &Env, anchor: &Address) -> AnchorInfo;
fn get_quote(&self, env: &Env, auth: &AuthResult, request: &QuoteRequest) -> Option<QuoteData>;
}Establishes authentication with an anchor service.
Parameters:
env: Soroban environmentanchor: Anchor's addresscredentials: Authentication credentials (API key, JWT, etc.)
Returns: AuthResult containing:
token: Authentication tokenexpires_at: Token expiration timestampanchor: Anchor address
Initiates a deposit transaction.
Parameters:
env: Soroban environmentauth: Authentication result fromauthenticate()request: Deposit parameters (asset, amount, destination, memo)
Returns: DepositResponse containing:
transaction_id: Unique transaction identifierstatus: Current transaction statusdeposit_address: Address to send funds toexpires_at: Deposit window expiration
Initiates a withdrawal transaction.
Parameters:
env: Soroban environmentauth: Authentication result fromauthenticate()request: Withdrawal parameters (asset, amount, destination, memo)
Returns: WithdrawResponse containing:
transaction_id: Unique transaction identifierstatus: Current transaction statusestimated_completion: Expected completion timestamp
Retrieves anchor capabilities and configuration.
Parameters:
env: Soroban environmentanchor: Anchor's address
Returns: AnchorInfo containing:
name: Anchor namesupported_services: List of supported service typessupported_assets: List of supported assetsmin_deposit/max_deposit: Deposit limitsmin_withdrawal/max_withdrawal: Withdrawal limits
Requests an exchange rate quote.
Parameters:
env: Soroban environmentauth: Authentication resultrequest: Quote parameters (assets, amount, operation type)
Returns: Option<QuoteData> - None if quotes not supported
use anchorkit::anchor_adapter::*;
use anchorkit::sep24_adapter::Sep24Adapter;
// Initialize adapter
let adapter = Sep24Adapter;
// Authenticate
let auth = adapter.authenticate(&env, &anchor_address, &credentials);
// Get anchor info
let info = adapter.get_info(&env, &anchor_address);
// Initiate deposit
let deposit_req = DepositRequest {
asset: String::from_str(&env, "USDC"),
amount: 100_0000000,
destination: user_address.clone(),
memo: None,
};
let deposit_resp = adapter.deposit(&env, &auth, &deposit_req);
// Initiate withdrawal
let withdraw_req = WithdrawRequest {
asset: String::from_str(&env, "USDC"),
amount: 50_0000000,
destination: String::from_str(&env, "bank_account_123"),
memo: None,
};
let withdraw_resp = adapter.withdraw(&env, &auth, &withdraw_req);To integrate a new anchor protocol:
- Create adapter struct:
pub struct CustomAdapter;- Implement AnchorAdapter trait:
impl AnchorAdapter for CustomAdapter {
fn authenticate(&self, env: &Env, anchor: &Address, credentials: &Bytes) -> AuthResult {
// Custom authentication logic
}
fn deposit(&self, env: &Env, auth: &AuthResult, request: &DepositRequest) -> DepositResponse {
// Custom deposit logic
}
// ... implement other methods
}- Handle protocol-specific details internally:
- HTTP endpoints and request formats
- Authentication mechanisms
- Response parsing
- Error handling
Reference implementation for SEP-24 compliant anchors (interactive deposits/withdrawals).
File: src/sep24_adapter.rs
Features:
- Standard SEP-24 authentication flow
- Interactive deposit/withdrawal support
- Info endpoint integration
- Consistency: Same API across all anchor integrations
- Flexibility: Easy to add new anchor protocols
- Maintainability: Protocol changes isolated to specific adapters
- Testability: Mock adapters for testing
- Composability: Adapters can be swapped at runtime
The adapter interface integrates with existing AnchorKit features:
- Session Management: Operations can be tracked in sessions
- Health Monitoring: Adapter calls can report health metrics
- Request ID Propagation: Request IDs flow through adapter calls
- Retry Logic: Failed adapter calls can use retry mechanisms
- Rate Limiting: Adapter calls respect rate limits
Adapters should handle errors gracefully and return appropriate error codes. Consider using AnchorKit's error mapping for consistent error reporting.
- Credentials: Never log or expose credentials
- Token Management: Implement token refresh logic
- Validation: Validate all inputs before making external calls
- Rate Limiting: Respect anchor rate limits
- Timeouts: Implement appropriate timeouts for all operations
Potential additions to the interface:
get_transaction_status()- Poll transaction statuscancel_transaction()- Cancel pending transactionsget_transaction_history()- Retrieve transaction historyupdate_kyc()- Submit KYC informationget_fees()- Query fee structure