abstract_adapter_utils/
identity.rs

1use cosmwasm_std::Env;
2
3pub trait Identify {
4    /// This should return wether the platform is available on the chain designated by chain_name
5    /// For instance, Wyndex is available on juno-1, so wyndex.is_available_on("juno") should return true
6    /// We will only pass the chain name and never the chain_id to this function
7    fn is_available_on(&self, chain_name: &str) -> bool;
8    fn name(&self) -> &'static str;
9}
10
11/// Helper to un-nest the platform name
12/// The platform_name has format juno>wyndex
13// Returns (Option<chain_id>, platform_name)
14pub fn decompose_platform_name(platform_name: &str) -> (Option<String>, String) {
15    let mut decomposed = platform_name.splitn(2, '>');
16    match (
17        decomposed.next().unwrap().to_owned(),
18        decomposed.next().map(str::to_string),
19    ) {
20        (chain_name, Some(platform_name)) => (Some(chain_name), platform_name),
21        (platform_name, None) => (None, platform_name),
22    }
23}
24
25fn get_chain_name(env: &Env) -> &str {
26    env.block.chain_id.rsplitn(2, '-').last().unwrap()
27}
28
29/// Helper to verify the DEX called is on the right chain
30pub fn is_current_chain(env: &Env, chain_name: &str) -> bool {
31    get_chain_name(env) == chain_name
32}
33
34/// Helper to verify the DEX called is on the right chain
35pub fn is_available_on(platform: Box<dyn Identify>, env: &Env, chain_name: Option<&str>) -> bool {
36    if let Some(chain_name) = chain_name {
37        platform.is_available_on(chain_name)
38    } else {
39        let chain_name = get_chain_name(env);
40        platform.is_available_on(chain_name)
41    }
42}