mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-12 05:59:23 +00:00
add specific network/coin/balance types (#619)
* add specific network/coin/balance types * misc fixes * fix clippy * misc fixes * fix pr comments * Make halting for external networks * fix encode/decode
This commit is contained in:
@@ -38,7 +38,7 @@ type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup
|
||||
|
||||
type LiquidityTokens<T> = coins_pallet::Pallet<T, coins_pallet::Instance1>;
|
||||
|
||||
fn create_coin<T: Config>(coin: &Coin) -> (T::AccountId, AccountIdLookupOf<T>) {
|
||||
fn create_coin<T: Config>(coin: &ExternalCoin) -> (T::AccountId, AccountIdLookupOf<T>) {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let caller_lookup = T::Lookup::unlookup(caller);
|
||||
assert_ok!(Coins::<T>::mint(
|
||||
@@ -47,12 +47,14 @@ fn create_coin<T: Config>(coin: &Coin) -> (T::AccountId, AccountIdLookupOf<T>) {
|
||||
));
|
||||
assert_ok!(Coins::<T>::mint(
|
||||
caller,
|
||||
Balance { coin: *coin, amount: Amount(INITIAL_COIN_BALANCE) }
|
||||
Balance { coin: (*coin).into(), amount: Amount(INITIAL_COIN_BALANCE) }
|
||||
));
|
||||
(caller, caller_lookup)
|
||||
}
|
||||
|
||||
fn create_coin_and_pool<T: Config>(coin: &Coin) -> (Coin, T::AccountId, AccountIdLookupOf<T>) {
|
||||
fn create_coin_and_pool<T: Config>(
|
||||
coin: &ExternalCoin,
|
||||
) -> (ExternalCoin, T::AccountId, AccountIdLookupOf<T>) {
|
||||
let (caller, caller_lookup) = create_coin::<T>(coin);
|
||||
assert_ok!(Dex::<T>::create_pool(*coin));
|
||||
|
||||
@@ -62,7 +64,7 @@ fn create_coin_and_pool<T: Config>(coin: &Coin) -> (Coin, T::AccountId, AccountI
|
||||
benchmarks! {
|
||||
add_liquidity {
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Bitcoin;
|
||||
let coin2 = ExternalCoin::Bitcoin;
|
||||
let (lp_token, caller, _) = create_coin_and_pool::<T>(&coin2);
|
||||
let add_amount: u64 = 1000;
|
||||
}: _(
|
||||
@@ -75,13 +77,13 @@ benchmarks! {
|
||||
caller
|
||||
)
|
||||
verify {
|
||||
let pool_id = Dex::<T>::get_pool_id(coin1, coin2).unwrap();
|
||||
let pool_id = Dex::<T>::get_pool_id(coin1, coin2.into()).unwrap();
|
||||
let lp_minted = Dex::<T>::calc_lp_amount_for_zero_supply(
|
||||
add_amount,
|
||||
1000u64,
|
||||
).unwrap();
|
||||
assert_eq!(
|
||||
LiquidityTokens::<T>::balance(caller, lp_token).0,
|
||||
LiquidityTokens::<T>::balance(caller, lp_token.into()).0,
|
||||
lp_minted
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -91,7 +93,7 @@ benchmarks! {
|
||||
assert_eq!(
|
||||
Coins::<T>::balance(
|
||||
Dex::<T>::get_pool_account(pool_id),
|
||||
Coin::Bitcoin,
|
||||
ExternalCoin::Bitcoin.into(),
|
||||
).0,
|
||||
1000
|
||||
);
|
||||
@@ -99,7 +101,7 @@ benchmarks! {
|
||||
|
||||
remove_liquidity {
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Monero;
|
||||
let coin2 = ExternalCoin::Monero;
|
||||
let (lp_token, caller, _) = create_coin_and_pool::<T>(&coin2);
|
||||
let add_amount: u64 = 100;
|
||||
let lp_minted = Dex::<T>::calc_lp_amount_for_zero_supply(
|
||||
@@ -117,7 +119,7 @@ benchmarks! {
|
||||
0u64,
|
||||
caller,
|
||||
)?;
|
||||
let total_supply = LiquidityTokens::<T>::supply(lp_token);
|
||||
let total_supply = LiquidityTokens::<T>::supply(Coin::from(lp_token));
|
||||
}: _(
|
||||
SystemOrigin::Signed(caller),
|
||||
coin2,
|
||||
@@ -127,7 +129,7 @@ benchmarks! {
|
||||
caller
|
||||
)
|
||||
verify {
|
||||
let new_total_supply = LiquidityTokens::<T>::supply(lp_token);
|
||||
let new_total_supply = LiquidityTokens::<T>::supply(Coin::from(lp_token));
|
||||
assert_eq!(
|
||||
new_total_supply,
|
||||
total_supply - remove_lp_amount
|
||||
@@ -136,8 +138,8 @@ benchmarks! {
|
||||
|
||||
swap_exact_tokens_for_tokens {
|
||||
let native = Coin::native();
|
||||
let coin1 = Coin::Bitcoin;
|
||||
let coin2 = Coin::Ether;
|
||||
let coin1 = ExternalCoin::Bitcoin;
|
||||
let coin2 = ExternalCoin::Ether;
|
||||
let (_, caller, _) = create_coin_and_pool::<T>(&coin1);
|
||||
let (_, _) = create_coin::<T>(&coin2);
|
||||
|
||||
@@ -168,21 +170,21 @@ benchmarks! {
|
||||
caller,
|
||||
)?;
|
||||
|
||||
let path = vec![coin1, native, coin2];
|
||||
let path = vec![Coin::from(coin1), native, Coin::from(coin2)];
|
||||
let path = BoundedVec::<_, T::MaxSwapPathLength>::try_from(path).unwrap();
|
||||
let native_balance = Coins::<T>::balance(caller, native).0;
|
||||
let coin1_balance = Coins::<T>::balance(caller, Coin::Bitcoin).0;
|
||||
let coin1_balance = Coins::<T>::balance(caller, ExternalCoin::Bitcoin.into()).0;
|
||||
}: _(SystemOrigin::Signed(caller), path, swap_amount, 1u64, caller)
|
||||
verify {
|
||||
let ed_bump = 2u64;
|
||||
let new_coin1_balance = Coins::<T>::balance(caller, Coin::Bitcoin).0;
|
||||
let new_coin1_balance = Coins::<T>::balance(caller, ExternalCoin::Bitcoin.into()).0;
|
||||
assert_eq!(new_coin1_balance, coin1_balance - 100u64);
|
||||
}
|
||||
|
||||
swap_tokens_for_exact_tokens {
|
||||
let native = Coin::native();
|
||||
let coin1 = Coin::Bitcoin;
|
||||
let coin2 = Coin::Ether;
|
||||
let coin1 = ExternalCoin::Bitcoin;
|
||||
let coin2 = ExternalCoin::Ether;
|
||||
let (_, caller, _) = create_coin_and_pool::<T>(&coin1);
|
||||
let (_, _) = create_coin::<T>(&coin2);
|
||||
|
||||
@@ -208,10 +210,10 @@ benchmarks! {
|
||||
0u64,
|
||||
caller,
|
||||
)?;
|
||||
let path = vec![coin1, native, coin2];
|
||||
let path = vec![Coin::from(coin1), native, Coin::from(coin2)];
|
||||
|
||||
let path: BoundedVec<_, T::MaxSwapPathLength> = BoundedVec::try_from(path).unwrap();
|
||||
let coin2_balance = Coins::<T>::balance(caller, Coin::Ether).0;
|
||||
let coin2_balance = Coins::<T>::balance(caller, ExternalCoin::Ether.into()).0;
|
||||
}: _(
|
||||
SystemOrigin::Signed(caller),
|
||||
path.clone(),
|
||||
@@ -220,7 +222,7 @@ benchmarks! {
|
||||
caller
|
||||
)
|
||||
verify {
|
||||
let new_coin2_balance = Coins::<T>::balance(caller, Coin::Ether).0;
|
||||
let new_coin2_balance = Coins::<T>::balance(caller, ExternalCoin::Ether.into()).0;
|
||||
assert_eq!(new_coin2_balance, coin2_balance + 100u64);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user