Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
use core::convert::{Into, TryFrom};
use std::task::{Context, Poll};
use std::{pin::Pin, sync::Arc};

use async_trait::async_trait;
use futures_core::Stream;
use futures_util::{future::try_join_all, StreamExt, TryStreamExt};
use tonic::service::interceptor::InterceptedService;
use tonic::{
    transport::{Channel, Endpoint},
    Request,
};

use m10_protos::sdk::{
    self, m10_query_service_client::M10QueryServiceClient,
    m10_tx_service_client::M10TxServiceClient,
};
use m10_signing::Signer;

use crate::OauthInterceptor;
use crate::{
    account::AccountId,
    builders::*,
    error::{M10Error, M10Result},
    types::*,
};

struct SyncStream<S> {
    inner: S,
}

impl<S> SyncStream<S> {
    fn new(inner: S) -> Self {
        Self { inner }
    }
}

impl<S> Stream for SyncStream<S>
where
    S: Stream + Send + 'static,
{
    type Item = S::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_next(cx) }
    }
}

unsafe impl<S> Sync for SyncStream<S> where S: Send {}

pub struct GrpcClient<S> {
    tx_client: M10TxServiceClient<InterceptedService<Channel, OauthInterceptor>>,
    query_client: M10QueryServiceClient<InterceptedService<Channel, OauthInterceptor>>,
    signer: Option<Arc<S>>,
}

impl<S> Clone for GrpcClient<S> {
    fn clone(&self) -> Self {
        Self {
            tx_client: self.tx_client.clone(),
            query_client: self.query_client.clone(),
            signer: self.signer.clone(),
        }
    }
}

impl<S> GrpcClient<S> {
    pub fn new(endpoint: Endpoint, signer: Option<Arc<S>>) -> M10Result<Self> {
        let oauth_interceptor = OauthInterceptor::new();
        let intercepted_channel =
            InterceptedService::new(endpoint.connect_lazy(), oauth_interceptor);
        let tx_client = M10TxServiceClient::new(intercepted_channel.clone());
        let query_client = M10QueryServiceClient::new(intercepted_channel);
        Ok(Self {
            tx_client,
            query_client,
            signer,
        })
    }

    pub fn new_with_access_token(
        endpoint: Endpoint,
        signer: Option<Arc<S>>,
        access_token: Option<&str>,
    ) -> M10Result<Self> {
        let oauth_interceptor = OauthInterceptor::new();
        if let Some(token) = access_token {
            oauth_interceptor.set_token(token);
        };
        let intercepted_channel =
            InterceptedService::new(endpoint.connect_lazy(), oauth_interceptor);
        let tx_client = M10TxServiceClient::new(intercepted_channel.clone());
        let query_client = M10QueryServiceClient::new(intercepted_channel);
        Ok(Self {
            tx_client,
            query_client,
            signer,
        })
    }

    pub async fn connect(endpoint: Endpoint, signer: Option<Arc<S>>) -> M10Result<Self> {
        let oauth_interceptor = OauthInterceptor::new();
        let intercepted_channel =
            InterceptedService::new(endpoint.connect().await?, oauth_interceptor);
        let tx_client = M10TxServiceClient::new(intercepted_channel.clone());
        let query_client = M10QueryServiceClient::new(intercepted_channel);
        Ok(Self {
            tx_client,
            query_client,
            signer,
        })
    }
}

#[async_trait]
impl<S: Signer> crate::m10_core_client::M10CoreClient for GrpcClient<S> {
    type Signer = S;

    fn signer(&self) -> M10Result<Arc<S>> {
        self.signer.clone().ok_or(M10Error::NoSigner)
    }

    fn set_signer(&mut self, signer: Arc<Self::Signer>) {
        self.signer = Some(signer);
    }

    // Transactions
    async fn create_transaction(
        &self,
        payload: sdk::RequestEnvelope,
    ) -> M10Result<sdk::TransactionResponse> {
        Ok(self
            .tx_client
            .clone()
            .create_transaction(Request::new(payload))
            .await
            .map(|res| res.into_inner())?
            .tx_error()?)
    }

    async fn bulk_create_transactions(
        &self,
        payload: sdk::BulkTransactions,
    ) -> M10Result<sdk::BulkTransactionsResponse> {
        Ok(self
            .tx_client
            .clone()
            .bulk_create_transactions(Request::new(payload))
            .await
            .map(|res| res.into_inner())?)
    }

    // Queries
    async fn get_block_height(&self) -> M10Result<u64> {
        Ok(self
            .query_client
            .clone()
            .get_chain_info(Request::new(()))
            .await?
            .into_inner()
            .block_height)
    }

    async fn get_offline_key(&self) -> M10Result<Vec<u8>> {
        Ok(self
            .query_client
            .clone()
            .get_offline_key(Request::new(()))
            .await?
            .into_inner()
            .offline_pk)
    }

