cw-orch-daemon 0.26.0

Scripting library for deploying and interacting with CosmWasm smart-contracts
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
use crate::{cosmos_modules, error::DaemonError, Daemon};
use cosmos_modules::ibc_channel;
use cosmrs::proto::cosmos::base::query::v1beta1::PageRequest;
use cosmrs::proto::ibc::{
    applications::transfer::v1::{DenomTrace, QueryDenomHashResponse, QueryDenomTraceResponse},
    core::{
        channel::v1::QueryPacketCommitmentResponse,
        client::v1::{IdentifiedClientState, QueryClientStatesResponse},
        connection::v1::{ConnectionEnd, IdentifiedConnection, State},
    },
    lightclients::tendermint::v1::ClientState,
};
use cw_orch_core::environment::{Querier, QuerierGetter};
use prost::Message;
use tokio::runtime::Handle;
use tonic::transport::Channel;

/// Querier for the Cosmos IBC module
/// All the async function are prefixed with `_`
pub struct Ibc {
    pub channel: Channel,
    pub rt_handle: Option<Handle>,
}

impl Ibc {
    pub fn new(daemon: &Daemon) -> Self {
        Self {
            channel: daemon.channel(),
            rt_handle: Some(daemon.rt_handle.clone()),
        }
    }

    pub fn new_async(channel: Channel) -> Self {
        Self {
            channel,
            rt_handle: None,
        }
    }
}

impl Querier for Ibc {
    type Error = DaemonError;
}

impl QuerierGetter<Ibc> for Daemon {
    fn querier(&self) -> Ibc {
        Ibc::new(self)
    }
}

impl Ibc {
    // ### Transfer queries ### //

    /// Get the trace of a specific denom
    pub async fn _denom_trace(&self, hash: String) -> Result<DenomTrace, DaemonError> {
        let denom_trace: QueryDenomTraceResponse = cosmos_query!(
            self,
            ibc_transfer,
            denom_trace,
            QueryDenomTraceRequest { hash: hash }
        );
        Ok(denom_trace.denom_trace.unwrap())
    }

    /// Get the hash of a specific denom from its trace
    pub async fn _denom_hash(&self, trace: String) -> Result<String, DaemonError> {
        let denom_hash: QueryDenomHashResponse = cosmos_query!(
            self,
            ibc_transfer,
            denom_hash,
            QueryDenomHashRequest { trace: trace }
        );
        Ok(denom_hash.hash)
    }

    // ### Client queries ###

    /// Get all the IBC clients for this daemon
    pub async fn _clients(&self) -> Result<Vec<IdentifiedClientState>, DaemonError> {
        let ibc_clients: QueryClientStatesResponse = cosmos_query!(
            self,
            ibc_client,
            client_states,
            QueryClientStatesRequest { pagination: None }
        );
        Ok(ibc_clients.client_states)
    }

    /// Get the state of a specific IBC client
    pub async fn _client_state(
        &self,
        client_id: impl ToString,
        // Add the necessary parameters here
    ) -> Result<cosmos_modules::ibc_client::QueryClientStateResponse, DaemonError> {
        let response: cosmos_modules::ibc_client::QueryClientStateResponse = cosmos_query!(
            self,
            ibc_client,
            client_state,
            QueryClientStateRequest {
                client_id: client_id.to_string(),
            }
        );
        Ok(response)
    }

    /// Get the consensus state of a specific IBC client
    pub async fn _consensus_states(
        &self,
        client_id: impl ToString,
    ) -> Result<cosmos_modules::ibc_client::QueryConsensusStatesResponse, DaemonError> {
        let client_id = client_id.to_string();
        let response: cosmos_modules::ibc_client::QueryConsensusStatesResponse = cosmos_query!(
            self,
            ibc_client,
            consensus_states,
            QueryConsensusStatesRequest {
                client_id: client_id,
                pagination: None,
            }
        );
        Ok(response)
    }

    /// Get the consensus status of a specific IBC client
    pub async fn _client_status(
        &self,
        client_id: impl ToString,
        // Add the necessary parameters here
    ) -> Result<cosmos_modules::ibc_client::QueryClientStatusResponse, DaemonError> {
        let response: cosmos_modules::ibc_client::QueryClientStatusResponse = cosmos_query!(
            self,
            ibc_client,
            client_status,
            QueryClientStatusRequest {
                client_id: client_id.to_string(),
            }
        );
        Ok(response)
    }

    /// Get the ibc client parameters
    pub async fn _client_params(
        &self,
    ) -> Result<cosmos_modules::ibc_client::QueryClientParamsResponse, DaemonError> {
        let response: cosmos_modules::ibc_client::QueryClientParamsResponse =
            cosmos_query!(self, ibc_client, client_params, QueryClientParamsRequest {});
        Ok(response)
    }

    // ### Connection queries ###

    /// Query the IBC connections for a specific chain
    pub async fn _connections(&self) -> Result<Vec<IdentifiedConnection>, DaemonError> {
        use cosmos_modules::ibc_connection::QueryConnectionsResponse;

        let ibc_connections: QueryConnectionsResponse = cosmos_query!(
            self,
            ibc_connection,
            connections,
            QueryConnectionsRequest { pagination: None }
        );
        Ok(ibc_connections.connections)
    }

    /// Search for open connections with a specific chain.
    pub async fn _open_connections(
        &self,
        client_chain_id: impl ToString,
    ) -> Result<Vec<IdentifiedConnection>, DaemonError> {
        let connections = self._connections().await?;
        let mut open_connections = Vec::new();
        for connection in connections {
            if connection.state() == State::Open {
                open_connections.push(connection);
            }
        }

        // now search for the connections that use a client with the correct chain ids
        let mut filtered_connections = Vec::new();
        for connection in open_connections {
            let client_state = self._connection_client(&connection.id).await?;
            if client_state.chain_id == client_chain_id.to_string() {
                filtered_connections.push(connection);
            }
        }

        Ok(filtered_connections)
    }

    // Get the information about a specific connection
    pub async fn _connection_end(
        &self,
        connection_id: impl Into<String>,
    ) -> Result<Option<ConnectionEnd>, DaemonError> {
        use cosmos_modules::ibc_connection::QueryConnectionResponse;

        let connection_id = connection_id.into();
        let ibc_client_connections: QueryConnectionResponse = cosmos_query!(
            self,
            ibc_connection,
            connection,
            QueryConnectionRequest {
                connection_id: connection_id.clone()
            }
        );

        Ok(ibc_client_connections.connection)
    }

    /// Get all the connections for this client
    pub async fn _client_connections(
        &self,
        client_id: impl Into<String>,
    ) -> Result<Vec<String>, DaemonError> {
        use cosmos_modules::ibc_connection::QueryClientConnectionsResponse;

        let client_id = client_id.into();
        let ibc_client_connections: QueryClientConnectionsResponse = cosmos_query!(
            self,
            ibc_connection,
            client_connections,
            QueryClientConnectionsRequest {
                client_id: client_id.clone()
            }
        );

        Ok(ibc_client_connections.connection_paths)
    }

    /// Get the (tendermint) client state for a specific connection
    pub async fn _connection_client(
        &self,
        connection_id: impl Into<String>,
    ) -> Result<ClientState, DaemonError> {
        use cosmos_modules::ibc_connection::QueryConnectionClientStateResponse;
        let connection_id = connection_id.into();

        let ibc_connection_client: QueryConnectionClientStateResponse = cosmos_query!(
            self,
            ibc_connection,
            connection_client_state,
            QueryConnectionClientStateRequest {
                connection_id: connection_id.clone()
            }
        );

        let client_state =
            ibc_connection_client
                .identified_client_state
                .ok_or(DaemonError::ibc_err(format!(
                    "error identifying client for connection {}",
                    connection_id
                )))?;

        let client_state = ClientState::decode(client_state.client_state.unwrap().value.as_slice())
            .map_err(|e| DaemonError::ibc_err(format!("error decoding client state: {}", e)))?;

        Ok(client_state)
    }

