-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoracle.rs
413 lines (368 loc) · 12.8 KB
/
oracle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use injective_std::types::injective::oracle::v1beta1::{
MsgRelayBandRates, MsgRelayBandRatesResponse, MsgRelayPriceFeedPrice,
MsgRelayPriceFeedPriceResponse, MsgRelayPythPrices, MsgRelayPythPricesResponse,
QueryModuleStateRequest, QueryModuleStateResponse, QueryOraclePriceRequest,
QueryOraclePriceResponse, QueryPythPriceRequest, QueryPythPriceResponse,
};
use test_tube_inj::module::Module;
use test_tube_inj::runner::Runner;
use test_tube_inj::{fn_execute, fn_query};
pub struct Oracle<'a, R: Runner<'a>> {
runner: &'a R,
}
impl<'a, R: Runner<'a>> Module<'a, R> for Oracle<'a, R> {
fn new(runner: &'a R) -> Self {
Self { runner }
}
}
impl<'a, R> Oracle<'a, R>
where
R: Runner<'a>,
{
fn_execute! {
pub relay_band_rates: MsgRelayBandRates => MsgRelayBandRatesResponse
}
fn_execute! {
pub relay_price_feed: MsgRelayPriceFeedPrice => MsgRelayPriceFeedPriceResponse
}
fn_execute! {
pub relay_pyth_prices: MsgRelayPythPrices => MsgRelayPythPricesResponse
}
fn_query! {
pub query_module_state ["/injective.oracle.v1beta1.Query/OracleModuleState"]: QueryModuleStateRequest => QueryModuleStateResponse
}
fn_query! {
pub query_oracle_price ["/injective.oracle.v1beta1.Query/OraclePrice"]: QueryOraclePriceRequest => QueryOraclePriceResponse
}
fn_query! {
pub query_pyth_price ["/injective.oracle.v1beta1.Query/PythPrice"]: QueryPythPriceRequest => QueryPythPriceResponse
}
}
#[cfg(test)]
mod tests {
use cosmwasm_std::Coin;
use injective_std::types::injective::oracle::v1beta1::{
MsgRelayPythPrices, Params, PriceAttestation,
};
use injective_std::{
shim::Any,
types::{
cosmos::gov,
cosmos::{
bank::v1beta1::MsgSend,
base::v1beta1::Coin as TubeCoin,
gov::v1::{MsgSubmitProposal, MsgVote},
},
injective::oracle,
injective::oracle::v1beta1::{
GrantPriceFeederPrivilegeProposal, MsgRelayPriceFeedPrice, MsgUpdateParams,
},
},
};
use prost::Message;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::{Account, Bank, Gov, InjectiveTestApp, Module, Oracle};
#[test]
fn price_feed_oracle_integration() {
let app = InjectiveTestApp::new();
let gov = Gov::new(&app);
let bank = Bank::new(&app);
let oracle = Oracle::new(&app);
let signer = app
.init_account(&[
Coin::new(100_000_000_000_000_000_000_000u128, "inj"),
Coin::new(100_000_000_000_000_000_000u128, "usdt"),
])
.unwrap();
let mut buf = vec![];
oracle::v1beta1::GrantPriceFeederPrivilegeProposal::encode(
&GrantPriceFeederPrivilegeProposal {
title: "test-proposal".to_string(),
description: "test-proposal".to_string(),
base: "inj".to_string(),
quote: "usdt".to_string(),
relayers: vec![signer.address()],
},
&mut buf,
)
.unwrap();
let validator = app
.get_first_validator_signing_account("inj".to_string(), 1.2f64)
.unwrap();
// fund the validator account
bank.send(
MsgSend {
from_address: signer.address(),
to_address: validator.address(),
amount: vec![TubeCoin {
amount: "1000000000000000000000".to_string(),
denom: "inj".to_string(),
}],
},
&signer,
)
.unwrap();
let res = gov
.submit_proposal_v1beta1(
gov::v1beta1::MsgSubmitProposal {
content: Some(Any {
type_url: "/injective.oracle.v1beta1.GrantPriceFeederPrivilegeProposal"
.to_string(),
value: buf,
}),
initial_deposit: vec![TubeCoin {
amount: "100000000000000000000".to_string(),
denom: "inj".to_string(),
}],
proposer: validator.address(),
},
&validator,
)
.unwrap();
let proposal_id = res
.events
.iter()
.find(|e| e.ty == "submit_proposal")
.unwrap()
.attributes[0]
.value
.clone();
gov.vote(
MsgVote {
proposal_id: u64::from_str(&proposal_id).unwrap(),
voter: validator.address(),
option: 1i32,
metadata: "".to_string(),
},
&validator,
)
.unwrap();
// NOTE: increase the block time in order to move past the voting period
app.increase_time(10u64);
let expected_price = "12000".to_string();
oracle
.relay_price_feed(
MsgRelayPriceFeedPrice {
sender: signer.address(),
base: vec!["inj".to_string()],
quote: vec!["usdt".to_string()],
price: vec![expected_price.clone()],
},
&signer,
)
.unwrap();
let price = oracle
.query_oracle_price(&oracle::v1beta1::QueryOraclePriceRequest {
oracle_type: 2i32,
base: "inj".to_string(),
quote: "usdt".to_string(),
scaling_options: None,
})
.unwrap()
.price_pair_state
.unwrap()
.pair_price;
assert_eq!(price, expected_price, "price should be equal");
}
#[test]
fn pyth_oracle_integration() {
let app = InjectiveTestApp::new();
let gov = Gov::new(&app);
let bank = Bank::new(&app);
let oracle = Oracle::new(&app);
let signer = app
.init_account(&[
Coin::new(100_000_000_000_000_000_000_000u128, "inj"),
Coin::new(100_000_000_000_000_000_000u128, "usdt"),
])
.unwrap();
let validator = app
.get_first_validator_signing_account("inj".to_string(), 1.2f64)
.unwrap();
// fund the validator account
bank.send(
MsgSend {
from_address: signer.address(),
to_address: validator.address(),
amount: vec![TubeCoin {
amount: "1000000000000000000000".to_string(),
denom: "inj".to_string(),
}],
},
&signer,
)
.unwrap();
let pyth_contract = app
.init_account(&[Coin::new(100_000_000_000_000_000_000_000u128, "inj")])
.unwrap();
let governance_module_address = "inj10d07y265gmmuvt4z0w9aw880jnsr700jstypyt";
let mut buf = vec![];
MsgUpdateParams::encode(
&MsgUpdateParams {
authority: governance_module_address.to_string(),
params: Some(Params {
pyth_contract: pyth_contract.address(),
}),
},
&mut buf,
)
.unwrap();
let res = gov
.submit_proposal(
MsgSubmitProposal {
messages: vec![Any {
type_url: "/injective.oracle.v1beta1.MsgUpdateParams".to_string(),
value: buf,
}],
initial_deposit: vec![TubeCoin {
amount: "100000000000000000000".to_string(),
denom: "inj".to_string(),
}],
proposer: validator.address(),
metadata: "".to_string(),
title: "Update params".to_string(),
summary: "Basically updating the params".to_string(),
expedited: false,
},
&validator,
)
.unwrap();
let proposal_id = res
.events
.iter()
.find(|e| e.ty == "submit_proposal")
.unwrap()
.attributes[0]
.value
.clone();
gov.vote(
MsgVote {
proposal_id: u64::from_str(&proposal_id).unwrap(),
voter: validator.address(),
option: 1i32,
metadata: "".to_string(),
},
&validator,
)
.unwrap();
// NOTE: increase the block time in order to move past the voting period
app.increase_time(11u64);
let inj_price_id = "0x7a5bc1d2b56ad029048cd63964b3ad2776eadf812edc1a43a31406cb54bff592";
let usdt_price_id = "0x1fc18861232290221461220bd4e2acd1dcdfbc89c84092c93c18bdc7756c1588";
let now = SystemTime::now();
let unix = now.duration_since(UNIX_EPOCH).unwrap();
let inj_price_attestation = PriceAttestation {
price_id: inj_price_id.to_string(),
price: 1456,
conf: 500,
expo: -12,
ema_price: 1672,
ema_conf: 2000,
ema_expo: -12,
publish_time: unix.as_millis() as i64,
};
let usdt_price_attestation = PriceAttestation {
price_id: usdt_price_id.to_string(),
price: 1,
conf: 500,
expo: -12,
ema_price: 121,
ema_conf: 2000,
ema_expo: -12,
publish_time: unix.as_millis() as i64,
};
oracle
.relay_pyth_prices(
MsgRelayPythPrices {
sender: pyth_contract.address(),
price_attestations: vec![
inj_price_attestation.clone(),
usdt_price_attestation.clone(),
],
},
&pyth_contract,
)
.unwrap();
let inj_price_state = oracle
.query_pyth_price(&oracle::v1beta1::QueryPythPriceRequest {
price_id: inj_price_id.to_string(),
})
.unwrap()
.price_state
.unwrap();
assert!(
inj_price_state.price_state.is_some(),
"inj price state should be some"
);
let inj_price = inj_price_state.price_state.unwrap();
assert_eq!(
inj_price.price,
1456000000.to_string(),
"inj price should be equal to the price attestation"
);
assert_eq!(
inj_price.cumulative_price, "0",
"inj cumulative price should equal to 0"
);
assert_eq!(
inj_price_state.conf,
500000000.to_string(),
"inj conf should be equal to the price attestation"
);
assert_eq!(
inj_price_state.ema_price,
1672000000.to_string(),
"inj ema_price should be equal to the price attestation"
);
assert_eq!(
inj_price_state.ema_conf,
2000000000.to_string(),
"inj ema_conf should be equal to the price attestation"
);
assert_eq!(
inj_price_state.publish_time, inj_price_attestation.publish_time as u64,
"inj publish_time should be equal to the price attestation"
);
let usdt_price_state = oracle
.query_pyth_price(&oracle::v1beta1::QueryPythPriceRequest {
price_id: usdt_price_id.to_string(),
})
.unwrap()
.price_state
.unwrap();
assert!(
usdt_price_state.price_state.is_some(),
"usdt price state should be some"
);
let usdt_price = usdt_price_state.price_state.unwrap();
assert_eq!(
usdt_price.price,
1000000.to_string(),
"usdt price should be equal to the price attestation"
);
assert_eq!(
usdt_price.cumulative_price, "0",
"usdt cumulative price should be equal to 0"
);
assert_eq!(
usdt_price_state.conf,
500000000.to_string(),
"usdt conf should be equal to the price attestation"
);
assert_eq!(
usdt_price_state.ema_price,
121000000.to_string(),
"usdt ema_price should be equal to the price attestation"
);
assert_eq!(
usdt_price_state.ema_conf,
2000000000.to_string(),
"usdt ema_conf should be equal to the price attestation"
);
assert_eq!(
usdt_price_state.publish_time, usdt_price_attestation.publish_time as u64,
"usdt publish_time should be equal to the price attestation"
);
}
}