Skip to content

Conversation

@WGB5445
Copy link
Contributor

@WGB5445 WGB5445 commented Nov 9, 2025

Summary

This PR enhances the AptosClientBuilder with several new features to improve flexibility and usability:

  • API Key Support: Added api_key() method to easily configure Bearer token authentication
  • Custom Headers: Added header() method to set custom HTTP headers
  • Timeout Configuration: Added timeout() method to configure request timeouts (default: 5 seconds)
  • Automatic Chain ID Fetching: The builder now automatically fetches chain_id from the server if not explicitly set
  • Improved Error Handling: build() method now returns Result for better error handling

Changes

AptosClientBuilder

  • Added api_key() method for Bearer token authentication
  • Added header() method for custom HTTP headers
  • Added timeout() method for request timeout configuration
  • Made build() method async and return Result<AptosFullnodeClient, anyhow::Error>
  • Automatically fetches chain_id from /v1 endpoint when not provided

AptosNetwork

  • Added optional chain_id field
  • Added with_chain_id() method for chain ID configuration
  • Enhanced flexibility to work with or without explicit chain ID

New Types

  • Added LedgerInfo struct to represent ledger information from RPC API
  • Supports deserializing chain ID and other ledger metadata

Testing

  • Added comprehensive tests for invalid URLs, unreachable servers, and chain ID fetching
  • Tests verify proper error handling for connection failures
  • Tests validate automatic chain ID retrieval from server

Benefits

  • Better Authentication: Easy API key configuration for authenticated requests
  • More Flexibility: Support for custom headers and timeout configuration
  • Improved UX: Automatic chain ID fetching reduces boilerplate code
  • Better Error Handling: Proper error propagation with Result types

Testing

All tests pass, including new tests for:

  • Invalid URL handling
  • Unreachable server scenarios
  • Automatic chain ID fetching from server

- 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.
@WGB5445 WGB5445 requested a review from gregnazario November 9, 2025 17:42
- Updated the `new` method in `AptosClientBuilder` to accept an `Option<HeaderMap>` and utilize `unwrap_or_default()` for cleaner initialization of headers.
Copy link

Copilot AI left a 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 fetch chain_id from the server when not explicitly set
  • Updated AptosNetwork to support optional chain IDs and provide builder methods
  • Added LedgerInfo struct to deserialize ledger information from the /v1 endpoint

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::env import is no longer used after removing the X_API_KEY environment 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 })
Copy link

Copilot AI Nov 18, 2025

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.

Suggested change
Ok(Self { client: client })
Ok(Self { client })

Copilot uses AI. Check for mistakes.
Comment on lines +24 to 28
/// * `coin_name` - Coin struct name (e.g., "AptosCoin")
pub async fn get_balance(
&self,
address: &str,
coin_type: &str,
Copy link

Copilot AI Nov 18, 2025

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.

Suggested change
/// * `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,

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants