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
use crate::{
    circuit::{XExecSignalId, XExecStepSideEffectId},
    xtx::LocalState,
    SpeedMode,
};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::dispatch::DispatchError;
use frame_system::{pallet_prelude::BlockNumberFor, Config};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::{crypto::AccountId32, hexdisplay::AsBytesRef, Hasher, H160, U256};
#[cfg(feature = "no_std")]
use sp_runtime::RuntimeDebug as Debug;
use sp_runtime::{traits::Zero, RuntimeDebug};
use sp_std::{convert::TryInto, default::Default, fmt::Debug, prelude::*};
use t3rn_types::sfx::TargetId;
pub use t3rn_types::sfx::{FullSideEffect, SecurityLvl, SideEffect};

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo)]
pub struct VacuumEVMOrder {
    pub destination: TargetId,

    pub asset: u32,

    pub amount: U256,

    pub reward_asset: H160,

    pub max_reward: U256,

    pub insurance: U256,

    pub target_account: AccountId32,
}

impl VacuumEVMOrder {
    pub fn new(
        destination: TargetId,
        asset: u32,
        amount: U256,
        reward_asset: H160,
        max_reward: U256,
        insurance: U256,
        target_account: AccountId32,
    ) -> Self {
        VacuumEVMOrder {
            destination,
            asset,
            amount,
            reward_asset,
            max_reward,
            insurance,
            target_account,
        }
    }

    pub fn from_rlp_encoded_packed(encoded_slice: &[u8]) -> Result<Self, DispatchError> {
        // Take the first 4 bytes and convert them to a TargetId
        let destination: TargetId = encoded_slice[0..4]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert destination"))?;

        // Take the next 4 bytes and convert them to a u32
        let asset: u32 = u32::from_be_bytes(
            encoded_slice[4..8]
                .try_into()
                .map_err(|_| DispatchError::Other("Failed to convert asset"))?,
        );

        // Take the next 32 bytes and convert them to a Vec<u8>
        let target_account: AccountId32 = AccountId32::decode(&mut &encoded_slice[8..40])
            .map_err(|_| DispatchError::Other("Failed to decode AccountId32"))?;

        // Take the next 32 bytes and convert them to a Balance
        let amount: U256 = encoded_slice[40..72]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert amount"))?;

        // Take the next 4 bytes and convert them to a u32
        let reward_asset: H160 = H160::decode(&mut &encoded_slice[72..92])
            .map_err(|_| DispatchError::Other("Failed to decode reward_asset"))?;

        // Take the next 32 bytes and convert them to a Balance
        let insurance: U256 = encoded_slice[92..124]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert insurance:"))?;

        // Take the next 32 bytes and convert them to a Balance
        let max_reward: U256 = encoded_slice[124..156]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert max_reward"))?;

        Ok(VacuumEVMOrder {
            destination,
            asset,
            amount,
            reward_asset,
            max_reward,
            insurance,
            target_account,
        })
    }
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
pub struct VacuumEVMTeleportOrder {
    pub gateway_id: [u8; 4],

    pub order_proof: Vec<u8>,
}

impl VacuumEVMTeleportOrder {
    pub fn new(gateway_id: [u8; 4], order_proof: Vec<u8>) -> Self {
        VacuumEVMTeleportOrder {
            gateway_id,
            order_proof,
        }
    }

    pub fn from_rlp(encoded_slice: &[u8]) -> Result<Self, DispatchError> {
        let mut gateway_id: [u8; 4] = Default::default();
        let mut order_proof = vec![];

        // Assume at least 4 bytes for the gateway_id
        if encoded_slice.len() < 4 {
            return Err(DispatchError::Other("Invalid encoded slice"))
        }

        // Take the first 4 bytes and convert them to a TargetId
        gateway_id.copy_from_slice(&encoded_slice[0..4]);

        let encoded_slice = &encoded_slice[4..];

        // Skip the first 4 bytes

        let mut index = 0;
        let mut proof_vec = vec![&mut order_proof];

        for proof in proof_vec.iter_mut() {
            let proof_len = encoded_slice[index] as usize;
            index += 1;
            proof.extend_from_slice(&encoded_slice[index..index + proof_len]);
            index += proof_len;
        }

        Ok(VacuumEVMTeleportOrder {
            gateway_id,
            order_proof,
        })
    }
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo)]
pub struct VacuumEVM3DOrder {
    pub destination: TargetId,

    pub asset: u32,

    pub amount: U256,

    pub reward_asset: H160,

    pub max_reward: U256,

    pub insurance: U256,

    pub target_account: AccountId32,

    pub nonce: u32,
}

impl VacuumEVM3DOrder {
    pub fn new(
        destination: TargetId,
        asset: u32,
        amount: U256,
        reward_asset: H160,
        max_reward: U256,
        insurance: U256,
        target_account: AccountId32,
        nonce: u32,
    ) -> Self {
        VacuumEVM3DOrder {
            destination,
            asset,
            amount,
            reward_asset,
            max_reward,
            insurance,
            target_account,
            nonce,
        }
    }

