Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pcl): denormalize spot price #17

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions 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.2"
version = "1.0.3"
authors = ["Astroport"]
edition = "2021"
description = "Astroport passive concentrated pair contract for Osmosis"
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_concentrated/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, Contra

match contract_version.contract.as_ref() {
"astroport-pcl-osmo" => match contract_version.version.as_ref() {
"1.0.0" | "1.0.1" => {}
"1.0.0" | "1.0.1" | "1.0.2" => {}
_ => return Err(ContractError::MigrationError {}),
},
_ => return Err(ContractError::MigrationError {}),
Expand Down
24 changes: 19 additions & 5 deletions contracts/pair_concentrated/src/queries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use astroport::asset::{native_asset_info, Asset, AssetInfo, AssetInfoExt, Decimal256Ext};
use astroport::cosmwasm_ext::{DecimalToInteger, IntegerToDecimal};
use astroport::observation::{query_observation, try_dec256_into_dec};
use astroport::observation::query_observation;
use astroport::pair::{
ConfigResponse, PoolResponse, ReverseSimulationResponse, SimulationResponse,
};
Expand All @@ -15,8 +15,8 @@ use astroport_pcl_common::{calc_d, get_xcp};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
ensure, to_json_binary, Binary, Decimal, Decimal256, Deps, Env, StdError, StdResult, Uint128,
Uint64,
ensure, to_json_binary, Binary, Decimal, Decimal256, DecimalRangeExceeded, Deps, Env, StdError,
StdResult, Uint128, Uint64,
};
use itertools::Itertools;

Expand Down Expand Up @@ -192,10 +192,24 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
};

// Calculate average from buy and sell prices
let spot_price = (get_spot_price(0)? + get_spot_price(1)?) / TWO;
let mut spot_price = (get_spot_price(0)? + get_spot_price(1)?) / TWO;

// Denormalize the price
let quote_precision = precisions
.get_precision(&AssetInfo::native(&quote_asset_denom))
.map_err(|err| StdError::generic_err(err.to_string()))?;
let base_precision = precisions
.get_precision(&AssetInfo::native(&base_asset_denom))
.map_err(|err| StdError::generic_err(err.to_string()))?;
spot_price *= Decimal256::from_ratio(
10u128.pow(quote_precision.into()),
10u128.pow(base_precision.into()),
);
epanchee marked this conversation as resolved.
Show resolved Hide resolved

to_json_binary(&SpotPriceResponse {
spot_price: try_dec256_into_dec(spot_price)?,
spot_price: spot_price
.try_into()
.map_err(|err: DecimalRangeExceeded| StdError::generic_err(err.to_string()))?,
})
}
// Osmosis confirmed we can safely set 0% here.
Expand Down
3 changes: 2 additions & 1 deletion contracts/pair_concentrated/tests/common/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn init_native_coins(test_coins: &[TestCoin]) -> Vec<Coin> {
.iter()
.filter_map(|test_coin| match test_coin {
TestCoin::Native(name) => {
let init_balance = INIT_BALANCE * 10u128.pow(NATIVE_TOKEN_PRECISION as u32);
let init_balance = u128::MAX / 2;
Some(coin(init_balance, name))
}
_ => None,
Expand Down Expand Up @@ -271,6 +271,7 @@ impl Helper {
("uusd".to_owned(), 6),
("rc".to_owned(), 6),
("foo".to_owned(), 5),
("eth".to_owned(), 18),
],
},
&[],
Expand Down
54 changes: 52 additions & 2 deletions contracts/pair_concentrated/tests/pair_concentrated_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,7 @@ fn check_prices() {

let helper = Helper::new(&owner, test_coins, common_pcl_params()).unwrap();
let err = helper.query_prices().unwrap_err();
assert_eq!(StdError::generic_err("Querier contract error: Generic error: Not implemented.Use { \"observe\" : { \"seconds_ago\" : ... } } instead.")
, err);
assert!(err.to_string().contains("Not implemented.Use"));
}

#[test]
Expand Down Expand Up @@ -1554,6 +1553,57 @@ fn test_osmosis_specific_queries() {
);
}

#[test]
fn test_spot_price_diff_decimals() {
let owner = Addr::unchecked("owner");

let test_coins = vec![TestCoin::native("uusd"), TestCoin::native("eth")];

let mut helper = Helper::new(
&owner,
test_coins.clone(),
ConcentratedPoolParams {
price_scale: Decimal::from_ratio(3000u16, 1u8),
..common_pcl_params()
},
)
.unwrap();

let provide_assets = [
helper.assets[&test_coins[0]].with_balance(3_000_000 * 1e6 as u128),
helper.assets[&test_coins[1]].with_balance(1_000 * 1e18 as u128),
];
helper.give_me_money(&provide_assets, &owner);
helper.provide_liquidity(&owner, &provide_assets).unwrap();

let resp = helper
.app
.wrap()
.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(),
},
)
.unwrap();
assert_eq!(resp.spot_price.to_string(), "0.000000003000010176");

// query inverted price
let price = helper
.app
.wrap()
.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(),
},
)
.unwrap();
assert_eq!(price.spot_price.to_string(), "333334464.080626");
}

#[test]
fn check_reverse_swaps() {
let owner = Addr::unchecked("owner");
Expand Down
2 changes: 1 addition & 1 deletion packages/astroport_on_osmosis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-on-osmosis"
version = "1.1.1"
version = "1.1.2"
authors = ["Astroport"]
edition = "2021"
description = "External API of Astroport contracts on Osmosis"
Expand Down
Loading