From 933d7d57102f6cb632abc5265583e064d17f80b3 Mon Sep 17 00:00:00 2001 From: Timofey <5527315+epanchee@users.noreply.github.com> Date: Mon, 27 May 2024 13:56:54 +0400 Subject: [PATCH] fix(pcl): denormalize spot price --- Cargo.lock | 16 +++--- contracts/pair_concentrated/Cargo.toml | 2 +- contracts/pair_concentrated/src/contract.rs | 2 +- contracts/pair_concentrated/src/queries.rs | 24 +++++++-- .../pair_concentrated/tests/common/helper.rs | 3 +- .../tests/pair_concentrated_integration.rs | 53 ++++++++++++++++++- packages/astroport_on_osmosis/Cargo.toml | 2 +- 7 files changed, 84 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8289ed5..9b03de7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,7 +185,7 @@ dependencies = [ [[package]] name = "astroport-on-osmosis" -version = "1.1.1" +version = "1.1.2" dependencies = [ "astroport 3.12.1", "cosmwasm-schema", @@ -225,7 +225,7 @@ dependencies = [ [[package]] name = "astroport-pcl-osmo" -version = "1.0.2" +version = "1.0.3" dependencies = [ "anyhow", "astroport 3.12.1", @@ -505,9 +505,9 @@ dependencies = [ [[package]] name = "cosmwasm-crypto" -version = "1.5.3" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9934c79e58d9676edfd592557dee765d2a6ef54c09d5aa2edb06156b00148966" +checksum = "dd50718a2b6830ce9eb5d465de5a018a12e71729d66b70807ce97e6dd14f931d" dependencies = [ "digest 0.10.7", "ecdsa", @@ -519,9 +519,9 @@ dependencies = [ [[package]] name = "cosmwasm-derive" -version = "1.5.3" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc5e72e330bd3bdab11c52b5ecbdeb6a8697a004c57964caeb5d876f0b088b3c" +checksum = "242e98e7a231c122e08f300d9db3262d1007b51758a8732cd6210b3e9faa4f3a" dependencies = [ "syn 1.0.109", ] @@ -552,9 +552,9 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.5.3" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef8666e572a3a2519010dde88c04d16e9339ae751b56b2bb35081fe3f7d6be74" +checksum = "78c1556156fdf892a55cced6115968b961eaaadd6f724a2c2cb7d1e168e32dd3" dependencies = [ "base64", "bech32", diff --git a/contracts/pair_concentrated/Cargo.toml b/contracts/pair_concentrated/Cargo.toml index df0c88d..de76deb 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.2" +version = "1.0.3" 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 f7d98e4..8302579 100644 --- a/contracts/pair_concentrated/src/contract.rs +++ b/contracts/pair_concentrated/src/contract.rs @@ -881,7 +881,7 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result 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 {}), diff --git a/contracts/pair_concentrated/src/queries.rs b/contracts/pair_concentrated/src/queries.rs index b531056..8fc4b09 100644 --- a/contracts/pair_concentrated/src/queries.rs +++ b/contracts/pair_concentrated/src/queries.rs @@ -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, }; @@ -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; @@ -192,10 +192,24 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { }; // 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("e_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()), + ); 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. diff --git a/contracts/pair_concentrated/tests/common/helper.rs b/contracts/pair_concentrated/tests/common/helper.rs index 64377dd..93e3579 100644 --- a/contracts/pair_concentrated/tests/common/helper.rs +++ b/contracts/pair_concentrated/tests/common/helper.rs @@ -109,7 +109,7 @@ pub fn init_native_coins(test_coins: &[TestCoin]) -> Vec { .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, @@ -271,6 +271,7 @@ impl Helper { ("uusd".to_owned(), 6), ("rc".to_owned(), 6), ("foo".to_owned(), 5), + ("eth".to_owned(), 18), ], }, &[], diff --git a/contracts/pair_concentrated/tests/pair_concentrated_integration.rs b/contracts/pair_concentrated/tests/pair_concentrated_integration.rs index b28057a..cdc510b 100644 --- a/contracts/pair_concentrated/tests/pair_concentrated_integration.rs +++ b/contracts/pair_concentrated/tests/pair_concentrated_integration.rs @@ -861,7 +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.") + assert_eq!(StdError::generic_err("Querier contract error: Generic error: Not implemented.Use { \"observe\": { \"seconds_ago\": ... } } instead.") , err); } @@ -1554,6 +1554,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::( + &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::( + &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"); diff --git a/packages/astroport_on_osmosis/Cargo.toml b/packages/astroport_on_osmosis/Cargo.toml index bcbac08..ec865e8 100644 --- a/packages/astroport_on_osmosis/Cargo.toml +++ b/packages/astroport_on_osmosis/Cargo.toml @@ -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"