11//! RPC interface for the custom Subtensor rpc methods
22
3+ use codec:: { Decode , Encode } ;
34use jsonrpsee:: {
45 core:: RpcResult ,
56 proc_macros:: rpc,
67 types:: { error:: ErrorObject , ErrorObjectOwned } ,
78} ;
89use sp_blockchain:: HeaderBackend ;
9- use sp_runtime:: traits:: Block as BlockT ;
10+ use sp_runtime:: { traits:: Block as BlockT , AccountId32 } ;
1011use std:: sync:: Arc ;
1112
1213use sp_api:: ProvideRuntimeApi ;
@@ -116,9 +117,12 @@ where
116117 let api = self . client . runtime_api ( ) ;
117118 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
118119
119- api. get_delegates ( at) . map_err ( |e| {
120- Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( )
121- } )
120+ match api. get_delegates ( at) {
121+ Ok ( result) => Ok ( result. encode ( ) ) ,
122+ Err ( e) => {
123+ Err ( Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( ) )
124+ }
125+ }
122126 }
123127
124128 fn get_delegate (
@@ -129,9 +133,20 @@ where
129133 let api = self . client . runtime_api ( ) ;
130134 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
131135
132- api. get_delegate ( at, delegate_account_vec) . map_err ( |e| {
133- Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( )
134- } )
136+ let delegate_account = match AccountId32 :: decode ( & mut & delegate_account_vec[ ..] ) {
137+ Ok ( delegate_account) => delegate_account,
138+ Err ( e) => {
139+ return Err (
140+ Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( ) ,
141+ )
142+ }
143+ } ;
144+ match api. get_delegate ( at, delegate_account) {
145+ Ok ( result) => Ok ( result. encode ( ) ) ,
146+ Err ( e) => {
147+ Err ( Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( ) )
148+ }
149+ }
135150 }
136151
137152 fn get_delegated (
@@ -142,9 +157,20 @@ where
142157 let api = self . client . runtime_api ( ) ;
143158 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
144159
145- api. get_delegated ( at, delegatee_account_vec) . map_err ( |e| {
146- Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( )
147- } )
160+ let delegatee_account = match AccountId32 :: decode ( & mut & delegatee_account_vec[ ..] ) {
161+ Ok ( delegatee_account) => delegatee_account,
162+ Err ( e) => {
163+ return Err (
164+ Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( ) ,
165+ )
166+ }
167+ } ;
168+ match api. get_delegated ( at, delegatee_account) {
169+ Ok ( result) => Ok ( result. encode ( ) ) ,
170+ Err ( e) => {
171+ Err ( Error :: RuntimeError ( format ! ( "Unable to get delegates info: {:?}" , e) ) . into ( ) )
172+ }
173+ }
148174 }
149175
150176 fn get_neurons_lite (
@@ -155,9 +181,12 @@ where
155181 let api = self . client . runtime_api ( ) ;
156182 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
157183
158- api. get_neurons_lite ( at, netuid) . map_err ( |e| {
159- Error :: RuntimeError ( format ! ( "Unable to get neurons lite info: {:?}" , e) ) . into ( )
160- } )
184+ match api. get_neurons_lite ( at, netuid) {
185+ Ok ( result) => Ok ( result. encode ( ) ) ,
186+ Err ( e) => {
187+ Err ( Error :: RuntimeError ( format ! ( "Unable to get neurons lite info: {:?}" , e) ) . into ( ) )
188+ }
189+ }
161190 }
162191
163192 fn get_neuron_lite (
@@ -169,17 +198,24 @@ where
169198 let api = self . client . runtime_api ( ) ;
170199 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
171200
172- api. get_neuron_lite ( at, netuid, uid) . map_err ( |e| {
173- Error :: RuntimeError ( format ! ( "Unable to get neurons lite info: {:?}" , e) ) . into ( )
174- } )
201+ match api. get_neuron_lite ( at, netuid, uid) {
202+ Ok ( result) => Ok ( result. encode ( ) ) ,
203+ Err ( e) => {
204+ Err ( Error :: RuntimeError ( format ! ( "Unable to get neurons lite info: {:?}" , e) ) . into ( ) )
205+ }
206+ }
175207 }
176208
177209 fn get_neurons ( & self , netuid : u16 , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
178210 let api = self . client . runtime_api ( ) ;
179211 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
180212
181- api. get_neurons ( at, netuid)
182- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get neurons info: {:?}" , e) ) . into ( ) )
213+ match api. get_neurons ( at, netuid) {
214+ Ok ( result) => Ok ( result. encode ( ) ) ,
215+ Err ( e) => {
216+ Err ( Error :: RuntimeError ( format ! ( "Unable to get neurons info: {:?}" , e) ) . into ( ) )
217+ }
218+ }
183219 }
184220
185221 fn get_neuron (
@@ -191,8 +227,12 @@ where
191227 let api = self . client . runtime_api ( ) ;
192228 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
193229
194- api. get_neuron ( at, netuid, uid)
195- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get neuron info: {:?}" , e) ) . into ( ) )
230+ match api. get_neuron ( at, netuid, uid) {
231+ Ok ( result) => Ok ( result. encode ( ) ) ,
232+ Err ( e) => {
233+ Err ( Error :: RuntimeError ( format ! ( "Unable to get neuron info: {:?}" , e) ) . into ( ) )
234+ }
235+ }
196236 }
197237
198238 fn get_subnet_info (
@@ -203,8 +243,12 @@ where
203243 let api = self . client . runtime_api ( ) ;
204244 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
205245
206- api. get_subnet_info ( at, netuid)
207- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get subnet info: {:?}" , e) ) . into ( ) )
246+ match api. get_subnet_info ( at, netuid) {
247+ Ok ( result) => Ok ( result. encode ( ) ) ,
248+ Err ( e) => {
249+ Err ( Error :: RuntimeError ( format ! ( "Unable to get subnet info: {:?}" , e) ) . into ( ) )
250+ }
251+ }
208252 }
209253
210254 fn get_subnet_hyperparams (
@@ -215,23 +259,36 @@ where
215259 let api = self . client . runtime_api ( ) ;
216260 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
217261
218- api. get_subnet_hyperparams ( at, netuid)
219- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get subnet info: {:?}" , e) ) . into ( ) )
262+ match api. get_subnet_hyperparams ( at, netuid) {
263+ Ok ( result) => Ok ( result. encode ( ) ) ,
264+ Err ( e) => {
265+ Err ( Error :: RuntimeError ( format ! ( "Unable to get subnet info: {:?}" , e) ) . into ( ) )
266+ }
267+ }
220268 }
221269
222270 fn get_all_dynamic_info ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
223271 let api = self . client . runtime_api ( ) ;
224272 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
225- api. get_all_dynamic_info ( at) . map_err ( |e| {
226- Error :: RuntimeError ( format ! ( "Unable to get dynamic subnets info: {:?}" , e) ) . into ( )
227- } )
273+
274+ match api. get_all_dynamic_info ( at) {
275+ Ok ( result) => Ok ( result. encode ( ) ) ,
276+ Err ( e) => Err ( Error :: RuntimeError ( format ! (
277+ "Unable to get dynamic subnets info: {:?}" ,
278+ e
279+ ) )
280+ . into ( ) ) ,
281+ }
228282 }
229283
230284 fn get_all_metagraphs ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
231285 let api = self . client . runtime_api ( ) ;
232286 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
233- api. get_all_metagraphs ( at)
234- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get metagraps: {:?}" , e) ) . into ( ) )
287+
288+ match api. get_all_metagraphs ( at) {
289+ Ok ( result) => Ok ( result. encode ( ) ) ,
290+ Err ( e) => Err ( Error :: RuntimeError ( format ! ( "Unable to get metagraps: {:?}" , e) ) . into ( ) ) ,
291+ }
235292 }
236293
237294 fn get_dynamic_info (
@@ -241,9 +298,15 @@ where
241298 ) -> RpcResult < Vec < u8 > > {
242299 let api = self . client . runtime_api ( ) ;
243300 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
244- api. get_dynamic_info ( at, netuid) . map_err ( |e| {
245- Error :: RuntimeError ( format ! ( "Unable to get dynamic subnets info: {:?}" , e) ) . into ( )
246- } )
301+
302+ match api. get_dynamic_info ( at, netuid) {
303+ Ok ( result) => Ok ( result. encode ( ) ) ,
304+ Err ( e) => Err ( Error :: RuntimeError ( format ! (
305+ "Unable to get dynamic subnets info: {:?}" ,
306+ e
307+ ) )
308+ . into ( ) ) ,
309+ }
247310 }
248311
249312 fn get_metagraph (
@@ -253,9 +316,14 @@ where
253316 ) -> RpcResult < Vec < u8 > > {
254317 let api = self . client . runtime_api ( ) ;
255318 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
256- api. get_metagraph ( at, netuid) . map_err ( |e| {
257- Error :: RuntimeError ( format ! ( "Unable to get dynamic subnets info: {:?}" , e) ) . into ( )
258- } )
319+ match api. get_metagraph ( at, netuid) {
320+ Ok ( result) => Ok ( result. encode ( ) ) ,
321+ Err ( e) => Err ( Error :: RuntimeError ( format ! (
322+ "Unable to get dynamic subnets info: {:?}" ,
323+ e
324+ ) )
325+ . into ( ) ) ,
326+ }
259327 }
260328
261329 fn get_subnet_state (
@@ -265,17 +333,25 @@ where
265333 ) -> RpcResult < Vec < u8 > > {
266334 let api = self . client . runtime_api ( ) ;
267335 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
268- api. get_subnet_state ( at, netuid) . map_err ( |e| {
269- Error :: RuntimeError ( format ! ( "Unable to get subnet state info: {:?}" , e) ) . into ( )
270- } )
336+
337+ match api. get_subnet_state ( at, netuid) {
338+ Ok ( result) => Ok ( result. encode ( ) ) ,
339+ Err ( e) => {
340+ Err ( Error :: RuntimeError ( format ! ( "Unable to get subnet state info: {:?}" , e) ) . into ( ) )
341+ }
342+ }
271343 }
272344
273345 fn get_subnets_info ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
274346 let api = self . client . runtime_api ( ) ;
275347 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
276348
277- api. get_subnets_info ( at)
278- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get subnets info: {:?}" , e) ) . into ( ) )
349+ match api. get_subnets_info ( at) {
350+ Ok ( result) => Ok ( result. encode ( ) ) ,
351+ Err ( e) => {
352+ Err ( Error :: RuntimeError ( format ! ( "Unable to get subnets info: {:?}" , e) ) . into ( ) )
353+ }
354+ }
279355 }
280356
281357 fn get_subnet_info_v2 (
@@ -286,16 +362,24 @@ where
286362 let api = self . client . runtime_api ( ) ;
287363 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
288364
289- api. get_subnet_info_v2 ( at, netuid)
290- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get subnet info: {:?}" , e) ) . into ( ) )
365+ match api. get_subnet_info_v2 ( at, netuid) {
366+ Ok ( result) => Ok ( result. encode ( ) ) ,
367+ Err ( e) => {
368+ Err ( Error :: RuntimeError ( format ! ( "Unable to get subnet info: {:?}" , e) ) . into ( ) )
369+ }
370+ }
291371 }
292372
293373 fn get_subnets_info_v2 ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
294374 let api = self . client . runtime_api ( ) ;
295375 let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
296376
297- api. get_subnets_info_v2 ( at)
298- . map_err ( |e| Error :: RuntimeError ( format ! ( "Unable to get subnets info: {:?}" , e) ) . into ( ) )
377+ match api. get_subnets_info_v2 ( at) {
378+ Ok ( result) => Ok ( result. encode ( ) ) ,
379+ Err ( e) => {
380+ Err ( Error :: RuntimeError ( format ! ( "Unable to get subnets info: {:?}" , e) ) . into ( ) )
381+ }
382+ }
299383 }
300384
301385 fn get_network_lock_cost ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < u64 > {
0 commit comments