celestia-client 0.1.0

Celestia client combining RPC and gRPC functionality.
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
use std::sync::Arc;

use celestia_rpc::{HeaderClient, StateClient};

use crate::client::Context;
use crate::proto::cosmos::bank::v1beta1::MsgSend;
use crate::proto::cosmos::staking::v1beta1::{
    MsgBeginRedelegate, MsgCancelUnbondingDelegation, MsgDelegate, MsgUndelegate,
};
use crate::tx::{GasEstimate, IntoProtobufAny, TxConfig, TxInfo, TxPriority};
use crate::types::state::{
    AccAddress, Address, Coin, PageRequest, QueryDelegationResponse, QueryRedelegationsResponse,
    QueryUnbondingDelegationResponse, ValAddress,
};
use crate::types::Blob;
use crate::utils::height_i64;
use crate::Result;

/// State API for quering and submiting TXs to a consensus node.
pub struct StateApi {
    ctx: Arc<Context>,
}

impl StateApi {
    pub(crate) fn new(ctx: Arc<Context>) -> StateApi {
        StateApi { ctx }
    }

    /// Retrieves the Celestia coin balance for the signer.
    ///
    /// # 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 [`StateApi::balance_unverified`].
    pub async fn balance(&self) -> Result<u64> {
        let address = self.ctx.address()?;
        self.balance_for_address(&address).await
    }

    /// Retrieves the Celestia coin balance for the signer.
    pub async fn balance_unverified(&self) -> Result<u64> {
        let address = self.ctx.address()?;
        self.balance_for_address_unverified(&address).await
    }

    /// Retrieves the Celestia coin balance for the given 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 [`StateApi::balance_for_address_unverified`].
    ///
    /// This is the only method of [`StateApi`] that fallbacks to RPC endpoint
    /// when gRPC endpoint wasn't set.
    pub async fn balance_for_address(&self, address: &AccAddress) -> Result<u64> {
        let address = Address::AccAddress(address.to_owned());

        let grpc = match self.ctx.grpc() {
            Ok(grpc) => grpc,
            Err(_) => {
                return Ok(self
                    .ctx
                    .rpc
                    .state_balance_for_address(&address)
                    .await?
                    .amount());
            }
        };

        let head = self.ctx.rpc.header_network_head().await?;
        head.validate()?;

        Ok(grpc.get_verified_balance(&address, &head).await?.amount())
    }

    /// Retrieves the Celestia coin balance for the given address.
    pub async fn balance_for_address_unverified(&self, address: &AccAddress) -> Result<u64> {
        Ok(self
            .ctx
            .grpc()?
            .get_balance(&address.to_owned().into(), "utia")
            .await?
            .amount())
    }

    /// 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, it returns the
    /// network min gas price.
    pub async fn estimate_gas_price(&self, priority: TxPriority) -> Result<f64> {
        Ok(self.ctx.grpc()?.estimate_gas_price(priority).await?)
    }

    /// Estimate gas price for transaction with given priority and estimate gas usage
    /// for 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,
    /// it returns the network min gas price.
    ///
    /// The gas used is estimated using the state machine simulation.
    pub async fn estimate_gas_price_and_usage(
        &self,
        priority: TxPriority,
        tx_bytes: Vec<u8>,
    ) -> Result<GasEstimate> {
        Ok(self
            .ctx
            .grpc()?
            .estimate_gas_price_and_usage(priority, tx_bytes)
            .await?)
    }

