Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plmc price to oracle #219

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions integration-tests/src/tests/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn values(
(0u32, FixedU128::from_float(dot)),
(1337u32, FixedU128::from_float(usdc)),
(1984u32, FixedU128::from_float(usdt)),
(2069u32, FixedU128::from_float(plmc))
(3344u32, FixedU128::from_float(plmc))
]
}

Expand All @@ -55,7 +55,7 @@ fn members_can_feed_data() {
(0u32, FixedU128::from_float(4.84)),
(1337u32, FixedU128::from_float(1.0)),
(1984u32, FixedU128::from_float(1.0)),
(2069u32, FixedU128::from_float(0.4)),
(3344u32, FixedU128::from_float(0.4)),
]);

for (key, value) in Oracle::get_all_values() {
Expand Down Expand Up @@ -97,7 +97,7 @@ fn data_is_correctly_combined() {
(0u32, FixedU128::from_float(2.0)),
(1337u32, FixedU128::from_float(1.0)),
(1984u32, FixedU128::from_float(1.1)),
(2069u32, FixedU128::from_float(0.22222)),
(3344u32, FixedU128::from_float(0.22222)),
]);

for (key, value) in Oracle::get_all_values() {
Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub type ProjectMigrationOriginsOf<T> =
pub type BucketOf<T> = Bucket<BalanceOf<T>, PriceOf<T>>;
pub type WeightInfoOf<T> = <T as Config>::WeightInfo;

pub const PLMC_FOREIGN_ID: u32 = 2069;
pub const PLMC_FOREIGN_ID: u32 = 3344;
pub const US_DOLLAR: u128 = 1_0_000_000_000;

#[frame_support::pallet]
Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ thread_local! {
(0u32, FixedU128::from_float(69f64)), // DOT
(1337u32, FixedU128::from_float(0.97f64)), // USDC
(1984u32, FixedU128::from_float(1.0f64)), // USDT
(2069u32, FixedU128::from_float(8.4f64)), // PLMC
(3344u32, FixedU128::from_float(8.4f64)), // PLMC
]));
}
pub struct ConstPriceProvider;
Expand Down
2 changes: 2 additions & 0 deletions pallets/oracle-ocw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pallet-balances = { workspace = true, features = ["std"] }
sp-consensus-aura = { workspace = true, features = ["std"] }
sp-keystore = { workspace = true, features = ["std"] }
parking_lot = "0.12.1"
polimec-common-test-utils.workspace = true

[features]
default = [ "std" ]
Expand All @@ -54,6 +55,7 @@ std = [
"sp-runtime/std",
"sp-std/std",
"substrate-fixed/std",
"polimec-common-test-utils/std",
]

try-runtime = [
Expand Down
6 changes: 5 additions & 1 deletion pallets/oracle-ocw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
use crate::{
traits::FetchPrice,
types::{
AssetName, AssetRequest, BitFinexFetcher, BitStampFetcher, CoinbaseFetcher, KrakenFetcher, OpenCloseVolume,
AssetName, AssetRequest, BitFinexFetcher, BitStampFetcher, CoinbaseFetcher, KrakenFetcher, MexcFetcher,
OpenCloseVolume, XTFetcher,
},
};
use core::ops::Rem;
Expand Down Expand Up @@ -147,6 +148,7 @@ pub mod pallet {
(AssetName::USDT, Zero::zero()),
(AssetName::USDC, Zero::zero()),
(AssetName::DOT, Zero::zero()),
(AssetName::PLMC, Zero::zero()),
]),
};
let assets = last_send_for_assets
Expand Down Expand Up @@ -199,6 +201,8 @@ pub mod pallet {
BitFinexFetcher::get_moving_average,
BitStampFetcher::get_moving_average,
CoinbaseFetcher::get_moving_average,
XTFetcher::get_moving_average,
MexcFetcher::get_moving_average,
];

