Skip to content

Commit

Permalink
WETH Oracle support
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Dec 2, 2024
1 parent dca7f69 commit 8fa2387
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 135 deletions.
101 changes: 0 additions & 101 deletions bacon.toml

This file was deleted.

57 changes: 33 additions & 24 deletions integration-tests/src/tests/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use pallet_funding::AcceptedFundingAsset;
/// Tests for the oracle pallet integration.
/// Alice, Bob, Charlie are members of the OracleProvidersMembers.
/// Only members should be able to feed data into the oracle.
use parity_scale_codec::alloc::collections::HashMap;
use polimec_common::PLMC_FOREIGN_ID;
use polimec_runtime::{Oracle, RuntimeOrigin};
use sp_runtime::{bounded_vec, BoundedVec, FixedU128};
use tests::defaults::*;

use AcceptedFundingAsset::{DOT, USDC, USDT, WETH};
fn values(
values: [f64; 4],
values: [f64; 5],
) -> BoundedVec<(u32, FixedU128), <polimec_runtime::Runtime as orml_oracle::Config<()>>::MaxFeedValues> {
let [dot, usdc, usdt, plmc] = values;
let [dot, usdc, usdt, weth, plmc] = values;
bounded_vec![
(10u32, FixedU128::from_float(dot)),
(1337u32, FixedU128::from_float(usdc)),
(1984u32, FixedU128::from_float(usdt)),
(3344u32, FixedU128::from_float(plmc))
(DOT.id(), FixedU128::from_float(dot)),
(USDC.id(), FixedU128::from_float(usdc)),
(USDT.id(), FixedU128::from_float(usdt)),
(WETH.id(), FixedU128::from_float(weth)),
(PLMC_FOREIGN_ID, FixedU128::from_float(plmc))
]
}

Expand All @@ -43,19 +47,20 @@ fn members_can_feed_data() {
// pallet_funding genesis builder already inputs prices, so we need to advance one block to feed new values.
inst.advance_time(1u32);
let alice = PolimecNet::account_id_of(ALICE);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(alice.clone()), values([4.84, 1.0, 1.0, 0.4])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(alice.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])));

let bob = PolimecNet::account_id_of(BOB);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(bob.clone()), values([4.84, 1.0, 1.0, 0.4])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(bob.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])));

let charlie = PolimecNet::account_id_of(CHARLIE);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(charlie.clone()), values([4.84, 1.0, 1.0, 0.4])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(charlie.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])));

let expected_values = HashMap::from([
(10u32, FixedU128::from_float(4.84)),
(1337u32, FixedU128::from_float(1.0)),
(1984u32, FixedU128::from_float(1.0)),
(3344u32, FixedU128::from_float(0.4)),
(DOT.id(), FixedU128::from_float(4.84)),
(USDC.id(), FixedU128::from_float(1.0)),
(USDT.id(), FixedU128::from_float(1.0)),
(WETH.id(), FixedU128::from_float(2500.0)),
(PLMC_FOREIGN_ID, FixedU128::from_float(0.4)),
]);

for (key, value) in Oracle::get_all_values() {
Expand All @@ -70,7 +75,7 @@ fn non_members_cannot_feed_data() {
PolimecNet::execute_with(|| {
let dave = PolimecNet::account_id_of(DAVE);
assert_noop!(
Oracle::feed_values(RuntimeOrigin::signed(dave.clone()), values([4.84, 1.0, 1.0, 0.4])),
Oracle::feed_values(RuntimeOrigin::signed(dave.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])),
orml_oracle::Error::<polimec_runtime::Runtime, ()>::NoPermission
);
});
Expand All @@ -84,20 +89,24 @@ fn data_is_correctly_combined() {
inst.advance_time(1u32);

let alice = PolimecNet::account_id_of(ALICE);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(alice.clone()), values([1.0, 1.5, 1.1, 0.11111])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(alice.clone()), values([1.0, 1.5, 1.1, 2500.0, 0.11111])));

let bob = PolimecNet::account_id_of(BOB);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(bob.clone()), values([2.0, 1.0, 1.2, 0.22222])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(bob.clone()), values([2.0, 1.0, 1.2, 2500.0, 0.22222])));

let charlie = PolimecNet::account_id_of(CHARLIE);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(charlie.clone()), values([3.0, 0.8, 1.1, 0.33333])));
assert_ok!(Oracle::feed_values(
RuntimeOrigin::signed(charlie.clone()),
values([3.0, 0.8, 1.1, 2500.0, 0.33333])
));

