pub struct TransactionManager { /* private fields */ }Expand description
Transaction manager responsible for wallet, sequence tracking, and transaction building.
This is the single source of truth for:
- Wallet and signing operations
- Sequence numbers (ensuring concurrent order operations don’t race)
- Authenticator resolution for permissioned key trading
§Thread Safety
All methods are safe to call from multiple tasks concurrently. Sequence allocation uses atomic compare-exchange operations for lock-free performance.
Implementations§
Source§impl TransactionManager
impl TransactionManager
Sourcepub fn new(
grpc_client: DydxGrpcClient,
private_key: &str,
wallet_address: String,
chain_id: ChainId,
) -> Result<Self, DydxError>
pub fn new( grpc_client: DydxGrpcClient, private_key: &str, wallet_address: String, chain_id: ChainId, ) -> Result<Self, DydxError>
Creates a new transaction manager.
Creates wallet from private key internally. The sequence number is initialized
to SEQUENCE_UNINITIALIZED and will be fetched from chain on first use, or
can be proactively initialized by calling Self::initialize_sequence.
§Errors
Returns error if wallet creation from private key fails.
Sourcepub async fn initialize_sequence(&self) -> Result<u64, DydxError>
pub async fn initialize_sequence(&self) -> Result<u64, DydxError>
Proactively initializes the sequence number from chain.
Call this during connect() to ensure orders can be submitted immediately without first-transaction latency penalty. Also catches auth errors early.
Returns the initialized sequence number.
§Errors
Returns error if chain query fails.
Sourcepub async fn resolve_authenticators(&self) -> Result<(), DydxError>
pub async fn resolve_authenticators(&self) -> Result<(), DydxError>
Resolves authenticator IDs if using permissioned keys (API wallet).
Compares the wallet’s signing address with the main account address. If they differ, fetches authenticators from chain and finds the one matching this wallet’s public key.
Call this during connect() after creating the TransactionManager.
§Errors
Returns error if:
- Using permissioned key but no authenticators found for main account
- No authenticator matches the wallet’s public key
- gRPC query fails
§Panics
Panics if the internal RwLock is poisoned.
Sourcepub async fn allocate_sequence(&self) -> Result<u64, DydxError>
pub async fn allocate_sequence(&self) -> Result<u64, DydxError>
Allocates the next sequence number atomically.
If the sequence is uninitialized (0), fetches from chain first. Uses compare-exchange for lock-free concurrent access.
§Errors
Returns error if chain query fails during initialization.
Sourcepub async fn allocate_sequences(
&self,
count: usize,
) -> Result<Vec<u64>, DydxError>
pub async fn allocate_sequences( &self, count: usize, ) -> Result<Vec<u64>, DydxError>
Allocates N sequence numbers for optimistic parallel broadcast.
Returns a vector of consecutive sequences that can be used concurrently. The caller is responsible for handling partial failures by resyncing.
§Arguments
count- Number of sequences to allocate
§Errors
Returns error if chain query fails during initialization.
§Example
let sequences = tx_manager.allocate_sequences(3).await?;
// sequences = [10, 11, 12] - three consecutive sequence numbersSourcepub async fn resync_sequence(&self) -> Result<(), DydxError>
pub async fn resync_sequence(&self) -> Result<(), DydxError>
Resyncs the sequence counter from chain after a mismatch error.
Called by the broadcaster’s retry logic when a sequence mismatch is detected. Unconditionally stores the chain’s current sequence.
§Errors
Returns error if chain query fails.
Sourcepub fn current_sequence(&self) -> u64
pub fn current_sequence(&self) -> u64
Returns the current sequence value without allocation.
Useful for logging and debugging. Returns SEQUENCE_UNINITIALIZED if not yet initialized.
Sourcepub async fn get_cached_sequence(&self) -> Result<u64, DydxError>
pub async fn get_cached_sequence(&self) -> Result<u64, DydxError>
Returns the cached sequence for short-term orders without incrementing.
§Errors
Returns error if chain query fails during initialization.
Sourcepub async fn build_transaction(
&self,
msgs: Vec<Any>,
sequence: u64,
operation: &str,
) -> Result<PreparedTransaction, DydxError>
pub async fn build_transaction( &self, msgs: Vec<Any>, sequence: u64, operation: &str, ) -> Result<PreparedTransaction, DydxError>
Builds and signs a transaction with the given messages and sequence.
Uses cached account_number (fetched once from chain) to avoid repeated queries.
§Arguments
msgs- Proto messages to include in transactionsequence- Pre-allocated sequence numberoperation- Human-readable name for logging
§Errors
Returns error if account lookup fails or transaction building fails.
§Panics
Panics if the internal RwLock is poisoned.
Sourcepub async fn prepare_transaction(
&self,
msgs: Vec<Any>,
operation: &str,
) -> Result<PreparedTransaction, DydxError>
pub async fn prepare_transaction( &self, msgs: Vec<Any>, operation: &str, ) -> Result<PreparedTransaction, DydxError>
Convenience method: allocate sequence, build, and return prepared transaction.
This is the typical flow for single transaction submission.
§Arguments
msgs- Proto messages to include in transactionoperation- Human-readable name for logging
§Errors
Returns error if sequence allocation or transaction building fails.
Sourcepub fn wallet_address(&self) -> &str
pub fn wallet_address(&self) -> &str
Returns the wallet address.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TransactionManager
impl !RefUnwindSafe for TransactionManager
impl Send for TransactionManager
impl Sync for TransactionManager
impl Unpin for TransactionManager
impl UnsafeUnpin for TransactionManager
impl !UnwindSafe for TransactionManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].