concordium-rust-sdk 9.0.1

An SDK to use the Concordium blockchain.
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
use super::{
    generated::{
        self, account_transaction_payload, dry_run_error_response, dry_run_request,
        DryRunInvokeInstance, DryRunMintToAccount, DryRunSignature, DryRunStateOperation,
    },
    AccountIdentifier, IntoBlockIdentifier, Upward,
};
use crate::{
    types::{
        smart_contracts::{ContractContext, InstanceInfo, ReturnValue},
        AccountInfo, AccountTransactionDetails, RejectReason,
    },
    v2::{generated::DryRunStateQuery, Require},
};
use concordium_base::{
    base::{Energy, ProtocolVersion},
    common::types::{CredentialIndex, KeyIndex, Timestamp},
    contracts_common::{AccountAddress, Amount, ContractAddress},
    hashes::BlockHash,
    smart_contracts::ContractTraceElement,
    transactions::{EncodedPayload, PayloadLike},
};
use futures::*;

mod shared_receiver {
    use futures::{stream::Stream, StreamExt};
    use tokio::{
        sync::{mpsc, oneshot},
        task::JoinHandle,
    };

    /// A `SharedReceiver` wraps an underlying stream so that multiple clients
    /// can queue to receive items from the stream.
    pub struct SharedReceiver<I> {
        senders: mpsc::UnboundedSender<oneshot::Sender<I>>,
        task: JoinHandle<()>,
    }

    impl<I> Drop for SharedReceiver<I> {
        fn drop(&mut self) {
            self.task.abort();
        }
    }

    impl<I> SharedReceiver<I>
    where
        I: Send + 'static,
    {
        /// Construct a new shared receiver. This spawns a background task that
        /// pairs waiters with incoming stream items.
        pub fn new(stream: impl Stream<Item = I> + Unpin + Send + 'static) -> Self {
            let (senders, rec_senders) = mpsc::unbounded_channel::<oneshot::Sender<I>>();
            let task = tokio::task::spawn(async {
                let mut zipped = stream.zip(tokio_stream::wrappers::UnboundedReceiverStream::from(
                    rec_senders,
                ));
                while let Some((item, sender)) = zipped.next().await {
                    let _ = sender.send(item);
                }
            });
            SharedReceiver { senders, task }
        }

        /// Claim the next item from the stream when it becomes available.
        /// Returns `None` if the stream is already closed. Otherwise returns a
        /// [`oneshot::Receiver`] that can be `await`ed to retrieve the item.
        pub fn next(&self) -> Option<oneshot::Receiver<I>> {
            let (send, recv) = oneshot::channel();
            self.senders.send(send).ok()?;
            Some(recv)
        }
    }
}

/// An error response to a dry-run request.
#[derive(thiserror::Error, Debug)]
pub enum ErrorResult {
    /// The current block state is undefined. It should be initialized with a
    /// `load_block_state` request before any other operations.
    #[error("block state not loaded")]
    NoState,
    /// The requested block was not found, so its state could not be loaded.
    /// Response to `load_block_state`.
    #[error("block not found")]
    BlockNotFound,
    /// The specified account was not found.
    /// Response to `get_account_info`, `mint_to_account` and `run_transaction`.
    #[error("account not found")]
    AccountNotFound,
    /// The specified instance was not found.
    /// Response to `get_instance_info`.
    #[error("contract instance not found")]
    InstanceNotFound,
    /// The amount to mint would overflow the total CCD supply.
    /// Response to `mint_to_account`.
    #[error("mint amount exceeds limit")]
    AmountOverLimit {
        /// The maximum amount that can be minted.
        amount_limit: Amount,
    },
    /// The balance of the sender account is not sufficient to pay for the
    /// operation. Response to `run_transaction`.
    #[error("account balance insufficient")]
    BalanceInsufficient {
        /// The balance required to pay for the operation.
        required_amount: Amount,
        /// The actual amount available on the account to pay for the operation.
        available_amount: Amount,
    },
    /// The energy supplied for the transaction was not sufficient to perform
    /// the basic checks. Response to `run_transaction`.
    #[error("energy insufficient")]
    EnergyInsufficient {
        /// The energy required to perform the basic checks on the transaction.
        /// Note that this may not be sufficient to also execute the
        /// transaction.
        energy_required: Energy,
    },
    /// The contract invocation failed.
    /// Response to `invoke_instance`.
    #[error("invoke instance failed")]
    InvokeFailure {
        /// If invoking a V0 contract this is not provided, otherwise it is
        /// the return value produced by the call unless the call failed
        /// with out of energy or runtime error. If the V1 contract
        /// terminated with a logic error then the return value is
        /// present.
        return_value: Option<ReturnValue>,
        /// Energy used by the execution.
        used_energy: Energy,
        /// Contract execution failed for the given reason.
        reason: RejectReason,
    },
}

