mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-11 21:49:26 +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:
@@ -18,7 +18,10 @@
|
||||
// It has been forked into a crate distributed under the AGPL 3.0.
|
||||
// Please check the current distribution for up-to-date copyright and licensing information.
|
||||
|
||||
use crate::{mock::*, *};
|
||||
use crate::{
|
||||
mock::{*, MEDIAN_PRICE_WINDOW_LENGTH},
|
||||
*,
|
||||
};
|
||||
use frame_support::{assert_noop, assert_ok};
|
||||
|
||||
pub use coins_pallet as coins;
|
||||
@@ -72,11 +75,13 @@ fn check_pool_accounts_dont_collide() {
|
||||
let mut map = HashSet::new();
|
||||
|
||||
for coin in coins() {
|
||||
let account = Dex::get_pool_account(coin);
|
||||
if map.contains(&account) {
|
||||
panic!("Collision at {coin:?}");
|
||||
if let Coin::External(c) = coin {
|
||||
let account = Dex::get_pool_account(c);
|
||||
if map.contains(&account) {
|
||||
panic!("Collision at {c:?}");
|
||||
}
|
||||
map.insert(account);
|
||||
}
|
||||
map.insert(account);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,11 +103,11 @@ fn can_create_pool() {
|
||||
let coin_account_deposit: u64 = 0;
|
||||
let user: PublicKey = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Monero);
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(1000) }));
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_eq!(balance(user, coin1), 1000 - coin_account_deposit);
|
||||
|
||||
@@ -111,15 +116,13 @@ fn can_create_pool() {
|
||||
[Event::<Test>::PoolCreated { pool_id, pool_account: Dex::get_pool_account(pool_id) }]
|
||||
);
|
||||
assert_eq!(pools(), vec![pool_id]);
|
||||
|
||||
assert_noop!(Dex::create_pool(coin1), Error::<Test>::EqualCoins);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_same_pool_twice_should_fail() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let coin = Coin::Dai;
|
||||
let coin = ExternalCoin::Dai;
|
||||
assert_ok!(Dex::create_pool(coin));
|
||||
assert_noop!(Dex::create_pool(coin), Error::<Test>::PoolExists);
|
||||
});
|
||||
@@ -129,13 +132,13 @@ fn create_same_pool_twice_should_fail() {
|
||||
fn different_pools_should_have_different_lp_tokens() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Bitcoin;
|
||||
let coin3 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Bitcoin);
|
||||
let coin3 = Coin::External(ExternalCoin::Ether);
|
||||
let pool_id_1_2 = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
let pool_id_1_3 = Dex::get_pool_id(coin1, coin3).unwrap();
|
||||
|
||||
let lp_token2_1 = coin2;
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
let lp_token3_1 = coin3;
|
||||
|
||||
assert_eq!(
|
||||
@@ -146,7 +149,7 @@ fn different_pools_should_have_different_lp_tokens() {
|
||||
}]
|
||||
);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin3));
|
||||
assert_ok!(Dex::create_pool(coin3.try_into().unwrap()));
|
||||
assert_eq!(
|
||||
events(),
|
||||
[Event::<Test>::PoolCreated {
|
||||
@@ -164,13 +167,13 @@ fn can_add_liquidity() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Dai;
|
||||
let coin3 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Dai);
|
||||
let coin3 = Coin::External(ExternalCoin::Monero);
|
||||
|
||||
let lp_token1 = coin2;
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
let lp_token2 = coin3;
|
||||
assert_ok!(Dex::create_pool(coin3));
|
||||
assert_ok!(Dex::create_pool(coin3.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(
|
||||
user,
|
||||
@@ -179,7 +182,15 @@ fn can_add_liquidity() {
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin3, amount: Amount(1000) }));
|
||||
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 10, 10000, 10, 10000, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
user,
|
||||
));
|
||||
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
assert!(events().contains(&Event::<Test>::LiquidityAdded {
|
||||
@@ -198,7 +209,15 @@ fn can_add_liquidity() {
|
||||
assert_eq!(pool_balance(user, lp_token1), 216);
|
||||
|
||||
// try to pass the non-native - native coins, the result should be the same
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin3, 10, 10000, 10, 10000, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin3.try_into().unwrap(),
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
user,
|
||||
));
|
||||
|
||||
let pool_id = Dex::get_pool_id(coin1, coin3).unwrap();
|
||||
assert!(events().contains(&Event::<Test>::LiquidityAdded {
|
||||
@@ -223,12 +242,15 @@ fn add_tiny_liquidity_leads_to_insufficient_liquidity_minted_error() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Bitcoin;
|
||||
let coin2 = ExternalCoin::Bitcoin;
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(1000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(
|
||||
user,
|
||||
Balance { coin: coin2.into(), amount: Amount(1000) }
|
||||
));
|
||||
|
||||
assert_noop!(
|
||||
Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 1, 1, 1, 1, user),
|
||||
@@ -242,11 +264,11 @@ fn add_tiny_liquidity_directly_to_pool_address() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Ether;
|
||||
let coin3 = Coin::Dai;
|
||||
let coin2 = Coin::External(ExternalCoin::Ether);
|
||||
let coin3 = Coin::External(ExternalCoin::Dai);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin3));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
assert_ok!(Dex::create_pool(coin3.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(10000 * 2) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(10000) }));
|
||||
@@ -259,7 +281,15 @@ fn add_tiny_liquidity_directly_to_pool_address() {
|
||||
Balance { coin: coin1, amount: Amount(1000) }
|
||||
));
|
||||
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 10, 10000, 10, 10000, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
user,
|
||||
));
|
||||
|
||||
// check the same but for coin3 (non-native token)
|
||||
let pallet_account = Dex::get_pool_account(Dex::get_pool_id(coin1, coin3).unwrap());
|
||||
@@ -267,7 +297,15 @@ fn add_tiny_liquidity_directly_to_pool_address() {
|
||||
pallet_account,
|
||||
Balance { coin: coin2, amount: Amount(1) }
|
||||
));
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin3, 10, 10000, 10, 10000, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin3.try_into().unwrap(),
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
user,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -276,11 +314,11 @@ fn can_remove_liquidity() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Monero);
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
|
||||
let lp_token = coin2;
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(
|
||||
user,
|
||||
@@ -290,7 +328,7 @@ fn can_remove_liquidity() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
100000,
|
||||
1000000000,
|
||||
100000,
|
||||
@@ -302,7 +340,7 @@ fn can_remove_liquidity() {
|
||||
|
||||
assert_ok!(Dex::remove_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
total_lp_received,
|
||||
0,
|
||||
0,
|
||||
@@ -334,15 +372,23 @@ fn can_not_redeem_more_lp_tokens_than_were_minted() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Dai;
|
||||
let coin2 = Coin::External(ExternalCoin::Dai);
|
||||
let lp_token = coin2;
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(10000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 10, 10000, 10, 10000, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
user,
|
||||
));
|
||||
|
||||
// Only 216 lp_tokens_minted
|
||||
assert_eq!(pool_balance(user, lp_token), 216);
|
||||
@@ -350,7 +396,7 @@ fn can_not_redeem_more_lp_tokens_than_were_minted() {
|
||||
assert_noop!(
|
||||
Dex::remove_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
216 + 1, // Try and redeem 10 lp tokens while only 9 minted.
|
||||
0,
|
||||
0,
|
||||
@@ -366,14 +412,22 @@ fn can_quote_price() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Ether);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(100000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 200, 10000, 1, 1, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
200,
|
||||
10000,
|
||||
1,
|
||||
1,
|
||||
user,
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, 3000, false,),
|
||||
@@ -481,14 +535,22 @@ fn quote_price_exact_tokens_for_tokens_matches_execution() {
|
||||
let user = system_address(b"user1").into();
|
||||
let user2 = system_address(b"user2").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Bitcoin;
|
||||
let coin2 = Coin::External(ExternalCoin::Bitcoin);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(100000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 200, 10000, 1, 1, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
200,
|
||||
10000,
|
||||
1,
|
||||
1,
|
||||
user,
|
||||
));
|
||||
|
||||
let amount = 1;
|
||||
let quoted_price = 49;
|
||||
@@ -518,14 +580,22 @@ fn quote_price_tokens_for_exact_tokens_matches_execution() {
|
||||
let user = system_address(b"user1").into();
|
||||
let user2 = system_address(b"user2").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Monero);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(100000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 200, 10000, 1, 1, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
200,
|
||||
10000,
|
||||
1,
|
||||
1,
|
||||
user,
|
||||
));
|
||||
|
||||
let amount = 49;
|
||||
let quoted_price = 1;
|
||||
@@ -557,10 +627,10 @@ fn can_swap_with_native() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Ether);
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(10000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
@@ -570,7 +640,7 @@ fn can_swap_with_native() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -602,8 +672,8 @@ fn can_swap_with_realistic_values() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let sri = Coin::native();
|
||||
let dai = Coin::Dai;
|
||||
assert_ok!(Dex::create_pool(dai));
|
||||
let dai = Coin::External(ExternalCoin::Dai);
|
||||
assert_ok!(Dex::create_pool(dai.try_into().unwrap()));
|
||||
|
||||
const UNIT: u64 = 1_000_000_000;
|
||||
|
||||
@@ -620,7 +690,7 @@ fn can_swap_with_realistic_values() {
|
||||
let liquidity_dai = 1_000_000 * UNIT;
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
dai,
|
||||
dai.try_into().unwrap(),
|
||||
liquidity_dai,
|
||||
liquidity_sri,
|
||||
1,
|
||||
@@ -653,9 +723,9 @@ fn can_not_swap_in_pool_with_no_liquidity_added_yet() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Monero);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
// Check can't swap an empty pool
|
||||
assert_noop!(
|
||||
@@ -676,11 +746,11 @@ fn check_no_panic_when_try_swap_close_to_empty_pool() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Bitcoin;
|
||||
let coin2 = Coin::External(ExternalCoin::Bitcoin);
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
let lp_token = coin2;
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(10000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
@@ -690,7 +760,7 @@ fn check_no_panic_when_try_swap_close_to_empty_pool() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -714,7 +784,7 @@ fn check_no_panic_when_try_swap_close_to_empty_pool() {
|
||||
|
||||
assert_ok!(Dex::remove_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
lp_token_minted,
|
||||
1,
|
||||
1,
|
||||
@@ -787,9 +857,9 @@ fn swap_should_not_work_if_too_much_slippage() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Ether);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(10000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
@@ -799,7 +869,7 @@ fn swap_should_not_work_if_too_much_slippage() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -827,10 +897,10 @@ fn can_swap_tokens_for_exact_tokens() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Dai;
|
||||
let coin2 = Coin::External(ExternalCoin::Dai);
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(20000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
@@ -844,7 +914,7 @@ fn can_swap_tokens_for_exact_tokens() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -882,11 +952,11 @@ fn can_swap_tokens_for_exact_tokens_when_not_liquidity_provider() {
|
||||
let user = system_address(b"user1").into();
|
||||
let user2 = system_address(b"user2").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Monero);
|
||||
let pool_id = Dex::get_pool_id(coin1, coin2).unwrap();
|
||||
let lp_token = coin2;
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
let base1 = 10000;
|
||||
let base2 = 1000;
|
||||
@@ -903,7 +973,7 @@ fn can_swap_tokens_for_exact_tokens_when_not_liquidity_provider() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user2),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -947,7 +1017,7 @@ fn can_swap_tokens_for_exact_tokens_when_not_liquidity_provider() {
|
||||
|
||||
assert_ok!(Dex::remove_liquidity(
|
||||
RuntimeOrigin::signed(user2),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
lp_token_minted,
|
||||
0,
|
||||
0,
|
||||
@@ -961,9 +1031,9 @@ fn swap_tokens_for_exact_tokens_should_not_work_if_too_much_slippage() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Ether);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(20000) }));
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||
@@ -973,7 +1043,7 @@ fn swap_tokens_for_exact_tokens_should_not_work_if_too_much_slippage() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -1001,11 +1071,11 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Dai;
|
||||
let coin3 = Coin::Monero;
|
||||
let coin2 = Coin::External(ExternalCoin::Dai);
|
||||
let coin3 = Coin::External(ExternalCoin::Monero);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin3));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
assert_ok!(Dex::create_pool(coin3.try_into().unwrap()));
|
||||
|
||||
let base1 = 10000;
|
||||
let base2 = 10000;
|
||||
@@ -1019,7 +1089,7 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -1028,7 +1098,7 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() {
|
||||
));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin3,
|
||||
coin3.try_into().unwrap(),
|
||||
liquidity3,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -1089,11 +1159,11 @@ fn swap_tokens_for_exact_tokens_in_multi_hops() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Bitcoin;
|
||||
let coin3 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Bitcoin);
|
||||
let coin3 = Coin::External(ExternalCoin::Ether);
|
||||
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin3));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
assert_ok!(Dex::create_pool(coin3.try_into().unwrap()));
|
||||
|
||||
let base1 = 10000;
|
||||
let base2 = 10000;
|
||||
@@ -1107,7 +1177,7 @@ fn swap_tokens_for_exact_tokens_in_multi_hops() {
|
||||
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2,
|
||||
coin2.try_into().unwrap(),
|
||||
liquidity2,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -1116,7 +1186,7 @@ fn swap_tokens_for_exact_tokens_in_multi_hops() {
|
||||
));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin3,
|
||||
coin3.try_into().unwrap(),
|
||||
liquidity3,
|
||||
liquidity1,
|
||||
1,
|
||||
@@ -1154,7 +1224,7 @@ fn swap_tokens_for_exact_tokens_in_multi_hops() {
|
||||
fn can_not_swap_same_coin() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = system_address(b"user1").into();
|
||||
let coin1 = Coin::Dai;
|
||||
let coin1 = Coin::External(ExternalCoin::Dai);
|
||||
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin1, amount: Amount(1000) }));
|
||||
|
||||
let exchange_amount = 10;
|
||||
@@ -1188,10 +1258,10 @@ fn validate_pool_id_sorting() {
|
||||
// Serai < Bitcoin < Ether < Dai < Monero.
|
||||
// coin1 <= coin2 for this test to pass.
|
||||
let native = Coin::native();
|
||||
let coin1 = Coin::Bitcoin;
|
||||
let coin2 = Coin::Monero;
|
||||
assert_eq!(Dex::get_pool_id(native, coin2).unwrap(), coin2);
|
||||
assert_eq!(Dex::get_pool_id(coin2, native).unwrap(), coin2);
|
||||
let coin1 = Coin::External(ExternalCoin::Bitcoin);
|
||||
let coin2 = Coin::External(ExternalCoin::Monero);
|
||||
assert_eq!(Dex::get_pool_id(native, coin2).unwrap(), coin2.try_into().unwrap());
|
||||
assert_eq!(Dex::get_pool_id(coin2, native).unwrap(), coin2.try_into().unwrap());
|
||||
assert!(matches!(Dex::get_pool_id(native, native), Err(Error::<Test>::EqualCoins)));
|
||||
assert!(matches!(Dex::get_pool_id(coin2, coin1), Err(Error::<Test>::PoolNotFound)));
|
||||
assert!(coin2 > coin1);
|
||||
@@ -1216,7 +1286,7 @@ fn cannot_block_pool_creation() {
|
||||
|
||||
// The target pool the user wants to create is Native <=> Coin(2)
|
||||
let coin1 = Coin::native();
|
||||
let coin2 = Coin::Ether;
|
||||
let coin2 = Coin::External(ExternalCoin::Ether);
|
||||
|
||||
// Attacker computes the still non-existing pool account for the target pair
|
||||
let pool_account = Dex::get_pool_account(Dex::get_pool_id(coin2, coin1).unwrap());
|
||||
@@ -1238,7 +1308,7 @@ fn cannot_block_pool_creation() {
|
||||
}
|
||||
|
||||
// User can still create the pool
|
||||
assert_ok!(Dex::create_pool(coin2));
|
||||
assert_ok!(Dex::create_pool(coin2.try_into().unwrap()));
|
||||
|
||||
// User has to transfer one Coin(2) token to the pool account (otherwise add_liquidity will
|
||||
// fail with `CoinTwoDepositDidNotMeetMinimum`), also transfer native token for the same error.
|
||||
@@ -1256,7 +1326,15 @@ fn cannot_block_pool_creation() {
|
||||
));
|
||||
|
||||
// add_liquidity shouldn't fail because of the number of consumers
|
||||
assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 100, 9900, 10, 9900, user,));
|
||||
assert_ok!(Dex::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
coin2.try_into().unwrap(),
|
||||
100,
|
||||
9900,
|
||||
10,
|
||||
9900,
|
||||
user,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1281,7 +1359,7 @@ fn test_median_price() {
|
||||
prices.push(OsRng.next_u64());
|
||||
}
|
||||
}
|
||||
let coin = Coin::Bitcoin;
|
||||
let coin = ExternalCoin::Bitcoin;
|
||||
|
||||
assert!(prices.len() >= (2 * usize::from(MEDIAN_PRICE_WINDOW_LENGTH)));
|
||||
for i in 0 .. prices.len() {
|
||||
|
||||
Reference in New Issue
Block a user