dydx 0.3.0

dYdX v4 asynchronous client.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
mod authenticators;
pub mod error;
mod governance;
mod megavault;
mod methods;

pub use self::authenticators::Authenticator;
pub use self::governance::Governance;
use self::{authenticators::Authenticators, megavault::MegaVault};
use super::{
    builder::TxBuilder, config::NodeConfig, order::*, sequencer::*, utils::*, wallet::Account,
};
pub use crate::indexer::{
    Address, ClientId, Height, OrderFlags, Subaccount, Ticker, Tokenized, Usdc,
};

use anyhow::{anyhow as err, Error, Result};
use bigdecimal::{
    num_bigint::{BigInt, Sign},
    BigDecimal, Signed,
};
#[cfg(feature = "noble")]
use chrono::{TimeDelta, Utc};
use cosmrs::tx::{self, Tx};
use derive_more::{Deref, DerefMut};
use dydx_proto::{
    cosmos_sdk_proto::{
        cosmos::{
            auth::v1beta1::query_client::QueryClient as AuthClient,
            bank::v1beta1::{query_client::QueryClient as BankClient, MsgSend},
            base::{
                abci::v1beta1::GasInfo,
                tendermint::v1beta1::service_client::ServiceClient as BaseClient,
            },
            distribution::v1beta1::query_client::QueryClient as DistributionClient,
            gov::v1::query_client::QueryClient as GovClient,
            staking::v1beta1::query_client::QueryClient as StakingClient,
            tx::v1beta1::{
                service_client::ServiceClient as TxClient, BroadcastMode, BroadcastTxRequest,
                GetTxRequest, SimulateRequest,
            },
        },
        traits::Message,
    },
    dydxprotocol::{
        accountplus::query_client::QueryClient as AccountPlusClient,
        affiliates::{
            query_client::QueryClient as AffiliatesClient, AffiliateInfoRequest,
            AffiliateInfoResponse, AffiliateTiers, AffiliateWhitelist, AffiliateWhitelistRequest,
            AllAffiliateTiersRequest, ReferredByRequest,
        },
        blocktime::query_client::QueryClient as BlocktimeClient,
        bridge::query_client::QueryClient as BridgeClient,
        clob::{
            query_client::QueryClient as ClobClient, MsgBatchCancel, MsgCancelOrder, MsgPlaceOrder,
            Order, OrderBatch,
        },
        feetiers::query_client::QueryClient as FeeTiersClient,
        listing::MsgCreateMarketPermissionless,
        perpetuals::query_client::QueryClient as PerpetualsClient,
        prices::query_client::QueryClient as PricesClient,
        ratelimit::query_client::QueryClient as RatelimitClient,
        revshare::{
            query_client::QueryClient as RevshareClient, MarketMapperRevShareDetails,
            MarketMapperRevenueShareParams, MsgSetOrderRouterRevShare, OrderRouterRevShare,
            QueryMarketMapperRevShareDetails, QueryMarketMapperRevenueShareParams,
            QueryUnconditionalRevShareConfig, UnconditionalRevShareConfig,
        },
        rewards::query_client::QueryClient as RewardsClient,
        sending::{MsgCreateTransfer, MsgDepositToSubaccount, MsgWithdrawFromSubaccount, Transfer},
        stats::query_client::QueryClient as StatsClient,
        subaccounts::{
            query_client::QueryClient as SubaccountsClient,
            QueryGetWithdrawalAndTransfersBlockedInfoRequest,
            QueryGetWithdrawalAndTransfersBlockedInfoResponse, SubaccountId,
        },
        vault::query_client::QueryClient as VaultClient,
    },
    ToAny,
};
pub use error::*;
#[cfg(feature = "noble")]
use ibc_proto::{
    cosmos::base::v1beta1::Coin as IbcProtoCoin,
    ibc::applications::transfer::v1::MsgTransfer as IbcMsgTransfer,
};
use std::iter;
use tokio::time::{sleep, Duration};
use tokio_tungstenite::tungstenite::http::uri::PathAndQuery;
use tonic::codec::ProstCodec;
use tonic::{
    client::Grpc,
    transport::{Channel, ClientTlsConfig},
    Code,
};
use tower::timeout::Timeout;

#[cfg(feature = "telemetry")]
use crate::telemetry::{
    LatencyMetric, TELEMETRY_BATCH_CANCEL_ORDER_DURATION, TELEMETRY_CANCEL_ORDER_DURATION,
    TELEMETRY_DESC_BATCH_CANCEL_ORDER_DURATION, TELEMETRY_DESC_CANCEL_ORDER_DURATION,
    TELEMETRY_DESC_ORDERS_CANCELLED, TELEMETRY_DESC_ORDERS_PLACED,
    TELEMETRY_DESC_PLACE_ORDER_DURATION, TELEMETRY_DESC_QUERY_TX_DURATION, TELEMETRY_LABEL_ADDRESS,
    TELEMETRY_ORDERS_CANCELLED, TELEMETRY_ORDERS_PLACED, TELEMETRY_PLACE_ORDER_DURATION,
    TELEMETRY_QUERY_TX_DURATION,
};

const DEFAULT_QUERY_TIMEOUT_SECS: u64 = 15;
const DEFAULT_QUERY_INTERVAL_SECS: u64 = 2;

/// Transaction hash.
///
/// internally Cosmos uses tendermint::Hash
pub type TxHash = String;

/// Wrapper over standard [Cosmos modules](https://github.com/cosmos/cosmos-sdk/tree/main/x) clients
/// and [dYdX modules](https://github.com/dydxprotocol/v4-chain/tree/main/protocol/x) clients.
pub struct Routes {
    /// Smart account features, includes authenticators.
    pub accountplus: AccountPlusClient<Timeout<Channel>>,
    // Affiliates
    pub affiliates: AffiliatesClient<Timeout<Channel>>,
    /// Authentication of accounts and transactions for Cosmos SDK applications.
    pub auth: AuthClient<Timeout<Channel>>,
    /// Token transfer functionalities.
    pub bank: BankClient<Timeout<Channel>>,
    /// Basic network information.
    pub base: BaseClient<Timeout<Channel>>,
    /// dYdX bridge to Ethereum
    pub bridge: BridgeClient<Timeout<Channel>>,
    /// dYdX orderbook
    pub clob: ClobClient<Timeout<Channel>>,
    /// dYdX distribution
    pub distribution: DistributionClient<Timeout<Channel>>,
    /// dYdX fees
    pub feetiers: FeeTiersClient<Timeout<Channel>>,
    /// dYdX perpetuals
    pub perpetuals: PerpetualsClient<Timeout<Channel>>,
    /// dYdX prices
    pub prices: PricesClient<Timeout<Channel>>,
    /// dYdX ratelimit
    pub ratelimit: RatelimitClient<Timeout<Channel>>,
    /// dYdX rewards
    pub rewards: RewardsClient<Timeout<Channel>>,
    /// dYdX governance
    pub governance: GovClient<Timeout<Channel>>,
    /// Proof-of-Stake layer for public blockchains.
    pub staking: StakingClient<Timeout<Channel>>,
    /// dYdX stats
    pub stats: StatsClient<Timeout<Channel>>,
    /// dYdX subaccounts
    pub subaccounts: SubaccountsClient<Timeout<Channel>>,
    /// Tx utilities for the Cosmos SDK.
    pub tx: TxClient<Timeout<Channel>>,
    /// Query client
    pub query: Grpc<Timeout<Channel>>,
    /// Vaults
    pub vault: VaultClient<Timeout<Channel>>,
    /// dYdX blocktime
    pub blocktime: BlocktimeClient<Timeout<Channel>>,
    /// Revshare
    pub revshare: RevshareClient<Timeout<Channel>>,
}

