From ba74589c841fa198a9863738eab9991be40a8a69 Mon Sep 17 00:00:00 2001 From: akildemir Date: Tue, 28 Nov 2023 12:26:13 +0300 Subject: [PATCH] bug fixes --- Cargo.lock | 20 ++++++++++---------- substrate/dex/pallet/src/lib.rs | 8 ++++---- substrate/runtime/src/lib.rs | 2 +- substrate/validator-sets/pallet/src/lib.rs | 15 ++++++--------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 20ed5c8f4..63d105935 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9942,9 +9942,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.88" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7daec296f25a1bae309c0cd5c29c4b260e510e6d813c286b19eaadf409d40fce" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -9952,9 +9952,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.88" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e397f4664c0e4e428e8313a469aaa58310d302159845980fd23b0f22a847f217" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" dependencies = [ "bumpalo", "log", @@ -9979,9 +9979,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.88" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5961017b3b08ad5f3fe39f1e79877f8ee7c23c5e5fd5eb80de95abc41f1f16b2" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -9989,9 +9989,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.88" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", @@ -10002,9 +10002,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.88" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d046c5d029ba91a1ed14da14dca44b68bf2f124cfbaf741c54151fdb3e0750b" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" [[package]] name = "wasm-encoder" diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index b521986e0..6937d0a4e 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -164,11 +164,11 @@ pub mod pallet { #[pallet::getter(fn oracle_prices)] pub type OraclePrices = StorageMap<_, Identity, [u8; 8], u16, OptionQuery>; impl Pallet { - // TODO: make sure this is correct /// Get the highest sustained(lowest) value of the window pub fn highest_sustained_price() -> Amount { let mut iter = OraclePrices::::iter_keys(); - iter.set_last_raw_key(0u64.to_be_bytes().to_vec()); + // we can unwrap because we will always have at least 1 key in this map + // before this function is called. Amount(u64::from_be_bytes(iter.next().unwrap())) } } @@ -181,7 +181,7 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - /// A successful call of the `CretaPool` extrinsic will create this event. + /// A successful call of the `CreatePool` extrinsic will create this event. PoolCreated { /// The pool id associated with the pool. Note that the order of the coins may not be /// the same as the order specified in the create pool extrinsic. @@ -338,7 +338,7 @@ pub mod pallet { for coin in Pools::::iter_keys() { // insert the new price to our oracle window let last = Self::quote_price_exact_tokens_for_tokens(coin, Coin::native(), 1, false) - .unwrap() + .unwrap_or(0) .to_be_bytes(); LastQuoteForBlock::::set(n, coin, last); let observed = OraclePrices::::get(last).unwrap_or(0); diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index 99f9ed713..51e1da4e2 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -327,9 +327,9 @@ construct_runtime!( TransactionPayment: transaction_payment, + Dex: dex, Coins: coins, LiquidityTokens: coins::::{Pallet, Call, Storage, Event}, - Dex: dex, ValidatorSets: validator_sets, diff --git a/substrate/validator-sets/pallet/src/lib.rs b/substrate/validator-sets/pallet/src/lib.rs index 3be493d16..358377054 100644 --- a/substrate/validator-sets/pallet/src/lib.rs +++ b/substrate/validator-sets/pallet/src/lib.rs @@ -727,10 +727,11 @@ pub mod pallet { // of each session since we have to wait for that block to end to update the window and // the oracle value accordingly. let price = Dex::::oracle_value(balance.coin).unwrap_or(Amount(0)); - let total_coin_value = balance.amount.0.saturating_mul(price.0); + let mut total_coin_value = balance.amount.0.saturating_mul(price.0); // required stake formula (COIN_VALUE * 1.5) + margin(20%) - total_coin_value.saturating_mul(3).saturating_div(2).saturating_div(5) + total_coin_value = total_coin_value.saturating_mul(3).saturating_div(2); + total_coin_value.saturating_add(total_coin_value.saturating_div(5)) } /// Returns the current total required stake for a given `network`. @@ -884,13 +885,9 @@ pub mod pallet { let current_required = Self::required_stake_for_network(balance.coin.network()); let new_required = current_required.saturating_add(Self::required_stake(balance)); - // get the total stake for the network - let staked = Self::total_allocated_stake(balance.coin.network()); - if staked.is_none() { - return false; - } - - staked.unwrap().0 >= new_required + // get the total stake for the network & compare. + let staked = Self::total_allocated_stake(balance.coin.network()).unwrap_or(Amount(0)); + staked.0 >= new_required } }