osmosis-labs / osmosis-rust

Rust libraries for osmosis
Apache License 2.0
59 stars 52 forks source link

TWAP test failed on "looking for a time thats too old" #53

Closed iboss-ptk closed 1 year ago

iboss-ptk commented 1 year ago

With the following code:

pub fn test_twap(deps: Deps, env: Env) -> StdResult<TestTwapResponse> {
    let start_time = env.block.time.minus_seconds(300);
    let start_time = OsmosisTimestamp {
        seconds: start_time.seconds() as i64,
        nanos: 0_i32,
    };

    let twap = TwapQuerier::new(&deps.querier)
        .arithmetic_twap_to_now(
            2,
            "uosmo".to_string(),
            "uatom".to_string(),
            Some(start_time.clone()),
        )?
        .arithmetic_twap;

    Ok(TestTwapResponse { price: twap })
}

This error can occur

QueryError { msg: "looking for a time thats too old, not in the historical index.  Try storing the accumulator value. (requested time 2022-10-26 20:09:03 +0000 UTC)" }

This might be caused by block time issue. To know for sure check

iboss-ptk commented 1 year ago

First, we need at least 2 swaps to create historical index.

Second, With current implementation every execution creates new block and block timestamp will +5 secs from last block.

working example can be found here. From genesis to latest, every execution +5 secs on block time, so the twap query needs to compensate them to be right in the range.

For more sophisticated block time control will be separated to another issue.

piobab commented 1 year ago

@iboss-ptk I modified your example to show a problem with custom twap module. The price is not deserialized correctly. Please take a look on this commit https://github.com/piobab/osmosis-rust/commit/3e10178038f6d2da2e997e65e837fe6a220f99f0

left: "0.944009649029192124" right: "944009649029192124"

iboss-ptk commented 1 year ago

created new issue to track this https://github.com/osmosis-labs/osmosis-rust/issues/59