impl Routes {
    /// Creates new modules clients wrapper.
    pub fn new(channel: Timeout<Channel>) -> Self {
        Self {
            accountplus: AccountPlusClient::new(channel.clone()),
            affiliates: AffiliatesClient::new(channel.clone()),
            auth: AuthClient::new(channel.clone()),
            bank: BankClient::new(channel.clone()),
            base: BaseClient::new(channel.clone()),
            bridge: BridgeClient::new(channel.clone()),
            clob: ClobClient::new(channel.clone()),
            distribution: DistributionClient::new(channel.clone()),
            feetiers: FeeTiersClient::new(channel.clone()),
            perpetuals: PerpetualsClient::new(channel.clone()),
            prices: PricesClient::new(channel.clone()),
            ratelimit: RatelimitClient::new(channel.clone()),
            rewards: RewardsClient::new(channel.clone()),
            governance: GovClient::new(channel.clone()),
            staking: StakingClient::new(channel.clone()),
            stats: StatsClient::new(channel.clone()),
            subaccounts: SubaccountsClient::new(channel.clone()),
            tx: TxClient::new(channel.clone()),
            query: Grpc::new(channel.clone()),
            blocktime: BlocktimeClient::new(channel.clone()),
            vault: VaultClient::new(channel.clone()),
            revshare: RevshareClient::new(channel),
        }
    }
}

/// Node (validator) client.
///
/// Serves to manage [orders](OrderBuilder) and funds, query transactions.
#[derive(Deref, DerefMut)]
pub struct NodeClient {
    config: NodeConfig,
    /// Transactions builder.
    pub builder: TxBuilder,
    #[deref]
    #[deref_mut]
    routes: Routes,
    sequencer: Box<dyn Sequencer>,
}

impl NodeClient {
    /// Connect to the node.
    pub async fn connect(config: NodeConfig) -> Result<Self, Error> {
        let tls = ClientTlsConfig::new().with_native_roots();
        let endpoint = config.endpoint.clone();
        let channel = Channel::from_shared(endpoint)?
            .tls_config(tls)?
            .connect()
            .await?;
        let timeout = Duration::from_millis(config.timeout);
        let timeout_channel = Timeout::new(channel, timeout);
        let chain_id = config.chain_id.clone().try_into()?;
        let builder = TxBuilder::new(chain_id, config.fee_denom.clone());
        let sequencer = Box::new(QueryingSequencer::new(timeout_channel.clone()));

        #[cfg(feature = "telemetry")]
        {
            metrics::describe_counter!(
                TELEMETRY_ORDERS_PLACED,
                metrics::Unit::Count,
                TELEMETRY_DESC_ORDERS_PLACED
            );
            metrics::describe_counter!(
                TELEMETRY_ORDERS_CANCELLED,
                metrics::Unit::Count,
                TELEMETRY_DESC_ORDERS_CANCELLED
            );
            metrics::describe_histogram!(
                TELEMETRY_PLACE_ORDER_DURATION,
                metrics::Unit::Milliseconds,
                TELEMETRY_DESC_PLACE_ORDER_DURATION
            );
            metrics::describe_histogram!(
                TELEMETRY_CANCEL_ORDER_DURATION,
                metrics::Unit::Milliseconds,
                TELEMETRY_DESC_CANCEL_ORDER_DURATION
            );
            metrics::describe_histogram!(
                TELEMETRY_BATCH_CANCEL_ORDER_DURATION,
                metrics::Unit::Milliseconds,
                TELEMETRY_DESC_BATCH_CANCEL_ORDER_DURATION
            );
            metrics::describe_histogram!(
                TELEMETRY_QUERY_TX_DURATION,
                metrics::Unit::Milliseconds,
                TELEMETRY_DESC_QUERY_TX_DURATION
            );
        }

        Ok(Self {
            config,
            builder,
            routes: Routes::new(timeout_channel),
            sequencer,
        })
    }

    /// Set `NodeClient`'s account sequence number mechanism
    pub fn with_sequencer(&mut self, sequencer: impl Sequencer) {
        self.sequencer = Box::new(sequencer);
    }

