4 releases

0.2.0-rc3 Sep 21, 2022
0.1.0 Aug 15, 2022

#6 in #fn-into-self

45 downloads per month

Apache-2.0

24KB
446 lines

chain-registry

A Rust API for interacting with the Cosmos chain registry repository

Warning

The chain registry is unversioned and syntax is unenforced. This library is written to ignore unrecognized or missing JSON fields but it isn't guaranteed to work for all registry items.

Features

  • Models for serializing and deserializing chain.json, assets.json and IBC path JSON files
  • Simple get/list methods for retrieving chain, asset, and path data
  • A cache type (currently only supports IBC Path data) that exposes additional filtering options

To do

  • Test which queries all objects
  • Release per registry commit?

lib.rs:

An API for the Cosmos chain registry

Examples

Populating a config struct with some of the information needed to execute transactions against a chain (doesn't compile)

use chain_registry::*;

#[derive(Clone, Default)]
struct BotConfig {
    account_prefix: String,
    chain_id: String,
    rpc_endpoint: String,
    default_asset: String,
    ibc_paths: HashMap<String, IBCPath>
}

impl BotConfig {
    pub fn set_default_asset(&mut self, asset: String) {
        self.default_asset = asset;
    }

    pub fn add_ibc_path(&mut self, path: IBCPath) {
        let key = format!("{}-{}", path.channel_1.chain_name, path.channel_2.chain_name);
        self.ibc_paths.insert(&key, path);
    }
}

impl Into<BotConfig> for ChainInfo {
    fn into(self) -> BotConfig {
        BotConfig {
            account_prefix: self.bech32_prefix,
            chain_id: self.chain_id,
            // realistically you should test for health endpoints before choosing
            rpc_endpoint: self.apis.rpc[0].address,
            ..Default::default()
        }
    }
}

#[tokio::main]
fn main() {
    let chain = get_chain("osmosis").await.unwrap();
    let assets = get_assets("osmosis").await.unwrap();
    let osmosis_hub_path = get_path("osmosis", "cosmoshub").await.unwrap();
    let mut config: BotConfig = chain.into();

    config.set_default_asset(assets[0]);
    config.add_ibc_path(osmosis_hub_path);
    // ...
}

Front-loading an IBC path cache for use with a REST API or indexer (doesn't compile)

use chain_registry::*;

#[tokio::main]
fn main {
    // This can take a long time as it has to send an individual request for each path
    // in the registry.
    let cache = RegistryCache::try_new().await.unwrap();

    // Instead of simply querying for an IBCPath, you can filter by Tag.
    let use_osmosis = cache.get_paths_filtered(Tag::Dex("osmosis".to_string()));
}

Dependencies

~9–24MB
~369K SLoC