Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle sienna #193

Merged
merged 28 commits into from Mar 29, 2022
Merged

Oracle sienna #193

merged 28 commits into from Mar 29, 2022

Conversation

DrPresident
Copy link
Collaborator

Sienna integration with oracle, all registered pairs for a given symbol will be queried for their current return on a 1SCRT trade, as well as their pool depths. The return amounts will be averaged together weighted by their respective pool CP's. That value is then translated to a USD price based on the SCRT/USD value provided by BAND.

@DrPresident DrPresident changed the base branch from main to dev March 11, 2022 22:58
pub static PAIR_INFO: &[u8] = b"pair_info";
pub static POOL: &[u8] = b"pool";

pub fn pair_info_r<S: Storage>(storage: &S) -> ReadonlySingleton<S, PairResponse> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contracts/oracle/src/state.rs Show resolved Hide resolved
take_pool: cosmwasm_std::Uint128,
) -> cosmwasm_std::Uint128 {
cosmwasm_std::Uint128(
take_pool.u128() - (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try using checked_sub, checked_mult and checked_add, and making testers for overflow issues

// indices will align with <pairs>
let mut results = vec![];

for pair in &pairs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're already iterating over the array here, you could get the max inside the match. It helps to reduce redundant iteration

Comment on lines 119 to 140
let mut results = vec![];

for pair in &pairs {
match pair.clone().dex {
Dex::SecretSwap => {
results.push(secretswap::price(&deps, pair.clone(), sscrt.clone(), band.clone())?);
},
Dex::SiennaSwap => {
results.push(sienna::price(&deps, pair.clone(), sscrt.clone(), band.clone())?);
},
/*
ShadeSwap => {
return Err(StdErr::generic_err("ShadeSwap Unavailable"));
},
*/
}
}
let max_amount = results.iter().max().unwrap();
let index = results.iter().position(|e| e == max_amount).unwrap();
let scrt_result = band::reference_data(deps, "SCRT".to_string(), "USD".to_string(), band)?;

Ok((mint::translate_price(scrt_result.rate, *max_amount), pairs[index].clone()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut results = vec![];
for pair in &pairs {
match pair.clone().dex {
Dex::SecretSwap => {
results.push(secretswap::price(&deps, pair.clone(), sscrt.clone(), band.clone())?);
},
Dex::SiennaSwap => {
results.push(sienna::price(&deps, pair.clone(), sscrt.clone(), band.clone())?);
},
/*
ShadeSwap => {
return Err(StdErr::generic_err("ShadeSwap Unavailable"));
},
*/
}
}
let max_amount = results.iter().max().unwrap();
let index = results.iter().position(|e| e == max_amount).unwrap();
let scrt_result = band::reference_data(deps, "SCRT".to_string(), "USD".to_string(), band)?;
Ok((mint::translate_price(scrt_result.rate, *max_amount), pairs[index].clone()))
let mut max_pair = (TradingPair::default(), 0);
for i in 1..pairs.len() {
let pair = pairs[i];
let price: Uint128; //idk the price() return type
match pair.clone().dex {
Dex::SecretSwap => {
price = secretswap::price(&deps, pair.clone(), sscrt.clone(), band.clone())?;
},
Dex::SiennaSwap => {
price = sienna::price(&deps, pair.clone(), sscrt.clone(), band.clone())?;
},
/*
ShadeSwap => {
return Err(StdErr::generic_err("ShadeSwap Unavailable"));
},
*/
}
if price > max_pair.1 {
max_pair = (pair, price);
}
}
let scrt_result = band::reference_data(deps, "SCRT".to_string(), "USD".to_string(), band)?;
Ok((mint::translate_price(scrt_result.rate, max_pair.1), max_pair.0))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: its an untested code suggestion, im not sure if it will run but the logic is there

Comment on lines 184 to 194
pub fn translate_price(scrt_price: Uint128, trade_price: Uint128) -> Uint128 {
scrt_price.multiply_ratio(10u128.pow(18), trade_price)
}

/* Normalize the price from snip20 amount with decimals to BAND rate
* amount: unsigned quantity received in trade for 1sSCRT
* decimals: number of decimals for received snip20
*/
pub fn normalize_price(amount: Uint128, decimals: u8) -> Uint128 {
(amount.u128() * 10u128.pow(18u32 - u32::try_from(decimals).unwrap())).into()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add unit tests?

@FloppyDisck
Copy link
Contributor

I recommend looking into https://github.com/hackbg/fadroma/tree/v100/crates/fadroma-ensemble
Its made by the Sienna devs and it offers cross contract unit testing, im planning on using it for governance and it would be super useful if you used it for the oracle to prove that it all works in a quick manner (way faster than the our python/rust integration tests)

@DrPresident
Copy link
Collaborator Author

I recommend looking into https://github.com/hackbg/fadroma/tree/v100/crates/fadroma-ensemble Its made by the Sienna devs and it offers cross contract unit testing, im planning on using it for governance and it would be super useful if you used it for the oracle to prove that it all works in a quick manner (way faster than the our python/rust integration tests)

This would be a full feature addition, this PR is just to get sienna integration into the oracle

@FloppyDisck FloppyDisck self-requested a review March 29, 2022 15:18
@DrPresident DrPresident merged commit 33bdfbf into dev Mar 29, 2022
@DrPresident DrPresident deleted the oracle-sienna branch March 29, 2022 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants