11//! RPC interface for the Swap pallet
22
3+ use codec:: Encode ;
34use std:: sync:: Arc ;
45
56use jsonrpsee:: {
@@ -10,13 +11,28 @@ use jsonrpsee::{
1011use sp_api:: ProvideRuntimeApi ;
1112use sp_blockchain:: HeaderBackend ;
1213use sp_runtime:: traits:: Block as BlockT ;
14+ use subtensor_runtime_common:: { AlphaCurrency , NetUid , TaoCurrency } ;
1315
1416pub use pallet_subtensor_swap_runtime_api:: SwapRuntimeApi ;
1517
1618#[ rpc( client, server) ]
1719pub 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.
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 ( ) ;
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}
0 commit comments