bolt-cw-sdk 1.0.0

SDK for the BOLT protocol, providing utilities for interacting with the BOLT contracts.
Documentation
use crate::router::client::RouterClient;
use crate::router::error::RouterError;
use cosmwasm_std::Addr;
use serde::{Deserialize, Serialize};

impl RouterClient {
    pub async fn markets(&self) -> Result<Vec<MarketWithAddr>, RouterError> {
        let query = Query {
            markets: MarketsRequest {},
        };
        let response: MarketsResponse = self
            .query_contract(self.router_contract_address.to_string(), &query, None)
            .await?
            .data;
        Ok(response.markets)
    }
}

#[derive(Serialize)]
struct Query {
    pub markets: MarketsRequest,
}

#[derive(Serialize)]
pub struct MarketsRequest {}

#[derive(Deserialize)]
pub struct MarketsResponse {
    pub markets: Vec<MarketWithAddr>,
}

#[derive(Deserialize)]
pub struct MarketWithAddr {
    pub market_address: Addr,
    pub base_asset_symbol: String,
    pub quote_assets_symbols: Vec<String>,
}

#[cfg(test)]
mod tests {
    use crate::oracle::client::OracleAdminClient;
    use crate::router::client::RouterAdminClient;
    use crate::test_utils::helpers::{TEST_ASSET_ARCH_SYMBOL, TEST_ASSET_USDT_SYMBOL};
    use crate::test_utils::test_scenario::TestScenario;
    use cosmwasm_std::{Decimal256, Uint128};
    use serial_test::serial;
    use std::ops::Add;
    use std::str::FromStr;
    use std::time::{Duration, SystemTime, UNIX_EPOCH};

    #[tokio::test]
    #[serial]
    async fn test_markets() {
        let mut test_scenario = TestScenario::new_from_config("config.json".to_string()).await;

        // Initialize oracle contract
        let price_threshold_ratio = Decimal256::from_str("0.5").unwrap();
        let price_expire_millis = Some(1000);
        let oracle_contract_address = test_scenario
            .instantiate_oracle_contract(price_threshold_ratio, price_expire_millis)
            .await;

        // Initialize router contract
        let default_protocol_fee = Decimal256::from_str("0.1").unwrap();
        let default_lp_fee = Decimal256::from_str("0.1").unwrap();
        let router_contract_address = test_scenario
            .instantiate_router_contract(
                oracle_contract_address.clone(),
                oracle_contract_address.clone(),
                default_protocol_fee,
                default_lp_fee,
            )
            .await;

        // Set default assets and prices
        let oracle_client =
            OracleAdminClient::from_scenario(&test_scenario, &oracle_contract_address)
                .expect("Failed to create oracle admin client");
        let price_expiry_time = SystemTime::now().add(Duration::from_secs(7200)); // 2h from now
        let price_expiry_timestamp = price_expiry_time
            .duration_since(UNIX_EPOCH)
            .expect("Converting to timestamp failed");
        test_scenario.set_default_assets(&oracle_client).await;
        test_scenario
            .set_default_prices(&oracle_client, price_expiry_timestamp, "50000")
            .await;

        // Create market
        let router_client = RouterAdminClient::from_scenario(
            &test_scenario,
            router_contract_address.parse().unwrap(),
        )
        .expect("Failed to create router admin client");
        test_scenario
            .create_market(
                &router_client,
                TEST_ASSET_ARCH_SYMBOL,
                &[TEST_ASSET_USDT_SYMBOL],
                Uint128::new(10),
            )
            .await;

        // Query markets
        let markets = router_client.public_router_client.markets().await.unwrap();

        markets.iter().for_each(|market| {
            assert!(market.market_address.to_string().starts_with("archway"));
            assert_eq!(market.base_asset_symbol, TEST_ASSET_ARCH_SYMBOL.to_string());
            assert_eq!(
                market.quote_assets_symbols,
                vec![TEST_ASSET_USDT_SYMBOL.to_string()]
            );
        })
    }

    #[tokio::test]
    #[serial]
    async fn no_market_configured() {
        let mut test_scenario = TestScenario::new_from_config("config.json".to_string()).await;

        // Initialize oracle contract
        let price_threshold_ratio = Decimal256::from_str("0.5").unwrap();
        let price_expire_millis = Some(1000);
        let oracle_contract_address = test_scenario
            .instantiate_oracle_contract(price_threshold_ratio, price_expire_millis)
            .await;

        // Initialize router contract
        let default_protocol_fee = Decimal256::from_str("0.1").unwrap();
        let default_lp_fee = Decimal256::from_str("0.1").unwrap();
        let router_contract_address = test_scenario
            .instantiate_router_contract(
                oracle_contract_address.clone(),
                oracle_contract_address.clone(),
                default_protocol_fee,
                default_lp_fee,
            )
            .await;

        // Set default assets and prices
        let oracle_client =
            OracleAdminClient::from_scenario(&test_scenario, &oracle_contract_address)
                .expect("Failed to create oracle admin client");
        let price_expiry_time = SystemTime::now().add(Duration::from_secs(7200)); // 2h from now
        let price_expiry_timestamp = price_expiry_time
            .duration_since(UNIX_EPOCH)
            .expect("Converting to timestamp failed");
        test_scenario.set_default_assets(&oracle_client).await;
        test_scenario
            .set_default_prices(&oracle_client, price_expiry_timestamp, "50000")
            .await;

        let router_client = RouterAdminClient::from_scenario(
            &test_scenario,
            router_contract_address.parse().unwrap(),
        )
        .expect("Failed to create router admin client");

        // Query markets
        let markets = router_client.public_router_client.markets().await.unwrap();
        assert!(markets.is_empty())
    }
}