Skip to content

Commit

Permalink
Demonstrate problem with twap decimals.
Browse files Browse the repository at this point in the history
  • Loading branch information
piobab committed Nov 17, 2022
1 parent 825ceac commit 3e10178
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 21 deletions.
12 changes: 0 additions & 12 deletions tests/osmosis-std-cosmwasm-test/src/bin/schema.rs

This file was deleted.

8 changes: 4 additions & 4 deletions tests/osmosis-std-cosmwasm-test/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use cw2::set_contract_version;
use osmosis_std::types::osmosis::epochs::v1beta1::{
QueryEpochsInfoRequest, QueryEpochsInfoResponse,
};
use osmosis_std::types::osmosis::gamm::v1beta1::{
QueryNumPoolsRequest, QueryNumPoolsResponse, QueryPoolParamsRequest, QueryPoolParamsResponse,
QueryPoolRequest, QueryPoolResponse,
};
use osmosis_std::types::osmosis::gamm::v1beta1::{QueryNumPoolsRequest, QueryNumPoolsResponse, QueryPoolParamsRequest, QueryPoolParamsResponse, QueryPoolRequest, QueryPoolResponse, QuerySpotPriceResponse};
use osmosis_std::types::osmosis::twap::v1beta1::ArithmeticTwapToNowResponse;
use serde::de::DeserializeOwned;
use serde::Serialize;
Expand Down Expand Up @@ -95,6 +92,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::QueryArithmeticTwapToNow(arithmetic_twap_request) => {
query_and_debug::<ArithmeticTwapToNowResponse>(&deps, arithmetic_twap_request)
},
QueryMsg::QuerySpotPrice(spot_request) => {
query_and_debug::<QuerySpotPriceResponse>(&deps, spot_request)
},
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/osmosis-std-cosmwasm-test/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use osmosis_std::types::osmosis::gamm::v1beta1::{
QueryNumPoolsResponse, QueryPoolParamsResponse, QueryPoolResponse,
};
pub use osmosis_std::types::osmosis::twap::v1beta1::{ArithmeticTwapToNowRequest,ArithmeticTwapToNowResponse};
pub use osmosis_std::types::osmosis::gamm::v1beta1::{QuerySpotPriceRequest,QuerySpotPriceResponse};

/// Message type for `instantiate` entry_point
#[cw_serde]
Expand Down Expand Up @@ -36,5 +37,8 @@ pub enum QueryMsg {
QueryPoolParams { pool_id: u64 },

#[returns(ArithmeticTwapToNowResponse)]
QueryArithmeticTwapToNow(ArithmeticTwapToNowRequest)
QueryArithmeticTwapToNow(ArithmeticTwapToNowRequest),

#[returns(QuerySpotPriceResponse)]
QuerySpotPrice(QuerySpotPriceRequest)
}
27 changes: 27 additions & 0 deletions tests/osmosis-std-cosmwasm-test/tests/gamm_spot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use cosmwasm_std::Coin;
use osmosis_std::types::osmosis::gamm::v1beta1::{
QuerySpotPriceResponse, QuerySpotPriceRequest
};

use osmosis_testing::{fn_execute, fn_query};
use osmosis_testing::{Module, Runner};

pub struct Gamm<'a, R: Runner<'a>> {
runner: &'a R,
}

impl<'a, R: Runner<'a>> Module<'a, R> for Gamm<'a, R> {
fn new(runner: &'a R) -> Self {
Self { runner }
}
}

impl<'a, R> Gamm<'a, R>
where
R: Runner<'a>,
{

fn_query! {
pub query_spot_price ["/osmosis.gamm.v1beta1.Query/SpotPrice"]: QuerySpotPriceRequest => QuerySpotPriceResponse
}
}
45 changes: 41 additions & 4 deletions tests/osmosis-std-cosmwasm-test/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mod helpers;
use core::time;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};

use cosmwasm_std::Coin;
use cosmwasm_std::{Coin, Decimal};
use helpers::with_env_setup;
use osmosis_std::{
shim::{Duration, Timestamp},
Expand All @@ -14,11 +15,16 @@ use osmosis_std::{
},
},
};
use osmosis_std::types::osmosis::gamm::v1beta1::{QuerySpotPriceRequest, QuerySpotPriceResponse};
use osmosis_std_cosmwasm_test::msg::{
ArithmeticTwapToNowRequest, ArithmeticTwapToNowResponse, QueryEpochsInfoResponse, QueryMsg,
QueryNumPoolsResponse, QueryPoolParamsResponse, QueryPoolResponse,
};
use osmosis_testing::{Account, Runner};
use osmosis_testing::{Account, Gamm, Module, Runner};
use crate::twap::Twap;

pub mod twap;
pub mod gamm_spot;

#[test]
fn test_u64_response_deser() {
Expand Down Expand Up @@ -206,7 +212,7 @@ fn test_twap_query() {
.checked_add(time::Duration::from_secs(30))
.unwrap();

let res: ArithmeticTwapToNowResponse = wasm
let res_wasm: ArithmeticTwapToNowResponse = wasm
.query(
&contract_addr,
&QueryMsg::QueryArithmeticTwapToNow(ArithmeticTwapToNowRequest {
Expand All @@ -221,7 +227,38 @@ fn test_twap_query() {
)
.unwrap();

dbg!(res);
let twap = Twap::new(app);
let res_mod = twap.query_twap_price(&ArithmeticTwapToNowRequest {
pool_id,
base_asset: "uosmo".to_string(),
quote_asset: "uion".to_string(),
start_time: Some(Timestamp {
seconds: time.as_secs() as i64,
nanos: 0,
})
}).unwrap();

assert_eq!(res_wasm.arithmetic_twap, res_mod.arithmetic_twap);

let res_wasm: QuerySpotPriceResponse = wasm
.query(
&contract_addr,
&QueryMsg::QuerySpotPrice(QuerySpotPriceRequest {
pool_id,
base_asset_denom: "uion".to_string(),
quote_asset_denom: "uosmo".to_string(),
}),
)
.unwrap();

let gamm = gamm_spot::Gamm::new(app);
let res_mod = gamm.query_spot_price(&QuerySpotPriceRequest {
pool_id,
base_asset_denom: "uion".to_string(),
quote_asset_denom: "uosmo".to_string(),
}).unwrap();

assert_eq!(res_wasm.spot_price, res_mod.spot_price);
},
true,
);
Expand Down
25 changes: 25 additions & 0 deletions tests/osmosis-std-cosmwasm-test/tests/twap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use osmosis_std::types::osmosis::twap::v1beta1::{
ArithmeticTwapToNowRequest, ArithmeticTwapToNowResponse,
};

use osmosis_testing::{fn_execute, fn_query};
use osmosis_testing::{Module, Runner};

pub struct Twap<'a, R: Runner<'a>> {
runner: &'a R,
}

impl<'a, R: Runner<'a>> Module<'a, R> for Twap<'a, R> {
fn new(runner: &'a R) -> Self {
Self { runner }
}
}

impl<'a, R> Twap<'a, R>
where
R: Runner<'a>,
{
fn_query! {
pub query_twap_price ["/osmosis.twap.v1beta1.Query/ArithmeticTwapToNow"]: ArithmeticTwapToNowRequest => ArithmeticTwapToNowResponse
}
}

0 comments on commit 3e10178

Please sign in to comment.