Skip to main content

TransactionManager

Struct TransactionManager 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 numbers
Source

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.

Source

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.

Source

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.

Source

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 transaction
  • sequence - Pre-allocated sequence number
  • operation - Human-readable name for logging
§Errors

Returns error if account lookup fails or transaction building fails.

§Panics

Panics if the internal RwLock is poisoned.

Source

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 transaction
  • operation - Human-readable name for logging
§Errors

Returns error if sequence allocation or transaction building fails.

Source

pub fn wallet_address(&self) -> &str

Returns the wallet address.

Source

pub fn chain_id(&self) -> &ChainId

Returns the chain ID.

Trait Implementations§

Source§

impl Debug for TransactionManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Ungil for T
where T: Send,