    pub fn from_rlp_encoded_packed(encoded_slice: &[u8]) -> Result<Self, DispatchError> {
        // Take the first 4 bytes and convert them to a TargetId
        let destination: TargetId = encoded_slice[0..4]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert destination"))?;

        // Take the next 4 bytes and convert them to a u32
        let asset: u32 = u32::from_be_bytes(
            encoded_slice[4..8]
                .try_into()
                .map_err(|_| DispatchError::Other("Failed to convert asset"))?,
        );

        // Take the next 32 bytes and convert them to a Vec<u8>
        let target_account: AccountId32 = AccountId32::decode(&mut &encoded_slice[8..40])
            .map_err(|_| DispatchError::Other("Failed to decode AccountId32"))?;

        // Take the next 32 bytes and convert them to a Balance
        let amount: U256 = encoded_slice[40..72]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert amount"))?;

        // Take the next 4 bytes and convert them to a u32
        let reward_asset: H160 = H160::decode(&mut &encoded_slice[72..92])
            .map_err(|_| DispatchError::Other("Failed to decode reward_asset"))?;

        // Take the next 32 bytes and convert them to a Balance
        let insurance: U256 = encoded_slice[92..124]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert insurance:"))?;

        // Take the next 32 bytes and convert them to a Balance
        let max_reward: U256 = encoded_slice[124..156]
            .try_into()
            .map_err(|_| DispatchError::Other("Failed to convert max_reward"))?;

        let nonce: u32 = u32::from_be_bytes(
            encoded_slice[156..160]
                .try_into()
                .map_err(|_| DispatchError::Other("Failed to convert nonce"))?,
        );

        Ok(VacuumEVM3DOrder {
            destination,
            asset,
            amount,
            reward_asset,
            max_reward,
            insurance,
            target_account,
            nonce,
        })
    }
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
pub struct VacuumEVMProof {
    pub circuit_gateway_id: [u8; 4],

    pub source_gateway_id: [u8; 4],

    pub destination_gateway_id: [u8; 4],

    pub order_proof: Vec<u8>,

    pub bid_proof: Vec<u8>,

    pub execution_proof: Vec<u8>,

    pub attestation_proof: Vec<u8>,
}

impl VacuumEVMProof {
    pub fn new(
        circuit_gateway_id: [u8; 4],
        source_gateway_id: [u8; 4],
        destination_gateway_id: [u8; 4],
        order_proof: Vec<u8>,
        bid_proof: Vec<u8>,
        execution_proof: Vec<u8>,
        attestation_proof: Vec<u8>,
    ) -> Self {
        VacuumEVMProof {
            circuit_gateway_id,
            source_gateway_id,
            destination_gateway_id,
            order_proof,
            bid_proof,
            execution_proof,
            attestation_proof,
        }
    }

