layer_climb_core/
prelude.rs

1// local "prelude" that isn't exported
2// some of these may be exported in the main prelude
3pub(crate) use crate::network::apply_grpc_height;
4pub(crate) use anyhow::{anyhow, bail, Context, Result};
5pub(crate) use layer_climb_address::{Address, ConfigAddressExt};
6pub(crate) use layer_climb_config::*;
7pub(crate) use layer_climb_proto::{proto_into_any, proto_into_bytes, Message};
8
9// common types
10pub use crate::{
11    cache::ClimbCache,
12    contract_helpers::contract_str_to_msg,
13    events::CosmosTxEvents,
14    querier::{Connection, ConnectionMode, QueryClient, QueryRequest},
15    signing::SigningClient,
16    transaction::TxBuilder,
17};
18
19#[cfg(not(target_arch = "wasm32"))]
20pub use crate::pool::*;
21
22// Common types that can be confusing between different proto files.
23// standardized here. In cases where we want helper methods, use extension traits
24// so that we don't have to deal with confusion between types.
25
26/// helper function to create a Coin
27pub fn new_coin(amount: impl ToString, denom: impl ToString) -> layer_climb_proto::Coin {
28    layer_climb_proto::Coin {
29        denom: denom.to_string(),
30        amount: amount.to_string(),
31    }
32}
33
34/// helper function to create a vec of coins from an iterator of tuples
35/// where the first is the amount, and the second is the denom.
36/// Example:
37/// ```ignore
38/// use layer_climb::prelude::*;
39///
40/// new_coins([
41///     ("uusd", "100"),
42///     ("uslay", "200")
43/// ])
44/// ```
45pub fn new_coins(
46    coins: impl IntoIterator<Item = (impl ToString, impl ToString)>,
47) -> Vec<layer_climb_proto::Coin> {
48    coins
49        .into_iter()
50        .map(|(amount, denom)| new_coin(amount, denom))
51        .collect()
52}
53
54/// A useful abstraction when we have either a Signing or Query client
55/// but need to delay the decision of requiring it to be a SigningClient until runtime.
56pub enum AnyClient {
57    Signing(SigningClient),
58    Query(QueryClient),
59}
60
61impl AnyClient {
62    // helper method to get the signing client via ref when we know it's what we have
63    // will panic if called with a QueryClient (for error handling in that case, use TryInto)
64    pub fn as_signing(&self) -> &SigningClient {
65        match self {
66            Self::Signing(client) => client,
67            Self::Query(_) => panic!("Expected SigningClient, got QueryClient"),
68        }
69    }
70
71    // helper method to get the query client via ref
72    pub fn as_querier(&self) -> &QueryClient {
73        match self {
74            Self::Query(client) => client,
75            Self::Signing(client) => &client.querier,
76        }
77    }
78}
79
80impl From<SigningClient> for AnyClient {
81    fn from(client: SigningClient) -> Self {
82        Self::Signing(client)
83    }
84}
85
86impl From<QueryClient> for AnyClient {
87    fn from(client: QueryClient) -> Self {
88        Self::Query(client)
89    }
90}
91
92impl TryFrom<AnyClient> for SigningClient {
93    type Error = anyhow::Error;
94
95    fn try_from(value: AnyClient) -> Result<Self> {
96        match value {
97            AnyClient::Signing(client) => Ok(client),
98            AnyClient::Query(_) => Err(anyhow!("Expected SigningClient, got QueryClient")),
99        }
100    }
101}
102
103impl From<AnyClient> for QueryClient {
104    fn from(client: AnyClient) -> Self {
105        match client {
106            AnyClient::Query(client) => client,
107            AnyClient::Signing(client) => client.querier,
108        }
109    }
110}