celestia-grpc 0.8.0

A client for interacting with Celestia validator nodes gRPC
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
use std::fmt;
use std::sync::Arc;

use ::tendermint::chain::Id;
use celestia_types::any::IntoProtobufAny;
use k256::ecdsa::VerifyingKey;
use lumina_utils::time::Interval;
use prost::Message;
use std::time::Duration;
use tokio::sync::{MappedMutexGuard, Mutex, MutexGuard};

use celestia_grpc_macros::grpc_method;
use celestia_proto::celestia::blob::v1::query_client::QueryClient as BlobQueryClient;
use celestia_proto::celestia::core::v1::gas_estimation::gas_estimator_client::GasEstimatorClient;
use celestia_proto::celestia::core::v1::tx::tx_client::TxClient as TxStatusClient;
use celestia_proto::cosmos::auth::v1beta1::query_client::QueryClient as AuthQueryClient;
use celestia_proto::cosmos::bank::v1beta1::query_client::QueryClient as BankQueryClient;
use celestia_proto::cosmos::base::node::v1beta1::service_client::ServiceClient as ConfigServiceClient;
use celestia_proto::cosmos::base::tendermint::v1beta1::service_client::ServiceClient as TendermintServiceClient;
use celestia_proto::cosmos::staking::v1beta1::query_client::QueryClient as StakingQueryClient;
use celestia_proto::cosmos::tx::v1beta1::service_client::ServiceClient as TxServiceClient;
use celestia_types::blob::{BlobParams, MsgPayForBlobs, RawBlobTx, RawMsgPayForBlobs};
use celestia_types::block::Block;
use celestia_types::consts::appconsts;
use celestia_types::hash::Hash;
use celestia_types::state::auth::{Account, AuthParams, BaseAccount};
use celestia_types::state::{
    AbciQueryResponse, PageRequest, QueryDelegationResponse, QueryRedelegationsResponse,
    QueryUnbondingDelegationResponse, RawTxBody, ValAddress,
};
use celestia_types::state::{
    AccAddress, Address, AddressTrait, Coin, ErrorCode, TxResponse, BOND_DENOM,
};
use celestia_types::{AppVersion, Blob, ExtendedHeader};

use crate::abci_proofs::ProofChain;
use crate::boxed::BoxedTransport;
use crate::builder::GrpcClientBuilder;
use crate::grpc::{
    AsyncGrpcCall, BroadcastMode, ConfigResponse, Context, GasEstimate, GasInfo, GetTxResponse,
    TxPriority, TxStatus, TxStatusResponse,
};
use crate::signer::{sign_tx, BoxedDocSigner};
use crate::tx::TxInfo;
use crate::{Error, Result, TxConfig};

// source https://github.com/celestiaorg/celestia-core/blob/v1.43.0-tm-v0.34.35/pkg/consts/consts.go#L19
const BLOB_TX_TYPE_ID: &str = "BLOB";

#[derive(Debug, Clone)]
struct ChainState {
    app_version: AppVersion,
    chain_id: Id,
}

pub(crate) struct SignerConfig {
    pub(crate) signer: BoxedDocSigner,
    pub(crate) pubkey: VerifyingKey,
}

/// gRPC client for the Celestia network
///
/// Under the hood, this struct wraps tonic and does type conversion
#[derive(Clone)]
pub struct GrpcClient {
    inner: Arc<GrpcClientInner>,
}

struct GrpcClientInner {
    transport: BoxedTransport,
    account: Mutex<Option<Account>>,
    chain_state: Mutex<Option<ChainState>>,
    signer: Option<SignerConfig>,
    context: Context,
}

impl GrpcClient {
    /// Create a new client wrapping given transport
    pub(crate) fn new(
        transport: BoxedTransport,
        signer: Option<SignerConfig>,
        context: Context,
    ) -> Self {
        Self {
            inner: Arc::new(GrpcClientInner {
                transport,
                account: Mutex::new(None),
                chain_state: Mutex::new(None),
                signer,
                context,
            }),
        }
    }

    /// Create a builder for [`GrpcClient`] connected to `url`
    pub fn builder() -> GrpcClientBuilder {
        GrpcClientBuilder::new()
    }

    // cosmos.auth

    /// Get auth params
    #[grpc_method(AuthQueryClient::params)]
    fn get_auth_params(&self) -> AsyncGrpcCall<AuthParams>;

