-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Enhance AptosClientBuilder with API key support, custom headers, and automatic chain_id fetching #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Updated `AptosClientBuilder` to accept optional headers and return a result in the `build` method, allowing for better error handling. - Modified `AptosNetwork` struct to include an optional `chain_id` and updated related methods for flexibility. - Introduced `LedgerInfo` struct to represent ledger information from the RPC API, enhancing data handling capabilities. - Updated various tests to accommodate the new async structure and improved error handling. - Added `anyhow` dependency for better error management across the SDK.
- Updated the `new` method in `AptosClientBuilder` to accept an `Option<HeaderMap>` and utilize `unwrap_or_default()` for cleaner initialization of headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the AptosClientBuilder to support API key authentication, custom headers, configurable timeouts, and automatic chain ID fetching. The builder's build() method is now async and returns a Result for better error handling. The changes improve flexibility while requiring callers to handle the async build process and potential errors.
- Added API key, custom header, and timeout configuration methods to
AptosClientBuilder - Made
build()async to automatically fetchchain_idfrom the server when not explicitly set - Updated
AptosNetworkto support optional chain IDs and provide builder methods - Added
LedgerInfostruct to deserialize ledger information from the/v1endpoint
Reviewed Changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/aptos-rust-sdk/src/client/builder.rs | Enhanced builder with API key, header, and timeout methods; made build() async with automatic chain_id fetching and error handling |
| crates/aptos-rust-sdk/src/client/config.rs | Made indexer_url optional and added chain_id field with builder methods |
| crates/aptos-rust-sdk/src/client/rest_api.rs | Updated client instantiation to use async build() method; removed debug print statement |
| crates/aptos-rust-sdk-types/src/api_types/ledger_info.rs | Added new LedgerInfo struct for deserializing ledger information from RPC |
| crates/aptos-rust-sdk-types/src/api_types/numbers.rs | Added utility methods to U64 for easier value access |
| crates/examples/src/view_function_example.rs | Converted example code to tests and updated to use async builder |
| crates/examples/src/lib.rs | Updated tests to use async builder and proper error propagation |
Comments suppressed due to low confidence (1)
crates/aptos-rust-sdk/src/client/builder.rs:9
- The
std::envimport is no longer used after removing theX_API_KEYenvironment variable handling. This unused import should be removed.
use std::env;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| pub async fn new(network: AptosNetwork) -> Result<Self, anyhow::Error> { | ||
| let builder = AptosClientBuilder::new(network, None); | ||
| let client = builder.build().await?; | ||
| Ok(Self { client: client }) |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant explicit field naming. The expression client: client can be simplified to just client using Rust's field init shorthand.
| Ok(Self { client: client }) | |
| Ok(Self { client }) |
| /// * `coin_name` - Coin struct name (e.g., "AptosCoin") | ||
| pub async fn get_balance( | ||
| &self, | ||
| address: &str, | ||
| coin_type: &str, |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking API change to get_balance(). The method signature changed from 2 parameters to 3 parameters, requiring callers to now pass coin_name explicitly. Consider if this breaking change is intentional or if backward compatibility should be maintained.
| /// * `coin_name` - Coin struct name (e.g., "AptosCoin") | |
| pub async fn get_balance( | |
| &self, | |
| address: &str, | |
| coin_type: &str, | |
| /// * `coin_name` - Coin struct name (e.g., "AptosCoin") | |
| /// Backward-compatible helper method to get account balance for standard coins | |
| pub async fn get_balance( | |
| &self, | |
| address: &str, | |
| coin_type: &str, | |
| ) -> Result<Value, Box<dyn std::error::Error>> { | |
| // Default coin name for standard coins | |
| self.get_balance_with_name(address, coin_type, "AptosCoin").await | |
| } | |
| /// Helper method to get account balance for custom coins | |
| pub async fn get_balance_with_name( | |
| &self, | |
| address: &str, | |
| coin_type: &str, |
Summary
This PR enhances the
AptosClientBuilderwith several new features to improve flexibility and usability:api_key()method to easily configure Bearer token authenticationheader()method to set custom HTTP headerstimeout()method to configure request timeouts (default: 5 seconds)chain_idfrom the server if not explicitly setbuild()method now returnsResultfor better error handlingChanges
AptosClientBuilder
api_key()method for Bearer token authenticationheader()method for custom HTTP headerstimeout()method for request timeout configurationbuild()method async and returnResult<AptosFullnodeClient, anyhow::Error>chain_idfrom/v1endpoint when not providedAptosNetwork
chain_idfieldwith_chain_id()method for chain ID configurationNew Types
LedgerInfostruct to represent ledger information from RPC APITesting
Benefits
ResulttypesTesting
All tests pass, including new tests for: