nym-validator-client 1.21.6

Client for interacting with Nyx Cosmos SDK blockchain
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
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

use crate::nyxd::coin::Coin;
use crate::nyxd::cosmwasm_client::types::{
    Account, CodeDetails, Contract, ContractCodeId, Model, SequenceResponse, SimulateResponse,
};
use crate::nyxd::error::NyxdError;
use crate::nyxd::{Height, Query};
use crate::rpc::{TendermintRpcClient, TendermintRpcClientExt};
use async_trait::async_trait;
use cosmrs::cosmwasm::{CodeInfoResponse, ContractCodeHistoryEntry};
use cosmrs::proto::cosmos::tx::v1beta1::{
    SimulateRequest, SimulateResponse as ProtoSimulateResponse,
};
use cosmrs::proto::cosmwasm::wasm::v1::{
    QueryAllContractStateRequest, QueryAllContractStateResponse, QueryCodeRequest,
    QueryCodeResponse, QueryCodesRequest, QueryCodesResponse, QueryContractHistoryRequest,
    QueryContractHistoryResponse, QueryContractInfoRequest, QueryContractInfoResponse,
    QueryContractsByCodeRequest, QueryContractsByCodeResponse, QueryRawContractStateRequest,
    QueryRawContractStateResponse, QuerySmartContractStateRequest, QuerySmartContractStateResponse,
};
use cosmrs::tendermint::{block, chain, Hash};
use prost::Message;
use serde::{Deserialize, Serialize};

use cosmrs::AccountId;
use std::time::Duration;
use tendermint_rpc::endpoint::{
    block::Response as BlockResponse, broadcast, tx::Response as TxResponse,
};

use crate::nyxd::helpers::{create_pagination, next_page_key};
use crate::rpc::types::ProvableAbciQueryResponse;

pub const DEFAULT_BROADCAST_POLLING_RATE: Duration = Duration::from_secs(4);
pub const DEFAULT_BROADCAST_TIMEOUT: Duration = Duration::from_secs(60);

