@@ -180,6 +180,7 @@ mod tests;
180180pub struct QueryRoot < C : ClientContext > {
181181 client : ChainClient < C :: Environment > ,
182182 genesis_config : Arc < GenesisConfig > ,
183+ faucet_storage : Arc < FaucetDatabase > ,
183184}
184185
185186/// The root GraphQL mutation type.
@@ -265,6 +266,26 @@ where
265266 async fn current_committee ( & self ) -> Result < Committee , Error > {
266267 Ok ( self . client . local_committee ( ) . await ?)
267268 }
269+
270+ /// Find the existing a chain with the given authentication key, if any.
271+ async fn chain_id ( & self , owner : AccountOwner ) -> Result < ChainId , Error > {
272+ // Check if this owner already has a chain.
273+ #[ cfg( with_metrics) ]
274+ let db_start_time = std:: time:: Instant :: now ( ) ;
275+
276+ let chain_id = self
277+ . faucet_storage
278+ . get_chain_id ( & owner)
279+ . await
280+ . map_err ( |e| Error :: new ( e. to_string ( ) ) ) ?;
281+
282+ #[ cfg( with_metrics) ]
283+ metrics:: DATABASE_OPERATION_LATENCY
284+ . with_label_values ( & [ "get_chain_id" ] )
285+ . observe ( db_start_time. elapsed ( ) . as_secs_f64 ( ) * 1000.0 ) ;
286+
287+ chain_id. ok_or ( Error :: new ( "This user has no chain yet" ) )
288+ }
268289}
269290
270291#[ async_graphql:: Object ( cache_control( no_cache) ) ]
@@ -896,6 +917,7 @@ where
896917 let query_root = QueryRoot {
897918 genesis_config : Arc :: clone ( & self . genesis_config ) ,
898919 client : self . client . clone ( ) ,
920+ faucet_storage : Arc :: clone ( & self . faucet_storage ) ,
899921 } ;
900922 Schema :: build ( query_root, mutation_root, EmptySubscription ) . finish ( )
901923 }
0 commit comments