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] Unregister Secret Swap pair #149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions contracts/oracle/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
HandleMsg::RegisterSswapPair {
pair,
} => handle::register_sswap_pair(deps, env, pair),
HandleMsg::UnregisterSswapPair {
pair,
} => handle::unregister_sswap_pair(deps, env, pair),
HandleMsg::RegisterIndex {
symbol,
basket,
Expand Down
93 changes: 69 additions & 24 deletions contracts/oracle/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use cosmwasm_std::{
use secret_toolkit::{
utils::Query,
snip20::{
token_info_query,
token_info_query,
TokenInfo,
},
};
use shade_protocol::{
Expand All @@ -27,7 +28,7 @@ use shade_protocol::{
use crate::state::{
config_w, config_r,
sswap_pairs_w, sswap_pairs_r,
index_w, index_r,
index_w,
};

pub fn register_sswap_pair<S: Storage, A: Api, Q: Querier>(
Expand All @@ -41,8 +42,67 @@ pub fn register_sswap_pair<S: Storage, A: Api, Q: Querier>(
return Err(StdError::Unauthorized { backtrace: None });
}

//Query for snip20's in the pair
let response: PairResponse = PairQuery::Pair {}.query(
let (token_contract, token_info) = fetch_token_paired_to_sscrt_on_sswap(
deps,
config.sscrt.address,
&pair
)?;

sswap_pairs_w(&mut deps.storage).save(token_info.symbol.as_bytes(), &SswapPair {
pair,
asset: Snip20Asset {
contract: token_contract,
token_info: token_info.clone(),
token_config: None,
}
})?;

Ok(HandleResponse {
messages: vec![],
log: vec![],
data: Some( to_binary( &HandleAnswer::RegisterSswapPair {
status: ResponseStatus::Success } )? )
})
}

pub fn unregister_sswap_pair<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
pair: Contract,
) -> StdResult<HandleResponse> {

let config = config_r(&deps.storage).load()?;
if env.message.sender != config.admin {
return Err(StdError::Unauthorized { backtrace: None });
}

let (_, token_info) = fetch_token_paired_to_sscrt_on_sswap(
deps,
config.sscrt.address,
&pair
)?;

sswap_pairs_w(&mut deps.storage).remove(token_info.symbol.as_bytes());

Ok(HandleResponse {
messages: vec![],
log: vec![],
data: Some( to_binary( &HandleAnswer::UnregisterSswapPair {
status: ResponseStatus::Success } )? )
})
}

///
/// Will fetch token Contract along with TokenInfo for {symbol} in pair argument.
/// Pair argument must represent Secret Swap contract for {symbol}/sSCRT or sSCRT/{symbol}.
///
fn fetch_token_paired_to_sscrt_on_sswap<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
sscrt_addr: HumanAddr,
pair: &Contract,
) -> StdResult<(Contract, TokenInfo)> {
// Query for snip20's in the pair
let response: PairResponse = PairQuery::Pair{}.query(
&deps.querier,
pair.code_hash.clone(),
pair.address.clone(),
Expand All @@ -53,37 +113,22 @@ pub fn register_sswap_pair<S: Storage, A: Api, Q: Querier>(
code_hash: response.asset_infos[0].token.token_code_hash.clone(),
};
// if thats sscrt, switch it
if token_contract.address == config.sscrt.address {
if token_contract.address == sscrt_addr {
token_contract = Contract {
address: response.asset_infos[1].token.contract_addr.clone(),
code_hash: response.asset_infos[1].token.token_code_hash.clone(),
}
}
// if neither is sscrt
else if response.asset_infos[1].token.contract_addr != config.sscrt.address {
else if response.asset_infos[1].token.contract_addr != sscrt_addr {
return Err(StdError::NotFound { kind: "Not an SSCRT Pair".to_string(), backtrace: None });
}

let token_info = token_info_query(&deps.querier, 1,
token_contract.code_hash.clone(),
token_contract.address.clone())?;

sswap_pairs_w(&mut deps.storage).save(token_info.symbol.as_bytes(), &SswapPair {
pair,
asset: Snip20Asset {
contract: token_contract,
token_info: token_info.clone(),
token_config: None,
}
})?;

Ok(HandleResponse {
messages: vec![],
log: vec![],
data: Some( to_binary( &HandleAnswer::RegisterSswapPair {
status: ResponseStatus::Success } )? )
})
token_contract.code_hash.clone(),
token_contract.address.clone())?;

Ok((token_contract, token_info))
}

pub fn register_index<S: Storage, A: Api, Q: Querier>(
Expand Down
1 change: 0 additions & 1 deletion contracts/oracle/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use shade_protocol::{
};
use crate::state::{
config_r,
hard_coded_r,
sswap_pairs_r,
index_r,
};
Expand Down
2 changes: 1 addition & 1 deletion contracts/oracle/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::query;
use cosmwasm_std::testing::{mock_dependencies, mock_env, MockStorage, MockApi, MockQuerier};

use cosmwasm_std::{Uint128};

macro_rules! normalize_price_tests {
Expand Down
7 changes: 6 additions & 1 deletion packages/shade_protocol/src/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ pub enum HandleMsg {
admin: Option<HumanAddr>,
band: Option<Contract>,
},
// Register Secret Swap Pair (should be */SCRT)
// Register Secret Swap Pair (should be */sSCRT or sSCRT/*)
RegisterSswapPair {
pair: Contract,
},
// Unregister Secret Swap Pair (opposite action to RegisterSswapPair)
UnregisterSswapPair {
pair: Contract,
},
RegisterIndex {
symbol: String,
basket: Vec<IndexElement>,
Expand All @@ -76,6 +80,7 @@ impl TestHandle for HandleMsg {}
pub enum HandleAnswer {
UpdateConfig { status: ResponseStatus},
RegisterSswapPair { status: ResponseStatus},
UnregisterSswapPair { status: ResponseStatus},
RegisterIndex { status: ResponseStatus},
}

Expand Down