    /// Submit given message to celestia network.
    ///
    /// # Example
    /// ```no_run
    /// # use celestia_client::{Client, Result};
    /// # use celestia_client::tx::TxConfig;
    /// # async fn docs() -> Result<()> {
    /// use celestia_proto::cosmos::bank::v1beta1::MsgSend;
    /// use celestia_types::state::{Address, Coin};
    ///
    /// let client = Client::builder()
    ///     .rpc_url("ws://localhost:26658")
    ///     .grpc_url("http://localhost:9090")
    ///     .private_key_hex("393fdb5def075819de55756b45c9e2c8531a8c78dd6eede483d3440e9457d839")
    ///     .build()
    ///     .await?;
    ///
    /// let msg = MsgSend {
    ///     from_address: client.address()?.to_string(),
    ///     to_address: "celestia169s50psyj2f4la9a2235329xz7rk6c53zhw9mm".to_string(),
    ///     amount: vec![Coin::utia(12345).into()],
    /// };
    ///
    /// client
    ///     .state()
    ///     .submit_message(msg, TxConfig::default())
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn submit_message<M>(&self, message: M, cfg: TxConfig) -> Result<TxInfo>
    where
        M: IntoProtobufAny,
    {
        Ok(self.ctx.grpc()?.submit_message(message, cfg).await?)
    }

    /// Sends the given amount of coins from signer's wallet to the given account address.
    pub async fn transfer(
        &self,
        to_address: &AccAddress,
        amount: u64,
        cfg: TxConfig,
    ) -> Result<TxInfo> {
        let from_address = self.ctx.address()?;

        let msg = MsgSend {
            from_address: from_address.to_string(),
            to_address: to_address.to_string(),
            amount: vec![Coin::utia(amount).into()],
        };

        self.submit_message(msg, cfg).await
    }