    // ### Channel queries ###

    /// Get the channel for a specific port and channel id
    pub async fn _channel(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
    ) -> Result<ibc_channel::Channel, DaemonError> {
        use cosmos_modules::ibc_channel::QueryChannelResponse;

        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_channel: QueryChannelResponse = cosmos_query!(
            self,
            ibc_channel,
            channel,
            QueryChannelRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
            }
        );

        ibc_channel.channel.ok_or(DaemonError::ibc_err(format!(
            "error fetching channel {} on port {}",
            channel_id, port_id
        )))
    }

    /// List all the channels
    pub async fn _channels(
        &self,
        pagination: Option<PageRequest>,
    ) -> Result<Vec<ibc_channel::IdentifiedChannel>, DaemonError> {
        use cosmos_modules::ibc_channel::QueryChannelsResponse;

        let ibc_channels: QueryChannelsResponse = cosmos_query!(
            self,
            ibc_channel,
            channels,
            QueryChannelsRequest {
                pagination: pagination
            }
        );

        Ok(ibc_channels.channels)
    }

    /// Get all the channels for a specific connection
    pub async fn _connection_channels(
        &self,
        connection_id: impl Into<String>,
    ) -> Result<Vec<ibc_channel::IdentifiedChannel>, DaemonError> {
        use cosmos_modules::ibc_channel::QueryConnectionChannelsResponse;

        let connection_id = connection_id.into();
        let ibc_connection_channels: QueryConnectionChannelsResponse = cosmos_query!(
            self,
            ibc_channel,
            connection_channels,
            QueryConnectionChannelsRequest {
                connection: connection_id.clone(),
                pagination: None,
            }
        );

        Ok(ibc_connection_channels.channels)
    }

    /// Get the client state for a specific channel and port
    pub async fn _channel_client_state(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
    ) -> Result<IdentifiedClientState, DaemonError> {
        use cosmos_modules::ibc_channel::QueryChannelClientStateResponse;

        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_channel_client_state: QueryChannelClientStateResponse = cosmos_query!(
            self,
            ibc_channel,
            channel_client_state,
            QueryChannelClientStateRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
            }
        );

        ibc_channel_client_state
            .identified_client_state
            .ok_or(DaemonError::ibc_err(format!(
                "error identifying client for channel {} on port {}",
                channel_id, port_id
            )))
    }

    // ### Packet queries ###

    // Commitment

    /// Get all the packet commitments for a specific channel and port
    pub async fn _packet_commitments(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
    ) -> Result<Vec<ibc_channel::PacketState>, DaemonError> {
        use cosmos_modules::ibc_channel::QueryPacketCommitmentsResponse;

        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_commitments: QueryPacketCommitmentsResponse = cosmos_query!(
            self,
            ibc_channel,
            packet_commitments,
            QueryPacketCommitmentsRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                pagination: None,
            }
        );

        Ok(ibc_packet_commitments.commitments)
    }

    /// Get the packet commitment for a specific channel, port and sequence
    pub async fn _packet_commitment(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
        sequence: u64,
    ) -> Result<QueryPacketCommitmentResponse, DaemonError> {
        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_commitment: QueryPacketCommitmentResponse = cosmos_query!(
            self,
            ibc_channel,
            packet_commitment,
            QueryPacketCommitmentRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                sequence: sequence,
            }
        );

        Ok(ibc_packet_commitment)
    }

    // Receipt

    /// Returns if the packet is received on the connected chain.
    pub async fn _packet_receipt(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
        sequence: u64,
    ) -> Result<bool, DaemonError> {
        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_receipt: ibc_channel::QueryPacketReceiptResponse = cosmos_query!(
            self,
            ibc_channel,
            packet_receipt,
            QueryPacketReceiptRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                sequence: sequence,
            }
        );

        Ok(ibc_packet_receipt.received)
    }

    // Acknowledgement

    /// Get all the packet acknowledgements for a specific channel, port and commitment sequences
    pub async fn _packet_acknowledgements(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
        packet_commitment_sequences: Vec<u64>,
    ) -> Result<Vec<ibc_channel::PacketState>, DaemonError> {
        use cosmos_modules::ibc_channel::QueryPacketAcknowledgementsResponse;

        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_acknowledgements: QueryPacketAcknowledgementsResponse = cosmos_query!(
            self,
            ibc_channel,
            packet_acknowledgements,
            QueryPacketAcknowledgementsRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                packet_commitment_sequences: packet_commitment_sequences,
                pagination: None,
            }
        );

        Ok(ibc_packet_acknowledgements.acknowledgements)
    }

    /// Get the packet acknowledgement for a specific channel, port and sequence
    pub async fn _packet_acknowledgement(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
        sequence: u64,
    ) -> Result<Vec<u8>, DaemonError> {
        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_acknowledgement: ibc_channel::QueryPacketAcknowledgementResponse = cosmos_query!(
            self,
            ibc_channel,
            packet_acknowledgement,
            QueryPacketAcknowledgementRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                sequence: sequence,
            }
        );

        Ok(ibc_packet_acknowledgement.acknowledgement)
    }

    /// No acknowledgement exists on receiving chain for the given packet commitment sequence on sending chain.
    /// Returns the packet sequences that have not yet been received.
    pub async fn _unreceived_packets(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
        packet_commitment_sequences: Vec<u64>,
    ) -> Result<Vec<u64>, DaemonError> {
        use cosmos_modules::ibc_channel::QueryUnreceivedPacketsResponse;

        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_unreceived: QueryUnreceivedPacketsResponse = cosmos_query!(
            self,
            ibc_channel,
            unreceived_packets,
            QueryUnreceivedPacketsRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                packet_commitment_sequences: packet_commitment_sequences,
            }
        );

        Ok(ibc_packet_unreceived.sequences)
    }

    /// Returns the acknowledgement sequences that have not yet been received.
    /// Given a list of acknowledgement sequences from counterparty, determine if an ack on the counterparty chain has been received on the executing chain.
    /// Returns the list of acknowledgement sequences that have not yet been received.
    pub async fn _unreceived_acks(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
        packet_ack_sequences: Vec<u64>,
    ) -> Result<Vec<u64>, DaemonError> {
        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let ibc_packet_unreceived: ibc_channel::QueryUnreceivedAcksResponse = cosmos_query!(
            self,
            ibc_channel,
            unreceived_acks,
            QueryUnreceivedAcksRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
                packet_ack_sequences: packet_ack_sequences,
            }
        );

        Ok(ibc_packet_unreceived.sequences)
    }

    /// Returns the acknowledgement sequences that have not yet been received.
    /// Given a list of acknowledgement sequences from counterparty, determine if an ack on the counterparty chain has been received on the executing chain.
    /// Returns the list of acknowledgement sequences that have not yet been received.
    pub async fn _next_sequence_receive(
        &self,
        port_id: impl Into<String>,
        channel_id: impl Into<String>,
    ) -> Result<u64, DaemonError> {
        let port_id = port_id.into();
        let channel_id = channel_id.into();
        let next_receive: ibc_channel::QueryNextSequenceReceiveResponse = cosmos_query!(
            self,
            ibc_channel,
            next_sequence_receive,
            QueryNextSequenceReceiveRequest {
                port_id: port_id.clone(),
                channel_id: channel_id.clone(),
            }
        );

        Ok(next_receive.next_sequence_receive)
    }
}