abstract_interface/
ibc.rs

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
use crate::{AbstractInterfaceError, IbcClient, IbcHost, Registry};
use abstract_std::{IBC_CLIENT, IBC_HOST};
use cw_orch::prelude::*;

#[derive(Clone)]
pub struct AbstractIbc<Chain: CwEnv> {
    pub client: IbcClient<Chain>,
    pub host: IbcHost<Chain>,
}

impl<Chain: CwEnv> AbstractIbc<Chain> {
    pub fn new(chain: &Chain) -> Self {
        let ibc_client = IbcClient::new(IBC_CLIENT, chain.clone());
        let ibc_host = IbcHost::new(IBC_HOST, chain.clone());
        Self {
            client: ibc_client,
            host: ibc_host,
        }
    }

    pub fn upload(&self) -> Result<(), crate::AbstractInterfaceError> {
        self.client.upload()?;
        self.host.upload()?;
        Ok(())
    }

    pub fn instantiate(&self, admin: &Addr) -> Result<(), CwOrchError> {
        self.client.instantiate(
            &abstract_std::ibc_client::InstantiateMsg {},
            Some(admin),
            &[],
        )?;

        self.host
            .instantiate(&abstract_std::ibc_host::InstantiateMsg {}, Some(admin), &[])?;
        Ok(())
    }

    pub fn register(&self, registry: &Registry<Chain>) -> Result<(), AbstractInterfaceError> {
        registry.register_natives(vec![
            (
                self.client.as_instance(),
                ibc_client::contract::CONTRACT_VERSION.to_string(),
            ),
            (
                self.host.as_instance(),
                ibc_host::contract::CONTRACT_VERSION.to_string(),
            ),
        ])
    }

    pub fn call_as(&self, sender: &<Chain as TxHandler>::Sender) -> Self {
        Self {
            client: self.client.call_as(sender),
            host: self.host.call_as(sender),
        }
    }
}

#[cfg(feature = "interchain")]
// Helpers to create connection with another chain
pub mod connection {
    use crate::Abstract;

    use super::*;
    use abstract_std::ibc_client::{ExecuteMsgFns, QueryMsgFns};
    use abstract_std::ibc_host::ExecuteMsgFns as _;
    use abstract_std::objects::TruncatedChainId;
    use cw_orch::environment::Environment;
    use cw_orch_interchain::prelude::*;
    use cw_orch_polytone::interchain::PolytoneConnection;

    impl<Chain: IbcQueryHandler> Abstract<Chain> {
        /// This is used for creating a testing connection between two Abstract connections.
        ///
        /// If a polytone deployment is already , it uses the existing deployment, If it doesn't exist, it creates it
        ///
        /// You usually don't need this function on actual networks if you're not an Abstract maintainer
        pub fn connect_to<IBC: InterchainEnv<Chain>>(
            &self,
            remote_abstr: &Abstract<Chain>,
            interchain: &IBC,
        ) -> Result<(), AbstractInterfaceError> {
            connect_one_way_to(self, remote_abstr, interchain)?;
            connect_one_way_to(remote_abstr, self, interchain)?;
            Ok(())
        }
    }

    impl AbstractIbc<Daemon> {
        /// Register infrastructure on connected chains
        pub fn register_infrastructure(&self) -> Result<(), AbstractInterfaceError> {
            let register_infrastructures =
                connection::list_ibc_infrastructures(self.host.environment().clone());

            for (chain, ibc_infrastructure) in register_infrastructures.counterparts {
                use abstract_std::ibc_client::ExecuteMsgFns;

                self.client.register_infrastructure(
                    chain,
                    ibc_infrastructure.remote_abstract_host,
                    ibc_infrastructure.polytone_note,
                )?;
            }
            Ok(())
        }
    }

