@@ -6,20 +6,36 @@ use jsonrpsee::{
66 proc_macros:: rpc,
77 types:: error:: { CallError , ErrorCode , ErrorObject } ,
88} ;
9+ use oracle_rpc_runtime_api:: BalanceWrapper ;
910use sp_api:: ProvideRuntimeApi ;
1011use sp_blockchain:: HeaderBackend ;
11- use sp_runtime:: traits:: Block as BlockT ;
12+ use sp_runtime:: {
13+ traits:: { Block as BlockT , MaybeDisplay , MaybeFromStr } ,
14+ DispatchError ,
15+ } ;
1216use std:: sync:: Arc ;
1317
1418pub use redeem_rpc_runtime_api:: RedeemApi as RedeemRuntimeApi ;
1519
20+ fn handle_response < T , E : std:: fmt:: Debug > ( result : Result < Result < T , DispatchError > , E > , msg : String ) -> RpcResult < T > {
21+ result
22+ . map_err ( |err| internal_err ( format ! ( "Runtime error: {:?}: {:?}" , msg, err) ) ) ?
23+ . map_err ( |err| internal_err ( format ! ( "Execution error: {:?}: {:?}" , msg, err) ) )
24+ }
25+
1626#[ rpc( client, server) ]
17- pub trait RedeemApi < BlockHash , AccountId , H256 , RedeemRequest > {
27+ pub trait RedeemApi < BlockHash , VaultId , Balance , AccountId , H256 , RedeemRequest >
28+ where
29+ Balance : Codec + MaybeDisplay + MaybeFromStr ,
30+ {
1831 #[ method( name = "redeem_getRedeemRequests" ) ]
1932 fn get_redeem_requests ( & self , account_id : AccountId , at : Option < BlockHash > ) -> RpcResult < Vec < H256 > > ;
2033
2134 #[ method( name = "redeem_getVaultRedeemRequests" ) ]
2235 fn get_vault_redeem_requests ( & self , vault_id : AccountId , at : Option < BlockHash > ) -> RpcResult < Vec < H256 > > ;
36+
37+ #[ method( name = "redeem_getPremiumRedeemVaults" , aliases = [ "vaultRegistry_getPremiumRedeemVaults" ] ) ]
38+ fn get_premium_redeem_vaults ( & self , at : Option < BlockHash > ) -> RpcResult < Vec < ( VaultId , BalanceWrapper < Balance > ) > > ;
2339}
2440
2541fn internal_err < T : ToString > ( message : T ) -> JsonRpseeError {
@@ -47,12 +63,14 @@ impl<C, B> Redeem<C, B> {
4763}
4864
4965#[ async_trait]
50- impl < C , Block , AccountId , H256 , RedeemRequest > RedeemApiServer < < Block as BlockT > :: Hash , AccountId , H256 , RedeemRequest >
51- for Redeem < C , Block >
66+ impl < C , Block , VaultId , Balance , AccountId , H256 , RedeemRequest >
67+ RedeemApiServer < < Block as BlockT > :: Hash , VaultId , Balance , AccountId , H256 , RedeemRequest > for Redeem < C , Block >
5268where
5369 Block : BlockT ,
5470 C : Send + Sync + ' static + ProvideRuntimeApi < Block > + HeaderBackend < Block > ,
55- C :: Api : RedeemRuntimeApi < Block , AccountId , H256 , RedeemRequest > ,
71+ C :: Api : RedeemRuntimeApi < Block , VaultId , Balance , AccountId , H256 , RedeemRequest > ,
72+ VaultId : Codec ,
73+ Balance : Codec + MaybeDisplay + MaybeFromStr ,
5674 AccountId : Codec ,
5775 H256 : Codec ,
5876 RedeemRequest : Codec ,
7694 api. get_vault_redeem_requests ( at, vault_id)
7795 . map_err ( |e| internal_err ( format ! ( "Unable to fetch redeem requests: {:?}" , e) ) )
7896 }
97+
98+ fn get_premium_redeem_vaults (
99+ & self ,
100+ at : Option < <Block as BlockT >:: Hash > ,
101+ ) -> RpcResult < Vec < ( VaultId , BalanceWrapper < Balance > ) > > {
102+ let api = self . client . runtime_api ( ) ;
103+ let at = at. unwrap_or_else ( || self . client . info ( ) . best_hash ) ;
104+
105+ handle_response (
106+ api. get_premium_redeem_vaults ( at) ,
107+ "Unable to find a vault below the premium redeem threshold" . into ( ) ,
108+ )
109+ }
79110}
0 commit comments