diff --git a/Cargo.lock b/Cargo.lock index 9851fd3..58212da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,7 +177,7 @@ dependencies = [ [[package]] name = "astroport-pcl-osmo" -version = "1.0.0" +version = "1.0.1" dependencies = [ "anyhow", "astroport 3.11.1", diff --git a/contracts/pair_concentrated/Cargo.toml b/contracts/pair_concentrated/Cargo.toml index b8277b6..9d8771c 100644 --- a/contracts/pair_concentrated/Cargo.toml +++ b/contracts/pair_concentrated/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "astroport-pcl-osmo" -version = "1.0.0" +version = "1.0.1" authors = ["Astroport"] edition = "2021" description = "Astroport passive concentrated pair contract for Osmosis" diff --git a/contracts/pair_concentrated/src/contract.rs b/contracts/pair_concentrated/src/contract.rs index 76928ef..747be44 100644 --- a/contracts/pair_concentrated/src/contract.rs +++ b/contracts/pair_concentrated/src/contract.rs @@ -26,8 +26,8 @@ use astroport_pcl_common::{calc_d, get_xcp}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - attr, coin, ensure, from_json, to_json_binary, Addr, Binary, Decimal, Decimal256, DepsMut, Env, - MessageInfo, Reply, Response, StdError, StdResult, SubMsg, Uint128, + attr, coin, ensure, from_json, to_json_binary, Addr, Binary, Decimal, Decimal256, DepsMut, + Empty, Env, MessageInfo, Reply, Response, StdError, StdResult, SubMsg, Uint128, }; use cw2::set_contract_version; use cw_utils::must_pay; @@ -874,3 +874,22 @@ pub fn update_config( Ok(Response::new().add_attribute("action", action)) } + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result { + let contract_version = cw2::get_contract_version(deps.storage)?; + + match contract_version.contract.as_ref() { + "astroport-pcl-osmo" => match contract_version.version.as_ref() { + "1.0.0" => {} + _ => return Err(ContractError::MigrationError {}), + }, + _ => return Err(ContractError::MigrationError {}), + }; + + Ok(Response::new() + .add_attribute("previous_contract_name", &contract_version.contract) + .add_attribute("previous_contract_version", &contract_version.version) + .add_attribute("new_contract_name", CONTRACT_NAME) + .add_attribute("new_contract_version", CONTRACT_VERSION)) +} diff --git a/contracts/pair_concentrated/src/error.rs b/contracts/pair_concentrated/src/error.rs index 2f41ce8..f7d76ef 100644 --- a/contracts/pair_concentrated/src/error.rs +++ b/contracts/pair_concentrated/src/error.rs @@ -1,10 +1,9 @@ -use cosmwasm_std::{ConversionOverflowError, OverflowError, StdError}; -use thiserror::Error; - use astroport::asset::MINIMUM_LIQUIDITY_AMOUNT; use astroport_circular_buffer::error::BufferError; use astroport_pcl_common::error::PclError; +use cosmwasm_std::{ConversionOverflowError, OverflowError, StdError}; use cw_utils::PaymentError; +use thiserror::Error; /// This enum describes pair contract errors #[derive(Error, Debug, PartialEq)] @@ -53,4 +52,7 @@ pub enum ContractError { #[error("Pool id is already set")] PoolIdAlreadySet {}, + + #[error("Failed to migrate contract")] + MigrationError {}, } diff --git a/contracts/pair_concentrated/src/queries.rs b/contracts/pair_concentrated/src/queries.rs index 4b3cb8a..4fcab40 100644 --- a/contracts/pair_concentrated/src/queries.rs +++ b/contracts/pair_concentrated/src/queries.rs @@ -14,8 +14,8 @@ use astroport_pcl_common::{calc_d, get_xcp}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - to_json_binary, Binary, Decimal, Decimal256, Deps, Env, Fraction, StdError, StdResult, Uint128, - Uint64, + ensure, to_json_binary, Binary, Decimal, Decimal256, Deps, Env, Fraction, StdError, StdResult, + Uint128, Uint64, }; use itertools::Itertools; @@ -131,22 +131,24 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { AssetInfo::Token { .. } => unreachable!("Token assets are not supported"), }) .collect_vec(); - let price = query_observation(deps, env, OBSERVATIONS, 0)?.price; - - let spot_price = if pool_denoms[0] == quote_asset_denom - && pool_denoms[1] == base_asset_denom - { - price - .inv() - .ok_or_else(|| StdError::generic_err("Price is zero")) - } else if pool_denoms[0] == base_asset_denom && pool_denoms[1] == quote_asset_denom { - Ok(price) - } else { - Err(StdError::generic_err(format!( + + ensure!( + pool_denoms.contains("e_asset_denom) && pool_denoms.contains(&base_asset_denom), + StdError::generic_err(format!( "Invalid pool denoms {quote_asset_denom} {base_asset_denom}. Must be {} {}", pool_denoms[0], pool_denoms[1] - ))) - }?; + )) + ); + + let spot_price = query_observation(deps, env, OBSERVATIONS, 0) + .map(|observation| { + if pool_denoms[0] == quote_asset_denom && pool_denoms[1] == base_asset_denom { + observation.price + } else { + observation.price.inv().unwrap_or_default() + } + }) + .unwrap_or(Decimal::zero()); to_json_binary(&SpotPriceResponse { spot_price }) } diff --git a/contracts/pair_concentrated/tests/pair_concentrated_integration.rs b/contracts/pair_concentrated/tests/pair_concentrated_integration.rs index f2eba0d..25e44de 100644 --- a/contracts/pair_concentrated/tests/pair_concentrated_integration.rs +++ b/contracts/pair_concentrated/tests/pair_concentrated_integration.rs @@ -1512,8 +1512,8 @@ fn test_osmosis_specific_queries() { .query_wasm_smart::( &helper.pair_addr, &QueryMsg::SpotPrice { - quote_asset_denom: helper.assets[&test_coins[1]].to_string(), - base_asset_denom: helper.assets[&test_coins[0]].to_string(), + quote_asset_denom: helper.assets[&test_coins[0]].to_string(), + base_asset_denom: helper.assets[&test_coins[1]].to_string(), }, ) .unwrap(); @@ -1527,8 +1527,8 @@ fn test_osmosis_specific_queries() { .query_wasm_smart::( &helper.pair_addr, &QueryMsg::SpotPrice { - quote_asset_denom: helper.assets[&test_coins[0]].to_string(), - base_asset_denom: helper.assets[&test_coins[1]].to_string(), + quote_asset_denom: helper.assets[&test_coins[1]].to_string(), + base_asset_denom: helper.assets[&test_coins[0]].to_string(), }, ) .unwrap();