@@ -12,14 +12,15 @@ use mars_red_bank_types::oracle::{
12
12
} ;
13
13
use mars_utils:: helpers:: validate_native_denom;
14
14
15
- use crate :: { error:: ContractResult , PriceSource } ;
15
+ use crate :: { error:: ContractResult , PriceSourceChecked , PriceSourceUnchecked } ;
16
16
17
17
const DEFAULT_LIMIT : u32 = 10 ;
18
18
const MAX_LIMIT : u32 = 30 ;
19
19
20
- pub struct OracleBase < ' a , P , C >
20
+ pub struct OracleBase < ' a , P , PU , C >
21
21
where
22
- P : PriceSource < C > ,
22
+ P : PriceSourceChecked < C > ,
23
+ PU : PriceSourceUnchecked < P , C > ,
23
24
C : CustomQuery ,
24
25
{
25
26
/// Contract's owner
@@ -28,28 +29,33 @@ where
28
29
pub config : Item < ' a , Config > ,
29
30
/// The price source of each coin denom
30
31
pub price_sources : Map < ' a , & ' a str , P > ,
32
+ /// Phantom data holds the unchecked price source type
33
+ pub unchecked_price_source : PhantomData < PU > ,
31
34
/// Phantom data holds the custom query type
32
35
pub custom_query : PhantomData < C > ,
33
36
}
34
37
35
- impl < ' a , P , C > Default for OracleBase < ' a , P , C >
38
+ impl < ' a , P , PU , C > Default for OracleBase < ' a , P , PU , C >
36
39
where
37
- P : PriceSource < C > ,
40
+ P : PriceSourceChecked < C > ,
41
+ PU : PriceSourceUnchecked < P , C > ,
38
42
C : CustomQuery ,
39
43
{
40
44
fn default ( ) -> Self {
41
45
Self {
42
46
owner : Owner :: new ( "owner" ) ,
43
47
config : Item :: new ( "config" ) ,
44
48
price_sources : Map :: new ( "price_sources" ) ,
49
+ unchecked_price_source : PhantomData ,
45
50
custom_query : PhantomData ,
46
51
}
47
52
}
48
53
}
49
54
50
- impl < ' a , P , C > OracleBase < ' a , P , C >
55
+ impl < ' a , P , PU , C > OracleBase < ' a , P , PU , C >
51
56
where
52
- P : PriceSource < C > ,
57
+ P : PriceSourceChecked < C > ,
58
+ PU : PriceSourceUnchecked < P , C > ,
53
59
C : CustomQuery ,
54
60
{
55
61
pub fn instantiate ( & self , deps : DepsMut < C > , msg : InstantiateMsg ) -> ContractResult < Response > {
77
83
& self ,
78
84
deps : DepsMut < C > ,
79
85
info : MessageInfo ,
80
- msg : ExecuteMsg < P > ,
86
+ msg : ExecuteMsg < PU > ,
81
87
) -> ContractResult < Response > {
82
88
match msg {
83
89
ExecuteMsg :: UpdateOwner ( update) => self . update_owner ( deps, info, update) ,
88
94
ExecuteMsg :: RemovePriceSource {
89
95
denom,
90
96
} => self . remove_price_source ( deps, info. sender , denom) ,
97
+ ExecuteMsg :: UpdateConfig {
98
+ base_denom,
99
+ } => self . update_config ( deps, info. sender , base_denom) ,
91
100
}
92
101
}
93
102
@@ -126,14 +135,14 @@ where
126
135
deps : DepsMut < C > ,
127
136
sender_addr : Addr ,
128
137
denom : String ,
129
- price_source : P ,
138
+ price_source : PU ,
130
139
) -> ContractResult < Response > {
131
140
self . owner . assert_owner ( deps. storage , & sender_addr) ?;
132
141
133
142
validate_native_denom ( & denom) ?;
134
143
135
144
let cfg = self . config . load ( deps. storage ) ?;
136
- price_source. validate ( & deps. querier , & denom, & cfg. base_denom ) ?;
145
+ let price_source = price_source . validate ( deps. as_ref ( ) , & denom, & cfg. base_denom ) ?;
137
146
self . price_sources . save ( deps. storage , & denom, & price_source) ?;
138
147
139
148
Ok ( Response :: new ( )
@@ -157,6 +166,31 @@ where
157
166
. add_attribute ( "denom" , denom) )
158
167
}
159
168
169
+ fn update_config (
170
+ & self ,
171
+ deps : DepsMut < C > ,
172
+ sender_addr : Addr ,
173
+ base_denom : Option < String > ,
174
+ ) -> ContractResult < Response > {
175
+ self . owner . assert_owner ( deps. storage , & sender_addr) ?;
176
+
177
+ if let Some ( bd) = & base_denom {
178
+ validate_native_denom ( bd) ?;
179
+ } ;
180
+
181
+ let mut config = self . config . load ( deps. storage ) ?;
182
+ let prev_base_denom = config. base_denom . clone ( ) ;
183
+ config. base_denom = base_denom. unwrap_or ( config. base_denom ) ;
184
+ self . config . save ( deps. storage , & config) ?;
185
+
186
+ let response = Response :: new ( )
187
+ . add_attribute ( "action" , "update_config" )
188
+ . add_attribute ( "prev_base_denom" , prev_base_denom)
189
+ . add_attribute ( "base_denom" , config. base_denom ) ;
190
+
191
+ Ok ( response)
192
+ }
193
+
160
194
fn query_config ( & self , deps : Deps < C > ) -> StdResult < ConfigResponse > {
161
195
let owner_state = self . owner . query ( deps. storage ) ?;
162
196
let cfg = self . config . load ( deps. storage ) ?;
@@ -204,13 +238,7 @@ where
204
238
let cfg = self . config . load ( deps. storage ) ?;
205
239
let price_source = self . price_sources . load ( deps. storage , & denom) ?;
206
240
Ok ( PriceResponse {
207
- price : price_source. query_price (
208
- & deps,
209
- & env,
210
- & denom,
211
- & cfg. base_denom ,
212
- & self . price_sources ,
213
- ) ?,
241
+ price : price_source. query_price ( & deps, & env, & denom, & cfg, & self . price_sources ) ?,
214
242
denom,
215
243
} )
216
244
}
@@ -233,7 +261,7 @@ where
233
261
. map ( |item| {
234
262
let ( k, v) = item?;
235
263
Ok ( PriceResponse {
236
- price : v. query_price ( & deps, & env, & k, & cfg. base_denom , & self . price_sources ) ?,
264
+ price : v. query_price ( & deps, & env, & k, & cfg, & self . price_sources ) ?,
237
265
denom : k,
238
266
} )
239
267
} )
0 commit comments