Remove redundant fields from dex-pallet, add cargo machete ignores

This commit is contained in:
Luke Parker
2023-12-11 07:47:23 -05:00
parent 11fdb6da1d
commit 4de4c186b1
12 changed files with 31 additions and 69 deletions

View File

@@ -3,7 +3,6 @@ use sp_runtime::BoundedVec;
use serai_primitives::*; use serai_primitives::*;
type PoolId = Coin; type PoolId = Coin;
type PoolCoinId = Coin;
type MaxSwapPathLength = sp_core::ConstU32<3>; type MaxSwapPathLength = sp_core::ConstU32<3>;
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)] #[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
@@ -44,7 +43,6 @@ pub enum Event {
PoolCreated { PoolCreated {
pool_id: PoolId, pool_id: PoolId,
pool_account: SeraiAddress, pool_account: SeraiAddress,
lp_token: PoolCoinId,
}, },
LiquidityAdded { LiquidityAdded {
@@ -53,7 +51,6 @@ pub enum Event {
pool_id: PoolId, pool_id: PoolId,
coin_amount: SubstrateAmount, coin_amount: SubstrateAmount,
sri_amount: SubstrateAmount, sri_amount: SubstrateAmount,
lp_token: PoolCoinId,
lp_token_minted: SubstrateAmount, lp_token_minted: SubstrateAmount,
}, },
@@ -63,7 +60,6 @@ pub enum Event {
pool_id: PoolId, pool_id: PoolId,
coin_amount: SubstrateAmount, coin_amount: SubstrateAmount,
sri_amount: SubstrateAmount, sri_amount: SubstrateAmount,
lp_token: PoolCoinId,
lp_token_burned: SubstrateAmount, lp_token_burned: SubstrateAmount,
}, },

View File

@@ -38,22 +38,18 @@ serai_test!(
DexEvent::PoolCreated { DexEvent::PoolCreated {
pool_id: Coin::Bitcoin, pool_id: Coin::Bitcoin,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Bitcoin.encode())).into(), pool_account: PublicKey::from_raw(blake2_256(&Coin::Bitcoin.encode())).into(),
lp_token: Coin::Bitcoin,
}, },
DexEvent::PoolCreated { DexEvent::PoolCreated {
pool_id: Coin::Ether, pool_id: Coin::Ether,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Ether.encode())).into(), pool_account: PublicKey::from_raw(blake2_256(&Coin::Ether.encode())).into(),
lp_token: Coin::Ether,
}, },
DexEvent::PoolCreated { DexEvent::PoolCreated {
pool_id: Coin::Dai, pool_id: Coin::Dai,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Dai.encode())).into(), pool_account: PublicKey::from_raw(blake2_256(&Coin::Dai.encode())).into(),
lp_token: Coin::Dai,
}, },
DexEvent::PoolCreated { DexEvent::PoolCreated {
pool_id: Coin::Monero, pool_id: Coin::Monero,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Monero.encode())).into(), 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, pool_id: Coin::Monero,
coin_amount: coin_amount.0, coin_amount: coin_amount.0,
sri_amount: sri_amount.0, sri_amount: sri_amount.0,
lp_token: Coin::Monero,
lp_token_minted: 49_999999990000 lp_token_minted: 49_999999990000
}] }]
); );
@@ -280,7 +275,6 @@ serai_test!(
pool_id: Coin::Bitcoin, pool_id: Coin::Bitcoin,
coin_amount: 10_000_000_000_000, // half of sent amount coin_amount: 10_000_000_000_000, // half of sent amount
sri_amount: 6_947_918_403_646, sri_amount: 6_947_918_403_646,
lp_token: Coin::Bitcoin,
lp_token_minted: 8333333333332 lp_token_minted: 8333333333332
}] }]
); );

View File

@@ -12,8 +12,11 @@ rust-version = "1.70"
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[dependencies] [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"] } scale-info = { version = "2", default-features = false, features = ["derive"] }
frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false } frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false }