// Default CombineData implementation is the median value
let expected_values = HashMap::from([
(10u32, FixedU128::from_float(2.0)),
(1337u32, FixedU128::from_float(1.0)),
(1984u32, FixedU128::from_float(1.1)),
(3344u32, FixedU128::from_float(0.22222)),
(DOT.id(), FixedU128::from_float(2.0)),
(USDC.id(), FixedU128::from_float(1.0)),
(USDT.id(), FixedU128::from_float(1.1)),
(WETH.id(), FixedU128::from_float(2500.0)),
(PLMC_FOREIGN_ID, FixedU128::from_float(0.22222)),
]);

for (key, value) in Oracle::get_all_values() {
Expand All @@ -116,13 +125,13 @@ fn pallet_funding_works() {
inst.advance_time(1u32);

let alice = PolimecNet::account_id_of(ALICE);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(alice.clone()), values([4.84, 1.0, 1.0, 0.4])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(alice.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])));

let bob = PolimecNet::account_id_of(BOB);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(bob.clone()), values([4.84, 1.0, 1.0, 0.4])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(bob.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])));

let charlie = PolimecNet::account_id_of(CHARLIE);
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(charlie.clone()), values([4.84, 1.0, 1.0, 0.4])));
assert_ok!(Oracle::feed_values(RuntimeOrigin::signed(charlie.clone()), values([4.84, 1.0, 1.0, 2500.0, 0.4])));

let _project_id = inst.create_finished_project(
default_project_metadata(ISSUER.into()),
Expand Down
6 changes: 6 additions & 0 deletions nodes/parachain/src/chain_spec/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use polimec_runtime::{
ExistentialDeposit, FeeRecipient, OracleProvidersMembershipConfig, Runtime, TreasuryAccount, PLMC,
};
use sp_core::{crypto::UncheckedInto, sr25519};
use sp_core::crypto::Ss58AddressFormat;
use sp_runtime::{traits::AccountIdConversion, Perbill, Percent};

pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;
Expand Down Expand Up @@ -65,6 +66,11 @@ pub fn eve() -> polimec_runtime::AccountId {
get_account_id_from_seed::<sr25519::Public>("Eve")
}

pub fn acc_from_ss58(string: &str) -> polimec_runtime::AccountId {
use sp_core::crypto::Ss58Codec;
sp_core::sr25519::Public::from_ss58check(string).unwrap().into()
}

pub struct GenesisConfigParams {
pub stakers: Vec<AccountId>,
pub council_members: Vec<AccountId>,
Expand Down
17 changes: 15 additions & 2 deletions nodes/parachain/src/chain_spec/polimec_paseo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,29 @@ use crate::chain_spec::{
get_properties, Extensions, GenericChainSpec, DEFAULT_PARA_ID,
};
use polimec_runtime::{AccountId, MinCandidateStk};
use crate::chain_spec::common::{acc_from_ss58};

pub fn get_local_chain_spec() -> GenericChainSpec {
let endowed_accounts = vec![alice(), bob(), charlie(), dave()];
let endowed_accounts = vec![
alice(),
bob(),
charlie(),
dave(),
acc_from_ss58("5Do5UoayFvDrHroGS1YMqxTVUysSkrhNwVMzmj1foVb3vzzb"),
acc_from_ss58("5E5E37FNZD9KVHyGgSHt8pc2kq8e3VUS5rf8GmrxCa7ySs8s"),
acc_from_ss58("5ELLzYckeuomgTnv4Pf1aT4itxu35cn1KWNCGcftzv5N2x7o"),
];
let endowed_accounts =
endowed_accounts.iter().map(|x| (x.clone(), MinCandidateStk::get() * 20)).collect::<Vec<_>>();
let genesis_config_params = GenesisConfigParams {
stakers: vec![alice(), bob()],
council_members: vec![alice()],
technical_committee_members: vec![alice()],
oracle_members: vec![alice(), bob(), charlie()],
oracle_members: vec![
acc_from_ss58("5Do5UoayFvDrHroGS1YMqxTVUysSkrhNwVMzmj1foVb3vzzb"),
acc_from_ss58("5E5E37FNZD9KVHyGgSHt8pc2kq8e3VUS5rf8GmrxCa7ySs8s"),
acc_from_ss58("5ELLzYckeuomgTnv4Pf1aT4itxu35cn1KWNCGcftzv5N2x7o"),
],
endowed_accounts,
funding_assets_owner: eve(),
id: DEFAULT_PARA_ID,
Expand Down
9 changes: 6 additions & 3 deletions pallets/oracle-ocw/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,26 @@ fn call_offchain_worker() {
assert_eq!(tx.signature.unwrap().0, 0);

match tx.call {
RuntimeCall::Oracle(orml_oracle::Call::feed_values { values }) =>
RuntimeCall::Oracle(orml_oracle::Call::feed_values { values }) => {
dbg!(&values);
for (asset, price) in values {
match asset {
10 => 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)),
10_000 => assert_close_enough(price, FixedU128::from_float(1260.00)),
_ => panic!("Unexpected asset"),
}
},
}
},
_ => panic!("Unexpected call"),
}
});
}

