Skip to content

Commit 6739521

Browse files
committed
fix and add tests
1 parent 13c1a94 commit 6739521

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

contracts/auction/price_oracle/src/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ fn get_price_from_astroport(
323323
.checked_add(res.spread_amount)?,
324324
0,
325325
)?;
326-
deps.api.debug(format!("res: {:?}", res).as_str());
327-
deps.api.debug(format!("Price step: {:?}", price).as_str());
326+
// deps.api.debug(format!("res: {:?}", res).as_str());
327+
// deps.api.debug(format!("Price step: {:?}", price).as_str());
328328

329329
Ok(price)
330330
},

tests/rust-tests/src/suite/suite_auction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,16 @@ impl Suite {
690690
.unwrap()
691691
}
692692

693+
pub fn query_oracle_local_price(&self, pair: Pair) -> Vec<GetPriceResponse> {
694+
self.app
695+
.wrap()
696+
.query_wasm_smart(
697+
self.oracle_addr.clone(),
698+
&price_oracle::msg::QueryMsg::GetLocalPrice { pair },
699+
)
700+
.unwrap()
701+
}
702+
693703
pub fn query_oracle_all_prices(
694704
&self,
695705
from: Option<Pair>,

tests/rust-tests/src/tests_auctions/test_astro.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_add_path_for_pair() {
4545
fn test_basic_astro_default() {
4646
let mut suite = SuiteBuilder::default().build_basic(true);
4747

48-
let old_oracle_price = suite.query_oracle_price(suite.pair.clone());
48+
let old_oracle_last_local_price = suite.query_oracle_local_price(suite.pair.clone())[0].clone();
4949
// This should error because we don't have astro path for the price yet
5050
// and no auction ran so far
5151
let err = suite.update_price_err(suite.pair.clone());
@@ -91,16 +91,16 @@ fn test_basic_astro_default() {
9191

9292
// Make sure the pool price is not the same as the old price
9393
// To confirm the price actually changed later
94-
assert_ne!(pool_price, old_oracle_price.price);
94+
assert_ne!(pool_price, old_oracle_last_local_price.price);
9595

9696
// Try to update again
9797
suite.update_price(suite.pair.clone()).unwrap();
9898

9999
// Verify we do get an acceptable price (query price from pool)
100-
let oracle_price = suite.query_oracle_price(suite.pair.clone());
100+
let oracle_last_local_price = suite.query_oracle_local_price(suite.pair.clone())[0].clone();
101101
println!("pool_price: {:?}", pool_price);
102-
println!("oracle_price: {:?}", oracle_price);
103-
assert_eq!(oracle_price.price, pool_price);
102+
println!("oracle_price: {:?}", oracle_last_local_price);
103+
assert_eq!(oracle_last_local_price.price, pool_price);
104104
}
105105

106106
// Test for a pair that doesn't have a direct pool, so the astro path is 2 pools

tests/rust-tests/src/tests_auctions/test_oracle.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::borrow::BorrowMut;
2+
13
use cosmwasm_std::{coins, Addr, Decimal};
24
use cw_multi_test::Executor;
35
use cw_utils::Expiration;
6+
use price_oracle::state::PriceStep;
47

58
use crate::suite::{
69
suite::{Suite, DAY, DEFAULT_BLOCK_TIME},
@@ -323,3 +326,77 @@ fn test_update_config() {
323326
assert_eq!(new_oracle_config.seconds_allow_manual_change, 12);
324327
assert_eq!(new_oracle_config.seconds_auction_prices_fresh, 455);
325328
}
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

Comments
 (0)