    /// Builds, signs and submits a PayForBlob transaction.
    ///
    /// # Note
    ///
    /// This is the same as [`BlobApi::submit`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use celestia_client::{Client, Result};
    /// # use celestia_client::tx::TxConfig;
    /// # async fn docs() -> Result<()> {
    /// use celestia_types::nmt::Namespace;
    /// use celestia_types::state::{Address, Coin};
    /// use celestia_types::{AppVersion, Blob};
    ///
    /// let client = Client::builder()
    ///     .rpc_url("ws://localhost:26658")
    ///     .grpc_url("http://localhost:9090")
    ///     .private_key_hex("393fdb5def075819de55756b45c9e2c8531a8c78dd6eede483d3440e9457d839")
    ///     .build()
    ///     .await?;
    ///
    /// let ns = Namespace::new_v0(b"abcd").unwrap();
    /// let blob = Blob::new(ns, "some data".into(), AppVersion::V3).unwrap();
    ///
    /// client
    ///     .state()
    ///     .submit_pay_for_blob(&[blob], TxConfig::default())
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`BlobApi::submit`]: crate::api::BlobApi::submit
    pub async fn submit_pay_for_blob(&self, blobs: &[Blob], cfg: TxConfig) -> Result<TxInfo> {
        Ok(self.ctx.grpc()?.submit_blobs(blobs, cfg).await?)
    }

    /// Cancels signer's pending undelegation from a validator.
    pub async fn cancel_unbonding_delegation(
        &self,
        validator_address: &ValAddress,
        amount: u64,
        creation_height: u64,
        cfg: TxConfig,
    ) -> Result<TxInfo> {
        let delegator_address = self.ctx.address()?;

        let msg = MsgCancelUnbondingDelegation {
            delegator_address: delegator_address.to_string(),
            validator_address: validator_address.to_string(),
            amount: Some(Coin::utia(amount).into()),
            creation_height: height_i64(creation_height)?,
        };

        self.submit_message(msg, cfg).await
    }

    /// Sends signer's delegated tokens to a new validator for redelegation.
    pub async fn begin_redelegate(
        &self,
        src_validator_address: &ValAddress,
        dest_validator_address: &ValAddress,
        amount: u64,
        cfg: TxConfig,
    ) -> Result<TxInfo> {
        let delegator_address = self.ctx.address()?;

        let msg = MsgBeginRedelegate {
            delegator_address: delegator_address.to_string(),
            validator_src_address: src_validator_address.to_string(),
            validator_dst_address: dest_validator_address.to_string(),
            amount: Some(Coin::utia(amount).into()),
        };

        self.submit_message(msg, cfg).await
    }

    /// Undelegates signer's delegated tokens, unbonding them from the current validator.
    pub async fn undelegate(
        &self,
        validator_address: &ValAddress,
        amount: u64,
        cfg: TxConfig,
    ) -> Result<TxInfo> {
        let delegator_address = self.ctx.address()?;

        let msg = MsgUndelegate {
            delegator_address: delegator_address.to_string(),
            validator_address: validator_address.to_string(),
            amount: Some(Coin::utia(amount).into()),
        };

        self.submit_message(msg, cfg).await
    }

    /// Sends signer's liquid tokens to a validator for delegation.
    pub async fn delegate(
        &self,
        validator_address: &ValAddress,
        amount: u64,
        cfg: TxConfig,
    ) -> Result<TxInfo> {
        let delegator_address = self.ctx.address()?;

        let msg = MsgDelegate {
            delegator_address: delegator_address.to_string(),
            validator_address: validator_address.to_string(),
            amount: Some(Coin::utia(amount).into()),
        };

        self.submit_message(msg, cfg).await
    }

    /// Retrieves the delegation information between signer and a validator.
    pub async fn query_delegation(
        &self,
        validator_address: &ValAddress,
    ) -> Result<QueryDelegationResponse> {
        let delegator_address = self.ctx.address()?;

        let resp = self
            .ctx
            .grpc()?
            .query_delegation(&delegator_address, validator_address)
            .await?;

        Ok(resp)
    }

    /// Retrieves the unbonding status between signer and a validator.
    pub async fn query_unbonding(
        &self,
        validator_address: &ValAddress,
    ) -> Result<QueryUnbondingDelegationResponse> {
        let delegator_address = self.ctx.address()?;

        let resp = self
            .ctx
            .grpc()?
            .query_unbonding(&delegator_address, validator_address)
            .await?;

        Ok(resp)
    }

    /// Retrieves the status of the redelegations between signer and a validator.
    pub async fn query_redelegations(
        &self,
        src_validator_address: &ValAddress,
        dest_validator_address: &ValAddress,
    ) -> Result<QueryRedelegationsResponse> {
        let delegator_address = self.ctx.address()?;

        let mut full_resp = QueryRedelegationsResponse {
            responses: Vec::new(),
            pagination: None,
        };

        let mut next_key = Vec::new();

        loop {
            let mut resp = self
                .ctx
                .grpc()?
                .query_redelegations(
                    &delegator_address,
                    src_validator_address,
                    dest_validator_address,
                    Some(PageRequest {
                        key: next_key,
                        ..Default::default()
                    }),
                )
                .await?;

            full_resp.responses.append(&mut resp.responses);

            match resp.pagination {
                Some(pagination) => next_key = pagination.next_key,
                None => break,
            }
        }

        Ok(full_resp)
    }
}

#[cfg(test)]
mod tests {
    use celestia_grpc::TxConfig;
    use k256::ecdsa::SigningKey;
    use lumina_utils::test_utils::async_test;

    use crate::test_utils::{new_client, new_read_only_client, node0_address, validator_address};
    use crate::Error;

    #[async_test]
    async fn transfer() {
        let client = new_client().await;

        let random_key = SigningKey::random(&mut rand::thread_rng());
        let random_acc = random_key.verifying_key().into();

        client
            .state()
            .transfer(&random_acc, 123, TxConfig::default())
            .await
            .unwrap();

        assert_eq!(
            client
                .state()
                .balance_for_address_unverified(&random_acc)
                .await
                .unwrap(),
            123
        );
    }

