Skip to content

Commit ffa23bc

Browse files
authored
Merge pull request #1942 from opentensor/feat/sim-swap-rpc
Add swap simulating RPC
2 parents a99df56 + 41948a6 commit ffa23bc

File tree

7 files changed

+154
-12
lines changed

7 files changed

+154
-12
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/swap/rpc/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ sp-api.workspace = true
1111
sp-blockchain.workspace = true
1212
sp-runtime.workspace = true
1313
pallet-subtensor-swap-runtime-api.workspace = true
14+
subtensor-runtime-common = { workspace = true, default-features = false }
15+
subtensor-swap-interface.workspace = true
1416

1517
[features]
1618
default = ["std"]
@@ -19,4 +21,6 @@ std = [
1921
"pallet-subtensor-swap-runtime-api/std",
2022
"sp-api/std",
2123
"sp-runtime/std",
24+
"subtensor-runtime-common/std",
25+
"subtensor-swap-interface/std",
2226
]

pallets/swap/rpc/src/lib.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! RPC interface for the Swap pallet
22
3+
use codec::Encode;
34
use std::sync::Arc;
45

56
use jsonrpsee::{
@@ -10,13 +11,28 @@ use jsonrpsee::{
1011
use sp_api::ProvideRuntimeApi;
1112
use sp_blockchain::HeaderBackend;
1213
use sp_runtime::traits::Block as BlockT;
14+
use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency};
1315

1416
pub use pallet_subtensor_swap_runtime_api::SwapRuntimeApi;
1517

1618
#[rpc(client, server)]
1719
pub trait SwapRpcApi<BlockHash> {
1820
#[method(name = "swap_currentAlphaPrice")]
19-
fn current_alpha_price(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<u64>;
21+
fn current_alpha_price(&self, netuid: NetUid, at: Option<BlockHash>) -> RpcResult<u64>;
22+
#[method(name = "swap_simSwapTaoForAlpha")]
23+
fn sim_swap_tao_for_alpha(
24+
&self,
25+
netuid: NetUid,
26+
tao: TaoCurrency,
27+
at: Option<BlockHash>,
28+
) -> RpcResult<Vec<u8>>;
29+
#[method(name = "swap_simSwapAlphaForTao")]
30+
fn sim_swap_alpha_for_tao(
31+
&self,
32+
netuid: NetUid,
33+
alpha: AlphaCurrency,
34+
at: Option<BlockHash>,
35+
) -> RpcResult<Vec<u8>>;
2036
}
2137

2238
/// Error type of this RPC api.
@@ -65,7 +81,7 @@ where
6581
{
6682
fn current_alpha_price(
6783
&self,
68-
netuid: u16,
84+
netuid: NetUid,
6985
at: Option<<Block as BlockT>::Hash>,
7086
) -> RpcResult<u64> {
7187
let api = self.client.runtime_api();
@@ -75,4 +91,40 @@ where
7591
Error::RuntimeError(format!("Unable to get current alpha price: {e:?}")).into()
7692
})
7793
}
94+
95+
fn sim_swap_tao_for_alpha(
96+
&self,
97+
netuid: NetUid,
98+
tao: TaoCurrency,
99+
at: Option<<Block as BlockT>::Hash>,
100+
) -> RpcResult<Vec<u8>> {
101+
let api = self.client.runtime_api();
102+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
103+
104+
match api.sim_swap_tao_for_alpha(at, netuid, tao) {
105+
Ok(result) => Ok(result.encode()),
106+
Err(e) => Err(Error::RuntimeError(format!(
107+
"Unable to simulate tao -> alpha swap: {e:?}"
108+
))
109+
.into()),
110+
}
111+
}
112+
113+
fn sim_swap_alpha_for_tao(
114+
&self,
115+
netuid: NetUid,
116+
alpha: AlphaCurrency,
117+
at: Option<<Block as BlockT>::Hash>,
118+
) -> RpcResult<Vec<u8>> {
119+
let api = self.client.runtime_api();
120+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
121+
122+
match api.sim_swap_alpha_for_tao(at, netuid, alpha) {
123+
Ok(result) => Ok(result.encode()),
124+
Err(e) => Err(Error::RuntimeError(format!(
125+
"Unable to simulate alpha -> tao swap: {e:?}"
126+
))
127+
.into()),
128+
}
129+
}
78130
}

pallets/swap/runtime-api/Cargo.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@ edition.workspace = true
66

77
[dependencies]
88
codec = { workspace = true, features = ["derive"] }
9+
frame-support.workspace = true
910
scale-info.workspace = true
1011
sp-api.workspace = true
1112
sp-std.workspace = true
13+
subtensor-macros.workspace = true
14+
subtensor-runtime-common = { workspace = true, default-features = false }
15+
subtensor-swap-interface.workspace = true
1216

1317
[features]
1418
default = ["std"]
15-
std = ["codec/std", "scale-info/std", "sp-api/std", "sp-std/std"]
19+
std = [
20+
"codec/std",
21+
"frame-support/std",
22+
"scale-info/std",
23+
"sp-api/std",
24+
"sp-std/std",
25+
"subtensor-runtime-common/std",
26+
"subtensor-swap-interface/std"
27+
]

pallets/swap/runtime-api/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