    /// Get account
    #[grpc_method(AuthQueryClient::account)]
    fn get_account(&self, account: &AccAddress) -> AsyncGrpcCall<Account>;

    /// Get accounts
    #[grpc_method(AuthQueryClient::accounts)]
    fn get_accounts(&self) -> AsyncGrpcCall<Vec<Account>>;

    // cosmos.bank

    /// Retrieves the verified Celestia coin balance for the address.
    ///
    /// # Notes
    ///
    /// This returns the verified balance which is the one that was reported by
    /// the previous network block. In other words, if you transfer some coins,
    /// you need to wait 1 more block in order to see the new balance. If you want
    /// something more immediate then use [`GrpcClient::get_balance`].
    pub fn get_verified_balance(
        &self,
        address: &Address,
        header: &ExtendedHeader,
    ) -> AsyncGrpcCall<Coin> {
        let this = self.clone();
        let address = address.clone();
        let header = header.clone();

        AsyncGrpcCall::new(move |context| async move {
            this.get_verified_balance_impl(&address, &header, &context)
                .await
        })
        .context(&self.inner.context)
    }

    /// Retrieves the Celestia coin balance for the given address.
    #[grpc_method(BankQueryClient::balance)]
    fn get_balance(&self, address: &Address, denom: impl Into<String>) -> AsyncGrpcCall<Coin>;

    /// Get balance of all coins
    #[grpc_method(BankQueryClient::all_balances)]
    fn get_all_balances(&self, address: &Address) -> AsyncGrpcCall<Vec<Coin>>;

    /// Get balance of all spendable coins
    #[grpc_method(BankQueryClient::spendable_balances)]
    fn get_spendable_balances(&self, address: &Address) -> AsyncGrpcCall<Vec<Coin>>;

    /// Get total supply
    #[grpc_method(BankQueryClient::total_supply)]
    fn get_total_supply(&self) -> AsyncGrpcCall<Vec<Coin>>;

    // cosmos.base.node

    /// Get node configuration
    #[grpc_method(ConfigServiceClient::config)]
    fn get_node_config(&self) -> AsyncGrpcCall<ConfigResponse>;

    // cosmos.base.tendermint

    /// Get latest block
    #[grpc_method(TendermintServiceClient::get_latest_block)]
    fn get_latest_block(&self) -> AsyncGrpcCall<Block>;

    /// Get block by height
    #[grpc_method(TendermintServiceClient::get_block_by_height)]
    fn get_block_by_height(&self, height: i64) -> AsyncGrpcCall<Block>;

    /// Issue a direct ABCI query to the application
    #[grpc_method(TendermintServiceClient::abci_query)]
    fn abci_query(
        &self,
        data: impl AsRef<[u8]>,
        path: impl Into<String>,
        height: u64,
        prove: bool,
    ) -> AsyncGrpcCall<AbciQueryResponse>;

    // cosmos.tx

    /// Broadcast prepared and serialised transaction
    #[grpc_method(TxServiceClient::broadcast_tx)]
    fn broadcast_tx(&self, tx_bytes: Vec<u8>, mode: BroadcastMode) -> AsyncGrpcCall<TxResponse>;

    /// Get Tx
    #[grpc_method(TxServiceClient::get_tx)]
    fn get_tx(&self, hash: Hash) -> AsyncGrpcCall<GetTxResponse>;

    /// Broadcast prepared and serialised transaction
    #[grpc_method(TxServiceClient::simulate)]
    fn simulate(&self, tx_bytes: Vec<u8>) -> AsyncGrpcCall<GasInfo>;

    // cosmos.staking

    /// Retrieves the delegation information between a delegator and a validator
    // TODO: Expose this to JS and  UniFFI
    #[grpc_method(StakingQueryClient::delegation)]
    fn query_delegation(
        &self,
        delegator_address: &AccAddress,
        validator_address: &ValAddress,
    ) -> AsyncGrpcCall<QueryDelegationResponse>;

    /// Retrieves the unbonding status between a delegator and a validator
    // TODO: Expose this to JS and  UniFFI
    #[grpc_method(StakingQueryClient::unbonding_delegation)]
    fn query_unbonding(
        &self,
        delegator_address: &AccAddress,
        validator_address: &ValAddress,
    ) -> AsyncGrpcCall<QueryUnbondingDelegationResponse>;