    /// Place [`Order`].
    ///
    /// Check [the short-order example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/place_order_short_term.rs)
    /// and [the long-term order example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/place_order_long_term.rs).
    pub async fn place_order(
        &mut self,
        account: &mut Account,
        order: Order,
    ) -> Result<TxHash, NodeError> {
        #[cfg(feature = "telemetry")]
        LatencyMetric::new(TELEMETRY_PLACE_ORDER_DURATION);
        let is_short_term = order
            .order_id
            .as_ref()
            .is_some_and(|id| id.order_flags == OrderFlags::ShortTerm as u32);

        // Check if order requires authorization
        let mut auth = None;
        if is_short_term {
            let order_for = &order
                .order_id
                .as_ref()
                .ok_or_else(|| err!("Order does not have an ID"))?
                .subaccount_id
                .as_ref()
                .ok_or_else(|| err!("Order does not have a subaccount ID"))?
                .owner;
            if order_for != account.address().as_ref() {
                auth = Some(order_for.parse::<Address>()?);
            }
        }

        let msg = MsgPlaceOrder { order: Some(order) };

        let tx_raw = self
            .create_base_transaction(account, msg, !is_short_term, auth.as_ref())
            .await?;

        let tx_hash = self.broadcast_transaction(tx_raw).await?;

        #[cfg(feature = "telemetry")]
        {
            let address = account.address();
            metrics::counter!(
                TELEMETRY_ORDERS_PLACED,
                &[(TELEMETRY_LABEL_ADDRESS, address.to_string())]
            )
            .increment(1);
        }

        Ok(tx_hash)
    }