    pub fn from_rlp(encoded_slice: &[u8]) -> Result<Self, DispatchError> {
        let mut circuit_gateway_id: [u8; 4] = Default::default();
        let mut source_gateway_id: [u8; 4] = Default::default();
        let mut destination_gateway_id: [u8; 4] = Default::default();
        let mut order_proof = vec![];
        let mut bid_proof = vec![];
        let mut execution_proof = vec![];
        let mut attestation_proof = vec![];

        // Assume at least 12 bytes for the circuit_gateway_id & source_gateway_id & destination_gateway_id
        if encoded_slice.len() < 12 {
            return Err(DispatchError::Other("Invalid encoded slice"))
        }

        // Take the first 4 bytes and convert them to a TargetId
        circuit_gateway_id.copy_from_slice(&encoded_slice[0..4]);

        // Take the next 4 bytes and convert them to a TargetId
        source_gateway_id.copy_from_slice(&encoded_slice[4..8]);

        // Take the next 4 bytes and convert them to a TargetId
        destination_gateway_id.copy_from_slice(&encoded_slice[8..12]);

        let encoded_slice = &encoded_slice[12..];

        // Skip the first 4 bytes

        let mut index = 0;
        let mut proof_vec = vec![
            &mut order_proof,
            &mut bid_proof,
            &mut execution_proof,
            &mut attestation_proof,
        ];

        for proof in proof_vec.iter_mut() {
            let proof_len = encoded_slice[index] as usize;
            index += 1;
            proof.extend_from_slice(&encoded_slice[index..index + proof_len]);
            index += proof_len;
        }

        Ok(VacuumEVMProof {
            circuit_gateway_id,
            source_gateway_id,
            destination_gateway_id,
            order_proof,
            bid_proof,
            execution_proof,
            attestation_proof,
        })
    }
}

type SystemHashing<T> = <T as Config>::Hashing;

/// Status of Circuit storage items:
/// Requested - default
/// Requested -> Validated - successfully passed the validation
/// Option<Validated -> PendingInsurance>: If there are some side effects that request insurance,
///         the status will stay in PendingInsurance until all insurance deposits are committed
/// Validated/PendingInsurance -> Ready - ready for relayers to pick up and start executing on targets
/// Ready -> PendingExecution - at least one side effect has already been confirmed, but not all of them
/// PendingExecution -> Finished - all of the side effects are confirmed, now awaiting for the decision about Revert/Commit
/// Circuit::Apply -> called internally - based on the side effects confirmations decides:
///     Ready -> Committed: All of the side effects have been successfully confirmed
///     Ready -> Reverted: Some of the side effects failed and the Xtx was reverted
#[derive(Clone, Eq, PartialEq, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo, Default)]
pub enum CircuitStatus {
    /// unvalidated xtx requested
    #[default]
    Requested,
    /// validated xtx with empty side effects - reserved for 3vm following execution
    Reserved,
    /// validated xtx pending for bidding; no bids has been posted so far
    PendingBidding,
    /// at least one bid has already been posted, still awaiting for bidding resolution
    InBidding,
    /// xtx killed on user's request or Dropped at Bidding
    Killed(Cause),
    /// all bids has been posted and xtx is awaiting confirmations of execution
    Ready,
    /// at least one valid confirmation of execution has already been accepted; xtx still in progress
    PendingExecution,
    /// xtx step successfully finished
    Finished,
    /// all of the steps has successfully finished - can still await for attestations to move to foreign consensus targets
    FinishedAllSteps,
    /// xtx reverts due timeout when confirmations haven't arrived on time,
    Reverted(Cause),
    Committed,
}

/// Kill or Revert cause
#[derive(Clone, Eq, PartialEq, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum Cause {
    /// timeout expired with incomplete expectations: either bids or SFX confirmations
    Timeout,
    /// Attempt to kill on user's request
    IntentionalKill,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum CircuitRole {
    Relayer,
    Executor,
    Requester,
    ContractAuthor,
    Local,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum InsuranceEnact {
    Reward,
    RefundAndPunish,
    RefundBoth,
}

impl CircuitStatus {
    pub fn check_transition<T: Config>(
        previous: CircuitStatus,
        new: CircuitStatus,
        maybe_forced: Option<CircuitStatus>,
    ) -> Result<CircuitStatus, DispatchError> {
        match maybe_forced {
            None => {
                match (previous.clone(), new.clone()) {
                    (CircuitStatus::Requested, CircuitStatus::Requested) => Ok(new),
                    // todo: shouldn't be allowed to schedule empty Xtx with no SFX, but load_local_state uses a lot for 3VM setup
                    (CircuitStatus::Reserved, CircuitStatus::Reserved) => Ok(new),
                    (CircuitStatus::Reserved, CircuitStatus::PendingBidding) => Ok(new),
                    (CircuitStatus::Requested, CircuitStatus::Reserved) => Ok(new),

                    // success flow
                    (CircuitStatus::Requested, CircuitStatus::PendingBidding) => Ok(new),
                    (CircuitStatus::Requested, CircuitStatus::InBidding) => Ok(new),
                    (CircuitStatus::PendingBidding, CircuitStatus::InBidding) => Ok(new),
                    (CircuitStatus::InBidding, CircuitStatus::InBidding) => Ok(new),
                    (CircuitStatus::InBidding, CircuitStatus::Ready) => Ok(new),
                    (CircuitStatus::PendingBidding, CircuitStatus::Ready) => Ok(new),
                    (CircuitStatus::Ready, CircuitStatus::PendingExecution) => Ok(new),
                    (CircuitStatus::Ready, CircuitStatus::FinishedAllSteps) => Ok(new),
                    (CircuitStatus::PendingExecution, CircuitStatus::PendingExecution) => Ok(new),
                    (CircuitStatus::PendingExecution, CircuitStatus::Finished) => Ok(new),
                    (CircuitStatus::PendingExecution, CircuitStatus::FinishedAllSteps) => Ok(new),
                    (CircuitStatus::PendingExecution, CircuitStatus::Committed) => Ok(new),
                    // next steps transitions
                    (CircuitStatus::Finished, CircuitStatus::PendingExecution) => Ok(new),
                    (CircuitStatus::Finished, CircuitStatus::Ready) => Ok(new),
                    (CircuitStatus::Finished, CircuitStatus::FinishedAllSteps) => Ok(new),

                    (CircuitStatus::FinishedAllSteps, CircuitStatus::Committed) => Ok(new),
                    (_, _) => {
                        log::error!(
                            "check_transition::UpdateStateTransitionDisallowedInvalid {:?} -> {:?}",
                            previous,
                            new
                        );
                        Err(DispatchError::Other("UpdateStateTransitionDisallowed"))
                    },
                }
            },
            Some(forced) => {
                if forced == new {
                    return Ok(new)
                }
                // Only cases we allow forced transitions are:
                // revert by protocol,
                // kill either by protocol or user's attempt,
                // from ready to pending execution for XBI's execution
                // from pending bidding to in bidding for posted bid
                // from reserved to pending execution
                match forced.clone() {
                    CircuitStatus::Killed(cause) => match cause {
                        Cause::IntentionalKill =>
                            if new <= CircuitStatus::PendingBidding {
                                Ok(forced)
                            } else {
                                Err(DispatchError::Other(
                                    "UpdateForcedStateTransitionDisallowed",
                                ))
                            },
                        Cause::Timeout =>
                            if new <= CircuitStatus::InBidding {
                                Ok(forced)
                            } else {
                                Err(DispatchError::Other(
                                    "UpdateForcedStateTransitionDisallowed",
                                ))
                            },
                    },
                    CircuitStatus::Reverted(cause) =>
                        if new < CircuitStatus::Ready {
                            Ok(CircuitStatus::Killed(cause))
                        } else if new < CircuitStatus::FinishedAllSteps {
                            Ok(forced)
                        } else {
                            // Revert assumed infallible - fallback to new state which results in no op. in apply.
                            Ok(new)
                        },
                    CircuitStatus::Ready =>
                        if previous == CircuitStatus::InBidding {
                            Ok(forced)
                        } else {
                            Err(DispatchError::Other(
                                "UpdateForcedStateTransitionDisallowed",
                            ))
                        },
                    CircuitStatus::InBidding =>
                        if new == CircuitStatus::PendingBidding || new == CircuitStatus::InBidding {
                            Ok(forced)
                        } else {
                            Err(DispatchError::Other(
                                "UpdateForcedStateTransitionDisallowed",
                            ))
                        },
                    CircuitStatus::PendingExecution =>
                        if new == CircuitStatus::Ready {
                            Ok(forced)
                        } else {
                            Err(DispatchError::Other(
                                "UpdateForcedStateTransitionDisallowed",
                            ))
                        },
                    CircuitStatus::Reserved =>
                        if new <= CircuitStatus::Reserved {
                            Ok(forced)
                        } else {
                            Err(DispatchError::Other(
                                "UpdateForcedStateTransitionDisallowed",
                            ))
                        },
                    CircuitStatus::Committed =>
                        if new == CircuitStatus::FinishedAllSteps {
                            Ok(CircuitStatus::Committed)
                        } else {
                            Err(DispatchError::Other(
                                "UpdateForcedStateTransitionDisallowed",
                            ))
                        },
                    _ => Err(DispatchError::Other(
                        "UpdateForcedStateTransitionDisallowed",
                    )),
                }
            },
        }
    }

    fn determine_fsx_bidding_status<T: Config, Balance: Clone>(
        fsx: FullSideEffect<T::AccountId, BlockNumberFor<T>, Balance>,
    ) -> CircuitStatus {
        if let Some(_bid) = fsx.best_bid {
            CircuitStatus::InBidding
        } else {
            CircuitStatus::PendingBidding
        }
    }

    /// Check if all FSX have the bidding companion.
    /// Additionally,
    /// if SFX::Optimistic check if the optional insurance and bonded_deposit fields are present
    /// if SFX::Escrow check if the optional insurance and bonded_deposit are set to None
    pub fn determine_bidding_status<T: Config, Balance: Clone>(
        fsx_step: &[FullSideEffect<T::AccountId, BlockNumberFor<T>, Balance>],
    ) -> CircuitStatus {
        let mut lowest_bidding_status = CircuitStatus::InBidding;
        let mut highest_bidding_status = CircuitStatus::PendingBidding;

        for fsx in fsx_step.iter() {
            let current_bidding_status =
                Self::determine_fsx_bidding_status::<T, Balance>(fsx.clone());
            if current_bidding_status == CircuitStatus::PendingBidding {
                lowest_bidding_status = CircuitStatus::PendingBidding;
            } else {
                highest_bidding_status = CircuitStatus::InBidding;
            }
        }
        if lowest_bidding_status == CircuitStatus::InBidding {
            // Check if all FSX have already executors assigned to them as a precondition for the CircuitStatus to be ready.
            if fsx_step
                .iter()
                .all(|fsx| fsx.input.enforce_executor.is_some())
            {
                CircuitStatus::Ready
            } else {
                CircuitStatus::InBidding
            }
        } else if highest_bidding_status == CircuitStatus::PendingBidding {
            CircuitStatus::PendingBidding
        } else {
            CircuitStatus::InBidding
        }
    }

    pub fn determine_execution_status<T: Config, Balance: Clone>(
        fsx_step: &[FullSideEffect<T::AccountId, BlockNumberFor<T>, Balance>],
    ) -> CircuitStatus {
        let mut lowest_execution_status = CircuitStatus::Finished;
        let mut highest_execution_status = CircuitStatus::Ready;

        for fsx in fsx_step.iter() {
            if fsx.confirmed.is_some() {
                highest_execution_status = CircuitStatus::Finished;
            } else {
                lowest_execution_status = CircuitStatus::Ready;
            }
        }
        if lowest_execution_status == CircuitStatus::Finished {
            CircuitStatus::Finished
        } else if highest_execution_status == CircuitStatus::Ready {
            CircuitStatus::Ready
        } else {
            CircuitStatus::PendingExecution
        }
    }

    /// Based solely on full steps + insurance deposits determine the execution status.
    /// Start with checking the criteria from the earliest status to latest
    pub fn determine_step_status<T: Config, Balance: Clone>(
        step: &[FullSideEffect<T::AccountId, BlockNumberFor<T>, Balance>],
    ) -> CircuitStatus {
        if step.is_empty() {
            return CircuitStatus::Finished
        }
        let determined_bidding_status = Self::determine_bidding_status::<T, Balance>(step);
        if determined_bidding_status < CircuitStatus::Ready {
            return determined_bidding_status
        }
        Self::determine_execution_status::<T, Balance>(step)
    }

    pub fn determine_xtx_status<T: Config, Balance: Clone>(
        steps: &[Vec<FullSideEffect<T::AccountId, BlockNumberFor<T>, Balance>>],
    ) -> CircuitStatus {
        let mut lowest_determined_status = CircuitStatus::Requested;

        // If all of the steps are empty assume CircuitStatus::Reserved status
        if steps.iter().all(|step| step.is_empty()) {
            return CircuitStatus::Reserved
        }
        for step in steps.iter() {
            let current_step_status = Self::determine_step_status::<T, Balance>(step);
            if current_step_status > lowest_determined_status {
                lowest_determined_status = current_step_status.clone();
            }
            // Xtx status is reflected with the lowest status of unresolved Step -
            //  break the loop on the first unresolved step
            if lowest_determined_status < CircuitStatus::Finished {
                return current_step_status
            }
        }
        CircuitStatus::FinishedAllSteps
    }
}

pub struct LocalXtxCtx<T: Config, Balance: Clone> {
    pub local_state: LocalState,
    pub xtx_id: XExecSignalId<T>,
    pub xtx: XExecSignal<T::AccountId, BlockNumberFor<T>>,
    pub full_side_effects: Vec<Vec<FullSideEffect<T::AccountId, BlockNumberFor<T>, Balance>>>,
}

/// A composable cross-chain (X) transaction that has already been verified to be valid and submittable
#[derive(Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct XExecSignal<AccountId, BlockNumber> {
    /// The owner of the bid
    pub requester: AccountId,

    /// The owner of the bid
    pub requester_nonce: u32,

    /// Expiry timeout
    pub timeouts_at: AdaptiveTimeout<BlockNumber, TargetId>,

    /// Speed of confirmation
    pub speed_mode: SpeedMode,

    /// Schedule execution of steps in the future intervals
    pub delay_steps_at: Option<Vec<BlockNumber>>,

    /// Has returned status already and what
    pub status: CircuitStatus,

    /// Has returned status already and what
    pub steps_cnt: (u32, u32),
}

impl<
        AccountId: Encode + Clone + Debug,
        BlockNumber: Ord + Copy + Zero + Encode + Clone + Debug,
    > XExecSignal<AccountId, BlockNumber>
{
    pub fn new(
        // Requester of xtx
        requester: &AccountId,
        // Requester' nonce of xtx
        requester_nonce: u32,
        // Expiry timeout
        timeouts_at: AdaptiveTimeout<BlockNumber, TargetId>,
        // Schedule execution of steps in the future intervals
        delay_steps_at: Option<Vec<BlockNumber>>,
        // Speed of confirmation
        speed_mode: SpeedMode,
        // Current steps count
        steps_cnt: (u32, u32),
    ) -> Self {
        XExecSignal {
            requester: requester.clone(),
            requester_nonce,
            timeouts_at,
            delay_steps_at,
            status: Default::default(),
            speed_mode,
            steps_cnt,
        }
    }

    // xtx_id is generated by hashing requester + requester_nonce. This ensures it will always be unique
    pub fn generate_id<T: Config, Hasher: sp_core::Hasher>(&self) -> XExecSignalId<T> {
        let mut requester_on_32b_as_vec = self.requester.encode();

        let nonce_as_4b_word: [u8; 4] = self.requester_nonce.to_be_bytes();
        let mut nonce_as_32b_word: [u8; 32];
        nonce_as_32b_word = [0; 32];
        nonce_as_32b_word[28..32].copy_from_slice(&nonce_as_4b_word);
        requester_on_32b_as_vec.extend_from_slice(&nonce_as_32b_word);

        let hash = sp_runtime::traits::Keccak256::hash(requester_on_32b_as_vec.as_slice());

        let mut system_hash: T::Hash = T::Hash::default();

        system_hash.as_mut().copy_from_slice(&hash.as_ref()[..32]);

        system_hash
    }

    pub fn generate_step_id<T: Config>(
        xtx_id: XExecSignalId<T>,
        n_step: usize,
    ) -> XExecStepSideEffectId<T> {
        let mut xtx_id_buf = xtx_id.encode();
        xtx_id_buf.append(&mut (n_step as u32).encode());
        SystemHashing::<T>::hash(xtx_id_buf.as_ref())
    }

    pub fn set_speed_mode(&mut self, speed_mode: SpeedMode) {
        self.speed_mode = speed_mode;
    }

    pub fn setup_fresh<T: frame_system::Config>(
        // Requester of xtx
        requester: &T::AccountId,
        // Expiry timeout
        timeouts_at: AdaptiveTimeout<BlockNumberFor<T>, TargetId>,
        // Speed of confirmation
        speed_mode: SpeedMode,
        // Schedule execution of steps in the future intervals
        delay_steps_at: Option<Vec<BlockNumberFor<T>>>,
    ) -> (
        XExecSignalId<T>,
        XExecSignal<T::AccountId, BlockNumberFor<T>>,
    ) {
        let requester_nonce = Decode::decode(
            &mut &frame_system::Pallet::<T>::account_nonce(requester).encode()[..],
        )
        .expect(
            "System::Index decoding try to u32 should always succeed since set in Runtime Config",
        );

        let signal = XExecSignal::new(
            requester,
            requester_nonce,
            timeouts_at,
            delay_steps_at,
            speed_mode,
            (0, 0),
        );
        let id = signal.generate_id::<T, SystemHashing<T>>();
        (id, signal)
    }
}

#[derive(Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct AdaptiveTimeout<BlockNumber, TargetId> {
    pub estimated_height_here: BlockNumber,
    pub estimated_height_there: BlockNumber,
    pub submit_by_height_here: BlockNumber,
    pub submit_by_height_there: BlockNumber,
    pub emergency_timeout_here: BlockNumber,
    pub there: TargetId,
    pub dlq: Option<BlockNumber>,
}

