mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-14 15:09:23 +00:00
misc fixes
This commit is contained in:
@@ -744,6 +744,10 @@ async fn main() {
|
|||||||
|
|
||||||
let coordinator = MessageQueue::from_env(Service::Processor(network_id));
|
let coordinator = MessageQueue::from_env(Service::Processor(network_id));
|
||||||
|
|
||||||
|
// This allow is necessary since each configuration deletes the other networks from the following
|
||||||
|
// match arms. So we match all cases but since all cases already there according to the compiler
|
||||||
|
// we put this to allow clippy to get pass this.
|
||||||
|
#[allow(unreachable_patterns)]
|
||||||
match network_id {
|
match network_id {
|
||||||
#[cfg(feature = "bitcoin")]
|
#[cfg(feature = "bitcoin")]
|
||||||
ExternalNetworkId::Bitcoin => run(db, Bitcoin::new(url).await, coordinator).await,
|
ExternalNetworkId::Bitcoin => run(db, Bitcoin::new(url).await, coordinator).await,
|
||||||
@@ -759,5 +763,6 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
#[cfg(feature = "monero")]
|
#[cfg(feature = "monero")]
|
||||||
ExternalNetworkId::Monero => run(db, Monero::new(url).await, coordinator).await,
|
ExternalNetworkId::Monero => run(db, Monero::new(url).await, coordinator).await,
|
||||||
|
_ => panic!("spawning a processor for an unsupported network"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1002,17 +1002,18 @@ pub mod pallet {
|
|||||||
Ok(amounts)
|
Ok(amounts)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used by the RPC service to provide price on coin/SRI pool.
|
/// Used by the RPC service to provide current prices.
|
||||||
pub fn quote_price_exact_tokens_for_tokens(
|
pub fn quote_price_exact_tokens_for_tokens(
|
||||||
coin: ExternalCoin,
|
coin1: Coin,
|
||||||
|
coin2: Coin,
|
||||||
amount: SubstrateAmount,
|
amount: SubstrateAmount,
|
||||||
include_fee: bool,
|
include_fee: bool,
|
||||||
) -> Option<SubstrateAmount> {
|
) -> Option<SubstrateAmount> {
|
||||||
let pool_id = Self::get_pool_id(Coin::native(), coin.into()).ok()?;
|
let pool_id = Self::get_pool_id(coin1, coin2).ok()?;
|
||||||
let pool_account = Self::get_pool_account(pool_id);
|
let pool_account = Self::get_pool_account(pool_id);
|
||||||
|
|
||||||
let balance1 = Self::get_balance(&pool_account, Coin::native());
|
let balance1 = Self::get_balance(&pool_account, coin1);
|
||||||
let balance2 = Self::get_balance(&pool_account, coin.into());
|
let balance2 = Self::get_balance(&pool_account, coin2);
|
||||||
if balance1 != 0 {
|
if balance1 != 0 {
|
||||||
if include_fee {
|
if include_fee {
|
||||||
Self::get_amount_out(amount, balance1, balance2).ok()
|
Self::get_amount_out(amount, balance1, balance2).ok()
|
||||||
@@ -1026,15 +1027,16 @@ pub mod pallet {
|
|||||||
|
|
||||||
/// Used by the RPC service to provide current prices.
|
/// Used by the RPC service to provide current prices.
|
||||||
pub fn quote_price_tokens_for_exact_tokens(
|
pub fn quote_price_tokens_for_exact_tokens(
|
||||||
coin: ExternalCoin,
|
coin1: Coin,
|
||||||
|
coin2: Coin,
|
||||||
amount: SubstrateAmount,
|
amount: SubstrateAmount,
|
||||||
include_fee: bool,
|
include_fee: bool,
|
||||||
) -> Option<SubstrateAmount> {
|
) -> Option<SubstrateAmount> {
|
||||||
let pool_id = Self::get_pool_id(Coin::native(), coin.into()).ok()?;
|
let pool_id = Self::get_pool_id(coin1, coin2).ok()?;
|
||||||
let pool_account = Self::get_pool_account(pool_id);
|
let pool_account = Self::get_pool_account(pool_id);
|
||||||
|
|
||||||
let balance1 = Self::get_balance(&pool_account, Coin::native());
|
let balance1 = Self::get_balance(&pool_account, coin1);
|
||||||
let balance2 = Self::get_balance(&pool_account, coin.into());
|
let balance2 = Self::get_balance(&pool_account, coin2);
|
||||||
if balance1 != 0 {
|
if balance1 != 0 {
|
||||||
if include_fee {
|
if include_fee {
|
||||||
Self::get_amount_in(amount, balance1, balance2).ok()
|
Self::get_amount_in(amount, balance1, balance2).ok()
|
||||||
@@ -1239,7 +1241,8 @@ sp_api::decl_runtime_apis! {
|
|||||||
/// Note that the price may have changed by the time the transaction is executed.
|
/// Note that the price may have changed by the time the transaction is executed.
|
||||||
/// (Use `amount_in_max` to control slippage.)
|
/// (Use `amount_in_max` to control slippage.)
|
||||||
fn quote_price_tokens_for_exact_tokens(
|
fn quote_price_tokens_for_exact_tokens(
|
||||||
coin: ExternalCoin,
|
coin1: Coin,
|
||||||
|
coin2: Coin,
|
||||||
amount: SubstrateAmount,
|
amount: SubstrateAmount,
|
||||||
include_fee: bool
|
include_fee: bool
|
||||||
) -> Option<SubstrateAmount>;
|
) -> Option<SubstrateAmount>;
|
||||||
@@ -1249,7 +1252,8 @@ sp_api::decl_runtime_apis! {
|
|||||||
/// Note that the price may have changed by the time the transaction is executed.
|
/// Note that the price may have changed by the time the transaction is executed.
|
||||||
/// (Use `amount_out_min` to control slippage.)
|
/// (Use `amount_out_min` to control slippage.)
|
||||||
fn quote_price_exact_tokens_for_tokens(
|
fn quote_price_exact_tokens_for_tokens(
|
||||||
coin: ExternalCoin,
|
coin1: Coin,
|
||||||
|
coin2: Coin,
|
||||||
amount: SubstrateAmount,
|
amount: SubstrateAmount,
|
||||||
include_fee: bool
|
include_fee: bool
|
||||||
) -> Option<SubstrateAmount>;
|
) -> Option<SubstrateAmount>;
|
||||||
|
|||||||
@@ -409,9 +409,9 @@ fn can_quote_price() {
|
|||||||
new_test_ext().execute_with(|| {
|
new_test_ext().execute_with(|| {
|
||||||
let user = system_address(b"user1").into();
|
let user = system_address(b"user1").into();
|
||||||
let coin1 = Coin::native();
|
let coin1 = Coin::native();
|
||||||
let coin2 = ExternalCoin::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: coin1, amount: Amount(100000) }));
|
||||||
assert_ok!(CoinsPallet::<Test>::mint(
|
assert_ok!(CoinsPallet::<Test>::mint(
|
||||||
@@ -419,38 +419,82 @@ fn can_quote_price() {
|
|||||||
Balance { coin: coin2.into(), amount: Amount(1000) }
|
Balance { coin: coin2.into(), 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(coin2, 3000, false,), Some(60));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, 3000, false,),
|
||||||
|
Some(60)
|
||||||
|
);
|
||||||
// including fee so should get less out...
|
// including fee so should get less out...
|
||||||
assert_eq!(Dex::quote_price_exact_tokens_for_tokens(coin2, 3000, true,), Some(46));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, 3000, true,),
|
||||||
|
Some(46)
|
||||||
|
);
|
||||||
// Check it still gives same price:
|
// Check it still gives same price:
|
||||||
// (if the above accidentally exchanged then it would not give same quote as before)
|
// (if the above accidentally exchanged then it would not give same quote as before)
|
||||||
assert_eq!(Dex::quote_price_exact_tokens_for_tokens(coin2, 3000, false,), Some(60));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, 3000, false,),
|
||||||
|
Some(60)
|
||||||
|
);
|
||||||
// including fee so should get less out...
|
// including fee so should get less out...
|
||||||
assert_eq!(Dex::quote_price_exact_tokens_for_tokens(coin2, 3000, true,), Some(46));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, 3000, true,),
|
||||||
|
Some(46)
|
||||||
|
);
|
||||||
|
|
||||||
// Check inverse:
|
// Check inverse:
|
||||||
assert_eq!(Dex::quote_price_exact_tokens_for_tokens(coin2, 60, false,), Some(3000));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(coin2, Coin::native(), 60, false,),
|
||||||
|
Some(3000)
|
||||||
|
);
|
||||||
// including fee so should get less out...
|
// including fee so should get less out...
|
||||||
assert_eq!(Dex::quote_price_exact_tokens_for_tokens(coin2, 60, true,), Some(2302));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(coin2, Coin::native(), 60, true,),
|
||||||
|
Some(2302)
|
||||||
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// same tests as above but for quote_price_tokens_for_exact_tokens:
|
// same tests as above but for quote_price_tokens_for_exact_tokens:
|
||||||
//
|
//
|
||||||
assert_eq!(Dex::quote_price_tokens_for_exact_tokens(coin2, 60, false,), Some(3000));
|
assert_eq!(
|
||||||
|
Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, 60, false,),
|
||||||
|
Some(3000)
|
||||||
|
);
|
||||||
// including fee so should need to put more in...
|
// including fee so should need to put more in...
|
||||||
assert_eq!(Dex::quote_price_tokens_for_exact_tokens(coin2, 60, true,), Some(4299));
|
assert_eq!(
|
||||||
|
Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, 60, true,),
|
||||||
|
Some(4299)
|
||||||
|
);
|
||||||
// Check it still gives same price:
|
// Check it still gives same price:
|
||||||
// (if the above accidentally exchanged then it would not give same quote as before)
|
// (if the above accidentally exchanged then it would not give same quote as before)
|
||||||
assert_eq!(Dex::quote_price_tokens_for_exact_tokens(coin2, 60, false,), Some(3000));
|
assert_eq!(
|
||||||
|
Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, 60, false,),
|
||||||
|
Some(3000)
|
||||||
|
);
|
||||||
// including fee so should need to put more in...
|
// including fee so should need to put more in...
|
||||||
assert_eq!(Dex::quote_price_tokens_for_exact_tokens(coin2, 60, true,), Some(4299));
|
assert_eq!(
|
||||||
|
Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, 60, true,),
|
||||||
|
Some(4299)
|
||||||
|
);
|
||||||
|
|
||||||
// Check inverse:
|
// Check inverse:
|
||||||
assert_eq!(Dex::quote_price_tokens_for_exact_tokens(coin2, 3000, false,), Some(60));
|
assert_eq!(
|
||||||
|
Dex::quote_price_tokens_for_exact_tokens(coin2, Coin::native(), 3000, false,),
|
||||||
|
Some(60)
|
||||||
|
);
|
||||||
// including fee so should need to put more in...
|
// including fee so should need to put more in...
|
||||||
assert_eq!(Dex::quote_price_tokens_for_exact_tokens(coin2, 3000, true,), Some(86));
|
assert_eq!(
|
||||||
|
Dex::quote_price_tokens_for_exact_tokens(coin2, Coin::native(), 3000, true,),
|
||||||
|
Some(86)
|
||||||
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// roundtrip: Without fees one should get the original number
|
// roundtrip: Without fees one should get the original number
|
||||||
@@ -458,24 +502,28 @@ fn can_quote_price() {
|
|||||||
let amount_in = 100;
|
let amount_in = 100;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Dex::quote_price_exact_tokens_for_tokens(coin2, amount_in, false,)
|
Dex::quote_price_exact_tokens_for_tokens(coin2, Coin::native(), amount_in, false,).and_then(
|
||||||
.and_then(|amount| { Dex::quote_price_exact_tokens_for_tokens(coin2, amount, false) }),
|
|amount| Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, amount, false,)
|
||||||
|
),
|
||||||
Some(amount_in)
|
Some(amount_in)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Dex::quote_price_exact_tokens_for_tokens(coin2, amount_in, false,)
|
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, amount_in, false,).and_then(
|
||||||
.and_then(|amount| Dex::quote_price_exact_tokens_for_tokens(coin2, amount, false,)),
|
|amount| Dex::quote_price_exact_tokens_for_tokens(coin2, Coin::native(), amount, false,)
|
||||||
|
),
|
||||||
Some(amount_in)
|
Some(amount_in)
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Dex::quote_price_tokens_for_exact_tokens(coin2, amount_in, false,)
|
Dex::quote_price_tokens_for_exact_tokens(coin2, Coin::native(), amount_in, false,).and_then(
|
||||||
.and_then(|amount| { Dex::quote_price_tokens_for_exact_tokens(coin2, amount, false) }),
|
|amount| Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, amount, false,)
|
||||||
|
),
|
||||||
Some(amount_in)
|
Some(amount_in)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Dex::quote_price_tokens_for_exact_tokens(coin2, amount_in, false,)
|
Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, amount_in, false,).and_then(
|
||||||
.and_then(|amount| Dex::quote_price_tokens_for_exact_tokens(coin2, amount, false,)),
|
|amount| Dex::quote_price_tokens_for_exact_tokens(coin2, Coin::native(), amount, false,)
|
||||||
|
),
|
||||||
Some(amount_in)
|
Some(amount_in)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -487,31 +535,36 @@ fn quote_price_exact_tokens_for_tokens_matches_execution() {
|
|||||||
let user = system_address(b"user1").into();
|
let user = system_address(b"user1").into();
|
||||||
let user2 = system_address(b"user2").into();
|
let user2 = system_address(b"user2").into();
|
||||||
let coin1 = Coin::native();
|
let coin1 = Coin::native();
|
||||||
let coin2 = ExternalCoin::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: coin1, amount: Amount(100000) }));
|
||||||
assert_ok!(CoinsPallet::<Test>::mint(
|
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));
|
||||||
user,
|
|
||||||
Balance { coin: coin2.into(), 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 amount = 1;
|
||||||
let quoted_price = 49;
|
let quoted_price = 49;
|
||||||
assert_eq!(Dex::quote_price_exact_tokens_for_tokens(coin2, amount, true,), Some(quoted_price));
|
assert_eq!(
|
||||||
|
Dex::quote_price_exact_tokens_for_tokens(coin2, coin1, amount, true,),
|
||||||
|
Some(quoted_price)
|
||||||
|
);
|
||||||
|
|
||||||
assert_ok!(CoinsPallet::<Test>::mint(
|
assert_ok!(CoinsPallet::<Test>::mint(user2, Balance { coin: coin2, amount: Amount(amount) }));
|
||||||
user2,
|
|
||||||
Balance { coin: coin2.into(), amount: Amount(amount) }
|
|
||||||
));
|
|
||||||
let prior_sri_balance = 0;
|
let prior_sri_balance = 0;
|
||||||
assert_eq!(prior_sri_balance, balance(user2, coin1));
|
assert_eq!(prior_sri_balance, balance(user2, coin1));
|
||||||
assert_ok!(Dex::swap_exact_tokens_for_tokens(
|
assert_ok!(Dex::swap_exact_tokens_for_tokens(
|
||||||
RuntimeOrigin::signed(user2),
|
RuntimeOrigin::signed(user2),
|
||||||
bvec![coin2.into(), coin1],
|
bvec![coin2, coin1],
|
||||||
amount,
|
amount,
|
||||||
1,
|
1,
|
||||||
user2,
|
user2,
|
||||||
@@ -547,7 +600,7 @@ fn quote_price_tokens_for_exact_tokens_matches_execution() {
|
|||||||
let amount = 49;
|
let amount = 49;
|
||||||
let quoted_price = 1;
|
let quoted_price = 1;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Dex::quote_price_tokens_for_exact_tokens(coin2.try_into().unwrap(), amount, true,),
|
Dex::quote_price_tokens_for_exact_tokens(coin2, coin1, amount, true,),
|
||||||
Some(quoted_price)
|
Some(quoted_price)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
|
|||||||
|
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
use serai_primitives::ExternalCoin;
|
|
||||||
// Re-export all components
|
// Re-export all components
|
||||||
pub use serai_primitives as primitives;
|
pub use serai_primitives as primitives;
|
||||||
pub use primitives::{BlockNumber, Header};
|
pub use primitives::{BlockNumber, Header};
|
||||||
@@ -611,23 +610,25 @@ sp_api::impl_runtime_apis! {
|
|||||||
|
|
||||||
impl dex::DexApi<Block> for Runtime {
|
impl dex::DexApi<Block> for Runtime {
|
||||||
fn quote_price_exact_tokens_for_tokens(
|
fn quote_price_exact_tokens_for_tokens(
|
||||||
asset: ExternalCoin,
|
coin1: Coin,
|
||||||
|
coin2: Coin,
|
||||||
amount: SubstrateAmount,
|
amount: SubstrateAmount,
|
||||||
include_fee: bool
|
include_fee: bool
|
||||||
) -> Option<SubstrateAmount> {
|
) -> Option<SubstrateAmount> {
|
||||||
Dex::quote_price_exact_tokens_for_tokens(asset, amount, include_fee)
|
Dex::quote_price_exact_tokens_for_tokens(coin1, coin2, amount, include_fee)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quote_price_tokens_for_exact_tokens(
|
fn quote_price_tokens_for_exact_tokens(
|
||||||
asset: ExternalCoin,
|
coin1: Coin,
|
||||||
|
coin2: Coin,
|
||||||
amount: SubstrateAmount,
|
amount: SubstrateAmount,
|
||||||
include_fee: bool
|
include_fee: bool
|
||||||
) -> Option<SubstrateAmount> {
|
) -> Option<SubstrateAmount> {
|
||||||
Dex::quote_price_tokens_for_exact_tokens(asset, amount, include_fee)
|
Dex::quote_price_tokens_for_exact_tokens(coin1, coin2, amount, include_fee)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_reserves(asset1: Coin, asset2: Coin) -> Option<(SubstrateAmount, SubstrateAmount)> {
|
fn get_reserves(coin1: Coin, coin2: Coin) -> Option<(SubstrateAmount, SubstrateAmount)> {
|
||||||
Dex::get_reserves(&asset1, &asset2).ok()
|
Dex::get_reserves(&coin1, &coin2).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user