|
| 1 | +use std::borrow::BorrowMut; |
| 2 | + |
1 | 3 | use cosmwasm_std::{coins, Addr, Decimal}; |
2 | 4 | use cw_multi_test::Executor; |
3 | 5 | use cw_utils::Expiration; |
| 6 | +use price_oracle::state::PriceStep; |
4 | 7 |
|
5 | 8 | use crate::suite::{ |
6 | 9 | suite::{Suite, DAY, DEFAULT_BLOCK_TIME}, |
@@ -323,3 +326,77 @@ fn test_update_config() { |
323 | 326 | assert_eq!(new_oracle_config.seconds_allow_manual_change, 12); |
324 | 327 | assert_eq!(new_oracle_config.seconds_auction_prices_fresh, 455); |
325 | 328 | } |
| 329 | + |
| 330 | +#[test] |
| 331 | +fn test_local_prices() { |
| 332 | + let mut suite = Suite::default(); |
| 333 | + let funds = coins(100_u128, suite.pair.0.clone()); |
| 334 | + |
| 335 | + // do 3 auctions |
| 336 | + suite.finalize_auction(&funds); |
| 337 | + suite.finalize_auction(&funds); |
| 338 | + suite.finalize_auction(&funds); |
| 339 | + |
| 340 | + // Update the price from auction |
| 341 | + suite.update_price(suite.pair.clone()).unwrap(); |
| 342 | + |
| 343 | + // Register the astro path |
| 344 | + let path = vec![PriceStep { |
| 345 | + denom1: suite.pair.0.to_string(), |
| 346 | + denom2: suite.pair.1.to_string(), |
| 347 | + pool_address: suite |
| 348 | + .astro_pools |
| 349 | + .get(&suite.pair.clone().into()) |
| 350 | + .unwrap() |
| 351 | + .clone(), |
| 352 | + }]; |
| 353 | + suite |
| 354 | + .add_astro_path_to_oracle(suite.pair.clone(), path) |
| 355 | + .unwrap(); |
| 356 | + |
| 357 | + // Randomize the pool a little to get a "nice" price |
| 358 | + let mut rng = rand::thread_rng(); |
| 359 | + |
| 360 | + for _ in 0..10 { |
| 361 | + suite.do_random_swap( |
| 362 | + rng.borrow_mut(), |
| 363 | + suite.pair.clone(), |
| 364 | + 100_000_u128, |
| 365 | + 1_000_000_u128, |
| 366 | + ); |
| 367 | + } |
| 368 | + |
| 369 | + let pool_price = suite.query_astro_pool_price( |
| 370 | + suite |
| 371 | + .astro_pools |
| 372 | + .get(&suite.pair.clone().into()) |
| 373 | + .unwrap() |
| 374 | + .clone(), |
| 375 | + suite.pair.clone(), |
| 376 | + ); |
| 377 | + |
| 378 | + // make sure the last price on local, is not the price from the pool |
| 379 | + let local_prices = suite.query_oracle_local_price(suite.pair.clone()); |
| 380 | + assert_ne!(local_prices[0].price, pool_price); |
| 381 | + |
| 382 | + // Update price, should get it from astro |
| 383 | + suite.update_block(DAY / DEFAULT_BLOCK_TIME * 2); |
| 384 | + |
| 385 | + // Update the price from auction |
| 386 | + suite.update_price(suite.pair.clone()).unwrap(); |
| 387 | + |
| 388 | + let local_prices = suite.query_oracle_local_price(suite.pair.clone()); |
| 389 | + |
| 390 | + // We should have 2 prices because we updated prices twice |
| 391 | + assert_eq!(local_prices.len(), 3); |
| 392 | + // The first price should be the pool price |
| 393 | + assert_eq!(local_prices[0].price, pool_price); |
| 394 | + |
| 395 | + // make sure the actual price is the avg of the local prices |
| 396 | + let oracle_price = suite.query_oracle_price(suite.pair.clone()); |
| 397 | + assert_eq!( |
| 398 | + oracle_price.price, |
| 399 | + (local_prices[0].price + local_prices[1].price + local_prices[2].price) |
| 400 | + / Decimal::from_atomics(3_u128, 0).unwrap() |
| 401 | + ); |
| 402 | +} |
0 commit comments