impl<BlockNumber: Zero + From<u32>, TargetId: Default> AdaptiveTimeout<BlockNumber, TargetId> {
    pub fn default_401() -> Self {
        AdaptiveTimeout {
            estimated_height_here: Zero::zero(),
            estimated_height_there: Zero::zero(),
            submit_by_height_here: Zero::zero(),
            submit_by_height_there: Zero::zero(),
            emergency_timeout_here: BlockNumber::from(401u32),
            there: TargetId::default(),
            dlq: None,
        }
    }

    pub fn new_emergency(emergency_timeout_here: BlockNumber) -> Self {
        AdaptiveTimeout {
            estimated_height_here: Zero::zero(),
            estimated_height_there: Zero::zero(),
            submit_by_height_here: Zero::zero(),
            submit_by_height_there: Zero::zero(),
            emergency_timeout_here,
            there: TargetId::default(),
            dlq: None,
        }
    }
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum SFXAction<Account, Asset, Balance, Destination, Input, MaxCost> {
    // All sorts of calls: composable, wasm, evm, etc. are vacuumed into a single Call SFX in the protocol level.
    Call(Destination, Account, Balance, MaxCost, Input),
    // All of the DEX-related SFXs are vacuumed into a Transfer SFX in the protocol level: swap, add_liquidity, remove_liquidity, transfer asset, transfer native
    Transfer(Destination, Asset, Account, Balance),
    DynamicDestinationDeal(Destination, Asset, Balance),
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct OrderSFX<AccountId, Asset, Balance, Destination, Input, MaxCost> {
    pub sfx_action: SFXAction<AccountId, Asset, Balance, Destination, Input, MaxCost>,
    pub max_reward: Balance,
    pub reward_asset: Asset,
    pub insurance: Balance,
    pub remote_origin_nonce: Option<u32>,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum OrderOrigin<AccountId> {
    Local(AccountId),
    Remote(u32),
}

impl<AccountId: Encode + Decode + Clone> OrderOrigin<AccountId> {
    pub fn is_local(&self) -> bool {
        match self {
            OrderOrigin::Local(_) => true,
            _ => false,
        }
    }

    pub fn is_remote(&self) -> bool {
        match self {
            OrderOrigin::Remote(_) => true,
            _ => false,
        }
    }

    pub fn try_remote_source(source: &AccountId) -> Result<OrderOrigin<AccountId>, DispatchError> {
        let maybe_remote = Self::new(source);
        match maybe_remote {
            OrderOrigin::Local(_) => Err("InvalidRemoteSource".into()),
            OrderOrigin::Remote(_) => Ok(maybe_remote),
        }
    }

    pub fn new(source: &AccountId) -> OrderOrigin<AccountId> {
        let source_bytes = source.encode();
        // check if first 28 bytes are 0
        if !source_bytes[..28].iter().all(|x| *x == 0) {
            return OrderOrigin::Local(source.clone())
        }
        let mut remote_origin_nonce = [0u8; 4];
        remote_origin_nonce.copy_from_slice(&source_bytes[28..32]);
        OrderOrigin::Remote(u32::from_be_bytes(remote_origin_nonce))
    }

    pub fn from_remote_nonce(remote_nonce: u32) -> OrderOrigin<AccountId> {
        OrderOrigin::Remote(remote_nonce)
    }

    pub fn to_account_id(&self) -> AccountId {
        match self {
            OrderOrigin::Local(a) => a.clone(),
            OrderOrigin::Remote(nonce) => {
                let mut account_bytes = [0u8; 32];
                let nonce_bytes = nonce.to_be_bytes();
                account_bytes[28..32].copy_from_slice(&nonce_bytes);
                AccountId::decode(&mut &account_bytes[..])
                    .expect("AccountId should always decode from [u8; 32]")
            },
        }
    }
}

impl<AccountId, Asset: Clone, Balance, Destination, Input, MaxCost>
    TryInto<SideEffect<AccountId, Balance>>
    for OrderSFX<AccountId, Asset, Balance, Destination, Input, MaxCost>
where
    u32: From<Asset>,
    Balance: Encode,
    MaxCost: Encode,
    AccountId: Encode,
    Input: AsBytesRef,
    Destination: From<[u8; 4]>,
    [u8; 4]: From<Destination>,
{
    type Error = DispatchError;

    fn try_into(self) -> Result<SideEffect<AccountId, Balance>, Self::Error> {
        let (action, target, encoded_args) = match self.sfx_action {
            SFXAction::Call(target, destination, value, max_cost, input) => {
                let mut encoded_args = vec![];
                // todo: lookup destination target and derive the ActionType (call evm / wasm / composable)
                encoded_args.push(destination.encode()); // target
                encoded_args.push(value.encode()); // value
                encoded_args.push(input.as_bytes_ref().to_vec()); // value
                encoded_args.push(max_cost.encode()); // value
                (*b"cevm", target.into(), encoded_args)
            },
            SFXAction::Transfer(target, asset, destination, amount) => {
                let mut encoded_args: Vec<Vec<u8>> = vec![];
                let asset_id: u32 = <Asset as Into<u32>>::into(asset);
                let action_id = if asset_id != 0 {
                    encoded_args.push(asset_id.to_le_bytes().to_vec());
                    *b"tass"
                } else {
                    *b"tran"
                };
                encoded_args.push(destination.encode());
                encoded_args.push(amount.encode());
                (action_id, target.into(), encoded_args)
            },
            SFXAction::DynamicDestinationDeal(target, asset, amount) => {
                let mut encoded_args: Vec<Vec<u8>> = vec![];
                let asset_id: u32 = <Asset as Into<u32>>::into(asset);
                let action_id = *b"tddd";
                encoded_args.push(asset_id.to_le_bytes().to_vec());
                encoded_args.push(amount.encode());
                (action_id, target.into(), encoded_args)
            },
        };

        let reward_asset_id = if <Asset as Into<u32>>::into(self.reward_asset.clone()) == 0 {
            None
        } else {
            Some(<Asset as Into<u32>>::into(self.reward_asset))
        };

        let side_effect = SideEffect {
            target,
            max_reward: self.max_reward,
            insurance: self.insurance,
            action,
            encoded_args,
            signature: vec![],
            enforce_executor: None,
            reward_asset_id,
        };

        Ok(side_effect)
    }
}

#[cfg(test)]
mod tests {
    use super::{OrderSFX, SFXAction};
    use crate::{
        circuit::{AdaptiveTimeout, XExecSignal},
        SpeedMode,
    };
    use frame_support::assert_ok;
    use hex_literal::hex;
    use mini_mock::MiniRuntime;
    use sp_core::crypto::AccountId32;
    use sp_runtime::traits::Keccak256;
    use sp_std::convert::TryInto;
    use t3rn_types::sfx::SideEffect;

    #[test]
    fn sfx_id_is_compatible_with_keccak_and_20b_accounts() {
        let _account_20b = [2u8; 20];
        let account_32b = AccountId32::new(hex!(
            "0000000000000000000000000202020202020202020202020202020202020202"
        ));
        let dest_account_32b = AccountId32::new(hex!(
            "0000000000000000000000000303030303030303030303030303030303030303"
        ));

        assert_eq!(
            account_32b,
            hex!("0000000000000000000000000202020202020202020202020202020202020202").into()
        );

        let xtx = XExecSignal::<AccountId32, u32>::new(
            &account_32b,
            5u32,
            AdaptiveTimeout::default_401(),
            None,
            SpeedMode::Finalized,
            (0, 0),
        );

        let xtx_id = xtx.generate_id::<MiniRuntime, Keccak256>();

        assert_eq!(
            xtx_id,
            hex!("94a85672500de62cf1c799e77e211ce7af26e924a11efc3274ee5de7c855dd74").into()
        );

        let order_sfx = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128> {
            sfx_action: SFXAction::Transfer([3u8; 4], 1u32, dest_account_32b, 100u128),
            max_reward: 200u128,
            insurance: 50u128,
            reward_asset: 1u32,
            remote_origin_nonce: Some(5u32),
        };

        let sfx: SideEffect<AccountId32, u128> = order_sfx.try_into().unwrap();
        let sfx_id = sfx.generate_id::<Keccak256>(xtx_id.0.as_slice(), 0);

        assert_eq!(
            sfx_id,
            hex!("e290576c0364fb6aab8d36e1cf94e32a1c1c23e25e30e455a62f597186981984").into()
        );
    }

    #[test]
    fn sfx_id_calculates_expected_values_for_nonce_0_1_and_2_for_hardcoded_20b_accounts() {
        let account_32b = AccountId32::new(hex!(
            "000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266"
        ));

        let xtx_nonce_0 = XExecSignal::<AccountId32, u32>::new(
            &account_32b,
            0u32,
            AdaptiveTimeout::default_401(),
            None,
            SpeedMode::Finalized,
            (0, 0),
        );

        let xtx_nonce_1 = XExecSignal::<AccountId32, u32>::new(
            &account_32b,
            1u32,
            AdaptiveTimeout::default_401(),
            None,
            SpeedMode::Finalized,
            (0, 0),
        );

        let xtx_nonce_2 = XExecSignal::<AccountId32, u32>::new(
            &account_32b,
            2u32,
            AdaptiveTimeout::default_401(),
            None,
            SpeedMode::Finalized,
            (0, 0),
        );

        let order_sfx = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128> {
            sfx_action: SFXAction::Transfer([3u8; 4], 1u32, account_32b.clone(), 100u128),
            max_reward: 200u128,
            insurance: 50u128,
            reward_asset: 1u32,
            remote_origin_nonce: Some(5u32),
        };

        let sfx: SideEffect<AccountId32, u128> = order_sfx.try_into().unwrap();

        let xtx_id_nonce_0 = xtx_nonce_0.generate_id::<MiniRuntime, Keccak256>();
        let xtx_id_nonce_1 = xtx_nonce_1.generate_id::<MiniRuntime, Keccak256>();
        let xtx_id_nonce_2 = xtx_nonce_2.generate_id::<MiniRuntime, Keccak256>();

        let sfx_id_nonce_0 = sfx.generate_id::<Keccak256>(xtx_id_nonce_0.0.as_slice(), 0);
        let sfx_id_nonce_1 = sfx.generate_id::<Keccak256>(xtx_id_nonce_1.0.as_slice(), 0);
        let sfx_id_nonce_2 = sfx.generate_id::<Keccak256>(xtx_id_nonce_2.0.as_slice(), 0);

        let expected_hash_nonce_0 =
            hex!("4fd5cefb43ccd33cfe1f4f9a0405af51205e021449892cb08809d97610cfe722");
        let expected_hash_nonce_1 =
            hex!("733fc9182521d4ac5f3465c0b0382a5b4bad7af476f8c7517e2739536a42bb94");
        let expected_hash_nonce_2 =
            hex!("8066494f08af53d6edb5c6df49048a3b4ae59e20df6d59c86bf8c8650304747e");

        assert_eq!(sfx_id_nonce_0, expected_hash_nonce_0.into());
        assert_eq!(sfx_id_nonce_1, expected_hash_nonce_1.into());
        assert_eq!(sfx_id_nonce_2, expected_hash_nonce_2.into());
    }

    #[test]
    fn test_try_into_transfer() {
        let order_sfx = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128> {
            sfx_action: SFXAction::Transfer([3u8; 4], 1u32, AccountId32::new([2u8; 32]), 100u128),
            max_reward: 200u128,
            insurance: 50u128,
            reward_asset: 1u32,
            remote_origin_nonce: None,
        };

        let result: Result<SideEffect<AccountId32, u128>, _> = order_sfx.try_into();
        assert_ok!(&result);

        let side_effect = result.unwrap();
        assert_eq!(side_effect.max_reward, 200);
        assert_eq!(side_effect.insurance, 50);
        assert_eq!(side_effect.target, [3u8; 4]);
        assert_eq!(side_effect.action, *b"tass");
        assert_eq!(side_effect.reward_asset_id, Some(1u32));
        assert_eq!(side_effect.encoded_args.len(), 3);
        assert_eq!(side_effect.encoded_args[0], vec![1u8, 0, 0, 0]);
        assert_eq!(side_effect.encoded_args[1], [2u8; 32]);
        assert_eq!(
            side_effect.encoded_args[2],
            vec![100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        );
    }

    #[test]
    fn test_try_into_dynamic_destination_deal() {
        let order_sfx = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128> {
            sfx_action: SFXAction::DynamicDestinationDeal([3u8; 4], 1u32, 100u128),
            max_reward: 200u128,
            insurance: 50u128,
            reward_asset: 1u32,
            remote_origin_nonce: None,
        };

        let result: Result<SideEffect<AccountId32, u128>, _> = order_sfx.try_into();
        assert_ok!(&result);

        let side_effect = result.unwrap();
        assert_eq!(side_effect.max_reward, 200);
        assert_eq!(side_effect.insurance, 50);
        assert_eq!(side_effect.target, [3u8; 4]);
        assert_eq!(side_effect.action, *b"tddd");
        assert_eq!(side_effect.reward_asset_id, Some(1u32));
        assert_eq!(side_effect.encoded_args.len(), 2);
        assert_eq!(side_effect.encoded_args[0], vec![1u8, 0, 0, 0]);
        assert_eq!(
            side_effect.encoded_args[1],
            vec![100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        );

        // Transformation of SFXAction::DynamicDestinationDeal into SFXAction::Transfer is also supported
        // let order_converted_back = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128>::from_sfx(side_effect);
    }

    #[test]
    fn test_try_into_transfer_native_for_asset_zero() {
        let order_sfx = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128> {
            sfx_action: SFXAction::Transfer([3u8; 4], 0u32, AccountId32::new([2u8; 32]), 100u128),
            max_reward: 200u128,
            insurance: 50u128,
            reward_asset: 0u32,
            remote_origin_nonce: None,
        };

        let result: Result<SideEffect<AccountId32, u128>, _> = order_sfx.try_into();
        assert_ok!(&result);

        let side_effect = result.unwrap();
        assert_eq!(side_effect.max_reward, 200);
        assert_eq!(side_effect.insurance, 50);
        assert_eq!(side_effect.target, [3u8; 4]);
        assert_eq!(side_effect.action, *b"tran");
        assert_eq!(side_effect.reward_asset_id, None);
        assert_eq!(side_effect.encoded_args.len(), 2);
        assert_eq!(side_effect.encoded_args[0], [2u8; 32]);
        assert_eq!(
            side_effect.encoded_args[1],
            vec![100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        );
    }

    #[test]
    fn test_try_into_call() {
        let order_sfx = OrderSFX::<AccountId32, u32, u128, [u8; 4], Vec<u8>, u128> {
            sfx_action: SFXAction::Call(
                [1u8; 4],
                AccountId32::new([2u8; 32]),
                100u128,
                200u128,
                vec![3u8; 4],
            ),
            max_reward: 200u128,
            insurance: 50u128,
            reward_asset: 1u32,
            remote_origin_nonce: None,
        };

        let result: Result<SideEffect<AccountId32, u128>, _> = order_sfx.try_into();
        assert_ok!(&result);

        let side_effect = result.unwrap();
        assert_eq!(side_effect.max_reward, 200);
        assert_eq!(side_effect.insurance, 50);
        assert_eq!(side_effect.action, *b"cevm");
        assert_eq!(side_effect.reward_asset_id, Some(1u32));
    }
}