let mut aggr_prices: BTreeMap<AssetName, Vec<(FixedU128, FixedU128)>> = BTreeMap::new();
Expand Down
20 changes: 19 additions & 1 deletion pallets/oracle-ocw/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Convert<(AssetName, FixedU128), (OracleKey, OracleValue)> for AssetPriceCon
AssetName::DOT => (0, price),
AssetName::USDC => (1337, price),
AssetName::USDT => (1984, price),
AssetName::PLMC => (2069, price),
AssetName::PLMC => (3344, price),
}
}
}
Expand Down Expand Up @@ -219,6 +219,20 @@ pub fn price_oracle_response(state: &mut testing::OffchainState) {
..Default::default()
});
}
state.expect_request(testing::PendingRequest {
method: "GET".into(),
uri: "https://sapi.xt.com/v4/public/kline?symbol=plmc_usdt&interval=30m&limit=10".into(),
response: Some(XT_PLMC_CORRECT.to_vec()),
sent: true,
..Default::default()
});
state.expect_request(testing::PendingRequest {
method: "GET".into(),
uri: "https://api.mexc.com/api/v3/klines?symbol=PLMCUSDT&interval=30m&limit=10".into(),
response: Some(MEXC_PLMC_CORRECT.to_vec()),
sent: true,
..Default::default()
});
}
pub fn run_to_block(n: u64) {
while System::block_number() < n {
Expand Down Expand Up @@ -270,3 +284,7 @@ pub(crate) const COINBASE_RESPONSES: &[(&str, &[u8])] =
&[("USDT-USD", COINBASE_USDT_CORRECT), ("DOT-USD", COINBASE_DOT_CORRECT)];
const COINBASE_USDT_CORRECT: &[u8] = br#"[[1701879000,1.00004,1.00005,1.00004,1.00005,186523.02],[1701878940,1.00004,1.00007,1.00006,1.00004,308533.8],[1701878880,1.00006,1.00007,1.00007,1.00007,139383.41],[1701878820,1.00005,1.00008,1.00008,1.00006,670513.37],[1701878760,1.00006,1.00008,1.00007,1.00007,321319.05],[1701878700,1.00006,1.00007,1.00007,1.00007,154885.47],[1701878640,1.00006,1.00007,1.00006,1.00006,138635.74],[1701878580,1.00006,1.00007,1.00007,1.00007,101704.53],[1701878520,1,1.00007,1,1.00007,476611.79],[1701878460,1,1.00006,1.00006,1.00001,469500.28],[1701878400,1.00005,1.00006,1.00005,1.00005,243522.52],[1701878340,1.00005,1.00007,1.00006,1.00006,209765.12],[1701878280,1.00006,1.00007,1.00006,1.00006,166088.23],[1701878220,1.00006,1.00007,1.00006,1.00006,187521.23],[1701878160,1.00006,1.00007,1.00007,1.00006,237587.95]]"#;
const COINBASE_DOT_CORRECT: &[u8] = br#"[[1701879120,6.138,6.15,6.142,6.15,446.879],[1701879060,6.128,6.141,6.13,6.141,988.289],[1701879000,6.118,6.131,6.12,6.13,418.213],[1701878940,6.108,6.119,6.109,6.119,1185.35],[1701878880,6.106,6.116,6.109,6.109,1666.431],[1701878820,6.103,6.124,6.124,6.108,5914.445],[1701878760,6.125,6.149,6.149,6.125,2211.268],[1701878700,6.147,6.163,6.159,6.147,2177.716],[1701878640,6.155,6.166,6.161,6.162,2345.747],[1701878580,6.146,6.16,6.154,6.16,3245.27],[1701878520,6.132,6.153,6.136,6.153,4739.668],[1701878460,6.137,6.145,6.137,6.138,2349.617],[1701878400,6.125,6.145,6.127,6.141,6026.081],[1701878340,6.125,6.141,6.136,6.125,1725.614],[1701878280,6.133,6.156,6.145,6.133,2828.037]]"#;

pub(crate) const XT_PLMC_CORRECT: &[u8] = br#"{"rc":0,"mc":"SUCCESS","ma":[],"result":[{"t":1711612800000,"o":"0.413","c":"0.413","h":"0.413","l":"0.413","q":"310.3300","v":"128.16629"},{"t":1711609200000,"o":"0.413","c":"0.413","h":"0.413","l":"0.413","q":"1650.0000","v":"681.450"},{"t":1711605600000,"o":"0.413","c":"0.413","h":"0.413","l":"0.413","q":"157.4729","v":"65.0363077"},{"t":1711604700000,"o":"0.417","c":"0.414","h":"0.417","l":"0.414","q":"181.6950","v":"75.4584304"},{"t":1711603800000,"o":"0.413","c":"0.416","h":"0.416","l":"0.413","q":"195.6683","v":"81.1338045"},{"t":1711602900000,"o":"0.410","c":"0.412","h":"0.412","l":"0.410","q":"186.1684","v":"76.4780487"},{"t":1711602000000,"o":"0.407","c":"0.407","h":"0.407","l":"0.407","q":"2.8544","v":"1.1617408"},{"t":1711601100000,"o":"0.418","c":"0.410","h":"0.418","l":"0.404","q":"1433.2745","v":"586.6181797"},{"t":1711599300000,"o":"0.410","c":"0.420","h":"0.420","l":"0.410","q":"897.5851","v":"374.447996"},{"t":1711598400000,"o":"0.408","c":"0.408","h":"0.408","l":"0.408","q":"12.6320","v":"5.153856"}]}"#;

pub(crate) const MEXC_PLMC_CORRECT: &[u8] = br#"[[1711610100000,"0.415","0.415","0.415","0.415","0.0",1711611000000,"0.0"],[1711611000000,"0.415","0.415","0.415","0.415","19.64",1711611900000,"8.1506"],[1711611900000,"0.415","0.4176","0.415","0.4176","391.68",1711612800000,"163.5655"],[1711612800000,"0.4176","0.4194","0.4176","0.4194","526.57",1711613700000,"220.298"],[1711613700000,"0.4194","0.4194","0.4194","0.4194","0.0",1711614600000,"0.0"],[1711614600000,"0.4194","0.4194","0.4194","0.4194","0.0",1711615500000,"0.0"],[1711615500000,"0.4194","0.4194","0.4194","0.4194","0.0",1711616400000,"0.0"],[1711616400000,"0.4194","0.4194","0.4194","0.4194","0.0",1711617300000,"0.0"],[1711617300000,"0.4194","0.4194","0.4194","0.4194","0.0",1711618200000,"0.0"],[1711618200000,"0.4194","0.4194","0.4194","0.4194","0.0",1711619100000,"0.0"]]"#;
56 changes: 32 additions & 24 deletions pallets/oracle-ocw/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
use crate::{
mock::*,
traits::FetchPrice,
types::{BitFinexFetcher, BitStampFetcher, CoinbaseFetcher, KrakenFetcher},
types::{AssetName, BitFinexFetcher, BitStampFetcher, CoinbaseFetcher, KrakenFetcher, MexcFetcher, XTFetcher},
};
use parity_scale_codec::Decode;
use polimec_common_test_utils::do_request;
use sp_runtime::FixedU128;

#[test]
Expand All @@ -42,6 +43,7 @@ fn call_offchain_worker() {
0 => assert_close_enough(price, FixedU128::from_float(6.138485575453039783)),
1984 => assert_close_enough(price, FixedU128::from_float(1.000154206100002620)),
1337 => assert_close_enough(price, FixedU128::from_float(1.000093378020633965)),
3344 => assert_close_enough(price, FixedU128::from_float(0.414564170729477207)),
_ => panic!("Unexpected asset"),
}
},
Expand All @@ -50,38 +52,44 @@ fn call_offchain_worker() {
});
}