    /// Retrieves the status of the redelegations between a delegator and a validator
    // TODO: Expose this to JS and  UniFFI
    #[grpc_method(StakingQueryClient::redelegations)]
    fn query_redelegations(
        &self,
        delegator_address: &AccAddress,
        src_validator_address: &ValAddress,
        dest_validator_address: &ValAddress,
        pagination: Option<PageRequest>,
    ) -> AsyncGrpcCall<QueryRedelegationsResponse>;

    // celestia.blob

    /// Get blob params
    #[grpc_method(BlobQueryClient::params)]
    fn get_blob_params(&self) -> AsyncGrpcCall<BlobParams>;

    // celestia.core.tx

    /// Get status of the transaction
    #[grpc_method(TxStatusClient::tx_status)]
    fn tx_status(&self, hash: Hash) -> AsyncGrpcCall<TxStatusResponse>;

    // celestia.core.gas_estimation

    /// Estimate gas price for given transaction priority based
    /// on the gas prices of the transactions in the last five blocks.
    ///
    /// If no transaction is found in the last five blocks, return the network
    /// min gas price.
    #[grpc_method(GasEstimatorClient::estimate_gas_price)]
    fn estimate_gas_price(&self, priority: TxPriority) -> AsyncGrpcCall<f64>;

    /// Estimate gas price for transaction with given priority and estimate gas usage
    /// for the provided serialised transaction.
    ///
    /// The gas price estimation is based on the gas prices of the transactions
    /// in the last five blocks.
    /// If no transaction is found in the last five blocks, return the network
    /// min gas price.
    ///
    /// The gas used is estimated using the state machine simulation.
    #[grpc_method(GasEstimatorClient::estimate_gas_price_and_usage)]
    fn estimate_gas_price_and_usage(
        &self,
        priority: TxPriority,
        tx_bytes: Vec<u8>,
    ) -> AsyncGrpcCall<GasEstimate>;

    /// Submit given message to celestia network.
    ///
    /// # Example
    /// ```no_run
    /// # async fn docs() {
    /// use celestia_grpc::{GrpcClient, TxConfig};
    /// use celestia_proto::cosmos::bank::v1beta1::MsgSend;
    /// use celestia_types::state::{Address, Coin};
    /// use tendermint::crypto::default::ecdsa_secp256k1::SigningKey;
    ///
    /// let signing_key = SigningKey::random(&mut rand::rngs::OsRng);
    /// let address = Address::from_account_veryfing_key(*signing_key.verifying_key());
    /// let grpc_url = "public-celestia-mocha4-consensus.numia.xyz:9090";
    ///
    /// let tx_client = GrpcClient::builder()
    ///     .url(grpc_url)
    ///     .signer_keypair(signing_key)
    ///     .build()
    ///     .unwrap();
    ///
    /// let msg = MsgSend {
    ///     from_address: address.to_string(),
    ///     to_address: "celestia169s50psyj2f4la9a2235329xz7rk6c53zhw9mm".to_string(),
    ///     amount: vec![Coin::utia(12345).into()],
    /// };
    ///
    /// tx_client
    ///     .submit_message(msg.clone(), TxConfig::default())
    ///     .await
    ///     .unwrap();
    /// # }
    /// ```
    pub fn submit_message<M>(&self, message: M, cfg: TxConfig) -> AsyncGrpcCall<TxInfo>
    where
        M: IntoProtobufAny + Send + 'static,
    {
        let this = self.clone();

        AsyncGrpcCall::new(move |context| async move {
            this.submit_message_impl(message, cfg, &context).await
        })
        .context(&self.inner.context)
    }

