Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Sep 27, 2024
1 parent 4617462 commit 5ab6268
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 54 deletions.
5 changes: 5 additions & 0 deletions processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ async fn main() {

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 {
#[cfg(feature = "bitcoin")]
ExternalNetworkId::Bitcoin => run(db, Bitcoin::new(url).await, coordinator).await,
Expand All @@ -759,5 +763,6 @@ async fn main() {
}
#[cfg(feature = "monero")]
ExternalNetworkId::Monero => run(db, Monero::new(url).await, coordinator).await,
_ => panic!("spawning a processor for an unsupported network"),
}
}
26 changes: 15 additions & 11 deletions substrate/dex/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,17 +1002,18 @@ pub mod pallet {
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(
coin: ExternalCoin,
coin1: Coin,
coin2: Coin,
amount: SubstrateAmount,
include_fee: bool,
) -> 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 balance1 = Self::get_balance(&pool_account, Coin::native());
let balance2 = Self::get_balance(&pool_account, coin.into());
let balance1 = Self::get_balance(&pool_account, coin1);
let balance2 = Self::get_balance(&pool_account, coin2);
if balance1 != 0 {
if include_fee {
Self::get_amount_out(amount, balance1, balance2).ok()
Expand All @@ -1026,15 +1027,16 @@ pub mod pallet {

/// Used by the RPC service to provide current prices.
pub fn quote_price_tokens_for_exact_tokens(
coin: ExternalCoin,
coin1: Coin,
coin2: Coin,
amount: SubstrateAmount,
include_fee: bool,
) -> 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 balance1 = Self::get_balance(&pool_account, Coin::native());
let balance2 = Self::get_balance(&pool_account, coin.into());
let balance1 = Self::get_balance(&pool_account, coin1);
let balance2 = Self::get_balance(&pool_account, coin2);
if balance1 != 0 {
if include_fee {
Self::get_amount_in(amount, balance1, balance2).ok()
Expand Down Expand Up @@ -1239,7 +1241,8 @@ sp_api::decl_runtime_apis! {
/// Note that the price may have changed by the time the transaction is executed.
/// (Use `amount_in_max` to control slippage.)
fn quote_price_tokens_for_exact_tokens(
coin: ExternalCoin,
coin1: Coin,
coin2: Coin,
amount: SubstrateAmount,
include_fee: bool
) -> Option<SubstrateAmount>;
Expand All @@ -1249,7 +1252,8 @@ sp_api::decl_runtime_apis! {
/// Note that the price may have changed by the time the transaction is executed.
/// (Use `amount_out_min` to control slippage.)
fn quote_price_exact_tokens_for_tokens(
coin: ExternalCoin,
coin1: Coin,
coin2: Coin,
amount: SubstrateAmount,
include_fee: bool
) -> Option<SubstrateAmount>;
Expand Down
125 changes: 89 additions & 36 deletions substrate/dex/pallet/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,73 +409,121 @@ fn can_quote_price() {
new_test_ext().execute_with(|| {
let user = system_address(b"user1").into();
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: 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...
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:
// (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...
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:
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...
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:
//
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...
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:
// (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...
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:
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...
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
//
let amount_in = 100;

assert_eq!(
Dex::quote_price_exact_tokens_for_tokens(coin2, amount_in, false,)
.and_then(|amount| { Dex::quote_price_exact_tokens_for_tokens(coin2, amount, false) }),
Dex::quote_price_exact_tokens_for_tokens(coin2, Coin::native(), amount_in, false,).and_then(
|amount| Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, amount, false,)
),
Some(amount_in)
);
assert_eq!(
Dex::quote_price_exact_tokens_for_tokens(coin2, amount_in, false,)
.and_then(|amount| Dex::quote_price_exact_tokens_for_tokens(coin2, amount, false,)),
Dex::quote_price_exact_tokens_for_tokens(Coin::native(), coin2, amount_in, false,).and_then(
|amount| Dex::quote_price_exact_tokens_for_tokens(coin2, Coin::native(), amount, false,)
),
Some(amount_in)
);

assert_eq!(
Dex::quote_price_tokens_for_exact_tokens(coin2, amount_in, false,)
.and_then(|amount| { Dex::quote_price_tokens_for_exact_tokens(coin2, amount, false) }),
Dex::quote_price_tokens_for_exact_tokens(coin2, Coin::native(), amount_in, false,).and_then(
|amount| Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, amount, false,)
),
Some(amount_in)
);
assert_eq!(
Dex::quote_price_tokens_for_exact_tokens(coin2, amount_in, false,)
.and_then(|amount| Dex::quote_price_tokens_for_exact_tokens(coin2, amount, false,)),
Dex::quote_price_tokens_for_exact_tokens(Coin::native(), coin2, amount_in, false,).and_then(
|amount| Dex::quote_price_tokens_for_exact_tokens(coin2, Coin::native(), amount, false,)
),
Some(amount_in)
);
});
Expand All @@ -487,31 +535,36 @@ 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 = 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(
assert_ok!(CoinsPallet::<Test>::mint(user, Balance { coin: coin2, amount: Amount(1000) }));

assert_ok!(Dex::add_liquidity(
RuntimeOrigin::signed(user),
coin2.try_into().unwrap(),
200,
10000,
1,
1,
user,
Balance { coin: coin2.into(), amount: Amount(1000) }
));

assert_ok!(Dex::add_liquidity(RuntimeOrigin::signed(user), coin2, 200, 10000, 1, 1, user,));

let amount = 1;
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(
user2,
Balance { coin: coin2.into(), amount: Amount(amount) }
));
assert_ok!(CoinsPallet::<Test>::mint(user2, Balance { coin: coin2, amount: Amount(amount) }));
let prior_sri_balance = 0;
assert_eq!(prior_sri_balance, balance(user2, coin1));
assert_ok!(Dex::swap_exact_tokens_for_tokens(
RuntimeOrigin::signed(user2),
bvec![coin2.into(), coin1],
bvec![coin2, coin1],
amount,
1,
user2,
Expand Down Expand Up @@ -547,7 +600,7 @@ fn quote_price_tokens_for_exact_tokens_matches_execution() {
let amount = 49;
let quoted_price = 1;
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)
);

Expand Down
15 changes: 8 additions & 7 deletions substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use core::marker::PhantomData;

use serai_primitives::ExternalCoin;
// Re-export all components
pub use serai_primitives as primitives;
pub use primitives::{BlockNumber, Header};
Expand Down Expand Up @@ -611,23 +610,25 @@ sp_api::impl_runtime_apis! {

impl dex::DexApi<Block> for Runtime {
fn quote_price_exact_tokens_for_tokens(
asset: ExternalCoin,
coin1: Coin,
coin2: Coin,
amount: SubstrateAmount,
include_fee: bool
) -> 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(
asset: ExternalCoin,
coin1: Coin,
coin2: Coin,
amount: SubstrateAmount,
include_fee: bool
) -> 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)> {
Dex::get_reserves(&asset1, &asset2).ok()
fn get_reserves(coin1: Coin, coin2: Coin) -> Option<(SubstrateAmount, SubstrateAmount)> {
Dex::get_reserves(&coin1, &coin2).ok()
}
}
}

0 comments on commit 5ab6268

Please sign in to comment.