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