From 8f06f160155e99574fcd245bb9b5e8ad6302aa58 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Rios Date: Fri, 29 Nov 2024 14:48:32 +0100 Subject: [PATCH] WETH Oracle support --- bacon.toml | 101 ------------------ integration-tests/src/tests/oracle.rs | 57 +++++----- nodes/parachain/src/chain_spec/common.rs | 10 +- .../parachain/src/chain_spec/polimec_paseo.rs | 18 +++- pallets/oracle-ocw/src/lib.rs | 32 ++++-- pallets/oracle-ocw/src/mock.rs | 16 +-- pallets/oracle-ocw/src/tests.rs | 11 +- pallets/oracle-ocw/src/traits.rs | 1 + pallets/oracle-ocw/src/types.rs | 7 +- scripts/zombienet/polimec-paseo-local.toml | 6 +- 10 files changed, 109 insertions(+), 150 deletions(-) delete mode 100644 bacon.toml diff --git a/bacon.toml b/bacon.toml deleted file mode 100644 index 22fbf444a..000000000 --- a/bacon.toml +++ /dev/null @@ -1,101 +0,0 @@ -# This is a configuration file for the bacon tool -# -# Bacon repository: https://github.com/Canop/bacon -# Complete help on configuration: https://dystroy.org/bacon/config/ -# You can also check bacon's own bacon.toml file -# as an example: https://github.com/Canop/bacon/blob/main/bacon.toml - -default_job = "check" - -[jobs.check] -command = ["cargo", "check", "--profile=test", "--workspace", "--color", "always"] -need_stdout = false - -[jobs.check-all] -command = ["cargo", "check", "--all-targets", "--color", "always"] -need_stdout = false - -# Run clippy on the default target -[jobs.clippy] -command = [ - "cargo", "clippy", - "--color", "always", -] -need_stdout = false - -# Run clippy on all targets -# To disable some lints, you may change the job this way: -# [jobs.clippy-all] -# command = [ -# "cargo", "clippy", -# "--all-targets", -# "--color", "always", -# "--", -# "-A", "clippy::bool_to_int_with_if", -# "-A", "clippy::collapsible_if", -# "-A", "clippy::derive_partial_eq_without_eq", -# ] -# need_stdout = false -[jobs.clippy-all] -command = [ - "cargo", "clippy", - "--all-targets", - "--color", "always", -] -need_stdout = false - -# This job lets you run -# - all tests: bacon test -# - a specific test: bacon test -- config::test_default_files -# - the tests of a package: bacon test -- -- -p config -[jobs.test] -command = [ - "cargo", "test", "--color", "always", - "--", "--color", "always", # see https://github.com/Canop/bacon/issues/124 -] -need_stdout = true - -[jobs.doc] -command = ["cargo", "doc", "--color", "always", "--no-deps"] -need_stdout = false - -# If the doc compiles, then it opens in your browser and bacon switches -# to the previous job -[jobs.doc-open] -command = ["cargo", "doc", "--color", "always", "--no-deps", "--open"] -need_stdout = false -on_success = "back" # so that we don't open the browser at each change - -# You can run your application and have the result displayed in bacon, -# *if* it makes sense for this crate. -# Don't forget the `--color always` part or the errors won't be -# properly parsed. -# If your program never stops (eg a server), you may set `background` -# to false to have the cargo run output immediately displayed instead -# of waiting for program's end. -[jobs.run] -command = [ - "cargo", "run", - "--color", "always", - # put launch parameters for your program behind a `--` separator -] -need_stdout = true -allow_warnings = true -background = true - -# This parameterized job runs the example of your choice, as soon -# as the code compiles. -# Call it as -# bacon ex -- my-example -[jobs.ex] -command = ["cargo", "run", "--color", "always", "--example"] -need_stdout = true -allow_warnings = true - -# You may define here keybindings that would be specific to -# a project, for example a shortcut to launch a specific job. -# Shortcuts to internal functions (scrolling, toggling, etc.) -# should go in your personal global prefs.toml file instead. -[keybindings] -# alt-m = "job:my-job" -c = "job:clippy-all" # comment this to have 'c' run clippy on only the default target diff --git a/integration-tests/src/tests/oracle.rs b/integration-tests/src/tests/oracle.rs index 337d761f7..15c6617fd 100644 --- a/integration-tests/src/tests/oracle.rs +++ b/integration-tests/src/tests/oracle.rs @@ -15,23 +15,27 @@ // along with this program. If not, see . 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), >::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)) ] } @@ -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() { @@ -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::::NoPermission ); }); @@ -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() { @@ -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()), diff --git a/nodes/parachain/src/chain_spec/common.rs b/nodes/parachain/src/chain_spec/common.rs index a1b5c383e..06e53ef94 100644 --- a/nodes/parachain/src/chain_spec/common.rs +++ b/nodes/parachain/src/chain_spec/common.rs @@ -12,7 +12,10 @@ use polimec_runtime::{ AccountId, AuraId as AuthorityId, Balance, BlockchainOperationTreasury, ContributionTreasuryAccount, ExistentialDeposit, FeeRecipient, OracleProvidersMembershipConfig, Runtime, TreasuryAccount, PLMC, }; -use sp_core::{crypto::UncheckedInto, sr25519}; +use sp_core::{ + crypto::{Ss58AddressFormat, UncheckedInto}, + sr25519, +}; use sp_runtime::{traits::AccountIdConversion, Perbill, Percent}; pub type ChainSpec = sc_service::GenericChainSpec; @@ -65,6 +68,11 @@ pub fn eve() -> polimec_runtime::AccountId { get_account_id_from_seed::("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, pub council_members: Vec, diff --git a/nodes/parachain/src/chain_spec/polimec_paseo.rs b/nodes/parachain/src/chain_spec/polimec_paseo.rs index 7266468a3..dbd05a8f8 100644 --- a/nodes/parachain/src/chain_spec/polimec_paseo.rs +++ b/nodes/parachain/src/chain_spec/polimec_paseo.rs @@ -21,20 +21,32 @@ use sc_service::ChainType; use crate::chain_spec::{ - common::{alice, bob, charlie, dave, eve, genesis_config, GenesisConfigParams}, + common::{acc_from_ss58, alice, bob, charlie, dave, eve, genesis_config, GenesisConfigParams}, get_properties, Extensions, GenericChainSpec, DEFAULT_PARA_ID, }; use polimec_runtime::{AccountId, MinCandidateStk}; 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::>(); 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, diff --git a/pallets/oracle-ocw/src/lib.rs b/pallets/oracle-ocw/src/lib.rs index 0138759d3..b688322a4 100644 --- a/pallets/oracle-ocw/src/lib.rs +++ b/pallets/oracle-ocw/src/lib.rs @@ -125,6 +125,8 @@ pub mod pallet { let account = public.clone().into_account(); if ::Members::contains(&account) { if let Ok(generic_public) = TryInto::>::try_into(public) { + log::trace!(target: LOG_TARGET, "OCW is a member!"); + return Some(generic_public.into()); } } @@ -132,6 +134,7 @@ pub mod pallet { }); if let Some(_authority_key) = maybe_key { + log::trace!(target: LOG_TARGET, "Executing Logic..."); let mut lock = StorageLock::