3+
use frame_support::pallet_prelude::*;
4+
use subtensor_macros::freeze_struct;
5+
use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency};
6+
7+
#[freeze_struct("3a4fd213b5de5eb6")]
8+
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
9+
pub struct SimSwapResult {
10+
pub tao_amount: TaoCurrency,
11+
pub alpha_amount: AlphaCurrency,
12+
pub tao_fee: TaoCurrency,
13+
pub alpha_fee: AlphaCurrency,
14+
}
15+
316
sp_api::decl_runtime_apis! {
417
pub trait SwapRuntimeApi {
5-
fn current_alpha_price(netuid: u16) -> u64;
18+
fn current_alpha_price(netuid: NetUid) -> u64;
19+
fn sim_swap_tao_for_alpha(netuid: NetUid, tao: TaoCurrency) -> SimSwapResult;
20+
fn sim_swap_alpha_for_tao(netuid: NetUid, alpha: AlphaCurrency) -> SimSwapResult;
621
}
722
}

pallets/swap/src/pallet/impls.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,13 +1253,20 @@ impl<T: Config> SwapHandler<T::AccountId> for Pallet<T> {
12531253

12541254
Self::swap(netuid, order_t, amount, price_limit, false, true)
12551255
}
1256-
_ => Ok(SwapResult {
1257-
amount_paid_in: amount,
1258-
amount_paid_out: amount,
1259-
fee_paid: 0,
1260-
tao_reserve_delta: 0,
1261-
alpha_reserve_delta: 0,
1262-
}),
1256+
_ => {
1257+
let actual_amount = if T::SubnetInfo::exists(netuid) {
1258+
amount
1259+
} else {
1260+
0
1261+
};
1262+
Ok(SwapResult {
1263+
amount_paid_in: actual_amount,
1264+
amount_paid_out: actual_amount,
1265+
fee_paid: 0,
1266+
tao_reserve_delta: 0,
1267+
alpha_reserve_delta: 0,
1268+
})
1269+
}
12631270
}
12641271
}
12651272

runtime/src/lib.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use pallet_subtensor::rpc_info::{
3636
stake_info::StakeInfo,
3737
subnet_info::{SubnetHyperparams, SubnetHyperparamsV2, SubnetInfo, SubnetInfov2},
3838
};
39+
use pallet_subtensor_swap_runtime_api::SimSwapResult;
3940
use runtime_common::prod_or_fast;
4041
use sp_api::impl_runtime_apis;
4142
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
@@ -62,6 +63,7 @@ use sp_version::NativeVersion;
6263
use sp_version::RuntimeVersion;
6364
use subtensor_precompiles::Precompiles;
6465
use subtensor_runtime_common::{AlphaCurrency, TaoCurrency, time::*, *};
66+
use subtensor_swap_interface::{OrderType, SwapHandler};
6567

6668
// A few exports that help ease life for downstream crates.
6769
pub use frame_support::{
@@ -2408,13 +2410,57 @@ impl_runtime_apis! {
24082410
}
24092411

24102412
impl pallet_subtensor_swap_runtime_api::SwapRuntimeApi<Block> for Runtime {
2411-
fn current_alpha_price(netuid: u16) -> u64 {
2413+
fn current_alpha_price(netuid: NetUid) -> u64 {
24122414
use substrate_fixed::types::U96F32;
24132415

24142416
pallet_subtensor_swap::Pallet::<Runtime>::current_price(netuid.into())
24152417
.saturating_mul(U96F32::from_num(1_000_000_000))
24162418
.saturating_to_num()
24172419
}
2420+
2421+
fn sim_swap_tao_for_alpha(netuid: NetUid, tao: TaoCurrency) -> SimSwapResult {
2422+
pallet_subtensor_swap::Pallet::<Runtime>::sim_swap(
2423+
netuid.into(),
2424+
OrderType::Buy,
2425+
tao.into(),
2426+
)
2427+
.map_or_else(
2428+
|_| SimSwapResult {
2429+
tao_amount: 0.into(),
2430+
alpha_amount: 0.into(),
2431+
tao_fee: 0.into(),
2432+
alpha_fee: 0.into(),
2433+
},
2434+
|sr| SimSwapResult {
2435+
tao_amount: sr.amount_paid_in.into(),
2436+
alpha_amount: sr.amount_paid_out.into(),
2437+
tao_fee: sr.fee_paid.into(),
2438+
alpha_fee: 0.into(),
2439+
},
2440+
)
2441+
}
2442+
2443+
fn sim_swap_alpha_for_tao(netuid: NetUid, alpha: AlphaCurrency) -> SimSwapResult {
2444+
pallet_subtensor_swap::Pallet::<Runtime>::sim_swap(
2445+
netuid.into(),
2446+
OrderType::Sell,
2447+
alpha.into(),
2448+
)
2449+
.map_or_else(
2450+
|_| SimSwapResult {
2451+
tao_amount: 0.into(),
2452+
alpha_amount: 0.into(),
2453+
tao_fee: 0.into(),
2454+
alpha_fee: 0.into(),
2455+
},
2456+
|sr| SimSwapResult {
2457+
tao_amount: sr.amount_paid_out.into(),
2458+
alpha_amount: sr.amount_paid_in.into(),
2459+
tao_fee: 0.into(),
2460+
alpha_fee: sr.fee_paid.into(),
2461+
},
2462+
)
2463+
}
24182464
}
24192465
}
24202466

0 commit comments

Comments
 (0)