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
Oracle sienna #193
Conversation
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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you change these to this? https://github.com/securesecrets/shade/blob/main/packages/shade_protocol/src/utils/storage/mod.rs
Heres an example using it https://github.com/securesecrets/shade/blob/staking/snip20-interface/packages/shade_protocol/src/shd_staking/stake.rs#L9-L21
take_pool: cosmwasm_std::Uint128, | ||
) -> cosmwasm_std::Uint128 { | ||
cosmwasm_std::Uint128( | ||
take_pool.u128() - ( |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
packages/shade_protocol/src/dex.rs
Outdated
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())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
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
packages/shade_protocol/src/mint.rs
Outdated
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() | ||
} |
There was a problem hiding this comment.
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?
I recommend looking into https://github.com/hackbg/fadroma/tree/v100/crates/fadroma-ensemble |
This would be a full feature addition, this PR is just to get sienna integration into the oracle |
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.