impl TryFrom<dry_run_error_response::Error> for ErrorResult {
    type Error = tonic::Status;

    fn try_from(value: dry_run_error_response::Error) -> Result<Self, Self::Error> {
        use dry_run_error_response::Error;
        let res = match value {
            Error::NoState(_) => Self::NoState,
            Error::BlockNotFound(_) => Self::BlockNotFound,
            Error::AccountNotFound(_) => Self::AccountNotFound,
            Error::InstanceNotFound(_) => Self::InstanceNotFound,
            Error::AmountOverLimit(e) => Self::AmountOverLimit {
                amount_limit: e.amount_limit.require()?.into(),
            },
            Error::BalanceInsufficient(e) => Self::BalanceInsufficient {
                required_amount: e.required_amount.require()?.into(),
                available_amount: e.available_amount.require()?.into(),
            },
            Error::EnergyInsufficient(e) => Self::EnergyInsufficient {
                energy_required: e.energy_required.require()?.into(),
            },
            Error::InvokeFailed(e) => Self::InvokeFailure {
                return_value: e.return_value.map(ReturnValue::from),
                used_energy: e.used_energy.require()?.into(),
                reason: e.reason.require()?.try_into()?,
            },
        };
        Ok(res)
    }
}

/// An error resulting from a dry-run operation.
#[derive(thiserror::Error, Debug)]
pub enum DryRunError {
    /// The server responded with an error code.
    /// In this case, no futher requests will be accepted in the dry-run
    /// session.
    #[error("gRPC error: {0}")]
    CallError(#[from] tonic::Status),
    /// The dry-run operation failed.
    /// In this case, further dry-run requests are possible in the same session.
    #[error("dry-run operation failed: {result}")]
    OperationFailed {
        /// The error result.
        #[source]
        result: ErrorResult,
        /// The energy quota remaining for subsequent dry-run requests in the
        /// session.
        quota_remaining: Energy,
    },
}

/// A result value together with the remaining energy quota at the completion of
/// the operation.
#[derive(Debug, Clone)]
pub struct WithRemainingQuota<T> {
    /// The result value.
    pub inner: T,
    /// The remaining energy quota.
    pub quota_remaining: Energy,
}

/// The successful result of [`DryRun::load_block_state`].
#[derive(Debug, Clone)]
pub struct BlockStateLoaded {
    /// The timestamp of the block, taken to be the current timestamp when
    /// executing transactions.
    pub current_timestamp: Timestamp,
    /// The hash of the block that was loaded.
    pub block_hash: BlockHash,
    /// The protocol version at the specified block. The behavior of operations
    /// can vary across protocol version.
    pub protocol_version: ProtocolVersion,
}

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<BlockStateLoaded>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(result, ErrorResult::BlockNotFound) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                match response {
                    generated::dry_run_success_response::Response::BlockStateLoaded(loaded) => {
                        let protocol_version =
                            generated::ProtocolVersion::try_from(loaded.protocol_version)
                                .map_err(|_| tonic::Status::unknown("Unknown protocol version"))?
                                .into();
                        let loaded = BlockStateLoaded {
                            current_timestamp: loaded.current_timestamp.require()?.into(),
                            block_hash: loaded.block_hash.require()?.try_into()?,
                            protocol_version,
                        };
                        Ok(WithRemainingQuota {
                            inner: loaded,
                            quota_remaining,
                        })
                    }
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<AccountInfo>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(result, ErrorResult::NoState | ErrorResult::AccountNotFound) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                use generated::dry_run_success_response::*;
                match response {
                    Response::AccountInfo(info) => Ok(WithRemainingQuota {
                        inner: info.try_into()?,
                        quota_remaining,
                    }),
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<InstanceInfo>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(result, ErrorResult::NoState | ErrorResult::InstanceNotFound) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                use generated::dry_run_success_response::*;
                match response {
                    Response::InstanceInfo(info) => Ok(WithRemainingQuota {
                        inner: info.try_into()?,
                        quota_remaining,
                    }),
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

impl From<&ContractContext> for DryRunInvokeInstance {
    fn from(context: &ContractContext) -> Self {
        DryRunInvokeInstance {
            invoker: context.invoker.as_ref().map(|a| a.into()),
            instance: Some((&context.contract).into()),
            amount: Some(context.amount.into()),
            entrypoint: Some(context.method.as_receive_name().into()),
            parameter: Some(context.parameter.as_ref().into()),
            energy: context.energy.map(From::from),
        }
    }
}

/// The successful result of [`DryRun::invoke_instance`].
#[derive(Debug, Clone)]
pub struct InvokeInstanceSuccess {
    /// The return value for a V1 contract call. Absent for a V0 contract call.
    pub return_value: Option<ReturnValue>,
    /// The effects produced by contract execution.
    pub events: Vec<Upward<ContractTraceElement>>,
    /// The energy used by the execution.
    pub used_energy: Energy,
}

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<InvokeInstanceSuccess>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(
                    result,
                    ErrorResult::NoState
                        | ErrorResult::InvokeFailure {
                            return_value: _,
                            used_energy: _,
                            reason: _,
                        }
                ) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                use generated::dry_run_success_response::*;
                match response {
                    Response::InvokeSucceeded(result) => {
                        let inner = InvokeInstanceSuccess {
                            return_value: result.return_value.map(|a| ReturnValue { value: a }),
                            events: result
                                .effects
                                .into_iter()
                                .map(|trace| {
                                    Ok(Upward::from(
                                        trace
                                            .element
                                            .map(ContractTraceElement::try_from)
                                            .transpose()?,
                                    ))
                                })
                                .collect::<Result<_, tonic::Status>>()?,
                            used_energy: result.used_energy.require()?.into(),
                        };
                        Ok(WithRemainingQuota {
                            inner,
                            quota_remaining,
                        })
                    }
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

/// The successful result of [`DryRun::set_timestamp`].
#[derive(Clone, Debug, Copy)]
pub struct TimestampSet;

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<TimestampSet>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(result, ErrorResult::NoState) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                match response {
                    generated::dry_run_success_response::Response::TimestampSet(_) => {
                        let inner = TimestampSet {};
                        Ok(WithRemainingQuota {
                            inner,
                            quota_remaining,
                        })
                    }
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

/// The successful result of [`DryRun::mint_to_account`].
#[derive(Clone, Debug, Copy)]
pub struct MintedToAccount;

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<MintedToAccount>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(
                    result,
                    ErrorResult::NoState | ErrorResult::AmountOverLimit { amount_limit: _ }
                ) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                match response {
                    generated::dry_run_success_response::Response::MintedToAccount(_) => {
                        let inner = MintedToAccount {};
                        Ok(WithRemainingQuota {
                            inner,
                            quota_remaining,
                        })
                    }
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

/// Representation of a transaction for the purposes of dry-running it.
/// Compared to a genuine transaction, this does not include an expiry time or
/// signatures. It is possible to specify which credentials and keys are assumed
/// to sign the transaction. This is only useful for transactions from
/// multi-signature accounts. In particular, it can ensure that the calculated
/// cost is correct when multiple signatures are used. For transactions that
/// update the keys on a multi-credential account, the transaction must be
/// signed by the credential whose keys are updated, so specifying which keys
/// sign is required here.
#[derive(Clone, Debug)]
pub struct DryRunTransaction {
    /// The account originating the transaction.
    pub sender: AccountAddress,
    /// The limit on the energy that may be used by the transaction.
    pub energy_amount: Energy,
    /// The transaction payload to execute.
    pub payload: EncodedPayload,
    /// The credential-keys that are treated as signing the transaction.
    /// If this is the empty vector, it is treated as the single key (0,0)
    /// signing.
    pub signatures: Vec<(CredentialIndex, KeyIndex)>,
}

impl DryRunTransaction {
    /// Create a [`DryRunTransaction`] given the sender address, energy limit
    /// and payload. The empty list is used for the signatures, meaning that
    /// it will be treated as though key 0 of credential 0 is the sole
    /// signature on the transaction. For most purposes, this is sufficient.
    pub fn new(sender: AccountAddress, energy_amount: Energy, payload: &impl PayloadLike) -> Self {
        DryRunTransaction {
            sender,
            energy_amount,
            payload: payload.encode(),
            signatures: vec![],
        }
    }
}

impl<P: PayloadLike> From<concordium_base::transactions::AccountTransaction<P>>
    for DryRunTransaction
{
    fn from(value: concordium_base::transactions::AccountTransaction<P>) -> Self {
        DryRunTransaction {
            sender: value.header.sender,
            energy_amount: value.header.energy_amount,
            payload: value.payload.encode(),
            signatures: value
                .signature
                .signatures
                .into_iter()
                .flat_map(|(c, v)| std::iter::repeat(c).zip(v.into_keys()))
                .collect(),
        }
    }
}

impl From<DryRunTransaction> for generated::DryRunTransaction {
    fn from(transaction: DryRunTransaction) -> Self {
        let payload = account_transaction_payload::Payload::RawPayload(transaction.payload.into());
        generated::DryRunTransaction {
            sender: Some(transaction.sender.into()),
            energy_amount: Some(transaction.energy_amount.into()),
            payload: Some(generated::AccountTransactionPayload {
                payload: Some(payload),
            }),
            signatures: transaction
                .signatures
                .into_iter()
                .map(|(cred, key)| DryRunSignature {
                    credential: cred.index.into(),
                    key: key.0.into(),
                })
                .collect(),
        }
    }
}

/// The successful result of [`DryRun::run_transaction`].
/// Note that a transaction can still be rejected (i.e. produce no effect beyond
/// charging the sender) even if it is executed.
#[derive(Clone, Debug)]
pub struct TransactionExecuted {
    /// The actual energy cost of executing the transaction.
    pub energy_cost: Energy,
    /// Detailed result of the transaction execution.
    pub details: AccountTransactionDetails,
    /// For V1 contract update and init transactions, the return value.
    pub return_value: Option<Vec<u8>>,
}

impl TryFrom<Option<Result<generated::DryRunResponse, tonic::Status>>>
    for WithRemainingQuota<TransactionExecuted>
{
    type Error = DryRunError;

    fn try_from(
        value: Option<Result<generated::DryRunResponse, tonic::Status>>,
    ) -> Result<Self, Self::Error> {
        let response =
            value.ok_or_else(|| tonic::Status::cancelled("server closed dry run stream"))??;
        let quota_remaining = response.quota_remaining.require()?.into();
        use generated::dry_run_response::*;
        match response.response.require()? {
            Response::Error(e) => {
                let result = e.error.require()?.try_into()?;
                if !matches!(
                    result,
                    ErrorResult::NoState
                        | ErrorResult::AccountNotFound
                        | ErrorResult::BalanceInsufficient { .. }
                        | ErrorResult::EnergyInsufficient { .. }
                ) {
                    Err(tonic::Status::unknown("unexpected error response type"))?
                }
                Err(DryRunError::OperationFailed {
                    result,
                    quota_remaining,
                })
            }
            Response::Success(s) => {
                let response = s.response.require()?;
                match response {
                    generated::dry_run_success_response::Response::TransactionExecuted(res) => {
                        let inner = TransactionExecuted {
                            energy_cost: res.energy_cost.require()?.into(),
                            details: res.details.require()?.try_into()?,
                            return_value: res.return_value,
                        };
                        Ok(WithRemainingQuota {
                            inner,
                            quota_remaining,
                        })
                    }
                    _ => Err(tonic::Status::unknown("unexpected success response type"))?,
                }
            }
        }
    }
}

pub type DryRunResult<T> = Result<WithRemainingQuota<T>, DryRunError>;

/// A dry-run session.
///
/// The operations available in two variants, with and without the `begin_`
/// prefix. The variants without a prefix will send the request and wait for the
/// result when `await`ed. This is typically the simplest to use.
/// The variants with the `begin_` prefix send the request when `await`ed,
/// returning a future that can be `await`ed to retrieve the result.
/// (This can be used to front-load operations where queries do not depend on
/// the results of previous queries. This may be more efficient in high-latency
/// situations.)
///
/// Before any other operations, [`DryRun::load_block_state`] (or
/// [`DryRun::begin_load_block_state`]) should be called to ensure a block state
/// is loaded. If it is not (or if loading the block state fails - for instance
/// if an invalid block hash is supplied), other operations will result in an
/// [`ErrorResult::NoState`] error.
pub struct DryRun {
    /// The channel used for sending requests to the server.
    /// This is `None` if the session has been closed.
    request_send: Option<channel::mpsc::Sender<generated::DryRunRequest>>,
    /// The channel used for receiving responses from the server.
    response_recv: shared_receiver::SharedReceiver<tonic::Result<generated::DryRunResponse>>,
    /// The timeout in milliseconds for the dry-run session to complete.
    timeout: u64,
    /// The energy quota for the dry-run session as a whole.
    energy_quota: u64,
}

impl DryRun {
    /// Start a new dry-run session.
    /// This may return `UNIMPLEMENTED` if the endpoint is not available on the
    /// server. It may return `UNAVAILABLE` if the endpoint is not currently
    /// available to due resource limitations.
    pub(crate) async fn new(
        client: &mut generated::queries_client::QueriesClient<tonic::transport::Channel>,
    ) -> tonic::Result<Self> {
        let (request_send, request_recv) = channel::mpsc::channel(10);
        let response = client.dry_run(request_recv).await?;
        let parse_meta_u64 = |key| response.metadata().get(key)?.to_str().ok()?.parse().ok();
        let timeout: u64 = parse_meta_u64("timeout").ok_or_else(|| {
            tonic::Status::internal("timeout metadata could not be parsed from server response")
        })?;
        let energy_quota: u64 = parse_meta_u64("quota").ok_or_else(|| {
            tonic::Status::internal(
                "energy quota metadata could not be parsed from server response",
            )
        })?;
        let response_stream = response.into_inner();
        let response_recv =
            shared_receiver::SharedReceiver::new(futures::stream::StreamExt::fuse(response_stream));
        Ok(DryRun {
            request_send: Some(request_send),
            response_recv,
            timeout,
            energy_quota,
        })
    }

    /// Get the timeout for the dry-run session set by the server.
    /// Returns `None` if the initial metadata did not include the timeout, or
    /// it could not be parsed.
    pub fn timeout(&self) -> std::time::Duration {
        std::time::Duration::from_millis(self.timeout)
    }

    /// Get the total energy quota set for the dry-run session.
    pub fn energy_quota(&self) -> Energy {
        self.energy_quota.into()
    }

    /// Load the state from a specified block.
    /// This can result in an error if the dry-run session has already been
    /// closed, either by [`DryRun::close`] or by the server closing the
    /// session. In this case, the response code indicates the cause.
    /// If successful, this returns a future that can be used to wait for the
    /// result of the operation. The following results are possible:
    ///
    ///  * [`BlockStateLoaded`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::BlockNotFound`] if the block could not be found.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 2000.
    pub async fn begin_load_block_state(
        &mut self,
        bi: impl IntoBlockIdentifier,
    ) -> tonic::Result<impl Future<Output = DryRunResult<BlockStateLoaded>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::LoadBlockState(
                (&bi.into_block_identifier()).into(),
            )),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Load the state from a specified block.
    /// The following results are possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`BlockStateLoaded`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::BlockNotFound`] if the block could not be found.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 2000.
    pub async fn load_block_state(
        &mut self,
        bi: impl IntoBlockIdentifier,
    ) -> DryRunResult<BlockStateLoaded> {
        self.begin_load_block_state(bi).await?.await
    }

    /// Get the account information for a specified account in the current
    /// state. This can result in an error if the dry-run session has
    /// already been closed, either by [`DryRun::close`] or by the server
    /// closing the session. In this case, the response code indicates the
    /// cause. If successful, this returns a future that can be used to wait
    /// for the result of the operation. The following results are possible:
    ///
    ///  * [`AccountInfo`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AccountNotFound`] if the account could not be found.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 200.
    pub async fn begin_get_account_info(
        &mut self,
        acc: &AccountIdentifier,
    ) -> tonic::Result<impl Future<Output = DryRunResult<AccountInfo>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::StateQuery(DryRunStateQuery {
                query: Some(generated::dry_run_state_query::Query::GetAccountInfo(
                    acc.into(),
                )),
            })),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Get the account information for a specified account in the current
    /// state. The following results are possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`AccountInfo`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AccountNotFound`] if the account could not be found.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 200.
    pub async fn get_account_info(&mut self, acc: &AccountIdentifier) -> DryRunResult<AccountInfo> {
        self.begin_get_account_info(acc).await?.await
    }

    /// Get the details of a specified smart contract instance in the current
    /// state. This operation can result in an error if the dry-run session has
    /// already been closed, either by [`DryRun::close`] or by the server
    /// closing the session. In this case, the response code indicates the
    /// cause. If successful, this returns a future that can be used to wait
    /// for the result of the operation. The following results are possible:
    ///
    ///  * [`InstanceInfo`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AccountNotFound`] if the account could not be found.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 200.
    pub async fn begin_get_instance_info(
        &mut self,
        address: &ContractAddress,
    ) -> tonic::Result<impl Future<Output = DryRunResult<InstanceInfo>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::StateQuery(DryRunStateQuery {
                query: Some(generated::dry_run_state_query::Query::GetInstanceInfo(
                    address.into(),
                )),
            })),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Get the details of a specified smart contract instance in the current
    /// state. The following results are possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`InstanceInfo`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AccountNotFound`] if the account could not be found.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 200.
    pub async fn get_instance_info(
        &mut self,
        address: &ContractAddress,
    ) -> DryRunResult<InstanceInfo> {
        self.begin_get_instance_info(address).await?.await
    }

    /// Invoke an entrypoint on a smart contract instance in the current state.
    /// Any changes this would make to the state will be rolled back so they are
    /// not observable by subsequent operations in the dry-run session. (To make
    /// updates that are observable within the dry-run session, use
    /// [`DryRun::run_transaction`] instead.) This operation can result in an
    /// error if the dry-run session has already been closed, either by
    /// [`DryRun::close`] or by the server closing the session. In this case,
    /// the response code indicates the cause. If successful, this returns a
    /// future that can be used to wait for the result of the operation. The
    /// following results are possible:
    ///
    ///  * [`InvokeInstanceSuccess`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::InvokeFailure`] if the invocation failed. (This can
    ///      be because the contract logic produced a reject, or a number of
    ///      other reasons, such as the endpoint not existing.)
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 200 plus the energy used by the
    /// execution of the contract endpoint.
    pub async fn begin_invoke_instance(
        &mut self,
        context: &ContractContext,
    ) -> tonic::Result<impl Future<Output = DryRunResult<InvokeInstanceSuccess>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::StateQuery(DryRunStateQuery {
                query: Some(generated::dry_run_state_query::Query::InvokeInstance(
                    context.into(),
                )),
            })),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Invoke an entrypoint on a smart contract instance in the current state.
    /// Any changes this would make to the state will be rolled back so they are
    /// not observable by subsequent operations in the dry-run session. (To make
    /// updates that are observable within the dry-run session, use
    /// [`DryRun::run_transaction`] instead.) The following results are
    /// possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`InvokeInstanceSuccess`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::InvokeFailure`] if the invocation failed. (This can
    ///      be because the contract logic produced a reject, or a number of
    ///      other reasons, such as the endpoint not existing.)
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 200 plus the energy used by the
    /// execution of the contract endpoint.
    pub async fn invoke_instance(
        &mut self,
        context: &ContractContext,
    ) -> DryRunResult<InvokeInstanceSuccess> {
        self.begin_invoke_instance(context).await?.await
    }

    /// Update the current timestamp for subsequent dry-run operations. The
    /// timestamp is automatically set to the timestamp of the block loaded
    /// by [`DryRun::load_block_state`]. For smart contracts that are time
    /// sensitive, overriding the timestamp can be useful. This operation can
    /// result in an error if the dry-run session has already been closed,
    /// either by [`DryRun::close`] or by the server closing the session. In
    /// this case, the response code indicates the cause. If successful,
    /// this returns a future that can be used to wait for the result of the
    /// operation. The following results are possible:
    ///
    ///  * [`TimestampSet`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 50.
    pub async fn begin_set_timestamp(
        &mut self,
        timestamp: Timestamp,
    ) -> tonic::Result<impl Future<Output = DryRunResult<TimestampSet>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::StateOperation(
                DryRunStateOperation {
                    operation: Some(generated::dry_run_state_operation::Operation::SetTimestamp(
                        timestamp.into(),
                    )),
                },
            )),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Update the current timestamp for subsequent dry-run operations. The
    /// timestamp is automatically set to the timestamp of the block loaded
    /// by [`DryRun::load_block_state`]. For smart contracts that are time
    /// sensitive, overriding the timestamp can be useful. The following results
    /// are possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`TimestampSet`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 50.
    pub async fn set_timestamp(&mut self, timestamp: Timestamp) -> DryRunResult<TimestampSet> {
        self.begin_set_timestamp(timestamp).await?.await
    }

    /// Mint a specified amount and award it to a specified account. This
    /// operation can result in an error if the dry-run session has already
    /// been closed, either by [`DryRun::close`] or by the server closing the
    /// session. In this case, the response code indicates the cause. If
    /// successful, this returns a future that can be used to wait for the
    /// result of the operation. The following results are possible:
    ///
    ///  * [`MintedToAccount`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AmountOverLimit`] if the minted amount would
    ///      overflow the total CCD supply.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 400.
    pub async fn begin_mint_to_account(
        &mut self,
        account_address: &AccountAddress,
        mint_amount: Amount,
    ) -> tonic::Result<impl Future<Output = DryRunResult<MintedToAccount>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::StateOperation(
                DryRunStateOperation {
                    operation: Some(
                        generated::dry_run_state_operation::Operation::MintToAccount(
                            DryRunMintToAccount {
                                account: Some(account_address.into()),
                                amount: Some(mint_amount.into()),
                            },
                        ),
                    ),
                },
            )),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Mint a specified amount and award it to a specified account. The
    /// following results are possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`MintedToAccount`] if the operation is successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AmountOverLimit`] if the minted amount would
    ///      overflow the total CCD supply.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 400.
    pub async fn mint_to_account(
        &mut self,
        account_address: &AccountAddress,
        mint_amount: Amount,
    ) -> DryRunResult<MintedToAccount> {
        self.begin_mint_to_account(account_address, mint_amount)
            .await?
            .await
    }

    /// Dry-run a transaction, updating the state of the dry-run session
    /// accordingly. This operation can result in an error if the dry-run
    /// session has already been closed, either by [`DryRun::close`] or by the
    /// server closing the session. In this case, the response code
    /// indicates the cause. If successful, this returns a future that can
    /// be used to wait for the result of the operation. The following
    /// results are possible:
    ///
    ///  * [`TransactionExecuted`] if the transaction was executed. This case
    ///    applies both if the transaction is rejected or successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AccountNotFound`] if the sender account does not
    ///      exist.
    ///    - [`ErrorResult::BalanceInsufficient`] if the sender account does not
    ///      have sufficient balance to pay the deposit for the transaction.
    ///    - [`ErrorResult::EnergyInsufficient`] if the specified energy is not
    ///      sufficient to cover the cost of the basic checks required for a
    ///      transaction to be included in the chain.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 400.   
    pub async fn begin_run_transaction(
        &mut self,
        transaction: DryRunTransaction,
    ) -> tonic::Result<impl Future<Output = DryRunResult<TransactionExecuted>>> {
        let request = generated::DryRunRequest {
            request: Some(dry_run_request::Request::StateOperation(
                DryRunStateOperation {
                    operation: Some(
                        generated::dry_run_state_operation::Operation::RunTransaction(
                            transaction.into(),
                        ),
                    ),
                },
            )),
        };
        Ok(self.request(request).await?.map(|z| z.try_into()))
    }

    /// Dry-run a transaction, updating the state of the dry-run session
    /// accordingly. The following results are possible:
    ///
    ///  * [`DryRunError::CallError`] if the dry-run session has already been
    ///    closed, either by [`DryRun::close`] or by the server closing the
    ///    session. In this case, the response code indicates the cause.
    ///  * [`TransactionExecuted`] if the transaction was executed. This case
    ///    applies both if the transaction is rejected or successful.
    ///  * [`DryRunError::OperationFailed`] if the operation failed, with one of
    ///    the following results:
    ///    - [`ErrorResult::NoState`] if no block state has been loaded.
    ///    - [`ErrorResult::AccountNotFound`] if the sender account does not
    ///      exist.
    ///    - [`ErrorResult::BalanceInsufficient`] if the sender account does not
    ///      have sufficient balance to pay the deposit for the transaction.
    ///    - [`ErrorResult::EnergyInsufficient`] if the specified energy is not
    ///      sufficient to cover the cost of the basic checks required for a
    ///      transaction to be included in the chain.
    ///  * [`DryRunError::CallError`] if the server produced an error code, or
    ///    if the server's response was unexpected.
    ///    - If the server's response could not be interpreted, the result code
    ///      `INVALID_ARGUMENT` or `UNKNOWN` is returned.
    ///    - If the execution of the query would exceed the energy quota,
    ///      `RESOURCE_EXHAUSTED` is returned.
    ///    - If the timeout for the dry-run session has expired,
    ///      `DEADLINE_EXCEEDED` is returned.
    ///
    /// The energy cost of this operation is 400.   
    pub async fn run_transaction(
        &mut self,
        transaction: DryRunTransaction,
    ) -> DryRunResult<TransactionExecuted> {
        self.begin_run_transaction(transaction).await?.await
    }

    /// Close the request stream. Any subsequent dry-run requests will result in
    /// a `CANCELLED` status code. Closing the request stream allows the
    /// server to free resources associated with the dry-run session. It is
    /// recommended to close the request stream if the [`DryRun`] object will
    /// be retained for any significant length of time after the last request is
    /// made.
    ///
    /// Note that dropping the [`DryRun`] object will stop the background task
    /// that services in-flight requests, so it should not be dropped before
    /// `await`ing any such requests. Closing the request stream does not stop
    /// the background task.
    pub fn close(&mut self) {
        self.request_send = None;
    }

    /// Helper function that issues a dry-run request and returns a future for
    /// the corresponding response.
    async fn request(
        &mut self,
        request: generated::DryRunRequest,
    ) -> tonic::Result<impl Future<Output = Option<tonic::Result<generated::DryRunResponse>>>> {
        let lazy_cancelled = || tonic::Status::cancelled("dry run already completed");
        let sender = self.request_send.as_mut().ok_or_else(lazy_cancelled)?;
        let send_result = sender.send(request).await;
        let receive_result = self.response_recv.next().ok_or_else(lazy_cancelled);
        match send_result {
            Ok(_) => receive_result.map(|r| r.map(|x| x.ok())),
            Err(_) => {
                // In this case, the server must have closed the stream. We query the response
                // stream to see if there is an error indicating the reason.
                if let Ok(Err(e)) = receive_result?.await {
                    Err(e)
                } else {
                    Err(lazy_cancelled())
                }
            }
        }
    }
}