#[test]
fn kraken_parser() {
for (_, body_str) in KRAKEN_RESPONSES.iter() {
let body = std::str::from_utf8(body_str).unwrap();
let data = KrakenFetcher::parse_body(body);
fn test_fetcher_against_real_api<F: FetchPrice>() {
for asset in vec![AssetName::DOT, AssetName::USDC, AssetName::USDT, AssetName::PLMC] {
let url = F::get_url(asset);
if url == "" {
continue;
}
let body = do_request(url);
let data = F::parse_body(&body);
assert!(data.is_some());
}
}

#[test]
fn bitfinex_parser() {
for (_, body_str) in BITFINEX_RESPONSES.iter() {
let body = std::str::from_utf8(body_str).unwrap();
let data = BitFinexFetcher::parse_body(body);
assert!(data.is_some());
}
fn test_coinbase_against_real_api() {
test_fetcher_against_real_api::<CoinbaseFetcher>();
}

#[test]
fn bitstamp_parser() {
for (_, body_str) in BITSTAMP_RESPONSES.iter() {
let body = std::str::from_utf8(body_str).unwrap();
let data = BitStampFetcher::parse_body(body);
assert!(data.is_some());
}
fn test_kraken_against_real_api() {
test_fetcher_against_real_api::<KrakenFetcher>();
}

#[test]
fn coinbase_parser() {
for (_, body_str) in COINBASE_RESPONSES.iter() {
let body = std::str::from_utf8(body_str).unwrap();
let data = CoinbaseFetcher::parse_body(body);
assert!(data.is_some());
}
fn test_bitfinex_against_real_api() {
test_fetcher_against_real_api::<BitFinexFetcher>();
}

#[test]
fn test_bitstamp_against_real_api() {
test_fetcher_against_real_api::<BitStampFetcher>();
}

#[test]
fn test_xt_against_real_api() {
test_fetcher_against_real_api::<XTFetcher>();
}

#[test]
fn test_mexc_against_real_api() {
test_fetcher_against_real_api::<MexcFetcher>();
}
Loading