    /// Cancel [`Order`].
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/cancel_order.rs).
    pub async fn cancel_order(
        &mut self,
        account: &mut Account,
        order_id: OrderId,
        until: impl Into<OrderGoodUntil>,
    ) -> Result<TxHash, NodeError> {
        #[cfg(feature = "telemetry")]
        LatencyMetric::new(TELEMETRY_CANCEL_ORDER_DURATION);

        let until = until.into();
        let msg = MsgCancelOrder {
            order_id: Some(order_id),
            good_til_oneof: Some(until.try_into()?),
        };

        let tx_raw = self
            .create_base_transaction(account, msg, true, None)
            .await?;

        let tx_hash = self.broadcast_transaction(tx_raw).await?;

        #[cfg(feature = "telemetry")]
        {
            let address = account.address();
            metrics::counter!(
                TELEMETRY_ORDERS_CANCELLED,
                &[(TELEMETRY_LABEL_ADDRESS, address.to_string())]
            )
            .increment(1);
        }

        Ok(tx_hash)
    }

    /// Cancel a batch of short-terms [`Order`]s.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/batch_cancel_orders.rs).
    pub async fn batch_cancel_orders(
        &mut self,
        account: &mut Account,
        subaccount: Subaccount,
        short_term_cancels: Vec<OrderBatch>,
        until_block: Height,
    ) -> Result<TxHash, NodeError> {
        #[cfg(feature = "telemetry")]
        LatencyMetric::new(TELEMETRY_BATCH_CANCEL_ORDER_DURATION);

        #[cfg(feature = "telemetry")]
        let count: u64 = short_term_cancels
            .iter()
            .map(|batch| batch.client_ids.len() as u64)
            .sum();

        let msg = MsgBatchCancel {
            subaccount_id: Some(subaccount.into()),
            short_term_cancels,
            good_til_block: until_block.0,
        };

        let tx_raw = self
            .create_base_transaction(account, msg, true, None)
            .await?;

        let tx_hash = self.broadcast_transaction(tx_raw).await?;

        #[cfg(feature = "telemetry")]
        {
            let address = account.address();
            metrics::counter!(
                TELEMETRY_ORDERS_CANCELLED,
                &[(TELEMETRY_LABEL_ADDRESS, address.to_string())]
            )
            .increment(count);
        }

        Ok(tx_hash)
    }

    /// Deposit funds (USDC) from the address to the subaccount.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/deposit.rs).
    pub async fn deposit(
        &mut self,
        account: &mut Account,
        sender: Address,
        recipient: Subaccount,
        amount: impl Into<Usdc>,
    ) -> Result<TxHash, NodeError> {
        let msg = MsgDepositToSubaccount {
            sender: sender.to_string(),
            recipient: Some(recipient.into()),
            asset_id: 0,
            quantums: amount.into().quantize_as_u64()?,
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Withdraw funds (USDC) from the subaccount to the address.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/withdraw.rs).
    pub async fn withdraw(
        &mut self,
        account: &mut Account,
        sender: Subaccount,
        recipient: Address,
        amount: impl Into<Usdc>,
    ) -> Result<TxHash, NodeError> {
        let msg = MsgWithdrawFromSubaccount {
            sender: Some(sender.into()),
            recipient: recipient.to_string(),
            asset_id: 0,
            quantums: amount.into().quantize_as_u64()?,
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Transfer funds (USDC) between subaccounts.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/transfer.rs).
    pub async fn transfer(
        &mut self,
        account: &mut Account,
        sender: Subaccount,
        recipient: Subaccount,
        amount: impl Into<Usdc>,
    ) -> Result<TxHash, NodeError> {
        let transfer = Transfer {
            sender: Some(sender.into()),
            recipient: Some(recipient.into()),
            asset_id: 0,
            amount: amount.into().quantize_as_u64()?,
        };
        let msg = MsgCreateTransfer {
            transfer: Some(transfer),
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Transfer a token asset from one address to another one.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/send_token.rs).
    pub async fn send_token(
        &mut self,
        account: &mut Account,
        sender: Address,
        recipient: Address,
        token: impl Tokenized,
    ) -> Result<TxHash, NodeError> {
        let msg = MsgSend {
            from_address: sender.to_string(),
            to_address: recipient.to_string(),
            amount: vec![token.coin()?],
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Transfer a token asset between blockchain networks.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/noble_transfer.rs).
    #[cfg(feature = "noble")]
    pub async fn send_token_ibc(
        &mut self,
        account: &mut Account,
        sender: Address,
        recipient: Address,
        token: impl Tokenized,
        source_channel: String,
    ) -> Result<TxHash, NodeError> {
        let coin = token.coin()?;
        let timeout = (Utc::now() + TimeDelta::seconds(60))
            .timestamp_nanos_opt()
            .ok_or_else(|| err!("Failed calculating timeout ns timestamp"))?
            .try_into()
            .map_err(|e| err!("Failed converting timestamp into u64: {e}"))?;

        let msg = IbcMsgTransfer {
            receiver: recipient.to_string(),
            sender: sender.to_string(),
            source_port: "transfer".to_string(),
            source_channel,
            timeout_timestamp: timeout,
            token: Some(IbcProtoCoin {
                amount: coin.amount,
                denom: coin.denom,
            }),
            timeout_height: None,
            memo: Default::default(),
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Close position for a given market.
    ///
    /// Opposite short-term market orders are used.
    /// If provided, the position is only reduced by a size of `reduce_by`.
    /// Note that at the moment dYdX [doesn't support](https://dydx.exchange/faq) spot trading.
    ///
    /// Check [the first example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/close_position.rs)
    /// and [the second example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/close_all_positions.rs).
    pub async fn close_position(
        &mut self,
        account: &mut Account,
        subaccount: Subaccount,
        market_params: impl Into<OrderMarketParams>,
        reduce_by: Option<BigDecimal>,
        client_id: impl Into<ClientId>,
    ) -> Result<Option<TxHash>, NodeError> {
        let subaccount_info = self.get_subaccount(&subaccount).await?;
        let market_params = market_params.into();
        let quantums_opt = subaccount_info
            .perpetual_positions
            .into_iter()
            .find(|pos| pos.perpetual_id == market_params.clob_pair_id.0)
            .map(|pos| BigInt::from_serializable_int(&pos.quantums))
            .transpose()?;

        let (side, size) = if let Some(quantums) = quantums_opt {
            let side = match quantums.sign() {
                Sign::Plus => OrderSide::Sell,
                Sign::Minus => OrderSide::Buy,
                _ => return Ok(None),
            };
            let mut size = market_params.dequantize_quantums(quantums.abs());
            if let Some(reduce_by) = reduce_by {
                // The quantity to reduce by should not be larger than the
                // current position
                size = size.min(reduce_by);
            }
            (side, size)
        } else {
            return Ok(None);
        };

        let height = self.latest_block_height().await?;

        let (_, order) = OrderBuilder::new(market_params, subaccount.clone())
            .market(side, size)
            .until(height.ahead(SHORT_TERM_ORDER_MAXIMUM_LIFETIME))
            .build(client_id.into())?;
        let tx_hash = self.place_order(account, order).await?;

        Ok(Some(tx_hash))
    }

    /// Access the vaults requests dispatcher
    pub fn megavault(&mut self) -> MegaVault<'_> {
        MegaVault::new(self)
    }

    /// Access the governance requests dispatcher
    pub fn governance(&mut self) -> Governance<'_> {
        Governance::new(self)
    }

    /// Access the authenticators/permissioned keys requests dispatcher.
    ///
    /// See the [example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/authenticator.rs).
    pub fn authenticators(&mut self) -> Authenticators<'_> {
        Authenticators::new(self)
    }

    /// Create a market permissionless
    pub async fn create_market_permissionless(
        &mut self,
        account: &mut Account,
        ticker: &Ticker,
        subaccount: &Subaccount,
    ) -> Result<TxHash, NodeError> {
        let subaccount_id = SubaccountId {
            owner: subaccount.address.to_string(),
            number: subaccount.number.0,
        };
        let msg = MsgCreateMarketPermissionless {
            ticker: ticker.to_string(),
            subaccount_id: Some(subaccount_id),
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Simulate a transaction.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/withdraw_other.rs).
    pub async fn simulate(&mut self, tx_raw: &tx::Raw) -> Result<GasInfo, NodeError> {
        let request = SimulateRequest {
            tx_bytes: tx_raw
                .to_bytes()
                .map_err(|e| err!("Raw Tx to bytes failed: {}", e))?,
            ..Default::default()
        };

        // Move to client/methods.rs
        self.tx
            .simulate(request)
            .await
            .map_err(BroadcastError::from)?
            .into_inner()
            .gas_info
            .ok_or_else(|| err!("Tx simulation request failed, gas info should exist.").into())
    }

    /// Fetch account's number and sequence number from the network.
    pub async fn query_address(&mut self, address: &Address) -> Result<(u64, u64), Error> {
        self.get_account(address)
            .await
            .map(|res| (res.account_number, res.sequence))
    }

    /// Create a transaction.
    pub async fn create_transaction(
        &mut self,
        account: &mut Account,
        msg: impl ToAny,
        auth: Option<&Address>,
    ) -> Result<tx::Raw, NodeError> {
        let tx_raw = self
            .create_base_transaction(account, msg, true, auth)
            .await?;
        let tx_bytes = tx_raw
            .to_bytes()
            .map_err(|e| err!("Raw Tx to bytes failed: {e}"))?;
        let tx = Tx::from_bytes(&tx_bytes)
            .map_err(|e| err!("Failed to decode received Tx bytes: {e}"))?;

        let simulated = self.simulate(&tx_raw).await?;
        let gas = simulated.gas_used;
        let fee = self.builder.calculate_fee(Some(gas))?;
        self.builder
            .build_transaction(account, tx.body.messages, Some(fee), None)
            .map_err(|e| e.into())
    }

    async fn create_base_transaction(
        &mut self,
        account: &mut Account,
        msg: impl ToAny,
        seqnum_required: bool,
        auth: Option<&Address>,
    ) -> Result<tx::Raw, Error> {
        if seqnum_required && self.config.manage_sequencing {
            if let Some(authing) = auth {
                if let Some((master, _)) = account.authenticators_mut().get_mut(authing) {
                    let nonce = self.sequencer.next_nonce(master.address()).await?;
                    master.set_next_nonce(nonce);
                }
            } else {
                let nonce = self.sequencer.next_nonce(account.address()).await?;
                account.set_next_nonce(nonce);
            }
        } else if !seqnum_required {
            if let Some(authing) = auth {
                if let Some((master, _)) = account.authenticators_mut().get_mut(authing) {
                    master.set_next_nonce(Nonce::Sequence(master.sequence_number()));
                }
            } else {
                account.set_next_nonce(Nonce::Sequence(account.sequence_number()));
            }
        }

        self.builder
            .build_transaction(account, iter::once(msg.to_any()), None, auth)
    }

    /// Broadcast a transaction
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/withdraw_other.rs).
    pub async fn broadcast_transaction(&mut self, tx_raw: tx::Raw) -> Result<TxHash, NodeError> {
        let request = BroadcastTxRequest {
            tx_bytes: tx_raw
                .to_bytes()
                .map_err(|e| err!("Raw Tx to bytes failed: {}", e))?,
            mode: BroadcastMode::Sync.into(),
        };

        let response = self
            .tx
            .broadcast_tx(request)
            .await
            .map_err(BroadcastError::from)?
            .into_inner()
            .tx_response
            .ok_or_else(|| err!("Tx not present in broadcast response"))?;

        if response.code == 0 {
            Ok(response.txhash)
        } else {
            Err(NodeError::Broadcast(BroadcastError {
                code: Some(response.code),
                message: response.raw_log,
            }))
        }
    }

    /// Generic query method.
    ///
    /// Note: specify the return type explicitly, to avoid warnings.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn send_query<I, O>(&mut self, msg: I, method: &'static str) -> Result<O, Error>
    where
        I: 'static + ToAny,
        O: Message + Default + 'static,
    {
        let path = PathAndQuery::from_static(method);
        let req = tonic::Request::new(msg);

        self.query
            .ready()
            .await
            .map_err(|e| err!("Service was not ready: {e}"))?;

        let response: tonic::Response<O> =
            self.query.unary(req, path, ProstCodec::default()).await?;

        Ok(response.into_inner())
    }

    /// Query the network for a transaction
    pub async fn query_transaction(&mut self, tx_hash: &TxHash) -> Result<Tx, Error> {
        #[cfg(feature = "telemetry")]
        LatencyMetric::new(TELEMETRY_QUERY_TX_DURATION);

        let attempts = DEFAULT_QUERY_TIMEOUT_SECS / DEFAULT_QUERY_INTERVAL_SECS;
        for _ in 0..attempts {
            match self
                .tx
                .get_tx(GetTxRequest {
                    hash: tx_hash.clone(),
                })
                .await
            {
                Ok(r) => {
                    let response = r
                        .into_inner()
                        .tx_response
                        .ok_or_else(|| err!("Tx not present in broadcast response"))?;
                    let tx_bytes = response
                        .tx
                        .ok_or_else(|| err!("TxResponse does not contain Tx bytes!"))?
                        .value;
                    let tx = Tx::from_bytes(&tx_bytes)
                        .map_err(|e| err!("Failed to decode received Tx bytes: {e}"))?;
                    return Ok(tx);
                }
                Err(status) if status.code() == Code::NotFound => {
                    sleep(Duration::from_secs(DEFAULT_QUERY_INTERVAL_SECS)).await;
                }
                Err(status) => {
                    return Err(err!("Error querying Tx {tx_hash}: {status}"));
                }
            }
        }
        Err(err!("Tx {tx_hash} not found after timeout"))
    }

    /// Query the network for a transaction result
    pub async fn query_transaction_result(
        &mut self,
        tx_hash: Result<TxHash, NodeError>,
    ) -> Result<Option<Tx>, Error> {
        match tx_hash {
            Ok(tx_hash) => self.query_transaction(&tx_hash).await.map(Some),
            Err(NodeError::Broadcast(err)) if err.get_collateral_reason().is_some() => Ok(None),
            Err(err) => Err(err.into()),
        }
    }

    /// Query the withrawal and transfer gating status.
    /// Information about whether withdrawals and transfers are blocked for
    /// a collateral pool associated with the passed in perpetual ID.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_withdrawal_and_transfer_gating_status(
        &mut self,
        perpetual_id: u32,
    ) -> Result<QueryGetWithdrawalAndTransfersBlockedInfoResponse, Error> {
        let req = QueryGetWithdrawalAndTransfersBlockedInfoRequest { perpetual_id };

        let info = self
            .subaccounts
            .get_withdrawal_and_transfers_blocked_info(req)
            .await?
            .into_inner();

        Ok(info)
    }

    /// Query the affiliate info for the given address.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_affiliate_info(
        &mut self,
        address: &Address,
    ) -> Result<AffiliateInfoResponse, Error> {
        let request = AffiliateInfoRequest {
            address: address.to_string(),
        };

        let response = self.affiliates.affiliate_info(request).await?;

        Ok(response.into_inner())
    }

    /// Query the affiliate that referred the given address.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_referred_by(&mut self, address: Address) -> Result<Address, Error> {
        let req = ReferredByRequest {
            address: address.into(),
        };
        let affiliate = self
            .affiliates
            .referred_by(req)
            .await?
            .into_inner()
            .affiliate_address;
        Ok(affiliate.into())
    }

    /// Query for the affiliate tiers.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_all_affiliate_tiers(&mut self) -> Result<AffiliateTiers, Error> {
        let req = AllAffiliateTiersRequest {};
        let tiers = self
            .affiliates
            .all_affiliate_tiers(req)
            .await?
            .into_inner()
            .tiers
            .ok_or_else(|| {
                err!("AllAffiliateTiers query response does not contain affiliate list")
            })?;
        Ok(tiers)
    }

    /// Query for the whitelisted affiliates.
    /// If an address is in the whitelist, then the affiliate fee share in
    /// this object will override fee share from the regular affiliate tiers.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_affiliate_whitelist(&mut self) -> Result<AffiliateWhitelist, Error> {
        let req = AffiliateWhitelistRequest {};
        let whitelist = self
            .affiliates
            .affiliate_whitelist(req)
            .await?
            .into_inner()
            .whitelist
            .ok_or_else(|| err!("AffiliateWhiteList query response does not contain whitelist"))?;
        Ok(whitelist)
    }

    /// Set the order router rev share.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn set_order_router_rev_share(
        &mut self,
        account: &mut Account,
        authority: Address,
        rev_share: OrderRouterRevShare,
    ) -> Result<TxHash, NodeError> {
        let msg = MsgSetOrderRouterRevShare {
            authority: authority.to_string(),
            order_router_rev_share: Some(rev_share),
        };

        let tx_raw = self.create_transaction(account, msg, None).await?;

        self.broadcast_transaction(tx_raw).await
    }

    /// Query the market mapper revenue share params.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_market_mapper_revenue_share_params(
        &mut self,
    ) -> Result<MarketMapperRevenueShareParams, Error> {
        let req = QueryMarketMapperRevenueShareParams {};

        let params = self
            .revshare
            .market_mapper_revenue_share_params(req)
            .await?
            .into_inner()
            .params
            .ok_or_else(|| {
                err!("MarketMapperRevenueShareParams query response does not contain params")
            })?;

        Ok(params)
    }

    /// Query the market mapper revenue share details.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_market_mapper_rev_share_details(
        &mut self,
        market_id: u32,
    ) -> Result<MarketMapperRevShareDetails, Error> {
        let req = QueryMarketMapperRevShareDetails { market_id };

        let details = self
            .revshare
            .market_mapper_rev_share_details(req)
            .await?
            .into_inner()
            .details
            .ok_or_else(|| {
                err!("MarketMapperRevShareDetails query response does not contain details")
            })?;

        Ok(details)
    }

    /// Query the unconditional rev share config.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/validator_get.rs).
    pub async fn get_unconditional_rev_share_config(
        &mut self,
    ) -> Result<UnconditionalRevShareConfig, Error> {
        let req = QueryUnconditionalRevShareConfig {};

        let config = self
            .revshare
            .unconditional_rev_share_config(req)
            .await?
            .into_inner()
            .config
            .ok_or_else(|| {
                err!("UnconditionalRevShareConfig query response does not contain config")
            })?;

        Ok(config)
    }
}