    pub fn connect_one_way_to<Chain: IbcQueryHandler, IBC: InterchainEnv<Chain>>(
        abstr_client: &Abstract<Chain>,
        abstr_host: &Abstract<Chain>,
        interchain: &IBC,
    ) -> Result<(), AbstractInterfaceError> {
        // First we register client and host respectively
        let chain1_id = abstr_client.ibc.client.environment().chain_id();
        let chain1_name = TruncatedChainId::from_chain_id(&chain1_id);

        let chain2_id = abstr_host.ibc.client.environment().chain_id();
        let chain2_name = TruncatedChainId::from_chain_id(&chain2_id);

        // We get the polytone connection
        let polytone_connection =
            PolytoneConnection::deploy_between_if_needed(interchain, &chain1_id, &chain2_id)?;

        // First, we register the host with the client.
        // We register the polytone note with it because they are linked
        // This triggers an IBC message that is used to get back the proxy address
        let proxy_tx_result = abstr_client.ibc.client.register_infrastructure(
            chain2_name.clone(),
            abstr_host.ibc.host.address()?.to_string(),
            polytone_connection.note.address()?.to_string(),
        )?;
        // We make sure the IBC execution is done so that the proxy address is saved inside the Abstract contract
        interchain.await_and_check_packets(&chain1_id, proxy_tx_result)?;

        // Finally, we get the proxy address and register the proxy with the ibc host for the host chain
        let proxy_address = abstr_client.ibc.client.host(chain2_name)?;

        abstr_host
            .ibc
            .host
            .register_chain_proxy(chain1_name, proxy_address.remote_polytone_proxy.unwrap())?;

        Ok(())
    }

    pub fn list_ibc_infrastructures<Chain: CwEnv>(
        chain: Chain,
    ) -> abstract_std::ibc_client::ListIbcInfrastructureResponse {
        let Ok(polytone) = cw_orch_polytone::Polytone::load_from(chain) else {
            return abstract_std::ibc_client::ListIbcInfrastructureResponse {
                counterparts: vec![],
            };
        };
        let abstract_state = crate::AbstractDaemonState::default();
        let deployment_id = "default".to_owned();

        let mut counterparts = vec![];
        for connected_polytone in polytone.connected_polytones() {
            // TODO: remove parse network after migrating to cw-orch 0.25 (we only need chain id now)
            let Ok(chain_info) = networks::parse_network(&connected_polytone.chain_id) else {
                continue;
            };
            let chain_name = chain_info.network_info.chain_name.to_owned();
            let env_info = EnvironmentInfo {
                chain_id: connected_polytone.chain_id.clone(),
                chain_name,
                deployment_id: deployment_id.clone(),
            };
            if let Some(remote_abstract_host) = abstract_state.contract_addr(&env_info, IBC_HOST) {
                let truncated_chain_id =
                    abstract_std::objects::TruncatedChainId::from_chain_id(&env_info.chain_id);
                counterparts.push((
                    truncated_chain_id,
                    abstract_std::ibc_client::state::IbcInfrastructure {
                        polytone_note: connected_polytone.note,
                        remote_abstract_host: remote_abstract_host.into(),
                        remote_proxy: None,
                    },
                ))
            }
        }
        abstract_std::ibc_client::ListIbcInfrastructureResponse { counterparts }
    }
}

#[cfg(feature = "interchain")]
#[cfg(test)]
mod test {
    use super::connection::*;
    use super::*;

    // From https://github.com/CosmosContracts/juno/blob/32568dba828ff7783aea8cb5bb4b8b5832888255/docker/test-user.env#L2
    const LOCAL_MNEMONIC: &str = "clip hire initial neck maid actor venue client foam budget lock catalog sweet steak waste crater broccoli pipe steak sister coyote moment obvious choose";

    #[test]
    fn list_ibc() {
        use networks::JUNO_1;

        // Deploy requires CwEnv, even for load
        let juno = DaemonBuilder::new(JUNO_1)
            .mnemonic(LOCAL_MNEMONIC)
            .build()
            .unwrap();
        let l = list_ibc_infrastructures(juno);
        l.counterparts.iter().any(|(chain_id, ibc_infra)| {
            chain_id.as_str() == "osmosis"
                && ibc_infra.remote_abstract_host.starts_with("osmo")
                && ibc_infra.polytone_note.to_string().starts_with("juno")
        });
    }
}