From 0de9633f8be0ad9f387713883c29bee2cb9b0514 Mon Sep 17 00:00:00 2001 From: Gautham Date: Fri, 8 Mar 2024 12:40:51 +0530 Subject: [PATCH 01/16] Automate withdrawals --- pallets/ocex/src/lib.rs | 106 +++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 39 deletions(-) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 3edd347fd..8daa277f7 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -810,9 +810,6 @@ pub mod pallet { // Anyone can claim the withdrawal for any user // This is to build services that can enable free withdrawals similar to CEXes. let _ = ensure_signed(origin)?; - // This vector will keep track of withdrawals processed already - let mut processed_withdrawals = vec![]; - let mut failed_withdrawals = vec![]; ensure!( >::contains_key(snapshot_id), Error::::InvalidWithdrawalIndex @@ -822,50 +819,36 @@ pub mod pallet { // return Err >::mutate(snapshot_id, |btree_map| { // Get mutable reference to the withdrawals vector - if let Some(withdrawal_vector) = btree_map.get_mut(&account) { - while !withdrawal_vector.is_empty() { - // Perform pop operation to ensure we do not leave any withdrawal left - // for a double spend - if let Some(withdrawal) = withdrawal_vector.pop() { - if Self::on_idle_withdrawal_processor(withdrawal.clone()) { - processed_withdrawals.push(withdrawal.to_owned()); - } else { - // Storing the failed withdrawals back into the storage item - failed_withdrawals.push(withdrawal.to_owned()); - Self::deposit_event(Event::WithdrawalFailed(withdrawal.to_owned())); - } - } else { - return Err(Error::::InvalidWithdrawalAmount); - } - } + if let Some(withdrawal_vector) = btree_map.remove(&account) { + let (failed_withdrawals, processed_withdrawals) = + Self::do_withdraw(withdrawal_vector); // Not removing key from BtreeMap so that failed withdrawals can still be // tracked btree_map.insert(account.clone(), failed_withdrawals); + + if !processed_withdrawals.is_empty() { + Self::deposit_event(Event::WithdrawalClaimed { + main: account.clone(), + withdrawals: processed_withdrawals.clone(), + }); + >::mutate(|onchain_events| { + onchain_events.push( + orderbook_primitives::ocex::OnChainEvents::OrderBookWithdrawalClaimed( + snapshot_id, + account.clone(), + processed_withdrawals, + ), + ); + }); + } Ok(()) } else { // This allows us to ensure we do not have someone with an invalid account Err(Error::::InvalidWithdrawalIndex) } })?; - if !processed_withdrawals.is_empty() { - Self::deposit_event(Event::WithdrawalClaimed { - main: account.clone(), - withdrawals: processed_withdrawals.clone(), - }); - >::mutate(|onchain_events| { - onchain_events.push( - orderbook_primitives::ocex::OnChainEvents::OrderBookWithdrawalClaimed( - snapshot_id, - account.clone(), - processed_withdrawals, - ), - ); - }); - Ok(Pays::No.into()) - } else { - // If someone withdraws nothing successfully - should pay for such transaction - Ok(Pays::Yes.into()) - } + + Ok(Pays::Yes.into()) } /// Allowlist Token @@ -904,6 +887,7 @@ pub mod pallet { _signatures: Vec<(u16, ::Signature)>, ) -> DispatchResult { ensure_none(origin)?; + let snapshot_id = summary.snapshot_id; // Update the trader's performance on-chain if let Some(ref metrics) = summary.trader_metrics { Self::update_lmp_scores(metrics)?; @@ -912,7 +896,29 @@ pub mod pallet { Self::process_egress_msg(summary.egress_messages.as_ref())?; if !summary.withdrawals.is_empty() { let withdrawal_map = Self::create_withdrawal_tree(&summary.withdrawals); - >::insert(summary.snapshot_id, withdrawal_map); + let mut failed_withdrawal_map = crate::pallet::WithdrawalsMap::::new(); + for (account, withdrawals) in withdrawal_map { + let (failed_withdraws, successful_withdraws) = Self::do_withdraw(withdrawals); + failed_withdrawal_map.insert(account.clone(), failed_withdraws); + if !successful_withdraws.is_empty() { + Self::deposit_event(Event::WithdrawalClaimed { + main: account.clone(), + withdrawals: successful_withdraws.clone(), + }); + >::mutate(|onchain_events| { + onchain_events.push( + orderbook_primitives::ocex::OnChainEvents::OrderBookWithdrawalClaimed( + snapshot_id, + account.clone(), + successful_withdraws, + ), + ); + }); + } + } + if !failed_withdrawal_map.is_empty() { + >::insert(summary.snapshot_id, failed_withdrawal_map); + } let fees = summary.get_fees(); Self::settle_withdrawal_fees(fees)?; >::mutate(|onchain_events| { @@ -1311,6 +1317,28 @@ pub mod pallet { StorageValue<_, AuctionInfo>, OptionQuery>; impl crate::pallet::Pallet { + pub fn do_withdraw( + mut withdrawal_vector: Vec>, + ) -> (Vec>, Vec>) { + let mut failed_withdrawals = Vec::new(); + let mut processed_withdrawals = Vec::new(); + while !withdrawal_vector.is_empty() { + // Perform pop operation to ensure we do not leave any withdrawal left + // for a double spend + if let Some(withdrawal) = withdrawal_vector.pop() { + if Self::on_idle_withdrawal_processor(withdrawal.clone()) { + processed_withdrawals.push(withdrawal.to_owned()); + } else { + // Storing the failed withdrawals back into the storage item + failed_withdrawals.push(withdrawal.to_owned()); + Self::deposit_event(Event::WithdrawalFailed(withdrawal.to_owned())); + } + } + } + + (failed_withdrawals, processed_withdrawals) + } + pub fn do_claim_lmp_rewards( main: T::AccountId, epoch: u16, From c91349784a1c6b870564a5cd57c907a0ed6b7444 Mon Sep 17 00:00:00 2001 From: Gautham Date: Fri, 8 Mar 2024 13:05:58 +0530 Subject: [PATCH 02/16] Add tests --- pallets/ocex/src/lib.rs | 46 +++++++++++++-------------------------- pallets/ocex/src/tests.rs | 30 +++++++++---------------- 2 files changed, 25 insertions(+), 51 deletions(-) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 8daa277f7..2394c1fb0 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -821,25 +821,17 @@ pub mod pallet { // Get mutable reference to the withdrawals vector if let Some(withdrawal_vector) = btree_map.remove(&account) { let (failed_withdrawals, processed_withdrawals) = - Self::do_withdraw(withdrawal_vector); + Self::do_withdraw(snapshot_id, withdrawal_vector); // Not removing key from BtreeMap so that failed withdrawals can still be // tracked btree_map.insert(account.clone(), failed_withdrawals); if !processed_withdrawals.is_empty() { Self::deposit_event(Event::WithdrawalClaimed { + snapshot_id, main: account.clone(), withdrawals: processed_withdrawals.clone(), }); - >::mutate(|onchain_events| { - onchain_events.push( - orderbook_primitives::ocex::OnChainEvents::OrderBookWithdrawalClaimed( - snapshot_id, - account.clone(), - processed_withdrawals, - ), - ); - }); } Ok(()) } else { @@ -898,22 +890,17 @@ pub mod pallet { let withdrawal_map = Self::create_withdrawal_tree(&summary.withdrawals); let mut failed_withdrawal_map = crate::pallet::WithdrawalsMap::::new(); for (account, withdrawals) in withdrawal_map { - let (failed_withdraws, successful_withdraws) = Self::do_withdraw(withdrawals); - failed_withdrawal_map.insert(account.clone(), failed_withdraws); + let (failed_withdraws, successful_withdraws) = + Self::do_withdraw(snapshot_id, withdrawals); + if !failed_withdraws.is_empty() { + failed_withdrawal_map.insert(account.clone(), failed_withdraws); + } if !successful_withdraws.is_empty() { Self::deposit_event(Event::WithdrawalClaimed { + snapshot_id, main: account.clone(), withdrawals: successful_withdraws.clone(), }); - >::mutate(|onchain_events| { - onchain_events.push( - orderbook_primitives::ocex::OnChainEvents::OrderBookWithdrawalClaimed( - snapshot_id, - account.clone(), - successful_withdraws, - ), - ); - }); } } if !failed_withdrawal_map.is_empty() { @@ -921,14 +908,6 @@ pub mod pallet { } let fees = summary.get_fees(); Self::settle_withdrawal_fees(fees)?; - >::mutate(|onchain_events| { - onchain_events.push( - orderbook_primitives::ocex::OnChainEvents::OrderbookWithdrawalProcessed( - summary.snapshot_id, - summary.withdrawals.clone(), - ), - ); - }); } let id = summary.snapshot_id; >::put(id); @@ -1109,6 +1088,7 @@ pub mod pallet { EnclaveCleanup(Vec), TradingPairIsNotOperational, WithdrawalClaimed { + snapshot_id: u64, main: T::AccountId, withdrawals: Vec>, }, @@ -1125,7 +1105,7 @@ pub mod pallet { /// AllowlistedTokenRemoved AllowlistedTokenRemoved(AssetId), /// Withdrawal failed - WithdrawalFailed(Withdrawal), + WithdrawalFailed(u64, Withdrawal), /// Exchange state has been updated ExchangeStateUpdated(bool), /// DisputePeriod has been updated @@ -1318,6 +1298,7 @@ pub mod pallet { impl crate::pallet::Pallet { pub fn do_withdraw( + snapshot_id: u64, mut withdrawal_vector: Vec>, ) -> (Vec>, Vec>) { let mut failed_withdrawals = Vec::new(); @@ -1331,7 +1312,10 @@ pub mod pallet { } else { // Storing the failed withdrawals back into the storage item failed_withdrawals.push(withdrawal.to_owned()); - Self::deposit_event(Event::WithdrawalFailed(withdrawal.to_owned())); + Self::deposit_event(Event::WithdrawalFailed( + snapshot_id, + withdrawal.to_owned(), + )); } } } diff --git a/pallets/ocex/src/tests.rs b/pallets/ocex/src/tests.rs index 9066368a3..33555c8c0 100644 --- a/pallets/ocex/src/tests.rs +++ b/pallets/ocex/src/tests.rs @@ -28,7 +28,7 @@ use std::str::FromStr; // or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. use crate::mock::*; use frame_support::traits::fungibles::Mutate as MutateAsset; -use frame_support::{testing_prelude::bounded_vec, BoundedVec}; +use frame_support::BoundedVec; use frame_system::EventRecord; use orderbook_primitives::ingress::{EgressMessages, IngressMessages}; use orderbook_primitives::ocex::AccountInfo; @@ -2036,16 +2036,10 @@ fn test_submit_snapshot() { assert_ok!(OCEX::submit_snapshot(RuntimeOrigin::none(), snapshot.clone(), Vec::new())); assert_eq!(Withdrawals::::contains_key(1), true); - assert_eq!(Withdrawals::::get(1), withdrawal_map.clone()); + assert_eq!(Withdrawals::::get(1), withdrawal_map); assert_eq!(Snapshots::::contains_key(1), true); assert_eq!(Snapshots::::get(1).unwrap(), snapshot.clone()); assert_eq!(SnapshotNonce::::get(), 1); - let onchain_events = - vec![orderbook_primitives::ocex::OnChainEvents::OrderbookWithdrawalProcessed( - 1, - snapshot.withdrawals.clone(), - )]; - assert_eq!(OnChainEvents::::get(), onchain_events); // Checking for redundant data inside snapshot assert_eq!(Snapshots::::get(1).unwrap().withdrawals, snapshot.withdrawals); }) @@ -2102,11 +2096,14 @@ fn test_withdrawal() { new_block(); new_block(); - assert_ok!(OCEX::claim_withdraw( - RuntimeOrigin::signed(account_id.clone().into()), - 1, - account_id.clone() - )); + assert_noop!( + OCEX::claim_withdraw( + RuntimeOrigin::signed(account_id.clone().into()), + 1, + account_id.clone() + ), + Error::::InvalidWithdrawalIndex + ); // Balances after withdrawal assert_eq!( ::NativeCurrency::free_balance(account_id.clone()), @@ -2116,13 +2113,6 @@ fn test_withdrawal() { ::NativeCurrency::free_balance(custodian_account.clone()), initial_balance - UNIT_BALANCE, // Dec ); - let withdrawal_claimed: orderbook_primitives::ocex::OnChainEvents = - orderbook_primitives::ocex::OnChainEvents::OrderBookWithdrawalClaimed( - 1, - account_id.clone().into(), - bounded_vec![snapshot.withdrawals[0].clone()], - ); - assert_eq!(OnChainEvents::::get()[1], withdrawal_claimed); }); } From 79c9b9974b49140b7fcba72d3d02abbd83e6a726 Mon Sep 17 00:00:00 2001 From: Gautham Date: Fri, 8 Mar 2024 13:40:24 +0530 Subject: [PATCH 03/16] Rename withdrawal event --- pallets/ocex/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 2394c1fb0..589f2585d 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -1104,8 +1104,8 @@ pub mod pallet { TokenAllowlisted(AssetId), /// AllowlistedTokenRemoved AllowlistedTokenRemoved(AssetId), - /// Withdrawal failed - WithdrawalFailed(u64, Withdrawal), + /// Withdrawal ready to claim + WithdrawalReady(u64, Withdrawal), /// Exchange state has been updated ExchangeStateUpdated(bool), /// DisputePeriod has been updated @@ -1312,7 +1312,7 @@ pub mod pallet { } else { // Storing the failed withdrawals back into the storage item failed_withdrawals.push(withdrawal.to_owned()); - Self::deposit_event(Event::WithdrawalFailed( + Self::deposit_event(Event::WithdrawalReady( snapshot_id, withdrawal.to_owned(), )); From 7d5ce4f81255e13fc8948259d75e342722f3b758 Mon Sep 17 00:00:00 2001 From: zktony Date: Fri, 8 Mar 2024 15:20:39 +0530 Subject: [PATCH 04/16] Added OBWithdrawalLimit const --- pallets/liquidity-mining/src/mock.rs | 2 ++ pallets/ocex/src/benchmarking.rs | 5 ++++- pallets/ocex/src/lib.rs | 8 ++++++++ pallets/ocex/src/mock.rs | 2 ++ runtimes/mainnet/src/lib.rs | 2 ++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pallets/liquidity-mining/src/mock.rs b/pallets/liquidity-mining/src/mock.rs index 7d8630f14..698d7c4a1 100644 --- a/pallets/liquidity-mining/src/mock.rs +++ b/pallets/liquidity-mining/src/mock.rs @@ -121,6 +121,7 @@ parameter_types! { pub const TresuryPalletId: PalletId = PalletId(*b"OCEX_TRE"); pub const LMPRewardsPalletId: PalletId = PalletId(*b"OCEX_TMP"); pub const MsPerDay: u64 = 86_400_000; + pub const OBWithdrawalLimit: u32 = 50; } impl crate::pallet::Config for Test { @@ -142,6 +143,7 @@ impl ocex::Config for Test { type AuthorityId = ocex::sr25519::AuthorityId; type GovernanceOrigin = EnsureRoot; type CrowdSourceLiqudityMining = LiqudityMining; + type OBWithdrawalLimit = OBWithdrawalLimit; type WeightInfo = ocex::weights::WeightInfo; } diff --git a/pallets/ocex/src/benchmarking.rs b/pallets/ocex/src/benchmarking.rs index b0ef15e11..7a0486b8a 100644 --- a/pallets/ocex/src/benchmarking.rs +++ b/pallets/ocex/src/benchmarking.rs @@ -311,6 +311,7 @@ benchmarks! { }: _(RawOrigin::Signed(main.clone()), x as u64, main.clone()) verify { assert_last_event::(Event::WithdrawalClaimed { + snapshot_id: x as u64, main, withdrawals: vec_withdrawals, }.into()); @@ -572,7 +573,9 @@ fn create_trade_metrics() -> TradingPairMetricsMap { fn get_dummy_snapshot() -> SnapshotSummary { let mut withdrawals = Vec::new(); - for _ in 0..20 { + let pallet_account = Ocex::::get_pallet_account(); + T::NativeCurrency::deposit_creating(&pallet_account, (1000u128 * UNIT_BALANCE).saturated_into()); + for _ in 0..50 { withdrawals.push(Withdrawal { main_account: T::AccountId::decode(&mut &[0u8; 32][..]).unwrap(), amount: Decimal::one(), diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 589f2585d..2ae8fb3a7 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -243,6 +243,10 @@ pub mod pallet { #[pallet::constant] type LMPRewardsPalletId: Get; + /// Orderbook withdrawal Limit + #[pallet::constant] + type OBWithdrawalLimit: Get; + /// Balances Pallet type NativeCurrency: Currency + ReservableCurrency @@ -2132,6 +2136,10 @@ impl>> Pallet>::get(snapshot_summary.validator_set_id).validators; diff --git a/pallets/ocex/src/mock.rs b/pallets/ocex/src/mock.rs index 869e97684..9c1380fba 100644 --- a/pallets/ocex/src/mock.rs +++ b/pallets/ocex/src/mock.rs @@ -125,6 +125,7 @@ parameter_types! { pub const TreasuryPalletId: PalletId = PalletId(*b"OCEX_TRS"); //pub const TreasuryPalletId: PalletId = PalletId(*b"OCEX_CRW"); pub const MsPerDay: u64 = 86_400_000; + pub const OBWithdrawalLimit: u32 = 50; } impl pallet_lmp::pallet::Config for Test { @@ -147,6 +148,7 @@ impl Config for Test { type GovernanceOrigin = EnsureRoot; type CrowdSourceLiqudityMining = LiqudityMining; type WeightInfo = crate::weights::WeightInfo; + type OBWithdrawalLimit = OBWithdrawalLimit; } parameter_types! { diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index b7d49be64..30a8ef0c1 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -1285,6 +1285,7 @@ parameter_types! { pub const OcexPalletId: PalletId = PalletId(*b"OCEX_LMP"); pub const LMPRewardsPalletId: PalletId = PalletId(*b"LMPREWAR"); pub const MsPerDay: u64 = 86_400_000; + pub const OBWithdrawalLimit: u32 = 50; } impl pallet_ocex_lmp::Config for Runtime { @@ -1298,6 +1299,7 @@ impl pallet_ocex_lmp::Config for Runtime { type AuthorityId = pallet_ocex_lmp::sr25519::AuthorityId; type GovernanceOrigin = EnsureRootOrHalfCouncil; type CrowdSourceLiqudityMining = (); + type OBWithdrawalLimit = OBWithdrawalLimit; type WeightInfo = pallet_ocex_lmp::weights::WeightInfo; } From 49151888fa0be8334039a40a64aa5af15b5d60dd Mon Sep 17 00:00:00 2001 From: Serhii Temchenko Date: Mon, 11 Mar 2024 01:29:30 -0700 Subject: [PATCH 05/16] OCEX pallet weights update --- pallets/ocex/src/weights.rs | 130 +++++++++++++++++------------------- 1 file changed, 61 insertions(+), 69 deletions(-) diff --git a/pallets/ocex/src/weights.rs b/pallets/ocex/src/weights.rs index d209e9ee7..6d25e7a0f 100644 --- a/pallets/ocex/src/weights.rs +++ b/pallets/ocex/src/weights.rs @@ -1,7 +1,7 @@ //! Autogenerated weights for `pallet_ocex_lmp` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-03-05, STEPS: `100`, REPEAT: `200`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-03-11, STEPS: `100`, REPEAT: `200`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `ip-172-31-9-163`, CPU: `AMD EPYC 7571` //! WASM-EXECUTION: `Compiled`, CHAIN: `None`, DB CACHE: 1024 @@ -41,15 +41,13 @@ impl crate::OcexWeightInfo for WeightInfo { /// Storage: `OCEX::IngressMessages` (r:1 w:1) /// Proof: `OCEX::IngressMessages` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `b` is `[0, 255]`. - fn register_main_account(b: u32, ) -> Weight { + fn register_main_account(_b: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `167` // Estimated: `3632` - // Minimum execution time: 62_580_000 picoseconds. - Weight::from_parts(64_219_291, 0) + // Minimum execution time: 63_460_000 picoseconds. + Weight::from_parts(65_286_637, 0) .saturating_add(Weight::from_parts(0, 3632)) - // Standard Error: 51 - .saturating_add(Weight::from_parts(2_279, 0).saturating_mul(b.into())) .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(3)) } @@ -62,15 +60,13 @@ impl crate::OcexWeightInfo for WeightInfo { /// Storage: `OCEX::IngressMessages` (r:1 w:1) /// Proof: `OCEX::IngressMessages` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `x` is `[0, 255]`. - fn add_proxy_account(x: u32, ) -> Weight { + fn add_proxy_account(_x: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `499` // Estimated: `3964` - // Minimum execution time: 77_011_000 picoseconds. - Weight::from_parts(79_608_430, 0) + // Minimum execution time: 78_230_000 picoseconds. + Weight::from_parts(80_411_838, 0) .saturating_add(Weight::from_parts(0, 3964)) - // Standard Error: 49 - .saturating_add(Weight::from_parts(160, 0).saturating_mul(x.into())) .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(3)) } @@ -81,13 +77,15 @@ impl crate::OcexWeightInfo for WeightInfo { /// Storage: `OCEX::IngressMessages` (r:1 w:1) /// Proof: `OCEX::IngressMessages` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `x` is `[1, 50000]`. - fn close_trading_pair(_x: u32, ) -> Weight { + fn close_trading_pair(x: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `359` // Estimated: `3824` - // Minimum execution time: 60_581_000 picoseconds. - Weight::from_parts(62_177_551, 0) + // Minimum execution time: 60_730_000 picoseconds. + Weight::from_parts(62_754_948, 0) .saturating_add(Weight::from_parts(0, 3824)) + // Standard Error: 0 + .saturating_add(Weight::from_parts(1, 0).saturating_mul(x.into())) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().writes(2)) } @@ -102,8 +100,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `359` // Estimated: `3824` - // Minimum execution time: 60_630_000 picoseconds. - Weight::from_parts(62_321_908, 0) + // Minimum execution time: 61_070_000 picoseconds. + Weight::from_parts(62_942_837, 0) .saturating_add(Weight::from_parts(0, 3824)) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().writes(2)) @@ -121,8 +119,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `224` // Estimated: `6164` - // Minimum execution time: 72_580_000 picoseconds. - Weight::from_parts(74_688_581, 0) + // Minimum execution time: 73_301_000 picoseconds. + Weight::from_parts(75_404_320, 0) .saturating_add(Weight::from_parts(0, 6164)) .saturating_add(T::DbWeight::get().reads(5)) .saturating_add(T::DbWeight::get().writes(2)) @@ -138,8 +136,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `388` // Estimated: `3853` - // Minimum execution time: 68_600_000 picoseconds. - Weight::from_parts(70_833_615, 0) + // Minimum execution time: 69_190_000 picoseconds. + Weight::from_parts(71_151_733, 0) .saturating_add(Weight::from_parts(0, 3853)) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().writes(2)) @@ -159,13 +157,15 @@ impl crate::OcexWeightInfo for WeightInfo { /// Storage: `OCEX::IngressMessages` (r:1 w:1) /// Proof: `OCEX::IngressMessages` (`max_values`: None, `max_size`: None, mode: `Measured`) /// The range of component `x` is `[1, 255]`. - fn deposit(_x: u32, ) -> Weight { + fn deposit(x: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `773` // Estimated: `6232` - // Minimum execution time: 158_171_000 picoseconds. - Weight::from_parts(161_592_723, 0) + // Minimum execution time: 159_980_000 picoseconds. + Weight::from_parts(163_099_371, 0) .saturating_add(Weight::from_parts(0, 6232)) + // Standard Error: 81 + .saturating_add(Weight::from_parts(620, 0).saturating_mul(x.into())) .saturating_add(T::DbWeight::get().reads(8)) .saturating_add(T::DbWeight::get().writes(4)) } @@ -182,50 +182,44 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `462` // Estimated: `3927` - // Minimum execution time: 65_600_000 picoseconds. - Weight::from_parts(67_673_877, 0) + // Minimum execution time: 66_520_000 picoseconds. + Weight::from_parts(68_848_667, 0) .saturating_add(Weight::from_parts(0, 3927)) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().writes(3)) } /// Storage: `OCEX::FinalizeLMPScore` (r:1 w:0) /// Proof: `OCEX::FinalizeLMPScore` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) - /// Storage: `System::Account` (r:1 w:1) + /// Storage: `System::Account` (r:2 w:2) /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) - /// Storage: `OCEX::OnChainEvents` (r:1 w:1) - /// Proof: `OCEX::OnChainEvents` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) /// Storage: `OCEX::IngressMessages` (r:1 w:1) /// Proof: `OCEX::IngressMessages` (`max_values`: None, `max_size`: None, mode: `Measured`) - /// Storage: `OCEX::Withdrawals` (r:0 w:1) - /// Proof: `OCEX::Withdrawals` (`max_values`: None, `max_size`: None, mode: `Measured`) /// Storage: `OCEX::Snapshots` (r:0 w:1) /// Proof: `OCEX::Snapshots` (`max_values`: None, `max_size`: None, mode: `Measured`) /// Storage: `OCEX::SnapshotNonce` (r:0 w:1) /// Proof: `OCEX::SnapshotNonce` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) fn submit_snapshot() -> Weight { // Proof Size summary in bytes: - // Measured: `333` - // Estimated: `3798` - // Minimum execution time: 1_430_335_000 picoseconds. - Weight::from_parts(1_461_555_000, 0) - .saturating_add(Weight::from_parts(0, 3798)) + // Measured: `359` + // Estimated: `6196` + // Minimum execution time: 7_064_595_000 picoseconds. + Weight::from_parts(7_134_556_000, 0) + .saturating_add(Weight::from_parts(0, 6196)) .saturating_add(T::DbWeight::get().reads(4)) - .saturating_add(T::DbWeight::get().writes(6)) + .saturating_add(T::DbWeight::get().writes(5)) } /// Storage: `OCEX::IngressMessages` (r:1 w:1) /// Proof: `OCEX::IngressMessages` (`max_values`: None, `max_size`: None, mode: `Measured`) /// Storage: `OCEX::ExchangeState` (r:0 w:1) /// Proof: `OCEX::ExchangeState` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) /// The range of component `x` is `[0, 100000]`. - fn set_exchange_state(x: u32, ) -> Weight { + fn set_exchange_state(_x: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `167` // Estimated: `3632` - // Minimum execution time: 38_190_000 picoseconds. - Weight::from_parts(39_450_923, 0) + // Minimum execution time: 39_121_000 picoseconds. + Weight::from_parts(40_594_450, 0) .saturating_add(Weight::from_parts(0, 3632)) - // Standard Error: 0 - .saturating_add(Weight::from_parts(1, 0).saturating_mul(x.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(2)) } @@ -235,20 +229,18 @@ impl crate::OcexWeightInfo for WeightInfo { /// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(222), added: 2697, mode: `MaxEncodedLen`) /// Storage: `Assets::Account` (r:2 w:1) /// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(146), added: 2621, mode: `MaxEncodedLen`) - /// Storage: `OCEX::OnChainEvents` (r:1 w:1) - /// Proof: `OCEX::OnChainEvents` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) /// The range of component `x` is `[1, 255]`. fn claim_withdraw(x: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `763` // Estimated: `6232` - // Minimum execution time: 144_320_000 picoseconds. - Weight::from_parts(147_449_100, 0) + // Minimum execution time: 137_620_000 picoseconds. + Weight::from_parts(140_722_186, 0) .saturating_add(Weight::from_parts(0, 6232)) - // Standard Error: 77 - .saturating_add(Weight::from_parts(202, 0).saturating_mul(x.into())) - .saturating_add(T::DbWeight::get().reads(5)) - .saturating_add(T::DbWeight::get().writes(4)) + // Standard Error: 92 + .saturating_add(Weight::from_parts(674, 0).saturating_mul(x.into())) + .saturating_add(T::DbWeight::get().reads(4)) + .saturating_add(T::DbWeight::get().writes(3)) } /// Storage: `OCEX::AllowlistedToken` (r:1 w:1) /// Proof: `OCEX::AllowlistedToken` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) @@ -257,8 +249,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `167` // Estimated: `1652` - // Minimum execution time: 33_740_000 picoseconds. - Weight::from_parts(35_028_116, 0) + // Minimum execution time: 34_680_000 picoseconds. + Weight::from_parts(36_072_035, 0) .saturating_add(Weight::from_parts(0, 1652)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -270,8 +262,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `205` // Estimated: `1690` - // Minimum execution time: 36_640_000 picoseconds. - Weight::from_parts(38_065_860, 0) + // Minimum execution time: 37_571_000 picoseconds. + Weight::from_parts(39_138_792, 0) .saturating_add(Weight::from_parts(0, 1690)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -282,8 +274,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 10_740_000 picoseconds. - Weight::from_parts(11_160_000, 0) + // Minimum execution time: 11_410_000 picoseconds. + Weight::from_parts(12_000_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -293,8 +285,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 24_290_000 picoseconds. - Weight::from_parts(25_000_000, 0) + // Minimum execution time: 25_220_000 picoseconds. + Weight::from_parts(26_080_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -304,7 +296,7 @@ impl crate::OcexWeightInfo for WeightInfo { /// Proof: `OCEX::LMPConfig` (`max_values`: None, `max_size`: None, mode: `Measured`) /// Storage: `OCEX::TotalScores` (r:1 w:0) /// Proof: `OCEX::TotalScores` (`max_values`: None, `max_size`: None, mode: `Measured`) - /// Storage: `OCEX::TraderMetrics` (r:1 w:0) + /// Storage: `OCEX::TraderMetrics` (r:1 w:1) /// Proof: `OCEX::TraderMetrics` (`max_values`: None, `max_size`: None, mode: `Measured`) /// Storage: `System::Account` (r:2 w:2) /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) @@ -312,11 +304,11 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `1268` // Estimated: `6196` - // Minimum execution time: 197_880_000 picoseconds. - Weight::from_parts(199_690_000, 0) + // Minimum execution time: 198_370_000 picoseconds. + Weight::from_parts(200_581_000, 0) .saturating_add(Weight::from_parts(0, 6196)) .saturating_add(T::DbWeight::get().reads(6)) - .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(T::DbWeight::get().writes(3)) } /// Storage: `OCEX::ExpectedLMPConfig` (r:1 w:1) /// Proof: `OCEX::ExpectedLMPConfig` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) @@ -328,8 +320,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `419` // Estimated: `3884` - // Minimum execution time: 49_100_000 picoseconds. - Weight::from_parts(49_951_000, 0) + // Minimum execution time: 49_430_000 picoseconds. + Weight::from_parts(50_431_000, 0) .saturating_add(Weight::from_parts(0, 3884)) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().writes(2)) @@ -340,8 +332,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 11_680_000 picoseconds. - Weight::from_parts(12_120_000, 0) + // Minimum execution time: 12_300_000 picoseconds. + Weight::from_parts(12_940_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -353,8 +345,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `326` // Estimated: `3593` - // Minimum execution time: 81_830_000 picoseconds. - Weight::from_parts(83_280_000, 0) + // Minimum execution time: 82_880_000 picoseconds. + Weight::from_parts(83_970_000, 0) .saturating_add(Weight::from_parts(0, 3593)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(2)) @@ -377,8 +369,8 @@ impl crate::OcexWeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `948` // Estimated: `6384` - // Minimum execution time: 92_200_000 picoseconds. - Weight::from_parts(93_700_000, 0) + // Minimum execution time: 91_651_000 picoseconds. + Weight::from_parts(93_500_000, 0) .saturating_add(Weight::from_parts(0, 6384)) .saturating_add(T::DbWeight::get().reads(9)) .saturating_add(T::DbWeight::get().writes(2)) From 4149cd35ef04d63e90873b4fc6534d18d44c948e Mon Sep 17 00:00:00 2001 From: Gautham Date: Tue, 12 Mar 2024 18:39:34 +0700 Subject: [PATCH 06/16] Add system accounts to get_ob_checkpoint --- pallets/ocex/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 3edd347fd..ab8077b21 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -1460,6 +1460,10 @@ pub mod pallet { FEE_POT_PALLET_ID.into_account_truncating() } + pub fn get_system_accounts() -> Vec { + vec![Self::get_pallet_account(), Self::get_pot_account()] + } + pub fn process_egress_msg(msgs: &Vec>) -> DispatchResult { for msg in msgs { // Process egress messages @@ -1803,12 +1807,18 @@ pub mod pallet { ), DispatchError, > { - let account_id = + let mut account_id = >::iter().fold(vec![], |mut ids_accum, (acc, acc_info)| { ids_accum.push((acc.clone(), acc_info.proxies)); ids_accum }); + let system_accounts = Self::get_system_accounts(); + + for account in system_accounts { + account_id.push((account, BoundedVec::new())); + } + let mut balances: BTreeMap = BTreeMap::new(); let mut account_ids: BTreeMap> = BTreeMap::new(); // all offchain balances for main accounts From 2f25683287d4db7c96783c760da8d32856231422 Mon Sep 17 00:00:00 2001 From: Gautham Date: Wed, 13 Mar 2024 11:34:19 +0700 Subject: [PATCH 07/16] Add event for claimed rewards --- pallets/ocex/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index ab8077b21..e510939a0 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -1145,6 +1145,13 @@ pub mod pallet { }, /// LMP Scores updated LMPScoresUpdated(u16), + /// LMP Reward Claimed + LMPRewardClaimed{ + epoch: u16, + market: TradingPair, + main: T::AccountId, + reward: u128 + } } ///Allowlisted tokens @@ -1347,6 +1354,13 @@ pub mod pallet { >::mutate((epoch, market, main.clone()), |(_, _, is_claimed)| { *is_claimed = true; }); + Self::deposit_event(Event::::LMPRewardClaimed{ + epoch, + main, + market, + reward: total_in_u128.saturated_into() + + }); Ok(total_in_u128) } From 2855b824f3e524e47889bdccbf51edda5631324d Mon Sep 17 00:00:00 2001 From: Gautham Date: Wed, 13 Mar 2024 12:13:41 +0700 Subject: [PATCH 08/16] Clippy --- pallets/ocex/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/ocex/src/benchmarking.rs b/pallets/ocex/src/benchmarking.rs index 7a0486b8a..bb492d1b6 100644 --- a/pallets/ocex/src/benchmarking.rs +++ b/pallets/ocex/src/benchmarking.rs @@ -574,7 +574,7 @@ fn create_trade_metrics() -> TradingPairMetricsMap { fn get_dummy_snapshot() -> SnapshotSummary { let mut withdrawals = Vec::new(); let pallet_account = Ocex::::get_pallet_account(); - T::NativeCurrency::deposit_creating(&pallet_account, (1000u128 * UNIT_BALANCE).saturated_into()); + let _imbalance = T::NativeCurrency::deposit_creating(&pallet_account, (1000u128 * UNIT_BALANCE).saturated_into()); for _ in 0..50 { withdrawals.push(Withdrawal { main_account: T::AccountId::decode(&mut &[0u8; 32][..]).unwrap(), From 5c73265a3b369c0b505e8a9c2bec4f0fd56d4b32 Mon Sep 17 00:00:00 2001 From: Gautham Date: Wed, 13 Mar 2024 12:16:26 +0700 Subject: [PATCH 09/16] Cargo fmt --- pallets/ocex/src/benchmarking.rs | 5 ++++- pallets/ocex/src/lib.rs | 11 +++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pallets/ocex/src/benchmarking.rs b/pallets/ocex/src/benchmarking.rs index bb492d1b6..ea5e5fe33 100644 --- a/pallets/ocex/src/benchmarking.rs +++ b/pallets/ocex/src/benchmarking.rs @@ -574,7 +574,10 @@ fn create_trade_metrics() -> TradingPairMetricsMap { fn get_dummy_snapshot() -> SnapshotSummary { let mut withdrawals = Vec::new(); let pallet_account = Ocex::::get_pallet_account(); - let _imbalance = T::NativeCurrency::deposit_creating(&pallet_account, (1000u128 * UNIT_BALANCE).saturated_into()); + let _imbalance = T::NativeCurrency::deposit_creating( + &pallet_account, + (1000u128 * UNIT_BALANCE).saturated_into(), + ); for _ in 0..50 { withdrawals.push(Withdrawal { main_account: T::AccountId::decode(&mut &[0u8; 32][..]).unwrap(), diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 7ee0a04b7..006caa178 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -1136,12 +1136,12 @@ pub mod pallet { /// LMP Scores updated LMPScoresUpdated(u16), /// LMP Reward Claimed - LMPRewardClaimed{ + LMPRewardClaimed { epoch: u16, market: TradingPair, main: T::AccountId, - reward: u128 - } + reward: u128, + }, } ///Allowlisted tokens @@ -1370,12 +1370,11 @@ pub mod pallet { >::mutate((epoch, market, main.clone()), |(_, _, is_claimed)| { *is_claimed = true; }); - Self::deposit_event(Event::::LMPRewardClaimed{ + Self::deposit_event(Event::::LMPRewardClaimed { epoch, main, market, - reward: total_in_u128.saturated_into() - + reward: total_in_u128.saturated_into(), }); Ok(total_in_u128) } From dcdcd05de54d7772d17309deb16db31f491f22bb Mon Sep 17 00:00:00 2001 From: Gautham Date: Thu, 14 Mar 2024 00:01:23 +0700 Subject: [PATCH 10/16] Update the correct spec. --- primitives/orderbook/src/types.rs | 6 +++--- runtimes/mainnet/src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/primitives/orderbook/src/types.rs b/primitives/orderbook/src/types.rs index 5e85119ea..c9d0443e4 100644 --- a/primitives/orderbook/src/types.rs +++ b/primitives/orderbook/src/types.rs @@ -155,10 +155,10 @@ impl Trade { /// * `config`: Trading pair configuration DTO. pub fn verify(&self, config: TradingPairConfig) -> bool { // Verify signatures - self.maker.verify_signature() & - self.taker.verify_signature() & + self.maker.verify_signature() && + self.taker.verify_signature() && // Verify pair configs - self.maker.verify_config(&config) & + self.maker.verify_config(&config) && self.taker.verify_config(&config) } diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index 30a8ef0c1..06536277b 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -122,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 339, + spec_version: 331, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, From bf7da0edd7442ba85b221489c6528846de7af873 Mon Sep 17 00:00:00 2001 From: Gautham Date: Thu, 14 Mar 2024 00:06:15 +0700 Subject: [PATCH 11/16] Remove PDEXHolderAccount --- runtimes/mainnet/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index 06536277b..61b8e612c 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -1334,7 +1334,6 @@ use polkadex_primitives::POLKADEX_NATIVE_ASSET_ID; parameter_types! { pub const PolkadexAssetId: u128 = POLKADEX_NATIVE_ASSET_ID; - pub const PDEXHolderAccount: AccountId32 = AccountId32::new([1u8;32]); //TODO Chnage Holder Account } impl thea::Config for Runtime { From 10bec2ad8f4c2cf780d04169534792c6f01c95ab Mon Sep 17 00:00:00 2001 From: Gautham Date: Thu, 14 Mar 2024 00:14:29 +0700 Subject: [PATCH 12/16] remove unused imports --- runtimes/mainnet/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index 61b8e612c..e1bfa28de 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -258,7 +258,6 @@ parameter_types! { pub const MaxPending: u16 = 32; } use scale_info::TypeInfo; -use sp_core::crypto::AccountId32; use sp_npos_elections::ExtendedBalance; /// The type used to represent the kinds of proxying allowed. From 7bb6987746d41424ddf299d9d06fedf87acbd0c4 Mon Sep 17 00:00:00 2001 From: Gautham Date: Thu, 14 Mar 2024 03:37:25 +0700 Subject: [PATCH 13/16] update DNS --- pallets/ocex/src/validator.rs | 2 +- runtimes/mainnet/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/ocex/src/validator.rs b/pallets/ocex/src/validator.rs index 129f9a1bb..c24cb37d2 100644 --- a/pallets/ocex/src/validator.rs +++ b/pallets/ocex/src/validator.rs @@ -66,7 +66,7 @@ pub const LAST_PROCESSED_SNAPSHOT: [u8; 26] = *b"offchain-ocex::snapshot_id"; /// As a future improvment, we can make it decentralized, by having the community run /// such aggregation endpoints -pub const AGGREGATOR: &str = "https://aggregator.polkadex.trade"; // Updated to production URL +pub const AGGREGATOR: &str = "https://ob.aggregator.polkadex.trade"; // Updated to production URL pub const CHECKPOINT_BLOCKS: u64 = 1260; type TraderMetricsType = BTreeMap< diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index e1bfa28de..7dc613e53 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -122,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 331, + spec_version: 332, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, From 9be74e73397f6b6cf271dc591e24a5d55683fefc Mon Sep 17 00:00:00 2001 From: Gautham Date: Thu, 14 Mar 2024 13:45:47 +0700 Subject: [PATCH 14/16] Re-org LMP config storage --- pallets/ocex/src/lib.rs | 16 +++++++++++----- pallets/ocex/src/lmp.rs | 11 +++++++++-- pallets/ocex/src/session.rs | 6 ++++-- pallets/ocex/src/validator.rs | 25 +++++++++---------------- runtimes/mainnet/src/lib.rs | 2 +- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 006caa178..1a143f51a 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -1008,11 +1008,6 @@ pub mod pallet { } ensure!(config.verify(), Error::::InvalidLMPConfig); >::put(config.clone()); - let current_blk = frame_system::Pallet::::current_block_number(); - >::mutate(current_blk, |ingress_messages| { - ingress_messages - .push(orderbook_primitives::ingress::IngressMessages::LMPConfig(config)) - }); Ok(()) } @@ -1050,6 +1045,17 @@ pub mod pallet { >::put(auction_info); Ok(()) } + + + /// Starts a new liquidity mining epoch + #[pallet::call_index(23)] + #[pallet::weight(< T as Config >::WeightInfo::set_fee_distribution())] + pub fn start_new_epoch_lmp(origin: OriginFor) -> DispatchResult { + ensure_root(origin)?; + let current_blk = frame_system::Pallet::::current_block_number(); + Self::start_new_epoch(current_blk); + Ok(()) + } } /// Events are a simple means of reporting specific conditions and diff --git a/pallets/ocex/src/lmp.rs b/pallets/ocex/src/lmp.rs index 5bd2bd649..d1bd5e92c 100644 --- a/pallets/ocex/src/lmp.rs +++ b/pallets/ocex/src/lmp.rs @@ -209,10 +209,17 @@ pub fn get_fees_paid_by_main_account_in_quote( } /// Returns the LMP configuration from offchain work state trie -pub fn get_lmp_config(state: &mut OffchainState) -> Result { +pub fn get_lmp_config(state: &mut OffchainState, current_on_chain_epoch: u16) -> Result { let key = LMP_CONFIG_KEY.encode(); Ok(match state.get(&key)? { - None => return Err("LMPConfigNotFound"), + None => { + if current_on_chain_epoch == 0 { + let config = LMPConfig{ epoch: current_on_chain_epoch, index: 0 }; + store_lmp_config(state,config); + return Ok(config) + } + return Err("LMPConfigNotFound"); + }, Some(encoded_config) => LMPConfig::decode(&mut &encoded_config[..]) .map_err(|_| "Unable to decode LMP config")?, }) diff --git a/pallets/ocex/src/session.rs b/pallets/ocex/src/session.rs index 4e2e25d51..8d62b825c 100644 --- a/pallets/ocex/src/session.rs +++ b/pallets/ocex/src/session.rs @@ -45,14 +45,16 @@ impl Pallet { } current_epoch = current_epoch.saturating_add(1); >::put(current_epoch); - >::insert(current_epoch, config); + >::insert(current_epoch, config.clone()); // Notify Liquidity Crowd sourcing pallet about new epoch T::CrowdSourceLiqudityMining::new_epoch(current_epoch); >::mutate(n, |ingress_messages| { ingress_messages.push(orderbook_primitives::ingress::IngressMessages::NewLMPEpoch( current_epoch, - )) + )); + ingress_messages + .push(orderbook_primitives::ingress::IngressMessages::LMPConfig(config)) }); } } diff --git a/pallets/ocex/src/validator.rs b/pallets/ocex/src/validator.rs index c24cb37d2..cafa639c4 100644 --- a/pallets/ocex/src/validator.rs +++ b/pallets/ocex/src/validator.rs @@ -56,6 +56,7 @@ use sp_runtime::{ }; use sp_std::{borrow::ToOwned, boxed::Box, collections::btree_map::BTreeMap, vec::Vec}; use trie_db::{TrieError, TrieMut}; +use crate::pallet::LMPEpoch; /// Key of the storage that stores the status of an offchain worker pub const WORKER_STATUS: [u8; 28] = *b"offchain-ocex::worker_status"; @@ -634,7 +635,7 @@ impl Pallet { /// Reset the offchain state's LMP index and set the epoch fn start_new_lmp_epoch(state: &mut OffchainState, epoch: u16) -> Result<(), &'static str> { let mut config = if epoch > 1 { - get_lmp_config(state)? + get_lmp_config(state, epoch)? } else { // To Handle the corner case of zero orderbook_primitives::lmp::LMPConfig { epoch, index: 0 } @@ -731,7 +732,8 @@ impl Pallet { withdrawals.push(withdrawal); }, UserActions::OneMinLMPReport(market, _total, scores) => { - Self::store_q_scores(state, *market, scores)?; + let current_on_chain_epoch = >::get(); + Self::store_q_scores(state, *market, scores,current_on_chain_epoch)?; }, } } @@ -744,8 +746,9 @@ impl Pallet { state: &mut OffchainState, market: TradingPair, scores: &BTreeMap, + current_on_chain_epoch: u16 ) -> Result<(), &'static str> { - let mut config = get_lmp_config(state)?; + let mut config = get_lmp_config(state,current_on_chain_epoch)?; let next_index = config.index.saturating_add(1); for (main, score) in scores { store_q_score_and_uptime( @@ -921,21 +924,11 @@ impl Pallet { /// Returns the FeeConfig from runtime for maker and taker pub fn get_fee_structure( - maker: &T::AccountId, - taker: &T::AccountId, + _maker: &T::AccountId, + _taker: &T::AccountId, ) -> Option<(FeeConfig, FeeConfig)> { // TODO: Read this from offchain state to avoid a race condition - let maker_config = match >::get(maker) { - None => return None, - Some(x) => x.fee_config, - }; - - let taker_config = match >::get(taker) { - None => return None, - Some(x) => x.fee_config, - }; - - Some((maker_config, taker_config)) + Some((Default::default(), Default::default())) } fn convert_account_id(acc: &AccountId) -> Result { diff --git a/runtimes/mainnet/src/lib.rs b/runtimes/mainnet/src/lib.rs index 7dc613e53..510c05536 100644 --- a/runtimes/mainnet/src/lib.rs +++ b/runtimes/mainnet/src/lib.rs @@ -122,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 332, + spec_version: 334, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 2, From cdc8455617156fb60cda002c50a447fdd3212ca0 Mon Sep 17 00:00:00 2001 From: gautham Date: Thu, 14 Mar 2024 12:28:03 +0530 Subject: [PATCH 15/16] cargo fmt --- pallets/ocex/src/lib.rs | 1 - pallets/ocex/src/lmp.rs | 11 +++++++---- pallets/ocex/src/validator.rs | 8 ++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pallets/ocex/src/lib.rs b/pallets/ocex/src/lib.rs index 1a143f51a..23afc3383 100644 --- a/pallets/ocex/src/lib.rs +++ b/pallets/ocex/src/lib.rs @@ -1046,7 +1046,6 @@ pub mod pallet { Ok(()) } - /// Starts a new liquidity mining epoch #[pallet::call_index(23)] #[pallet::weight(< T as Config >::WeightInfo::set_fee_distribution())] diff --git a/pallets/ocex/src/lmp.rs b/pallets/ocex/src/lmp.rs index d1bd5e92c..9dda18e12 100644 --- a/pallets/ocex/src/lmp.rs +++ b/pallets/ocex/src/lmp.rs @@ -209,14 +209,17 @@ pub fn get_fees_paid_by_main_account_in_quote( } /// Returns the LMP configuration from offchain work state trie -pub fn get_lmp_config(state: &mut OffchainState, current_on_chain_epoch: u16) -> Result { +pub fn get_lmp_config( + state: &mut OffchainState, + current_on_chain_epoch: u16, +) -> Result { let key = LMP_CONFIG_KEY.encode(); Ok(match state.get(&key)? { None => { if current_on_chain_epoch == 0 { - let config = LMPConfig{ epoch: current_on_chain_epoch, index: 0 }; - store_lmp_config(state,config); - return Ok(config) + let config = LMPConfig { epoch: current_on_chain_epoch, index: 0 }; + store_lmp_config(state, config); + return Ok(config); } return Err("LMPConfigNotFound"); }, diff --git a/pallets/ocex/src/validator.rs b/pallets/ocex/src/validator.rs index cafa639c4..4c5b64279 100644 --- a/pallets/ocex/src/validator.rs +++ b/pallets/ocex/src/validator.rs @@ -17,6 +17,7 @@ // along with this program. If not, see . use crate::lmp::{get_lmp_config, get_total_maker_volume, store_lmp_config}; +use crate::pallet::LMPEpoch; use crate::{ aggregator::AggregatorClient, lmp::{ @@ -56,7 +57,6 @@ use sp_runtime::{ }; use sp_std::{borrow::ToOwned, boxed::Box, collections::btree_map::BTreeMap, vec::Vec}; use trie_db::{TrieError, TrieMut}; -use crate::pallet::LMPEpoch; /// Key of the storage that stores the status of an offchain worker pub const WORKER_STATUS: [u8; 28] = *b"offchain-ocex::worker_status"; @@ -733,7 +733,7 @@ impl Pallet { }, UserActions::OneMinLMPReport(market, _total, scores) => { let current_on_chain_epoch = >::get(); - Self::store_q_scores(state, *market, scores,current_on_chain_epoch)?; + Self::store_q_scores(state, *market, scores, current_on_chain_epoch)?; }, } } @@ -746,9 +746,9 @@ impl Pallet { state: &mut OffchainState, market: TradingPair, scores: &BTreeMap, - current_on_chain_epoch: u16 + current_on_chain_epoch: u16, ) -> Result<(), &'static str> { - let mut config = get_lmp_config(state,current_on_chain_epoch)?; + let mut config = get_lmp_config(state, current_on_chain_epoch)?; let next_index = config.index.saturating_add(1); for (main, score) in scores { store_q_score_and_uptime( From fed4cf597a7aa74882e1b124eb42208a7368a2a5 Mon Sep 17 00:00:00 2001 From: gautham Date: Thu, 14 Mar 2024 13:08:47 +0530 Subject: [PATCH 16/16] update tests --- pallets/ocex/src/tests.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pallets/ocex/src/tests.rs b/pallets/ocex/src/tests.rs index 33555c8c0..291a4bacd 100644 --- a/pallets/ocex/src/tests.rs +++ b/pallets/ocex/src/tests.rs @@ -487,8 +487,8 @@ fn test_trade_between_two_accounts_with_balance() { // add balance to alice let alice_account_id = get_alice_key_pair().public(); - let initial_asset_1_alice_has = 40; - let _initial_pdex_alice_has = 0; + let initial_asset_1_alice_has: Decimal = 40.into(); + let _initial_pdex_alice_has: Decimal = 0.into(); assert_ok!(add_balance( &mut state, &alice_account_id.into(), @@ -498,8 +498,8 @@ fn test_trade_between_two_accounts_with_balance() { //add balance to bob let bob_account_id = get_bob_key_pair().public(); - let initial_pdex_bob_has = 20; - let initial_asset_1_bob_has = 0; + let initial_pdex_bob_has: Decimal = 20.into(); + let initial_asset_1_bob_has: Decimal = 0.into(); assert_ok!(add_balance( &mut state, &bob_account_id.into(), @@ -514,7 +514,7 @@ fn test_trade_between_two_accounts_with_balance() { //alice bought 20 PDEX from bob for a price of 2 PDEX per Asset(1) // total trade value = 20 PDEX and 40 Asset(1) - //so alice should have 20 PDEX and bob should have 20 less PDEX + //so alice should have 20-0.001*20 = 19.98 PDEX and bob should have 20 less PDEX //also, alice should have 40 less Asset(1) and bob should have 40 more Asset(1) let trade = create_trade_between_alice_and_bob(price, amount); let (maker_fees, taker_fees) = @@ -525,22 +525,22 @@ fn test_trade_between_two_accounts_with_balance() { //check has 20 pdex now let encoded = state.get(&alice_account_id.0.to_vec()).unwrap().unwrap(); let account_info: BTreeMap = BTreeMap::decode(&mut &encoded[..]).unwrap(); - assert_eq!(account_info.get(&AssetId::Polkadex).unwrap(), &20.into()); + assert_eq!(account_info.get(&AssetId::Polkadex).unwrap(), &Decimal::from_f64(19.98).unwrap()); //check if bob has 20 less pdex let encoded = state.get(&bob_account_id.0.to_vec()).unwrap().unwrap(); let account_info: BTreeMap = BTreeMap::decode(&mut &encoded[..]).unwrap(); assert_eq!( account_info.get(&AssetId::Polkadex).unwrap(), - &(initial_pdex_bob_has - 20).into() + &(initial_pdex_bob_has - Decimal::from(20)).into() ); - //check if bob has 40 more asset_1 + //check if bob has 40-0.001*40 = 39.96 more asset_1 let encoded = state.get(&bob_account_id.0.to_vec()).unwrap().unwrap(); let account_info: BTreeMap = BTreeMap::decode(&mut &encoded[..]).unwrap(); assert_eq!( account_info.get(&AssetId::Asset(1)).unwrap(), - &(initial_asset_1_bob_has + 40).into() + &(initial_asset_1_bob_has.saturating_add(Decimal::from_f64(39.96).unwrap())).into() ); //check if alice has 40 less asset_1 @@ -548,7 +548,7 @@ fn test_trade_between_two_accounts_with_balance() { let account_info: BTreeMap = BTreeMap::decode(&mut &encoded[..]).unwrap(); assert_eq!( account_info.get(&AssetId::Asset(1)).unwrap(), - &(initial_asset_1_alice_has - 40).into() + &(initial_asset_1_alice_has - Decimal::from_f64(40.0).unwrap()).into() ); }); }