diff --git a/contracts/oracle/src/contract.rs b/contracts/oracle/src/contract.rs index eff86e4b3..b40ca8971 100644 --- a/contracts/oracle/src/contract.rs +++ b/contracts/oracle/src/contract.rs @@ -59,6 +59,9 @@ pub fn handle( HandleMsg::RegisterSswapPair { pair, } => handle::register_sswap_pair(deps, env, pair), + HandleMsg::UnregisterSswapPair { + pair, + } => handle::unregister_sswap_pair(deps, env, pair), HandleMsg::RegisterIndex { symbol, basket, diff --git a/contracts/oracle/src/handle.rs b/contracts/oracle/src/handle.rs index 334b00d21..5c35115cf 100644 --- a/contracts/oracle/src/handle.rs +++ b/contracts/oracle/src/handle.rs @@ -7,7 +7,8 @@ use cosmwasm_std::{ use secret_toolkit::{ utils::Query, snip20::{ - token_info_query, + token_info_query, + TokenInfo, }, }; use shade_protocol::{ @@ -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( @@ -41,8 +42,67 @@ pub fn register_sswap_pair( 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( + deps: &mut Extern, + env: Env, + pair: Contract, +) -> StdResult { + + 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( + deps: &mut Extern, + 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(), @@ -53,37 +113,22 @@ pub fn register_sswap_pair( 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( diff --git a/contracts/oracle/src/query.rs b/contracts/oracle/src/query.rs index 1b3412339..a3ae6e41c 100644 --- a/contracts/oracle/src/query.rs +++ b/contracts/oracle/src/query.rs @@ -24,7 +24,6 @@ use shade_protocol::{ }; use crate::state::{ config_r, - hard_coded_r, sswap_pairs_r, index_r, }; diff --git a/contracts/oracle/src/test.rs b/contracts/oracle/src/test.rs index 2bbee3d33..adbfd7d72 100644 --- a/contracts/oracle/src/test.rs +++ b/contracts/oracle/src/test.rs @@ -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 { diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index 980770ba4..ad7eec19c 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -54,10 +54,14 @@ pub enum HandleMsg { admin: Option, band: Option, }, - // 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, @@ -76,6 +80,7 @@ impl TestHandle for HandleMsg {} pub enum HandleAnswer { UpdateConfig { status: ResponseStatus}, RegisterSswapPair { status: ResponseStatus}, + UnregisterSswapPair { status: ResponseStatus}, RegisterIndex { status: ResponseStatus}, }