bolt-cw-sdk 1.0.0

SDK for the BOLT protocol, providing utilities for interacting with the BOLT contracts.
Documentation
use crate::market::client::MarketClient;
use crate::market::error::MarketError;
use cosmwasm_std::{Addr, Decimal256, Uint128};
use serde::{Deserialize, Serialize};

impl MarketClient {
    pub async fn config(&self, market_address: String) -> Result<ConfigResponse, MarketError> {
        let query = Query {
            config: ConfigRequest {},
        };
        let response = self
            .cosmos_client
            .query_contract(market_address, &query, None)
            .await?
            .data;

        Ok(response)
    }
}

#[derive(Serialize)]
struct Query {
    pub config: ConfigRequest,
}
#[derive(Serialize)]
pub struct ConfigRequest {}

#[derive(Debug, Deserialize)]
pub struct ConfigResponse {
    pub price_oracle_contract: Addr,
    pub protocol_fee_recipient: Addr,
    pub protocol_fee: Decimal256,
    pub lp_fee: Decimal256,
    pub min_base_out: Uint128,
}

#[cfg(test)]
mod tests {
    use crate::market::client::MarketClient;
    use crate::market::error::MarketError;
    use crate::oracle::client::OracleAdminClient;
    use crate::test_utils::helpers::{TEST_ASSET_ARCH_SYMBOL, TEST_ASSET_USDT_SYMBOL};
    use crate::test_utils::test_scenario::TestScenario;
    use cosmwasm_std::{Addr, 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_config() {
        let mut test_scenario = TestScenario::new_from_config("config.json".to_string()).await;

        let price_threshold_ratio = Decimal256::from_str("0.1").unwrap();
        let price_expire_millis = Some(1000);
        let price_oracle_contract = test_scenario
            .instantiate_oracle_contract(price_threshold_ratio, price_expire_millis)
            .await;
        let client = OracleAdminClient::from_scenario(&test_scenario, &price_oracle_contract)
            .expect("Failed to create oracle admin client");

        test_scenario.set_default_assets(&client).await;
        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_prices(&client, price_expiry_timestamp, "50000")
            .await;

        let protocol_fee_recipient = test_scenario.admin_address.clone();
        let protocol_fee = Decimal256::percent(10);
        let lp_fee = Decimal256::percent(10);
        let min_base_out = Uint128::from(1000u64);

        let market_contract_addr = test_scenario
            .instantiate_settlement_contract(
                price_oracle_contract.clone(),
                protocol_fee_recipient.clone(),
                protocol_fee,
                lp_fee,
                TEST_ASSET_ARCH_SYMBOL.to_owned(),
                vec![TEST_ASSET_USDT_SYMBOL.to_owned()],
                min_base_out,
            )
            .await;

        let market_client =
            MarketClient::new(test_scenario.cosmos_client).expect("Failed to create market client");

        let config_err = market_client
            .config(price_oracle_contract.clone())
            .await
            .expect_err("Should fail with invalid market address");
        assert!(matches!(config_err, MarketError::CosmosClientError(_)));

        let config = market_client
            .config(market_contract_addr.clone())
            .await
            .expect("Failed to get config");
        assert_eq!(
            config.price_oracle_contract,
            Addr::unchecked(price_oracle_contract)
        );
        assert_eq!(
            config.protocol_fee_recipient,
            Addr::unchecked(protocol_fee_recipient)
        );
        assert_eq!(config.protocol_fee, protocol_fee);
        assert_eq!(config.lp_fee, lp_fee);
        assert_eq!(config.min_base_out, min_base_out);
    }
}