@@ -6,7 +6,7 @@ use crate::coins::Coin;
66use crate :: encoding:: Binary ;
77use crate :: errors:: { contract_err, StdResult , Utf8StringErr } ;
88use crate :: query:: { AllBalanceResponse , BalanceResponse , BankQuery , QueryRequest , WasmQuery } ;
9- use crate :: serde:: to_vec ;
9+ use crate :: serde:: to_binary ;
1010use crate :: storage:: MemoryStorage ;
1111use crate :: traits:: { Api , Extern , Querier } ;
1212use crate :: types:: { BlockInfo , CanonicalAddr , ContractInfo , Env , HumanAddr , MessageInfo } ;
@@ -127,14 +127,34 @@ pub fn mock_env<T: Api, U: Into<HumanAddr>>(api: &T, sender: U, sent: &[Coin]) -
127127#[ derive( Clone ) ]
128128pub struct MockQuerier {
129129 bank : BankQuerier ,
130+ #[ cfg( feature = "staking" ) ]
131+ staking : staking:: StakingQuerier ,
130132}
131133
132134impl MockQuerier {
135+ #[ cfg( not( feature = "staking" ) ) ]
133136 pub fn new ( balances : & [ ( & HumanAddr , & [ Coin ] ) ] ) -> Self {
134137 MockQuerier {
135138 bank : BankQuerier :: new ( balances) ,
136139 }
137140 }
141+
142+ #[ cfg( feature = "staking" ) ]
143+ pub fn new ( balances : & [ ( & HumanAddr , & [ Coin ] ) ] ) -> Self {
144+ MockQuerier {
145+ bank : BankQuerier :: new ( balances) ,
146+ staking : staking:: StakingQuerier :: new ( & [ ] , & [ ] ) ,
147+ }
148+ }
149+
150+ #[ cfg( feature = "staking" ) ]
151+ pub fn with_staking (
152+ & mut self ,
153+ validators : & [ crate :: query:: Validator ] ,
154+ delegations : & [ crate :: query:: Delegation ] ,
155+ ) {
156+ self . staking = staking:: StakingQuerier :: new ( validators, delegations) ;
157+ }
138158}
139159
140160#[ derive( Clone ) ]
@@ -166,16 +186,70 @@ impl BankQuerier {
166186 denom : denom. to_string ( ) ,
167187 } ,
168188 } ;
169- let api_res = to_vec ( & bank_res) . map ( Binary ) . map_err ( |e| e. into ( ) ) ;
170- Ok ( api_res)
189+ Ok ( to_binary ( & bank_res) . map_err ( |e| e. into ( ) ) )
171190 }
172191 BankQuery :: AllBalances { address } => {
173192 // proper error on not found, serialize result on found
174193 let bank_res = AllBalanceResponse {
175194 amount : self . balances . get ( address) . cloned ( ) . unwrap_or_default ( ) ,
176195 } ;
177- let api_res = to_vec ( & bank_res) . map ( Binary ) . map_err ( |e| e. into ( ) ) ;
178- Ok ( api_res)
196+ Ok ( to_binary ( & bank_res) . map_err ( |e| e. into ( ) ) )
197+ }
198+ }
199+ }
200+ }
201+
202+ #[ cfg( feature = "staking" ) ]
203+ mod staking {
204+ use crate :: api:: { ApiError , ApiSystemError } ;
205+ use crate :: encoding:: Binary ;
206+ use crate :: query:: {
207+ Delegation , DelegationsResponse , StakingQuery , Validator , ValidatorsResponse ,
208+ } ;
209+ use crate :: to_binary;
210+
211+ #[ derive( Clone ) ]
212+ pub struct StakingQuerier {
213+ validators : Vec < Validator > ,
214+ delegations : Vec < Delegation > ,
215+ }
216+
217+ impl StakingQuerier {
218+ pub fn new ( validators : & [ Validator ] , delegations : & [ Delegation ] ) -> Self {
219+ StakingQuerier {
220+ validators : validators. to_vec ( ) ,
221+ delegations : delegations. to_vec ( ) ,
222+ }
223+ }
224+
225+ pub fn query (
226+ & self ,
227+ request : & StakingQuery ,
228+ ) -> Result < Result < Binary , ApiError > , ApiSystemError > {
229+ match request {
230+ StakingQuery :: Validators { } => {
231+ let val_res = ValidatorsResponse {
232+ validators : self . validators . clone ( ) ,
233+ } ;
234+ Ok ( to_binary ( & val_res) . map_err ( |e| e. into ( ) ) )
235+ }
236+ StakingQuery :: Delegations {
237+ delegator,
238+ validator,
239+ } => {
240+ let matches = |d : & & Delegation | {
241+ if let Some ( val) = validator {
242+ if val != & d. validator {
243+ return false ;
244+ }
245+ }
246+ & d. delegator == delegator
247+ } ;
248+ let delegations: Vec < _ > =
249+ self . delegations . iter ( ) . filter ( matches) . cloned ( ) . collect ( ) ;
250+ let val_res = DelegationsResponse { delegations } ;
251+ Ok ( to_binary ( & val_res) . map_err ( |e| e. into ( ) ) )
252+ }
179253 }
180254 }
181255 }
@@ -186,9 +260,7 @@ impl Querier for MockQuerier {
186260 match request {
187261 QueryRequest :: Bank ( bank_query) => self . bank . query ( bank_query) ,
188262 #[ cfg( feature = "staking" ) ]
189- QueryRequest :: Staking ( _) => Err ( ApiSystemError :: InvalidRequest {
190- error : "staking not yet implemented" . to_string ( ) ,
191- } ) ,
263+ QueryRequest :: Staking ( staking_query) => self . staking . query ( staking_query) ,
192264 QueryRequest :: Wasm ( msg) => {
193265 let addr = match msg {
194266 WasmQuery :: Smart { contract_addr, .. } => contract_addr,
0 commit comments