View File

@@ -12,8 +12,11 @@ rust-version = "1.70"
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[dependencies] [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"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false } sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
@@ -33,7 +36,7 @@ serai-primitives = { path = "../../primitives", default-features = false }
[features] [features]
default = ["std"] default = ["std"]
std = [ std = [
"codec/std", "scale/std",
"scale-info/std", "scale-info/std",
"sp-std/std", "sp-std/std",

View File

@@ -52,9 +52,7 @@ fn create_coin<T: Config>(coin: &Coin) -> (T::AccountId, AccountIdLookupOf<T>) {
(caller, caller_lookup) (caller, caller_lookup)
} }
fn create_coin_and_pool<T: Config>( fn create_coin_and_pool<T: Config>(coin: &Coin) -> (Coin, T::AccountId, AccountIdLookupOf<T>) {
coin: &Coin,
) -> (PoolCoinId, T::AccountId, AccountIdLookupOf<T>) {
let (caller, caller_lookup) = create_coin::<T>(coin); let (caller, caller_lookup) = create_coin::<T>(coin);
assert_ok!(Dex::<T>::create_pool(*coin)); assert_ok!(Dex::<T>::create_pool(*coin));

View File

@@ -112,9 +112,6 @@ pub mod pallet {
/// migration. /// migration.
pub type PoolId = Coin; 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. /// LiquidityTokens Pallet as an instance of coins pallet.
pub type LiquidityTokens<T> = coins_pallet::Pallet<T, coins_pallet::Instance1>; pub type LiquidityTokens<T> = coins_pallet::Pallet<T, coins_pallet::Instance1>;
@@ -149,10 +146,10 @@ pub mod pallet {
type WeightInfo: WeightInfo; 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. /// created rather than people sending tokens directly to a pool's public account.
#[pallet::storage] #[pallet::storage]
pub type Pools<T: Config> = StorageMap<_, Blake2_128Concat, PoolId, PoolInfo<Coin>, OptionQuery>; pub type Pools<T: Config> = StorageMap<_, Blake2_128Concat, PoolId, (), OptionQuery>;
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn spot_price_for_block)] #[pallet::getter(fn spot_price_for_block)]
@@ -196,9 +193,6 @@ pub mod pallet {
pool_id: PoolId, pool_id: PoolId,
/// The account ID of the pool. /// The account ID of the pool.
pool_account: T::AccountId, 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. /// A successful call of the `AddLiquidity` extrinsic will create this event.
@@ -213,8 +207,6 @@ pub mod pallet {
coin_amount: SubstrateAmount, coin_amount: SubstrateAmount,
/// The amount of the SRI that was added to the pool. /// The amount of the SRI that was added to the pool.
sri_amount: SubstrateAmount, 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. /// The amount of lp tokens that were minted of that id.
lp_token_minted: SubstrateAmount, lp_token_minted: SubstrateAmount,
}, },
@@ -231,8 +223,6 @@ pub mod pallet {
coin_amount: SubstrateAmount, coin_amount: SubstrateAmount,
/// The amount of the second coin that was removed from the pool. /// The amount of the second coin that was removed from the pool.
sri_amount: SubstrateAmount, 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. /// The amount of lp tokens that were burned of that id.
lp_token_burned: SubstrateAmount, lp_token_burned: SubstrateAmount,
}, },
@@ -289,8 +279,6 @@ pub mod pallet {
pub enum Error<T> { pub enum Error<T> {
/// Provided coins are equal. /// Provided coins are equal.
EqualCoins, EqualCoins,
/// Provided coin is not supported for pool.
UnsupportedCoin,
/// Pool already exists. /// Pool already exists.
PoolExists, PoolExists,
/// Desired amount can't be zero. /// Desired amount can't be zero.
@@ -326,22 +314,16 @@ pub mod pallet {
ZeroLiquidity, ZeroLiquidity,
/// Amount can't be zero. /// Amount can't be zero.
ZeroAmount, ZeroAmount,
/// Insufficient liquidity in the pool.
InsufficientLiquidity,
/// Calculated amount out is less than provided minimum amount. /// Calculated amount out is less than provided minimum amount.
ProvidedMinimumNotSufficientForSwap, ProvidedMinimumNotSufficientForSwap,
/// Provided maximum amount is not sufficient for swap. /// Provided maximum amount is not sufficient for swap.
ProvidedMaximumNotSufficientForSwap, ProvidedMaximumNotSufficientForSwap,
/// Only pools with native on one side are valid.
PoolMustContainNativeCurrency,
/// The provided path must consists of 2 coins at least. /// The provided path must consists of 2 coins at least.
InvalidPath, InvalidPath,
/// It was not possible to calculate path data. /// It was not possible to calculate path data.
PathError, PathError,
/// The provided path must consists of unique coins. /// The provided path must consists of unique coins.
NonUniquePath, 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 /// 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 /// with another. For example, an array of coins constituting a `path` should have a
/// corresponding array of `amounts` along the path. /// corresponding array of `amounts` along the path.
@@ -427,10 +409,9 @@ pub mod pallet {
let pool_account = Self::get_pool_account(pool_id); let pool_account = Self::get_pool_account(pool_id);
frame_system::Pallet::<T>::inc_providers(&pool_account); frame_system::Pallet::<T>::inc_providers(&pool_account);
let pool_info = PoolInfo { lp_token: coin }; Pools::<T>::insert(pool_id, ());
Pools::<T>::insert(pool_id, pool_info);
Self::deposit_event(Event::PoolCreated { pool_id, pool_account, lp_token: coin }); Self::deposit_event(Event::PoolCreated { pool_id, pool_account });
Ok(()) Ok(())
} }
@@ -474,8 +455,7 @@ pub mod pallet {
let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap(); let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap();
let maybe_pool = Pools::<T>::get(pool_id); Pools::<T>::get(pool_id).as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool_account = Self::get_pool_account(pool_id); let pool_account = Self::get_pool_account(pool_id);
let sri_reserve = Self::get_balance(&pool_account, Coin::Serai); let sri_reserve = Self::get_balance(&pool_account, Coin::Serai);
@@ -540,7 +520,6 @@ pub mod pallet {
pool_id, pool_id,
coin_amount, coin_amount,
sri_amount, sri_amount,
lp_token: pool.lp_token,
lp_token_minted: lp_token_amount, lp_token_minted: lp_token_amount,
}); });
@@ -566,8 +545,7 @@ pub mod pallet {
let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap(); let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap();
ensure!(lp_token_burn > 0, Error::<T>::ZeroLiquidity); ensure!(lp_token_burn > 0, Error::<T>::ZeroLiquidity);
let maybe_pool = Pools::<T>::get(pool_id); Pools::<T>::get(pool_id).as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool_account = Self::get_pool_account(pool_id); let pool_account = Self::get_pool_account(pool_id);
let sri_reserve = Self::get_balance(&pool_account, Coin::Serai); let sri_reserve = Self::get_balance(&pool_account, Coin::Serai);
@@ -609,7 +587,6 @@ pub mod pallet {
pool_id, pool_id,
coin_amount, coin_amount,
sri_amount, sri_amount,
lp_token: pool.lp_token,
lp_token_burned: lp_token_burn, lp_token_burned: lp_token_burn,
}); });

View File

@@ -101,7 +101,6 @@ fn can_create_pool() {
let coin2 = Coin::Monero; let coin2 = Coin::Monero;
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap(); let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
let lp_token = coin2;
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(1000) })); assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(1000) }));
assert_ok!(Dex::create_pool(coin2)); assert_ok!(Dex::create_pool(coin2));
@@ -109,11 +108,7 @@ fn can_create_pool() {
assert_eq!( assert_eq!(
events(), events(),
[Event::<Test>::PoolCreated { [Event::<Test>::PoolCreated { pool_id, pool_account: Dex::get_pool_account(pool_id) }]
pool_id,
pool_account: Dex::get_pool_account(pool_id),
lp_token
}]
); );
assert_eq!(pools(), vec![pool_id]); assert_eq!(pools(), vec![pool_id]);
@@ -148,7 +143,6 @@ fn different_pools_should_have_different_lp_tokens() {
[Event::<Test>::PoolCreated { [Event::<Test>::PoolCreated {
pool_id: pool_id_1_2, pool_id: pool_id_1_2,
pool_account: Dex::get_pool_account(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::<Test>::PoolCreated { [Event::<Test>::PoolCreated {
pool_id: pool_id_1_3, pool_id: pool_id_1_3,
pool_account: Dex::get_pool_account(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, pool_id,
sri_amount: 10000, sri_amount: 10000,
coin_amount: 10, coin_amount: 10,
lp_token: lp_token1,
lp_token_minted: 216, lp_token_minted: 216,
})); }));
let pallet_account = Dex::get_pool_account(pool_id); let pallet_account = Dex::get_pool_account(pool_id);
@@ -215,7 +207,6 @@ fn can_add_liquidity() {
pool_id, pool_id,
sri_amount: 10000, sri_amount: 10000,
coin_amount: 10, coin_amount: 10,
lp_token: lp_token2,
lp_token_minted: 216, lp_token_minted: 216,
})); }));
let pallet_account = Dex::get_pool_account(pool_id); let pallet_account = Dex::get_pool_account(pool_id);
@@ -324,7 +315,6 @@ fn can_remove_liquidity() {
pool_id, pool_id,
sri_amount: 999990000, sri_amount: 999990000,
coin_amount: 99999, coin_amount: 99999,
lp_token,
lp_token_burned: total_lp_received, lp_token_burned: total_lp_received,
})); }));
@@ -715,7 +705,6 @@ fn check_no_panic_when_try_swap_close_to_empty_pool() {
pool_id, pool_id,
sri_amount: liquidity1, sri_amount: liquidity1,
coin_amount: liquidity2, coin_amount: liquidity2,
lp_token,
lp_token_minted, lp_token_minted,
})); }));

