Skip to content

Commit

Permalink
fix(pcl): invert price in spot price query
Browse files Browse the repository at this point in the history
  • Loading branch information
epanchee committed Mar 13, 2024
1 parent 847dcb5 commit 3a18c60
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/pair_concentrated/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
23 changes: 21 additions & 2 deletions contracts/pair_concentrated/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Response, ContractError> {
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))
}
8 changes: 5 additions & 3 deletions contracts/pair_concentrated/src/error.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -53,4 +52,7 @@ pub enum ContractError {

#[error("Pool id is already set")]
PoolIdAlreadySet {},

#[error("Failed to migrate contract")]
MigrationError {},
}
34 changes: 18 additions & 16 deletions contracts/pair_concentrated/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -131,22 +131,24 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
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(&quote_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 })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1512,8 +1512,8 @@ fn test_osmosis_specific_queries() {
.query_wasm_smart::<SpotPriceResponse>(
&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();
Expand All @@ -1527,8 +1527,8 @@ fn test_osmosis_specific_queries() {
.query_wasm_smart::<SpotPriceResponse>(
&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();
Expand Down

0 comments on commit 3a18c60

Please sign in to comment.