From 4de4c186b190e85b306652bda2cbbf47988c1bd6 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 11 Dec 2023 07:47:23 -0500 Subject: [PATCH] Remove redundant fields from dex-pallet, add cargo machete ignores --- substrate/abi/src/dex.rs | 4 --- substrate/client/tests/dex.rs | 6 ---- substrate/coins/pallet/Cargo.toml | 5 ++- substrate/dex/pallet/Cargo.toml | 7 +++-- substrate/dex/pallet/src/benchmarking.rs | 4 +-- substrate/dex/pallet/src/lib.rs | 35 ++++----------------- substrate/dex/pallet/src/tests.rs | 13 +------- substrate/dex/pallet/src/types.rs | 10 ------ substrate/in-instructions/pallet/Cargo.toml | 3 ++ substrate/runtime/Cargo.toml | 7 +++-- substrate/signals/pallet/Cargo.toml | 3 ++ substrate/validator-sets/pallet/Cargo.toml | 3 ++ 12 files changed, 31 insertions(+), 69 deletions(-) diff --git a/substrate/abi/src/dex.rs b/substrate/abi/src/dex.rs index 5b69456a..5136e974 100644 --- a/substrate/abi/src/dex.rs +++ b/substrate/abi/src/dex.rs @@ -3,7 +3,6 @@ use sp_runtime::BoundedVec; use serai_primitives::*; type PoolId = Coin; -type PoolCoinId = Coin; type MaxSwapPathLength = sp_core::ConstU32<3>; #[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)] @@ -44,7 +43,6 @@ pub enum Event { PoolCreated { pool_id: PoolId, pool_account: SeraiAddress, - lp_token: PoolCoinId, }, LiquidityAdded { @@ -53,7 +51,6 @@ pub enum Event { pool_id: PoolId, coin_amount: SubstrateAmount, sri_amount: SubstrateAmount, - lp_token: PoolCoinId, lp_token_minted: SubstrateAmount, }, @@ -63,7 +60,6 @@ pub enum Event { pool_id: PoolId, coin_amount: SubstrateAmount, sri_amount: SubstrateAmount, - lp_token: PoolCoinId, lp_token_burned: SubstrateAmount, }, diff --git a/substrate/client/tests/dex.rs b/substrate/client/tests/dex.rs index 5b096adf..8796fe0b 100644 --- a/substrate/client/tests/dex.rs +++ b/substrate/client/tests/dex.rs @@ -38,22 +38,18 @@ serai_test!( DexEvent::PoolCreated { pool_id: Coin::Bitcoin, pool_account: PublicKey::from_raw(blake2_256(&Coin::Bitcoin.encode())).into(), - lp_token: Coin::Bitcoin, }, DexEvent::PoolCreated { pool_id: Coin::Ether, pool_account: PublicKey::from_raw(blake2_256(&Coin::Ether.encode())).into(), - lp_token: Coin::Ether, }, DexEvent::PoolCreated { pool_id: Coin::Dai, pool_account: PublicKey::from_raw(blake2_256(&Coin::Dai.encode())).into(), - lp_token: Coin::Dai, }, DexEvent::PoolCreated { pool_id: Coin::Monero, pool_account: PublicKey::from_raw(blake2_256(&Coin::Monero.encode())).into(), - lp_token: Coin::Monero, }, ] ); @@ -96,7 +92,6 @@ serai_test!( pool_id: Coin::Monero, coin_amount: coin_amount.0, sri_amount: sri_amount.0, - lp_token: Coin::Monero, lp_token_minted: 49_999999990000 }] ); @@ -280,7 +275,6 @@ serai_test!( pool_id: Coin::Bitcoin, coin_amount: 10_000_000_000_000, // half of sent amount sri_amount: 6_947_918_403_646, - lp_token: Coin::Bitcoin, lp_token_minted: 8333333333332 }] ); diff --git a/substrate/coins/pallet/Cargo.toml b/substrate/coins/pallet/Cargo.toml index afbae927..c05dc9fd 100644 --- a/substrate/coins/pallet/Cargo.toml +++ b/substrate/coins/pallet/Cargo.toml @@ -12,8 +12,11 @@ rust-version = "1.70" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.cargo-machete] +ignored = ["scale", "scale-info"] + [dependencies] -parity-scale-codec = { version = "3", default-features = false, features = ["derive"] } +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2", default-features = false, features = ["derive"] } frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false } diff --git a/substrate/dex/pallet/Cargo.toml b/substrate/dex/pallet/Cargo.toml index 971d5c6d..a3cb1a97 100644 --- a/substrate/dex/pallet/Cargo.toml +++ b/substrate/dex/pallet/Cargo.toml @@ -12,8 +12,11 @@ rust-version = "1.70" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.cargo-machete] +ignored = ["scale", "scale-info"] + [dependencies] -codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false } +scale = { package = "parity-scale-codec", version = "3.6.1", default-features = false } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false } @@ -33,7 +36,7 @@ serai-primitives = { path = "../../primitives", default-features = false } [features] default = ["std"] std = [ - "codec/std", + "scale/std", "scale-info/std", "sp-std/std", diff --git a/substrate/dex/pallet/src/benchmarking.rs b/substrate/dex/pallet/src/benchmarking.rs index 4fe301f6..a00b6edc 100644 --- a/substrate/dex/pallet/src/benchmarking.rs +++ b/substrate/dex/pallet/src/benchmarking.rs @@ -52,9 +52,7 @@ fn create_coin(coin: &Coin) -> (T::AccountId, AccountIdLookupOf) { (caller, caller_lookup) } -fn create_coin_and_pool( - coin: &Coin, -) -> (PoolCoinId, T::AccountId, AccountIdLookupOf) { +fn create_coin_and_pool(coin: &Coin) -> (Coin, T::AccountId, AccountIdLookupOf) { let (caller, caller_lookup) = create_coin::(coin); assert_ok!(Dex::::create_pool(*coin)); diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index 20753fb9..92857945 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -112,9 +112,6 @@ pub mod pallet { /// migration. pub type PoolId = Coin; - /// Liquidity token id is the same as pool id. - pub type PoolCoinId = Coin; - /// LiquidityTokens Pallet as an instance of coins pallet. pub type LiquidityTokens = coins_pallet::Pallet; @@ -149,10 +146,10 @@ pub mod pallet { type WeightInfo: WeightInfo; } - /// Map from `PoolCoinId` to `PoolInfo`. This establishes whether a pool has been officially + /// Map from `PoolId` to `()`. This establishes whether a pool has been officially /// created rather than people sending tokens directly to a pool's public account. #[pallet::storage] - pub type Pools = StorageMap<_, Blake2_128Concat, PoolId, PoolInfo, OptionQuery>; + pub type Pools = StorageMap<_, Blake2_128Concat, PoolId, (), OptionQuery>; #[pallet::storage] #[pallet::getter(fn spot_price_for_block)] @@ -196,9 +193,6 @@ pub mod pallet { pool_id: PoolId, /// The account ID of the pool. pool_account: T::AccountId, - /// The id of the liquidity tokens that will be minted when coins are added to this - /// pool. - lp_token: PoolCoinId, }, /// A successful call of the `AddLiquidity` extrinsic will create this event. @@ -213,8 +207,6 @@ pub mod pallet { coin_amount: SubstrateAmount, /// The amount of the SRI that was added to the pool. sri_amount: SubstrateAmount, - /// The id of the lp token that was minted. - lp_token: PoolCoinId, /// The amount of lp tokens that were minted of that id. lp_token_minted: SubstrateAmount, }, @@ -231,8 +223,6 @@ pub mod pallet { coin_amount: SubstrateAmount, /// The amount of the second coin that was removed from the pool. sri_amount: SubstrateAmount, - /// The id of the lp token that was burned. - lp_token: PoolCoinId, /// The amount of lp tokens that were burned of that id. lp_token_burned: SubstrateAmount, }, @@ -289,8 +279,6 @@ pub mod pallet { pub enum Error { /// Provided coins are equal. EqualCoins, - /// Provided coin is not supported for pool. - UnsupportedCoin, /// Pool already exists. PoolExists, /// Desired amount can't be zero. @@ -326,22 +314,16 @@ pub mod pallet { ZeroLiquidity, /// Amount can't be zero. ZeroAmount, - /// Insufficient liquidity in the pool. - InsufficientLiquidity, /// Calculated amount out is less than provided minimum amount. ProvidedMinimumNotSufficientForSwap, /// Provided maximum amount is not sufficient for swap. ProvidedMaximumNotSufficientForSwap, - /// Only pools with native on one side are valid. - PoolMustContainNativeCurrency, /// The provided path must consists of 2 coins at least. InvalidPath, /// It was not possible to calculate path data. PathError, /// The provided path must consists of unique coins. NonUniquePath, - /// It was not possible to get or increment the Id of the pool. - IncorrectPoolCoinId, /// Unable to find an element in an array/vec that should have one-to-one correspondence /// with another. For example, an array of coins constituting a `path` should have a /// corresponding array of `amounts` along the path. @@ -427,10 +409,9 @@ pub mod pallet { let pool_account = Self::get_pool_account(pool_id); frame_system::Pallet::::inc_providers(&pool_account); - let pool_info = PoolInfo { lp_token: coin }; - Pools::::insert(pool_id, pool_info); + Pools::::insert(pool_id, ()); - Self::deposit_event(Event::PoolCreated { pool_id, pool_account, lp_token: coin }); + Self::deposit_event(Event::PoolCreated { pool_id, pool_account }); Ok(()) } @@ -474,8 +455,7 @@ pub mod pallet { let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap(); - let maybe_pool = Pools::::get(pool_id); - let pool = maybe_pool.as_ref().ok_or(Error::::PoolNotFound)?; + Pools::::get(pool_id).as_ref().ok_or(Error::::PoolNotFound)?; let pool_account = Self::get_pool_account(pool_id); let sri_reserve = Self::get_balance(&pool_account, Coin::Serai); @@ -540,7 +520,6 @@ pub mod pallet { pool_id, coin_amount, sri_amount, - lp_token: pool.lp_token, lp_token_minted: lp_token_amount, }); @@ -566,8 +545,7 @@ pub mod pallet { let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap(); ensure!(lp_token_burn > 0, Error::::ZeroLiquidity); - let maybe_pool = Pools::::get(pool_id); - let pool = maybe_pool.as_ref().ok_or(Error::::PoolNotFound)?; + Pools::::get(pool_id).as_ref().ok_or(Error::::PoolNotFound)?; let pool_account = Self::get_pool_account(pool_id); let sri_reserve = Self::get_balance(&pool_account, Coin::Serai); @@ -609,7 +587,6 @@ pub mod pallet { pool_id, coin_amount, sri_amount, - lp_token: pool.lp_token, lp_token_burned: lp_token_burn, }); diff --git a/substrate/dex/pallet/src/tests.rs b/substrate/dex/pallet/src/tests.rs index 522b6590..8e75ef80 100644 --- a/substrate/dex/pallet/src/tests.rs +++ b/substrate/dex/pallet/src/tests.rs @@ -101,7 +101,6 @@ fn can_create_pool() { let coin2 = Coin::Monero; let pool_id = Dex::get_pool_id(coin1, coin2).unwrap(); - let lp_token = coin2; assert_ok!(CoinsPallet::::mint(user, Balance { coin: coin1, amount: Amount(1000) })); assert_ok!(Dex::create_pool(coin2)); @@ -109,11 +108,7 @@ fn can_create_pool() { assert_eq!( events(), - [Event::::PoolCreated { - pool_id, - pool_account: Dex::get_pool_account(pool_id), - lp_token - }] + [Event::::PoolCreated { pool_id, pool_account: Dex::get_pool_account(pool_id) }] ); assert_eq!(pools(), vec![pool_id]); @@ -148,7 +143,6 @@ fn different_pools_should_have_different_lp_tokens() { [Event::::PoolCreated { pool_id: pool_id_1_2, pool_account: Dex::get_pool_account(pool_id_1_2), - lp_token: lp_token2_1 }] ); @@ -158,7 +152,6 @@ fn different_pools_should_have_different_lp_tokens() { [Event::::PoolCreated { pool_id: pool_id_1_3, pool_account: Dex::get_pool_account(pool_id_1_3), - lp_token: lp_token3_1, }] ); @@ -195,7 +188,6 @@ fn can_add_liquidity() { pool_id, sri_amount: 10000, coin_amount: 10, - lp_token: lp_token1, lp_token_minted: 216, })); let pallet_account = Dex::get_pool_account(pool_id); @@ -215,7 +207,6 @@ fn can_add_liquidity() { pool_id, sri_amount: 10000, coin_amount: 10, - lp_token: lp_token2, lp_token_minted: 216, })); let pallet_account = Dex::get_pool_account(pool_id); @@ -324,7 +315,6 @@ fn can_remove_liquidity() { pool_id, sri_amount: 999990000, coin_amount: 99999, - lp_token, lp_token_burned: total_lp_received, })); @@ -715,7 +705,6 @@ fn check_no_panic_when_try_swap_close_to_empty_pool() { pool_id, sri_amount: liquidity1, coin_amount: liquidity2, - lp_token, lp_token_minted, })); diff --git a/substrate/dex/pallet/src/types.rs b/substrate/dex/pallet/src/types.rs index 94dd340e..ee344564 100644 --- a/substrate/dex/pallet/src/types.rs +++ b/substrate/dex/pallet/src/types.rs @@ -20,20 +20,10 @@ use super::*; -use codec::{Decode, Encode, MaxEncodedLen}; -use scale_info::TypeInfo; - /// This needs to be long enough for arbitrage to occur and make holding /// any fake price up sufficiently unprofitable. pub const ORACLE_WINDOW_SIZE: u32 = 1000; -/// Stores the lp_token coin id a particular pool has been assigned. -#[derive(Decode, Encode, Default, PartialEq, Eq, MaxEncodedLen, TypeInfo)] -pub struct PoolInfo { - /// Liquidity pool coin - pub lp_token: PoolCoinId, -} - /// Trait for providing methods to swap between the various coin classes. pub trait Swap { /// Swap exactly `amount_in` of coin `path[0]` for coin `path[1]`. diff --git a/substrate/in-instructions/pallet/Cargo.toml b/substrate/in-instructions/pallet/Cargo.toml index fc342c3b..2416b291 100644 --- a/substrate/in-instructions/pallet/Cargo.toml +++ b/substrate/in-instructions/pallet/Cargo.toml @@ -12,6 +12,9 @@ rust-version = "1.74" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.cargo-machete] +ignored = ["scale", "scale-info"] + [dependencies] scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "max-encoded-len"] } scale-info = { version = "2", default-features = false, features = ["derive"] } diff --git a/substrate/runtime/Cargo.toml b/substrate/runtime/Cargo.toml index 1e905894..a5992522 100644 --- a/substrate/runtime/Cargo.toml +++ b/substrate/runtime/Cargo.toml @@ -12,8 +12,11 @@ rust-version = "1.74" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.cargo-machete] +ignored = ["scale", "scale-info"] + [dependencies] -codec = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2", default-features = false, features = ["derive"] } sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false } @@ -66,7 +69,7 @@ substrate-wasm-builder = { git = "https://github.com/serai-dex/substrate" } [features] std = [ - "codec/std", + "scale/std", "scale-info/std", "sp-core/std", diff --git a/substrate/signals/pallet/Cargo.toml b/substrate/signals/pallet/Cargo.toml index 50b9c8a5..d12490c2 100644 --- a/substrate/signals/pallet/Cargo.toml +++ b/substrate/signals/pallet/Cargo.toml @@ -12,6 +12,9 @@ rust-version = "1.74" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.cargo-machete] +ignored = ["scale", "scale-info"] + [dependencies] scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2", default-features = false, features = ["derive"] } diff --git a/substrate/validator-sets/pallet/Cargo.toml b/substrate/validator-sets/pallet/Cargo.toml index 1974abbe..50d47ab7 100644 --- a/substrate/validator-sets/pallet/Cargo.toml +++ b/substrate/validator-sets/pallet/Cargo.toml @@ -12,6 +12,9 @@ rust-version = "1.74" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.cargo-machete] +ignored = ["scale", "scale-info"] + [dependencies] scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2", default-features = false, features = ["derive"] }