1
1
//! RPC interface for the custom Subtensor rpc methods
2
2
3
+ use codec:: { Decode , Encode } ;
3
4
use jsonrpsee:: {
4
5
core:: RpcResult ,
5
6
proc_macros:: rpc,
6
7
types:: { error:: ErrorObject , ErrorObjectOwned } ,
7
8
} ;
8
9
use sp_blockchain:: HeaderBackend ;
9
- use sp_runtime:: traits:: Block as BlockT ;
10
+ use sp_runtime:: { traits:: Block as BlockT , AccountId32 } ;
10
11
use std:: sync:: Arc ;
11
12
12
13
use sp_api:: ProvideRuntimeApi ;
@@ -116,9 +117,12 @@ where
116
117
let api = self . client . runtime_api ( ) ;
117
118
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
118
119
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
+ }
122
126
}
123
127
124
128
fn get_delegate (
@@ -129,9 +133,20 @@ where
129
133
let api = self . client . runtime_api ( ) ;
130
134
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
131
135
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
+ }
135
150
}
136
151
137
152
fn get_delegated (
@@ -142,9 +157,20 @@ where
142
157
let api = self . client . runtime_api ( ) ;
143
158
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
144
159
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
+ }
148
174
}
149
175
150
176
fn get_neurons_lite (
@@ -155,9 +181,12 @@ where
155
181
let api = self . client . runtime_api ( ) ;
156
182
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
157
183
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
+ }
161
190
}
162
191
163
192
fn get_neuron_lite (
@@ -169,17 +198,24 @@ where
169
198
let api = self . client . runtime_api ( ) ;
170
199
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
171
200
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
+ }
175
207
}
176
208
177
209
fn get_neurons ( & self , netuid : u16 , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
178
210
let api = self . client . runtime_api ( ) ;
179
211
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
180
212
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
+ }
183
219
}
184
220
185
221
fn get_neuron (
@@ -191,8 +227,12 @@ where
191
227
let api = self . client . runtime_api ( ) ;
192
228
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
193
229
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
+ }
196
236
}
197
237
198
238
fn get_subnet_info (
@@ -203,8 +243,12 @@ where
203
243
let api = self . client . runtime_api ( ) ;
204
244
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
205
245
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
+ }
208
252
}
209
253
210
254
fn get_subnet_hyperparams (
@@ -215,23 +259,36 @@ where
215
259
let api = self . client . runtime_api ( ) ;
216
260
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
217
261
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
+ }
220
268
}
221
269
222
270
fn get_all_dynamic_info ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
223
271
let api = self . client . runtime_api ( ) ;
224
272
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
+ }
228
282
}
229
283
230
284
fn get_all_metagraphs ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
231
285
let api = self . client . runtime_api ( ) ;
232
286
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
+ }
235
292
}
236
293
237
294
fn get_dynamic_info (
@@ -241,9 +298,15 @@ where
241
298
) -> RpcResult < Vec < u8 > > {
242
299
let api = self . client . runtime_api ( ) ;
243
300
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
+ }
247
310
}
248
311
249
312
fn get_metagraph (
@@ -253,9 +316,14 @@ where
253
316
) -> RpcResult < Vec < u8 > > {
254
317
let api = self . client . runtime_api ( ) ;
255
318
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
+ }
259
327
}
260
328
261
329
fn get_subnet_state (
@@ -265,17 +333,25 @@ where
265
333
) -> RpcResult < Vec < u8 > > {
266
334
let api = self . client . runtime_api ( ) ;
267
335
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
+ }
271
343
}
272
344
273
345
fn get_subnets_info ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
274
346
let api = self . client . runtime_api ( ) ;
275
347
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
276
348
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
+ }
279
355
}
280
356
281
357
fn get_subnet_info_v2 (
@@ -286,16 +362,24 @@ where
286
362
let api = self . client . runtime_api ( ) ;
287
363
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
288
364
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
+ }
291
371
}
292
372
293
373
fn get_subnets_info_v2 ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < Vec < u8 > > {
294
374
let api = self . client . runtime_api ( ) ;
295
375
let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
296
376
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
+ }
299
383
}
300
384
301
385
fn get_network_lock_cost ( & self , at : Option < <Block as BlockT >:: Hash > ) -> RpcResult < u64 > {
0 commit comments