    /// Submit given blobs to celestia network.
    ///
    /// # Example
    /// ```no_run
    /// # async fn docs() {
    /// use celestia_grpc::{GrpcClient, TxConfig};
    /// use celestia_types::state::{Address, Coin};
    /// use celestia_types::{AppVersion, Blob};
    /// use celestia_types::nmt::Namespace;
    /// use tendermint::crypto::default::ecdsa_secp256k1::SigningKey;
    ///
    /// let signing_key = SigningKey::random(&mut rand::rngs::OsRng);
    /// let address = Address::from_account_veryfing_key(*signing_key.verifying_key());
    /// let grpc_url = "public-celestia-mocha4-consensus.numia.xyz:9090";
    ///
    /// let tx_client = GrpcClient::builder()
    ///     .url(grpc_url)
    ///     .signer_keypair(signing_key)
    ///     .build()
    ///     .unwrap();
    ///
    /// let ns = Namespace::new_v0(b"abcd").unwrap();
    /// let blob = Blob::new(ns, "some data".into(), None, AppVersion::V3).unwrap();
    ///
    /// tx_client
    ///     .submit_blobs(&[blob], TxConfig::default())
    ///     .await
    ///     .unwrap();
    /// # }
    /// ```
    pub fn submit_blobs(&self, blobs: &[Blob], cfg: TxConfig) -> AsyncGrpcCall<TxInfo> {
        let this = self.clone();
        let blobs = blobs.to_vec();

        AsyncGrpcCall::new(move |context| async move {
            this.submit_blobs_impl(&blobs, cfg, &context).await
        })
        .context(&self.inner.context)
    }

    /// Get client's app version
    pub fn app_version(&self) -> AsyncGrpcCall<AppVersion> {
        let this = self.clone();

        AsyncGrpcCall::new(move |context| async move {
            let ChainState { app_version, .. } = this.load_chain_state(&context).await?;
            Ok(app_version)
        })
        .context(&self.inner.context)
    }

    /// Get client's chain id
    pub fn chain_id(&self) -> AsyncGrpcCall<Id> {
        let this = self.clone();

        AsyncGrpcCall::new(move |context| async move {
            let ChainState { chain_id, .. } = this.load_chain_state(&context).await?;
            Ok(chain_id)
        })
        .context(&self.inner.context)
    }

    /// Get client's account public key if the signer is set
    pub fn get_account_pubkey(&self) -> Option<VerifyingKey> {
        self.inner.signer.as_ref().map(|config| config.pubkey)
    }

    /// Get client's account address if the signer is set
    pub fn get_account_address(&self) -> Option<AccAddress> {
        self.get_account_pubkey().map(Into::into)
    }
}

impl GrpcClient {
    async fn get_verified_balance_impl(
        &self,
        address: &Address,
        header: &ExtendedHeader,
        context: &Context,
    ) -> Result<Coin> {
        // construct the key for querying account balance from bank state
        let mut prefixed_account_key = Vec::with_capacity(1 + 1 + appconsts::SIGNER_SIZE + 4);
        prefixed_account_key.push(0x02); // balances prefix
        prefixed_account_key.push(address.as_bytes().len() as u8); // address length
        prefixed_account_key.extend_from_slice(address.as_bytes()); // address
        prefixed_account_key.extend_from_slice(BOND_DENOM.as_bytes()); // denom

        // abci queries are possible only from 2nd block, but we can rely on the node to
        // return an error if that is the case. We only want to prevent height == 0 here,
        // because that will make node interpret that as a network head. It would also fail,
        // on proof verification, but it would be harder to debug as the message would just
        // say that computed root is different than expected.
        let height = 1.max(header.height().value().saturating_sub(1));

        let response = self
            .abci_query(&prefixed_account_key, "store/bank/key", height, true)
            .context(context)
            .await?;
        if response.code != ErrorCode::Success {
            return Err(Error::AbciQuery(response.code, response.log));
        }

        // If account doesn't exist yet, just return 0
        if response.value.is_empty() {
            return Ok(Coin::utia(0));
        }

        // NOTE: don't put `ProofChain` directly in the AbciQueryResponse, because
        // it supports only small subset of proofs that are required for the balance
        // queries
        let proof: ProofChain = response.proof_ops.unwrap_or_default().try_into()?;
        proof.verify_membership(
            &header.header.app_hash,
            [prefixed_account_key.as_slice(), b"bank"],
            &response.value,
        )?;

        let amount = std::str::from_utf8(&response.value)
            .map_err(|_| Error::FailedToParseResponse)?
            .parse()
            .map_err(|_| Error::FailedToParseResponse)?;

        Ok(Coin::utia(amount))
    }

    async fn submit_message_impl<M>(
        &self,
        message: M,
        cfg: TxConfig,
        context: &Context,
    ) -> Result<TxInfo>
    where
        M: IntoProtobufAny,
    {
        let tx_body = RawTxBody {
            messages: vec![message.into_any()],
            memo: cfg.memo.clone().unwrap_or_default(),
            ..RawTxBody::default()
        };

        let (tx_hash, sequence) = self.sign_and_broadcast_tx(tx_body, cfg, context).await?;

        self.confirm_tx(tx_hash, sequence, context).await
    }