View File

@@ -20,20 +20,10 @@
use super::*; use super::*;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
/// This needs to be long enough for arbitrage to occur and make holding /// This needs to be long enough for arbitrage to occur and make holding
/// any fake price up sufficiently unprofitable. /// any fake price up sufficiently unprofitable.
pub const ORACLE_WINDOW_SIZE: u32 = 1000; 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<PoolCoinId> {
/// Liquidity pool coin
pub lp_token: PoolCoinId,
}
/// Trait for providing methods to swap between the various coin classes. /// Trait for providing methods to swap between the various coin classes.
pub trait Swap<AccountId, Balance, MultiCoinId> { pub trait Swap<AccountId, Balance, MultiCoinId> {
/// Swap exactly `amount_in` of coin `path[0]` for coin `path[1]`. /// Swap exactly `amount_in` of coin `path[0]` for coin `path[1]`.

View File

@@ -12,6 +12,9 @@ rust-version = "1.74"
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[dependencies] [dependencies]
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "max-encoded-len"] } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2", default-features = false, features = ["derive"] } scale-info = { version = "2", default-features = false, features = ["derive"] }

View File

@@ -12,8 +12,11 @@ rust-version = "1.74"
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[dependencies] [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"] } scale-info = { version = "2", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false } 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] [features]
std = [ std = [
"codec/std", "scale/std",
"scale-info/std", "scale-info/std",
"sp-core/std", "sp-core/std",

View File

@@ -12,6 +12,9 @@ rust-version = "1.74"
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[dependencies] [dependencies]
scale = { 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"] } scale-info = { version = "2", default-features = false, features = ["derive"] }

View File

@@ -12,6 +12,9 @@ rust-version = "1.74"
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[dependencies] [dependencies]
scale = { 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"] } scale-info = { version = "2", default-features = false, features = ["derive"] }