fn test_fetcher_against_real_api<F: FetchPrice>() {
for asset in [AssetName::DOT, AssetName::USDC, AssetName::USDT, AssetName::PLMC] {
for asset in [AssetName::DOT, AssetName::USDC, AssetName::USDT, AssetName::PLMC, AssetName::WETH] {
let url = F::get_url(asset);
if url.is_empty() {
continue;
Expand Down
6 changes: 4 additions & 2 deletions pallets/oracle-ocw/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl FetchPrice for KrakenFetcher {
AssetName::DOT => "https://api.kraken.com/0/public/OHLC?pair=DOTUSD&interval=1",
AssetName::USDC => "https://api.kraken.com/0/public/OHLC?pair=USDCUSD&interval=1",
AssetName::PLMC => "",
AssetName::WETH => "",
AssetName::WETH => "https://api.kraken.com/0/public/OHLC?pair=ETHUSD&interval=1",
}
}
}
Expand Down Expand Up @@ -160,6 +160,7 @@ impl FetchPrice for BitFinexFetcher {
AssetName::USDT => "https://api-pub.bitfinex.com/v2/candles/trade%3A1m%3AtUSTUSD/hist?limit=15",
AssetName::DOT => "https://api-pub.bitfinex.com/v2/candles/trade%3A1m%3AtDOTUSD/hist?limit=15",
AssetName::USDC => "https://api-pub.bitfinex.com/v2/candles/trade%3A1m%3AtUDCUSD/hist?limit=15",
AssetName::WETH => "https://api-pub.bitfinex.com/v2/candles/trade%3A1m%3AtETHUSD/hist?limit=15",
_ => "",
}
}
Expand Down Expand Up @@ -226,7 +227,7 @@ impl FetchPrice for BitStampFetcher {
AssetName::DOT => "https://www.bitstamp.net/api/v2/ohlc/dotusd/?step=60&limit=15",
AssetName::USDC => "https://www.bitstamp.net/api/v2/ohlc/usdcusd/?step=60&limit=15",
AssetName::PLMC => "",
AssetName::WETH => "",
AssetName::WETH => "https://www.bitstamp.net/api/v2/ohlc/ethusd/?step=60&limit=15",
}
}
}
Expand Down Expand Up @@ -257,6 +258,7 @@ impl FetchPrice for CoinbaseFetcher {
match name {
AssetName::USDT => "https://api.exchange.coinbase.com/products/USDT-USD/candles?granularity=60",
AssetName::DOT => "https://api.exchange.coinbase.com/products/DOT-USD/candles?granularity=60",
AssetName::WETH => "https://api.exchange.coinbase.com/products/ETH-USD/candles?granularity=60",
_ => "",
}
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/zombienet/polimec-paseo-local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ chain = "polimec-paseo-local"
[[parachains.collators]]
name = "collator1"
command = "./target/release/polimec-node"
args = ["--offchain-worker when-authority"]
args = ["--offchain-worker=always --enable-offchain-indexing=true"]
# ss58 key: 5Do5UoayFvDrHroGS1YMqxTVUysSkrhNwVMzmj1foVb3vzzb
keystore_key_types = ["aura", "plmc_sr"]

[[parachains.collators]]
name = "collator2"
command = "./target/release/polimec-node"
args = ["--offchain-worker when-authority"]
args = ["--offchain-worker=always --enable-offchain-indexing=true"]
# ss58 key: 5E5E37FNZD9KVHyGgSHt8pc2kq8e3VUS5rf8GmrxCa7ySs8s
keystore_key_types = ["aura", "plmc_sr"]

[[parachains.collators]]
name = "collator3"
command = "./target/release/polimec-node"
args = ["--offchain-worker when-authority"]
args = ["--offchain-worker=always --enable-offchain-indexing=true"]
# ss58 key: 5ELLzYckeuomgTnv4Pf1aT4itxu35cn1KWNCGcftzv5N2x7o
keystore_key_types = ["aura", "plmc_sr"]

0 comments on commit 8fa2387

Please sign in to comment.