    async fn submit_blobs_impl(
        &self,
        blobs: &[Blob],
        cfg: TxConfig,
        context: &Context,
    ) -> Result<TxInfo> {
        if blobs.is_empty() {
            return Err(Error::TxEmptyBlobList);
        }
        let app_version = self.app_version().await?;
        for blob in blobs {
            blob.validate(app_version)?;
        }

        let (tx_hash, sequence) = self
            .sign_and_broadcast_blobs(blobs.to_vec(), cfg, context)
            .await?;

        self.confirm_tx(tx_hash, sequence, context).await
    }

    async fn load_chain_state(&self, context: &Context) -> Result<ChainState> {
        let mut chain_state_guard = self.inner.chain_state.lock().await;

        if chain_state_guard.is_none() {
            let block = self.get_latest_block().context(context).await?;
            let app_version = block.header.version.app;
            let app_version = AppVersion::from_u64(app_version)
                .ok_or(celestia_types::Error::UnsupportedAppVersion(app_version))?;
            let chain_id = block.header.chain_id;

            *chain_state_guard = Some(ChainState {
                app_version,
                chain_id,
            });
        }
        Ok((*chain_state_guard)
            .clone()
            .expect("state to be initialised"))
    }

    async fn load_account(
        &self,
        context: &Context,
    ) -> Result<(MappedMutexGuard<'_, Account>, &SignerConfig)> {
        let signer = self.inner.signer.as_ref().ok_or(Error::MissingSigner)?;
        let mut account_guard = self.inner.account.lock().await;

        if account_guard.is_none() {
            let address = AccAddress::from(signer.pubkey);
            let account = self.get_account(&address).context(context).await?;

            *account_guard = Some(account);
        }
        let mapped_guard = MutexGuard::map(account_guard, |acc| {
            acc.as_mut().expect("account data present")
        });

        Ok((mapped_guard, signer))
    }

    /// compute gas limit and gas price according to provided TxConfig for serialised
    /// transaction, potentially calling gas estimation service
    async fn calculate_transaction_gas_params(
        &self,
        tx_body: &RawTxBody,
        cfg: &TxConfig,
        chain_id: Id,
        account: &BaseAccount,
        context: &Context,
    ) -> Result<(u64, f64)> {
        let signer = self.inner.signer.as_ref().ok_or(Error::MissingSigner)?;

        Ok(match (cfg.gas_limit, cfg.gas_price) {
            (Some(gas_limit), Some(gas_price)) => (gas_limit, gas_price),
            (Some(gas_limit), None) => {
                let gas_price = self
                    .estimate_gas_price(cfg.priority)
                    .context(context)
                    .await?;
                (gas_limit, gas_price)
            }
            (None, maybe_gas_price) => {
                let tx = sign_tx(
                    tx_body.clone(),
                    chain_id,
                    account,
                    &signer.pubkey,
                    &signer.signer,
                    0,
                    1,
                )
                .await?;

                let GasEstimate { price, usage } = self
                    .estimate_gas_price_and_usage(cfg.priority, tx.encode_to_vec())
                    .context(context)
                    .await?;
                (usage, maybe_gas_price.unwrap_or(price))
            }
        })
    }

    async fn sign_and_broadcast_blobs(
        &self,
        blobs: Vec<Blob>,
        cfg: TxConfig,
        context: &Context,
    ) -> Result<(Hash, u64)> {
        let ChainState { chain_id, .. } = self.load_chain_state(context).await?;
        // lock the account; tx signing and broadcast must be atomic
        // because node requires all transactions to be sequenced by account.sequence
        let (account, signer) = self.load_account(context).await?;

        let pfb = MsgPayForBlobs::new(&blobs, account.address.clone())?;
        let pfb = RawTxBody {
            messages: vec![RawMsgPayForBlobs::from(pfb).into_any()],
            memo: cfg.memo.clone().unwrap_or_default(),
            ..RawTxBody::default()
        };

        let (gas_limit, gas_price) = self
            .calculate_transaction_gas_params(&pfb, &cfg, chain_id.clone(), &account, context)
            .await?;

        let fee = (gas_limit as f64 * gas_price).ceil() as u64;
        let tx = sign_tx(
            pfb,
            chain_id.clone(),
            &account,
            &signer.pubkey,
            &signer.signer,
            gas_limit,
            fee,
        )
        .await?;

        let blobs = blobs.into_iter().map(Into::into).collect();
        let blob_tx = RawBlobTx {
            tx: tx.encode_to_vec(),
            blobs,
            type_id: BLOB_TX_TYPE_ID.to_string(),
        };

        self.broadcast_tx_with_account(blob_tx.encode_to_vec(), cfg, account, context)
            .await
    }

