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
No known key found for this signature in database
12 changed files with 31 additions and 69 deletions

View file

@ -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,
},

View file

@ -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
}]
);

View file

@ -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 }

View file

@ -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",

View file

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

View file

@ -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<T> = coins_pallet::Pallet<T, coins_pallet::Instance1>;
@ -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<T: Config> = StorageMap<_, Blake2_128Concat, PoolId, PoolInfo<Coin>, OptionQuery>;
pub type Pools<T: Config> = 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<T> {
/// 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::<T>::inc_providers(&pool_account);
let pool_info = PoolInfo { lp_token: coin };
Pools::<T>::insert(pool_id, pool_info);
Pools::<T>::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::<T>::get(pool_id);
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
Pools::<T>::get(pool_id).as_ref().ok_or(Error::<T>::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::<T>::ZeroLiquidity);
let maybe_pool = Pools::<T>::get(pool_id);
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
Pools::<T>::get(pool_id).as_ref().ok_or(Error::<T>::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,
});

View file

@ -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::<Test>::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::<Test>::PoolCreated {
pool_id,
pool_account: Dex::get_pool_account(pool_id),
lp_token
}]
[Event::<Test>::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::<Test>::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::<Test>::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,
}));

View file

@ -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<PoolCoinId> {
/// Liquidity pool coin
pub lp_token: PoolCoinId,
}
/// Trait for providing methods to swap between the various coin classes.
pub trait Swap<AccountId, Balance, MultiCoinId> {
/// 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
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"] }

View file

@ -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",

View file

@ -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"] }

View file

@ -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"] }