    async fn get_account(&self, id: AccountId) -> M10Result<Account> {
        let account = self.get_indexed_account(id).await?;
        Account::try_from(account)
    }

    async fn get_indexed_account(&self, id: AccountId) -> M10Result<sdk::IndexedAccount> {
        let req = self
            .signer()?
            .sign_request(sdk::GetAccountRequest { id: id.to_vec() })
            .await?;
        let account = self
            .query_client
            .clone()
            .get_indexed_account(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Ok(account)
    }

    async fn get_account_info(&self, id: AccountId) -> M10Result<AccountInfo> {
        let account = self.get_raw_account_info(id.to_vec()).await?;
        AccountInfo::try_from(account)
    }

    async fn get_raw_account_info(&self, id: Vec<u8>) -> M10Result<sdk::AccountInfo> {
        let req = self
            .signer()?
            .sign_request(sdk::GetAccountRequest { id })
            .await?;
        let account = self
            .query_client
            .clone()
            .get_account_info(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Ok(account)
    }

    async fn list_accounts(
        &self,
        filter: PageBuilder<Vec<u8>, AccountMetadataFilter>,
    ) -> M10Result<Vec<Account>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListAccountMetadataRequest>(filter.into())
            .await?;
        let accounts = self
            .query_client
            .clone()
            .list_account_metadata(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let accounts = try_join_all(
            accounts
                .accounts
                .into_iter()
                // TODO: Remove indexed vs non-indexed differentiation
                .filter_map(|account| match AccountId::try_from_be_slice(&account.id) {
                    Ok(id) => Some(async move { self.get_account(id).await }),
                    Err(_) => None,
                }),
        )
        .await?;
        Ok(accounts)
    }

    // Transfers

    async fn get_transfer(&self, tx_id: TxId) -> M10Result<Transfer> {
        let transfer = self.get_raw_transfer(tx_id).await?;
        Transfer::try_from(transfer)
    }

    async fn get_raw_transfer(&self, tx_id: TxId) -> M10Result<sdk::FinalizedTransfer> {
        let req = self
            .signer()?
            .sign_request(sdk::GetTransferRequest { tx_id })
            .await?;
        let transfer = self
            .query_client
            .clone()
            .get_transfer(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Ok(transfer)
    }

    async fn get_enhanced_transfer(&self, tx_id: TxId) -> M10Result<ExpandedTransfer> {
        let req = self
            .signer()?
            .sign_request(sdk::GetTransferRequest { tx_id })
            .await?;
        let transfer = self
            .query_client
            .clone()
            .get_transfer(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let enhanced = self.enhance_transfer(transfer).await?;
        ExpandedTransfer::try_from(enhanced)
    }

    async fn get_enhanced_transfers(
        &self,
        filter: TxnFilter<TransferFilter>,
    ) -> M10Result<Vec<ExpandedTransfer>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListTransferRequest>(filter.into())
            .await?;
        let transfers = self
            .query_client
            .clone()
            .list_transfers(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        self.enhance_transfers(transfers.transfers)
            .await?
            .into_iter()
            .map(ExpandedTransfer::try_from)
            .collect::<M10Result<_>>()
    }

    async fn list_transfers(&self, filter: TxnFilter<TransferFilter>) -> M10Result<Vec<Transfer>> {
        let transfers = self.list_raw_transfers(filter).await?;
        let transfers = transfers
            .transfers
            .into_iter()
            .map(Transfer::try_from)
            .collect::<M10Result<_>>()?;
        Ok(transfers)
    }

    async fn list_raw_transfers(
        &self,
        filter: TxnFilter<TransferFilter>,
    ) -> M10Result<sdk::FinalizedTransfers> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListTransferRequest>(filter.into())
            .await?;
        let transfers = self
            .query_client
            .clone()
            .list_transfers(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Ok(transfers)
    }

    // Actions

    async fn get_action(&self, tx_id: TxId) -> M10Result<Action> {
        let req = self
            .signer()?
            .sign_request(sdk::GetActionRequest { tx_id })
            .await?;
        let action = self
            .query_client
            .clone()
            .get_action(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Action::try_from(action)
    }

    async fn list_actions(&self, filter: TxnFilter<ActionsFilter>) -> M10Result<Vec<Action>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListActionsRequest>(filter.into())
            .await?;
        let actions = self
            .query_client
            .clone()
            .list_actions(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let actions = actions
            .actions
            .into_iter()
            .map(Action::try_from)
            .collect::<M10Result<_>>()?;
        Ok(actions)
    }

    // Transactions

    async fn list_transactions(
        &self,
        filter: TxnFilter<ContextFilter>,
    ) -> M10Result<Vec<Transaction>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListTransactionsRequest>(filter.into())
            .await?;
        let txs = self
            .query_client
            .clone()
            .list_transactions(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let txs = txs
            .transactions
            .into_iter()
            .map(Transaction::try_from)
            .collect::<M10Result<_>>()?;
        Ok(txs)
    }

    async fn group_transactions(
        &self,
        filter: TxnFilter<GroupingFilter>,
    ) -> M10Result<Vec<Vec<Transaction>>> {
        let req = self
            .signer()?
            .sign_request::<sdk::GroupTransactionsRequest>(filter.into())
            .await?;
        let groups = self
            .query_client
            .clone()
            .group_transactions(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let groups = groups
            .groups
            .into_iter()
            .map(|txs| {
                txs.transactions
                    .into_iter()
                    .map(Transaction::try_from)
                    .collect::<M10Result<Vec<_>>>()
            })
            .collect::<M10Result<_>>()?;
        Ok(groups)
    }

    // Banks

    async fn get_bank(&self, id: Vec<u8>) -> M10Result<Bank> {
        let req = self
            .signer()?
            .sign_request(sdk::GetBankRequest { id })
            .await?;
        let bank = self
            .query_client
            .clone()
            .get_bank(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Bank::try_from(bank)
    }

    async fn list_banks(&self, builder: PageBuilder<Vec<u8>>) -> M10Result<Vec<Bank>> {
        let req = self
            .signer()?
            .sign_request(sdk::ListBanksRequest {
                page: builder.into(),
            })
            .await?;
        let banks = self
            .query_client
            .clone()
            .list_banks(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let banks = banks
            .banks
            .into_iter()
            .map(Bank::try_from)
            .collect::<M10Result<_>>()?;
        Ok(banks)
    }

    // Roles

    async fn get_role(&self, id: Vec<u8>) -> M10Result<Role> {
        let req = self
            .signer()?
            .sign_request(sdk::GetRoleRequest { id })
            .await?;
        let role = self
            .query_client
            .clone()
            .get_role(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        Role::try_from(role)
    }

    async fn list_roles(&self, builder: PageBuilder<Vec<u8>, RoleFilter>) -> M10Result<Vec<Role>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListRolesRequest>(builder.into())
            .await?;
        let roles = self
            .query_client
            .clone()
            .list_roles(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let roles = roles
            .roles
            .into_iter()
            .map(Role::try_from)
            .collect::<M10Result<_>>()?;
        Ok(roles)
    }

    // Role-bindings

    async fn get_role_binding(&self, id: Vec<u8>) -> M10Result<RoleBinding> {
        let req = self
            .signer()?
            .sign_request(sdk::GetRoleBindingRequest { id })
            .await?;
        let role_binding = self
            .query_client
            .clone()
            .get_role_binding(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        RoleBinding::try_from(role_binding)
    }

    async fn list_role_bindings(
        &self,
        builder: PageBuilder<Vec<u8>, RoleBindingFilter>,
    ) -> M10Result<Vec<RoleBinding>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListRoleBindingsRequest>(builder.into())
            .await?;
        let role_bindings = self
            .query_client
            .clone()
            .list_role_bindings(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let role_bindings = role_bindings
            .role_bindings
            .into_iter()
            .map(RoleBinding::try_from)
            .collect::<M10Result<_>>()?;
        Ok(role_bindings)
    }

    // AccountSets

    async fn get_account_set(&self, id: Vec<u8>) -> M10Result<AccountSet> {
        let req = self
            .signer()?
            .sign_request(sdk::GetAccountSetRequest { id })
            .await?;
        let set = self
            .query_client
            .clone()
            .get_account_set(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        AccountSet::try_from(set)
    }

    async fn list_account_sets(
        &self,
        builder: PageBuilder<Vec<u8>, NameOrOwnerFilter>,
    ) -> M10Result<Vec<AccountSet>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListAccountSetsRequest>(builder.into())
            .await?;
        let account_sets = self
            .query_client
            .clone()
            .list_account_sets(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let account_sets = account_sets
            .account_sets
            .into_iter()
            .map(AccountSet::try_from)
            .collect::<M10Result<_>>()?;
        Ok(account_sets)
    }
    // Account Metadata

    async fn get_account_metadata(&self, id: Vec<u8>) -> M10Result<AccountMetadata> {
        let req = self
            .signer()?
            .sign_request(sdk::GetAccountRequest { id })
            .await?;
        let metadata = self
            .query_client
            .clone()
            .get_account_metadata(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        AccountMetadata::try_from(metadata)
    }

    async fn list_account_metadata(
        &self,
        builder: PageBuilder<Vec<u8>, AccountMetadataFilter>,
    ) -> M10Result<Vec<AccountMetadata>> {
        let req = self
            .signer()?
            .sign_request::<sdk::ListAccountMetadataRequest>(builder.into())
            .await?;
        let account_metadata = self
            .query_client
            .clone()
            .list_account_metadata(Request::new(req.into()))
            .await
            .map(|res| res.into_inner())?;
        let account_sets = account_metadata
            .accounts
            .into_iter()
            .map(AccountMetadata::try_from)
            .collect::<M10Result<_>>()?;
        Ok(account_sets)
    }

    // Observers
    async fn observe_accounts(
        &self,
        filter: AccountFilter,
    ) -> M10Result<Pin<Box<dyn Stream<Item = M10Result<Vec<AccountUpdate>>> + Send + Sync + 'static>>>
    {
        let req = self
            .signer()?
            .sign_request::<sdk::ObserveAccountsRequest>(filter.into())
            .await?;
        let stream = self
            .query_client
            .clone()
            .observe_accounts(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map(|res| match res {
                Ok(txs) => Ok(txs
                    .transactions
                    .into_iter()
                    .map(AccountUpdate::try_from)
                    .collect::<M10Result<Vec<_>>>()?),
                Err(err) => Err(M10Error::from(err)),
            });

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }

    async fn observe_transfers(
        &self,
        filter: AccountFilter,
    ) -> M10Result<Pin<Box<dyn Stream<Item = M10Result<Vec<Transfer>>> + Send + Sync + 'static>>>
    {
        let req = self
            .signer()?
            .sign_request::<sdk::ObserveAccountsRequest>(filter.into())
            .await?;
        let stream = self
            .query_client
            .clone()
            .observe_transfers(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map(|res| match res {
                Ok(txs) => Ok(txs
                    .transactions
                    .into_iter()
                    .map(Transfer::try_from)
                    .collect::<M10Result<Vec<_>>>()?),
                Err(err) => Err(M10Error::from(err)),
            });

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }

    async fn observe_transactions(
        &self,
        filter: AccountFilter,
    ) -> M10Result<
        Pin<Box<dyn Stream<Item = M10Result<sdk::FinalizedTransactions>> + Send + Sync + 'static>>,
    > {
        let req = self
            .signer()?
            .sign_request::<sdk::ObserveAccountsRequest>(filter.into())
            .await?;
        let stream = self
            .query_client
            .clone()
            .observe_transfers(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map(|res| match res {
                Ok(txs) => Ok(txs),
                Err(err) => Err(M10Error::from(err)),
            });

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }

    async fn observe_actions(
        &self,
        filter: AccountFilter<NamedAction>,
    ) -> M10Result<Pin<Box<dyn Stream<Item = M10Result<Vec<Action>>> + Send + Sync + 'static>>>
    {
        let req = self
            .signer()?
            .sign_request::<sdk::ObserveActionsRequest>(filter.into())
            .await?;
        let stream = self
            .query_client
            .clone()
            .observe_actions(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map(|res| match res {
                Ok(txs) => Ok(txs
                    .transactions
                    .into_iter()
                    .map(Action::try_from)
                    .collect::<M10Result<Vec<_>>>()?),
                Err(err) => Err(M10Error::from(err)),
            });

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }

    async fn observe_raw_actions(
        &self,
        filter: AccountFilter<NamedAction>,
    ) -> M10Result<
        Pin<Box<dyn Stream<Item = M10Result<sdk::FinalizedTransactions>> + Send + Sync + 'static>>,
    > {
        let req = self
            .signer()?
            .sign_request::<sdk::ObserveActionsRequest>(filter.into())
            .await?;
        let stream = self
            .query_client
            .clone()
            .observe_actions(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map(|res| match res {
                Ok(txs) => Ok(txs),
                Err(err) => Err(M10Error::from(err)),
            });

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }

    async fn observe_resources(
        &self,
        request: sdk::ObserveResourcesRequest,
    ) -> M10Result<
        Pin<Box<dyn Stream<Item = M10Result<sdk::FinalizedTransactions>> + Send + Sync + 'static>>,
    > {
        let req = self.signer()?.sign_request(request).await?;
        let stream = self
            .query_client
            .clone()
            .observe_resources(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map(|res| match res {
                Ok(txs) => Ok(txs),
                Err(err) => Err(M10Error::from(err)),
            });

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }

    async fn observe_metrics(
        &self,
        filter: AccountFilter,
    ) -> M10Result<
        Pin<Box<dyn Stream<Item = M10Result<sdk::TransactionMetrics>> + Send + Sync + 'static>>,
    > {
        let req = self
            .signer()?
            .sign_request::<sdk::ObserveAccountsRequest>(filter.into())
            .await?;
        let stream = self
            .query_client
            .clone()
            .observe_metrics(Request::new(req.into()))
            .await
            .map(tonic::Response::into_inner)?
            .into_stream()
            .map_err(M10Error::from);

        let sync_stream = SyncStream::new(stream);
        Ok(Box::pin(sync_stream))
    }
}