    async fn sign_and_broadcast_tx(
        &self,
        tx: RawTxBody,
        cfg: TxConfig,
        context: &Context,
    ) -> Result<(Hash, u64)> {
        let ChainState { chain_id, .. } = self.load_chain_state(context).await?;

        // lock the account; tx signing and broadcast must be atomic
        // because node requires all transactions to be sequenced by account.sequence
        let (account, signer) = self.load_account(context).await?;

        let (gas_limit, gas_price) = self
            .calculate_transaction_gas_params(&tx, &cfg, chain_id.clone(), &account, context)
            .await?;

        let fee = (gas_limit as f64 * gas_price).ceil();
        let tx = sign_tx(
            tx,
            chain_id,
            &account,
            &signer.pubkey,
            &signer.signer,
            gas_limit,
            fee as u64,
        )
        .await?;

        self.broadcast_tx_with_account(tx.encode_to_vec(), cfg, account, context)
            .await
    }

    async fn broadcast_tx_with_account(
        &self,
        tx: Vec<u8>,
        cfg: TxConfig,
        mut account: MappedMutexGuard<'_, Account>,
        context: &Context,
    ) -> Result<(Hash, u64)> {
        let resp = self
            .broadcast_tx(tx, BroadcastMode::Sync)
            .context(context)
            .await?;

        if resp.code != ErrorCode::Success {
            // if transaction failed due to insufficient fee, include info
            // whether gas price was estimated or explicitely set
            let message = if resp.code == ErrorCode::InsufficientFee {
                if cfg.gas_price.is_some() {
                    format!("Gas price was set via config. {}", resp.raw_log)
                } else {
                    format!("Gas price was estimated. {}", resp.raw_log)
                }
            } else {
                resp.raw_log
            };

            return Err(Error::TxBroadcastFailed(resp.txhash, resp.code, message));
        }

        let tx_sequence = account.sequence;
        account.sequence += 1;

        Ok((resp.txhash, tx_sequence))
    }

    async fn confirm_tx(&self, hash: Hash, sequence: u64, context: &Context) -> Result<TxInfo> {
        let mut interval = Interval::new(Duration::from_millis(500)).await;

        loop {
            let tx_status = self.tx_status(hash).context(context).await?;
            match tx_status.status {
                TxStatus::Pending => interval.tick().await,
                TxStatus::Committed => {
                    if tx_status.execution_code == ErrorCode::Success {
                        return Ok(TxInfo {
                            hash,
                            height: tx_status.height,
                        });
                    } else {
                        return Err(Error::TxExecutionFailed(
                            hash,
                            tx_status.execution_code,
                            tx_status.error,
                        ));
                    }
                }
                // node will treat this transaction like if it never happened, so
                // we need to revert the account's sequence to the one of evicted tx.
                // all transactions that were already submitted after this one will fail
                // due to incorrect sequence number.
                TxStatus::Evicted => {
                    let mut acc = self.inner.account.lock().await;
                    acc.as_mut().expect("account data present").sequence = sequence;
                    return Err(Error::TxEvicted(hash));
                }
                // this case should never happen for node that accepted a broadcast
                // however we handle it the same as evicted for extra safety
                TxStatus::Unknown => {
                    let mut acc = self.inner.account.lock().await;
                    acc.as_mut().expect("account data present").sequence = sequence;
                    return Err(Error::TxNotFound(hash));
                }
            }
        }
    }
}

impl fmt::Debug for GrpcClient {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("GrpcClient { .. }")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lumina_utils::test_utils::async_test;

    #[async_test]
    async fn extending_client_context() {
        let client = GrpcClient::builder()
            .url("http://foo")
            .metadata("x-token", "secret-token")
            .build()
            .unwrap();
        let call = client.app_version().block_height(1234);

        assert!(call.context.metadata.contains_key("x-token"));
        assert!(call.context.metadata.contains_key("x-cosmos-block-height"));
    }
}