diff --git a/processor/src/main.rs b/processor/src/main.rs index 441db6755..98b09a066 100644 --- a/processor/src/main.rs +++ b/processor/src/main.rs @@ -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, @@ -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"), } } diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index cb42e88d3..10dcb11e0 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -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 { - 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() @@ -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 { - 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() @@ -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; @@ -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; diff --git a/substrate/dex/pallet/src/tests.rs b/substrate/dex/pallet/src/tests.rs index 1e8bce256..57573115e 100644 --- a/substrate/dex/pallet/src/tests.rs +++ b/substrate/dex/pallet/src/tests.rs @@ -409,9 +409,9 @@ 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::::mint(user, Balance { coin: coin1, amount: Amount(100000) })); assert_ok!(CoinsPallet::::mint( @@ -419,38 +419,82 @@ fn can_quote_price() { 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 @@ -458,24 +502,28 @@ fn can_quote_price() { 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) ); }); @@ -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::::mint(user, Balance { coin: coin1, amount: Amount(100000) })); - assert_ok!(CoinsPallet::::mint( + assert_ok!(CoinsPallet::::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::::mint( - user2, - Balance { coin: coin2.into(), amount: Amount(amount) } - )); + assert_ok!(CoinsPallet::::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, @@ -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) ); diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index c3b1365e8..93aba991d 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -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}; @@ -611,23 +610,25 @@ sp_api::impl_runtime_apis! { impl dex::DexApi for Runtime { fn quote_price_exact_tokens_for_tokens( - asset: ExternalCoin, + coin1: Coin, + coin2: Coin, amount: SubstrateAmount, include_fee: bool ) -> Option { - 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 { - 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() } } }