// this trait should only be concerned with the cosmwasm module,
// so all other legacy methods are deprecated and will be removed in the future
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait CosmWasmClient: TendermintRpcClientExt {
    // helper method to remove duplicate code involved in making abci requests with protobuf messages
    // TODO: perhaps it should have an additional argument to determine whether the response should
    // require proof?
    #[deprecated(note = "use TendermintRpcClientExt::make_abci_query_without_proof instead")]
    async fn make_abci_query<Req, Res>(
        &self,
        path: Option<String>,
        req: Req,
    ) -> Result<Res, NyxdError>
    where
        Req: Message,
        Res: Message + Default,
    {
        TendermintRpcClientExt::make_abci_query_without_proof(self, path, req, None).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_chain_id instead")]
    async fn get_chain_id(&self) -> Result<chain::Id, NyxdError> {
        TendermintRpcClientExt::get_chain_id(self).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_height instead")]
    async fn get_height(&self) -> Result<block::Height, NyxdError> {
        TendermintRpcClientExt::get_height(self).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_account instead")]
    async fn get_account(&self, address: &AccountId) -> Result<Option<Account>, NyxdError> {
        TendermintRpcClientExt::get_account(self, address).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_sequence instead")]
    async fn get_sequence(&self, address: &AccountId) -> Result<SequenceResponse, NyxdError> {
        TendermintRpcClientExt::get_sequence(self, address).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_block instead")]
    async fn get_block(&self, height: Option<u32>) -> Result<BlockResponse, NyxdError> {
        TendermintRpcClientExt::get_block(self, height).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_balance instead")]
    async fn get_balance(
        &self,
        address: &AccountId,
        search_denom: String,
    ) -> Result<Option<Coin>, NyxdError> {
        TendermintRpcClientExt::get_balance(self, address, search_denom).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_all_balances instead")]
    async fn get_all_balances(&self, address: &AccountId) -> Result<Vec<Coin>, NyxdError> {
        TendermintRpcClientExt::get_all_balances(self, address).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_total_supply instead")]
    async fn get_total_supply(&self) -> Result<Vec<Coin>, NyxdError> {
        TendermintRpcClientExt::get_total_supply(self).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::get_tx instead")]
    async fn get_tx(&self, id: Hash) -> Result<TxResponse, NyxdError> {
        TendermintRpcClientExt::get_tx(self, id).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::search_tx instead")]
    async fn search_tx(&self, query: Query) -> Result<Vec<TxResponse>, NyxdError> {
        TendermintRpcClientExt::search_tx(self, query).await
    }

    /// Broadcast a transaction, returning immediately.
    #[deprecated(note = "use TendermintRpcClientExt::broadcast_tx_async instead")]
    async fn broadcast_tx_async<T>(&self, tx: T) -> Result<broadcast::tx_async::Response, NyxdError>
    where
        T: Into<Vec<u8>> + Send,
    {
        TendermintRpcClientExt::broadcast_tx_async(self, tx).await
    }

    /// Broadcast a transaction, returning the response from `CheckTx`.
    #[deprecated(note = "use TendermintRpcClientExt::broadcast_tx_sync instead")]
    async fn broadcast_tx_sync<T>(&self, tx: T) -> Result<broadcast::tx_sync::Response, NyxdError>
    where
        T: Into<Vec<u8>> + Send,
    {
        TendermintRpcClientExt::broadcast_tx_sync(self, tx).await
    }

    /// Broadcast a transaction, returning the response from `DeliverTx`.
    #[deprecated(note = "use TendermintRpcClientExt::broadcast_tx_commit instead")]
    async fn broadcast_tx_commit<T>(
        &self,
        tx: T,
    ) -> Result<broadcast::tx_commit::Response, NyxdError>
    where
        T: Into<Vec<u8>> + Send,
    {
        TendermintRpcClientExt::broadcast_tx_commit(self, tx).await
    }

    #[deprecated(note = "use TendermintRpcClientExt::broadcast_tx instead")]
    async fn broadcast_tx<T>(
        &self,
        tx: T,
        timeout: impl Into<Option<Duration>> + Send,
        poll_interval: impl Into<Option<Duration>> + Send,
    ) -> Result<TxResponse, NyxdError>
    where
        T: Into<Vec<u8>> + Send,
    {
        TendermintRpcClientExt::broadcast_tx(self, tx, timeout, poll_interval).await
    }

    async fn get_codes(&self) -> Result<Vec<CodeInfoResponse>, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/Codes".to_owned());

        let mut raw_codes = Vec::new();
        let mut pagination = None;

        loop {
            let req = QueryCodesRequest { pagination };

            let mut res = self
                .make_abci_query_without_proof::<_, QueryCodesResponse>(path.clone(), req, None)
                .await?;

            let early_break = res.code_infos.is_empty();
            raw_codes.append(&mut res.code_infos);

            if early_break {
                break;
            }

            if let Some(next_key) = next_page_key(res.pagination) {
                pagination = Some(create_pagination(next_key))
            } else {
                break;
            }
        }

        Ok(raw_codes
            .into_iter()
            .map(TryFrom::try_from)
            .collect::<Result<_, _>>()?)
    }

    async fn get_code_details(&self, code_id: ContractCodeId) -> Result<CodeDetails, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/Code".to_owned());

        let req = QueryCodeRequest { code_id };

        let res = self
            .make_abci_query_without_proof::<_, QueryCodeResponse>(path, req, None)
            .await?;

        if let Some(code_info) = res.code_info {
            Ok(CodeDetails::new(code_info.try_into()?, res.data))
        } else {
            Err(NyxdError::NoCodeInformation(code_id))
        }
    }
    async fn get_contracts(&self, code_id: ContractCodeId) -> Result<Vec<AccountId>, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/ContractsByCode".to_owned());

        let mut raw_contracts = Vec::new();
        let mut pagination = None;

        loop {
            let req = QueryContractsByCodeRequest {
                code_id,
                pagination,
            };

            let mut res = self
                .make_abci_query_without_proof::<_, QueryContractsByCodeResponse>(
                    path.clone(),
                    req,
                    None,
                )
                .await?;

            let early_break = res.contracts.is_empty();
            raw_contracts.append(&mut res.contracts);

            if early_break {
                break;
            }

            if let Some(next_key) = next_page_key(res.pagination) {
                pagination = Some(create_pagination(next_key))
            } else {
                break;
            }
        }

        raw_contracts
            .iter()
            .map(|raw| raw.parse())
            .collect::<Result<_, _>>()
            .map_err(|_| NyxdError::DeserializationError("Contract addresses".to_owned()))
    }

    async fn get_contract(&self, address: &AccountId) -> Result<Contract, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/ContractInfo".to_owned());

        let req = QueryContractInfoRequest {
            address: address.to_string(),
        };

        let res = self
            .make_abci_query_without_proof::<_, QueryContractInfoResponse>(path, req, None)
            .await?;

        let response_address = res.address;
        if let Some(contract_info) = res.contract_info {
            let address = response_address
                .parse()
                .map_err(|_| NyxdError::MalformedAccountAddress(response_address))?;
            Ok(Contract::new(address, contract_info.try_into()?))
        } else {
            Err(NyxdError::NoContractInformation(address.clone()))
        }
    }

    async fn get_contract_code_history(
        &self,
        address: &AccountId,
    ) -> Result<Vec<ContractCodeHistoryEntry>, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/ContractHistory".to_owned());

        let mut raw_entries = Vec::new();
        let mut pagination = None;

        loop {
            let req = QueryContractHistoryRequest {
                address: address.to_string(),
                pagination,
            };

            let mut res = self
                .make_abci_query_without_proof::<_, QueryContractHistoryResponse>(
                    path.clone(),
                    req,
                    None,
                )
                .await?;

            let early_break = res.entries.is_empty();
            raw_entries.append(&mut res.entries);

            if early_break {
                break;
            }

            if let Some(next_key) = next_page_key(res.pagination) {
                pagination = Some(create_pagination(next_key))
            } else {
                break;
            }
        }

        Ok(raw_entries
            .into_iter()
            .map(TryFrom::try_from)
            .collect::<Result<_, _>>()?)
    }

    async fn query_all_contract_state(&self, address: &AccountId) -> Result<Vec<Model>, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/AllContractState".to_owned());

        let mut models = Vec::new();
        let mut pagination = None;

        loop {
            let req = QueryAllContractStateRequest {
                address: address.to_string(),
                pagination,
            };

            let mut res = self
                .make_abci_query_without_proof::<_, QueryAllContractStateResponse>(
                    path.clone(),
                    req,
                    None,
                )
                .await?;

            let empty_response = res.models.is_empty();
            models.append(&mut res.models);

            if empty_response {
                break;
            }
            if let Some(next_key) = next_page_key(res.pagination) {
                pagination = Some(create_pagination(next_key))
            } else {
                break;
            }
        }

        Ok(models.into_iter().map(Into::into).collect())
    }

    async fn query_contract_raw(
        &self,
        address: &AccountId,
        query_data: Vec<u8>,
    ) -> Result<Vec<u8>, NyxdError> {
        self.query_contract_raw_at_height(address, query_data, None)
            .await
    }

    async fn query_contract_raw_at_height(
        &self,
        address: &AccountId,
        query_data: Vec<u8>,
        height: Option<Height>,
    ) -> Result<Vec<u8>, NyxdError> {
        let path = Some("/cosmwasm.wasm.v1.Query/RawContractState".to_owned());

        let req = QueryRawContractStateRequest {
            address: address.to_string(),
            query_data,
        };

        let res = self
            .make_abci_query_without_proof::<_, QueryRawContractStateResponse>(path, req, height)
            .await?;

        Ok(res.data)
    }

    async fn query_contract_raw_with_proof(
        &self,
        address: &AccountId,
        query_data: Vec<u8>,
        height: Option<Height>,
    ) -> Result<ProvableAbciQueryResponse<Vec<u8>>, NyxdError> {
        let path = Some("/store/wasm/key".to_owned());
        let key = crate::nyxd::cosmwasm_client::contract_storage_key(address, &query_data);

        self.make_raw_abci_query_with_proof(path, key, height).await
    }

    async fn query_contract_smart<M, T>(
        &self,
        address: &AccountId,
        query_msg: &M,
    ) -> Result<T, NyxdError>
    where
        M: ?Sized + Serialize + Sync,
        for<'a> T: Deserialize<'a>,
    {
        self.query_contract_smart_at_height(address, query_msg, None)
            .await
    }

    async fn query_contract_smart_at_height<M, T>(
        &self,
        address: &AccountId,
        query_msg: &M,
        height: Option<Height>,
    ) -> Result<T, NyxdError>
    where
        M: ?Sized + Serialize + Sync,
        for<'a> T: Deserialize<'a>,
    {
        let path = Some(
            "/cosmwasm.wasm.v1.Query/SmartContractState"
                .parse()
                .unwrap(),
        );

        // As per serde documentation:
        // Serialization can fail if `T`'s implementation of `Serialize` decides to
        // fail, or if `T` contains a map with non-string keys.
        let req = QuerySmartContractStateRequest {
            address: address.to_string(),
            query_data: serde_json::to_vec(query_msg)?,
        };

        let res = self
            .make_abci_query_without_proof::<_, QuerySmartContractStateResponse>(path, req, height)
            .await?;

        tracing::trace!("raw query response: {}", String::from_utf8_lossy(&res.data));
        Ok(serde_json::from_slice(&res.data)?)
    }

    #[deprecated(note = "use TendermintRpcClientExt::query_simulate instead")]
    async fn query_simulate(&self, tx_bytes: Vec<u8>) -> Result<SimulateResponse, NyxdError> {
        let path = Some("/cosmos.tx.v1beta1.Service/Simulate".to_owned());

        let req = SimulateRequest {
            tx_bytes,
            ..Default::default()
        };

        let res = self
            .make_abci_query_without_proof::<_, ProtoSimulateResponse>(path, req, None)
            .await?;

        res.try_into()
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<T> CosmWasmClient for T where T: TendermintRpcClient {}