From 4eb281e6a6ab8c67a51ad805676f77c283f12d46 Mon Sep 17 00:00:00 2001 From: iurii Date: Sun, 19 Dec 2021 11:53:38 +0300 Subject: [PATCH 1/7] [oracle] Unregister Secret Swap pair --- contracts/oracle/src/contract.rs | 3 +++ contracts/oracle/src/handle.rs | 22 +++++++++++++++++++++- contracts/oracle/src/query.rs | 1 - contracts/oracle/src/test.rs | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/contracts/oracle/src/contract.rs b/contracts/oracle/src/contract.rs index eff86e4b3..356b15798 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 { + symbol, + } => handle::unregister_sswap_pair(deps, env, symbol), HandleMsg::RegisterIndex { symbol, basket, diff --git a/contracts/oracle/src/handle.rs b/contracts/oracle/src/handle.rs index 334b00d21..cbbe14547 100644 --- a/contracts/oracle/src/handle.rs +++ b/contracts/oracle/src/handle.rs @@ -27,7 +27,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( @@ -83,7 +83,27 @@ pub fn register_sswap_pair( data: Some( to_binary( &HandleAnswer::RegisterSswapPair { status: ResponseStatus::Success } )? ) }) +} +pub fn unregister_sswap_pair( + deps: &mut Extern, + env: Env, + symbol: String, +) -> StdResult { + + let config = config_r(&deps.storage).load()?; + if env.message.sender != config.admin { + return Err(StdError::Unauthorized { backtrace: None }); + } + + sswap_pairs_w(&mut deps.storage).remove(symbol.as_bytes()); + + Ok(HandleResponse { + messages: vec![], + log: vec![], + data: Some( to_binary( &HandleAnswer::UnregisterSswapPair { + status: ResponseStatus::Success } )? ) + }) } 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 { From ae2ad739f0d0d3a7b563750b5f5dcd6ae2841940 Mon Sep 17 00:00:00 2001 From: iurii Date: Sun, 19 Dec 2021 12:00:08 +0300 Subject: [PATCH 2/7] Update HandleMsg enum with new type of message --- packages/shade_protocol/src/oracle.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index 980770ba4..b7e7fa79c 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -58,6 +58,10 @@ pub enum HandleMsg { RegisterSswapPair { pair: Contract, }, + // Unregister Secret Swap Pair (symbol/SCRT) + UnregisterSswapPair { + symbol: String, + }, 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}, } From f4915b7a0bd1fceeb5a6f2d5aadf3c9fbf6a5c46 Mon Sep 17 00:00:00 2001 From: iurii Date: Sun, 19 Dec 2021 12:07:02 +0300 Subject: [PATCH 3/7] Clarify UnregisterSswapPair comment a bit --- packages/shade_protocol/src/oracle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index b7e7fa79c..adf15e137 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -58,7 +58,7 @@ pub enum HandleMsg { RegisterSswapPair { pair: Contract, }, - // Unregister Secret Swap Pair (symbol/SCRT) + // Unregister Secret Swap Pair that corresponds to symbol/SCRT or SCRT/symbol UnregisterSswapPair { symbol: String, }, From f6c84e7b618d5bbc784335303b1109a96a983c93 Mon Sep 17 00:00:00 2001 From: iurii Date: Sun, 19 Dec 2021 12:16:02 +0300 Subject: [PATCH 4/7] Fix UnregisterSswapPair comment --- packages/shade_protocol/src/oracle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index adf15e137..58ae58c9c 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -58,7 +58,7 @@ pub enum HandleMsg { RegisterSswapPair { pair: Contract, }, - // Unregister Secret Swap Pair that corresponds to symbol/SCRT or SCRT/symbol + // Unregister Secret Swap Pair that corresponds to symbol/SCRT or SSCRT/symbol UnregisterSswapPair { symbol: String, }, From 3576782ca5445c581b25a815db3094c1d226d882 Mon Sep 17 00:00:00 2001 From: iurii Date: Sun, 19 Dec 2021 12:35:14 +0300 Subject: [PATCH 5/7] Clarify RegisterSswapPair comment --- packages/shade_protocol/src/oracle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index 58ae58c9c..b76736635 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -54,7 +54,7 @@ pub enum HandleMsg { admin: Option, band: Option, }, - // Register Secret Swap Pair (should be */SCRT) + // Register Secret Swap Pair (should be */SCRT or SSCRT/*) RegisterSswapPair { pair: Contract, }, From 7cb37ec52958d09b7f826ca987abe48bed10add3 Mon Sep 17 00:00:00 2001 From: iurii Date: Mon, 20 Dec 2021 12:36:43 +0300 Subject: [PATCH 6/7] Update HandleMsg comments --- packages/shade_protocol/src/oracle.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index b76736635..0583b06c9 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -54,11 +54,11 @@ pub enum HandleMsg { admin: Option, band: Option, }, - // Register Secret Swap Pair (should be */SCRT or SSCRT/*) + // Register Secret Swap Pair (should be */sSCRT or sSCRT/*) RegisterSswapPair { pair: Contract, }, - // Unregister Secret Swap Pair that corresponds to symbol/SCRT or SSCRT/symbol + // Unregister Secret Swap Pair that corresponds to symbol/sSCRT or sSCRT/symbol UnregisterSswapPair { symbol: String, }, From cbcdfa200b41ae5885613439bed13ac6e553e351 Mon Sep 17 00:00:00 2001 From: iurii Date: Mon, 20 Dec 2021 13:20:41 +0300 Subject: [PATCH 7/7] Change UnregisterSswapPair to accept pair instead of symbol --- contracts/oracle/src/contract.rs | 4 +- contracts/oracle/src/handle.rs | 81 ++++++++++++++++++--------- packages/shade_protocol/src/oracle.rs | 4 +- 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/contracts/oracle/src/contract.rs b/contracts/oracle/src/contract.rs index 356b15798..b40ca8971 100644 --- a/contracts/oracle/src/contract.rs +++ b/contracts/oracle/src/contract.rs @@ -60,8 +60,8 @@ pub fn handle( pair, } => handle::register_sswap_pair(deps, env, pair), HandleMsg::UnregisterSswapPair { - symbol, - } => handle::unregister_sswap_pair(deps, env, symbol), + 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 cbbe14547..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::{ @@ -41,33 +42,12 @@ pub fn register_sswap_pair( return Err(StdError::Unauthorized { backtrace: None }); } - //Query for snip20's in the pair - let response: PairResponse = PairQuery::Pair {}.query( - &deps.querier, - pair.code_hash.clone(), - pair.address.clone(), + let (token_contract, token_info) = fetch_token_paired_to_sscrt_on_sswap( + deps, + config.sscrt.address, + &pair )?; - let mut token_contract = Contract { - address: response.asset_infos[0].token.contract_addr.clone(), - code_hash: response.asset_infos[0].token.token_code_hash.clone(), - }; - // if thats sscrt, switch it - if token_contract.address == config.sscrt.address { - 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 { - 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 { @@ -88,7 +68,7 @@ pub fn register_sswap_pair( pub fn unregister_sswap_pair( deps: &mut Extern, env: Env, - symbol: String, + pair: Contract, ) -> StdResult { let config = config_r(&deps.storage).load()?; @@ -96,7 +76,13 @@ pub fn unregister_sswap_pair( return Err(StdError::Unauthorized { backtrace: None }); } - sswap_pairs_w(&mut deps.storage).remove(symbol.as_bytes()); + 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![], @@ -106,6 +92,45 @@ pub fn unregister_sswap_pair( }) } +/// +/// 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(), + )?; + + let mut token_contract = Contract { + address: response.asset_infos[0].token.contract_addr.clone(), + code_hash: response.asset_infos[0].token.token_code_hash.clone(), + }; + // if thats sscrt, switch it + 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 != 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())?; + + Ok((token_contract, token_info)) +} + pub fn register_index( deps: &mut Extern, env: Env, diff --git a/packages/shade_protocol/src/oracle.rs b/packages/shade_protocol/src/oracle.rs index 0583b06c9..ad7eec19c 100644 --- a/packages/shade_protocol/src/oracle.rs +++ b/packages/shade_protocol/src/oracle.rs @@ -58,9 +58,9 @@ pub enum HandleMsg { RegisterSswapPair { pair: Contract, }, - // Unregister Secret Swap Pair that corresponds to symbol/sSCRT or sSCRT/symbol + // Unregister Secret Swap Pair (opposite action to RegisterSswapPair) UnregisterSswapPair { - symbol: String, + pair: Contract, }, RegisterIndex { symbol: String,