    #[async_test]
    async fn delegation() {
        let client = new_client().await;
        let validator_addr = validator_address();
        let client_addr = client.address().unwrap();

        // Test delegation
        client
            .state()
            .delegate(&validator_addr, 100, TxConfig::default())
            .await
            .unwrap();

        let del = client
            .state()
            .query_delegation(&validator_addr)
            .await
            .unwrap();

        assert_eq!(del.response.balance, 100);
        assert_eq!(del.response.delegation.delegator_address, client_addr);
        assert_eq!(del.response.delegation.validator_address, validator_addr);
        assert_eq!(del.response.delegation.shares, 100.into());

        // Test unbonding
        let unbond_tx_height = client
            .state()
            .undelegate(&validator_addr, 10, TxConfig::default())
            .await
            .unwrap()
            .height
            .value();

        let unbond = client
            .state()
            .query_unbonding(&validator_addr)
            .await
            .unwrap();

        assert_eq!(unbond.unbond.delegator_address, client_addr);
        assert_eq!(unbond.unbond.validator_address, validator_addr);
        assert_eq!(unbond.unbond.entries.len(), 1);
        assert_eq!(
            unbond.unbond.entries[0].creation_height.value(),
            unbond_tx_height
        );
        assert_eq!(unbond.unbond.entries[0].initial_balance, 10);
        assert_eq!(unbond.unbond.entries[0].balance, 10);

        let del = client
            .state()
            .query_delegation(&validator_addr)
            .await
            .unwrap();

        assert_eq!(del.response.balance, 90);
        assert_eq!(del.response.delegation.delegator_address, client_addr);
        assert_eq!(del.response.delegation.validator_address, validator_addr);
        assert_eq!(del.response.delegation.shares, 90.into());

        // Test partial cancel unbonding
        client
            .state()
            .cancel_unbonding_delegation(&validator_addr, 3, unbond_tx_height, TxConfig::default())
            .await
            .unwrap();

        let unbond = client
            .state()
            .query_unbonding(&validator_addr)
            .await
            .unwrap();

        assert_eq!(unbond.unbond.delegator_address, client_addr);
        assert_eq!(unbond.unbond.validator_address, validator_addr);
        assert_eq!(unbond.unbond.entries.len(), 1);
        assert_eq!(
            unbond.unbond.entries[0].creation_height.value(),
            unbond_tx_height
        );
        assert_eq!(unbond.unbond.entries[0].initial_balance, 7);
        assert_eq!(unbond.unbond.entries[0].balance, 7);

        let del = client
            .state()
            .query_delegation(&validator_addr)
            .await
            .unwrap();

        assert_eq!(del.response.balance, 93);
        assert_eq!(del.response.delegation.delegator_address, client_addr);
        assert_eq!(del.response.delegation.validator_address, validator_addr);
        assert_eq!(del.response.delegation.shares, 93.into());

        // Test fully cancel unbonding
        client
            .state()
            .cancel_unbonding_delegation(&validator_addr, 7, unbond_tx_height, TxConfig::default())
            .await
            .unwrap();

        let err = client
            .state()
            .query_unbonding(&validator_addr)
            .await
            .unwrap_err();

        assert_eq!(err.as_grpc_status().unwrap().code(), tonic::Code::NotFound);

        let del = client
            .state()
            .query_delegation(&validator_addr)
            .await
            .unwrap();

        assert_eq!(del.response.balance, 100);
        assert_eq!(del.response.delegation.delegator_address, client_addr);
        assert_eq!(del.response.delegation.validator_address, validator_addr);
        assert_eq!(del.response.delegation.shares, 100.into());
    }

    #[async_test]
    async fn balance_for_address() {
        let client_ro = new_read_only_client().await;

        // Read only mode allows calling `balance_for_address`
        let addr = node0_address();
        let balance = client_ro.state().balance_for_address(&addr).await.unwrap();
        assert!(balance > 0);

        // Read only mode does not allow calling `balance_for_address_unverified`.
        let e = client_ro
            .state()
            .balance_for_address_unverified(&addr)
            .await
            .unwrap_err();
        assert!(matches!(e, Error::ReadOnlyMode));

        // Read only mode does not allow calling `balance`
        let e = client_ro.state().balance().await.unwrap_err();
        assert!(matches!(e, Error::ReadOnlyMode));

        // Read only mode does not allow calling `balance_unverified`
        let e = client_ro.state().balance().await.unwrap_err();
        assert!(matches!(e, Error::ReadOnlyMode));
    }
}