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
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
use crate::{
    assets::LendingAssets,
    conn::{new_websocket_client, new_websocket_client_with_retry},
    metadata, notify_retry,
    types::*,
    AccountId, AssetRegistry, CurrencyId, Error, FixedU128 as UnsignedFixedPoint, InterBtcRuntime, InterBtcSigner,
    RetryPolicy, RichH256Le, ShutdownSender, SubxtError,
};
use async_trait::async_trait;
use bitcoin::RawTransactionProof;
use codec::{Decode, Encode};
use futures::{future::join_all, stream::StreamExt, FutureExt, SinkExt, Stream};
use module_bitcoin::{
    merkle::{MerkleProof, PartialTransactionProof},
    parser::{parse_block_header, parse_transaction},
    types::FullTransactionProof,
};
use primitives::BalanceWrapper;
use serde_json::Value;
use std::{convert::TryInto, future::Future, ops::Range, sync::Arc, time::Duration};
use subxt::{
    blocks::ExtrinsicEvents,
    client::OnlineClient,
    events::StaticEvent,
    metadata::DecodeWithMetadata,
    rpc::{rpc_params, RpcClientT},
    storage::{address::Yes, StorageAddress},
    tx::TxPayload,
    utils::Static,
};
use tokio::{
    sync::RwLock,
    time::{sleep, timeout},
};

// timeout before retrying parachain calls (5 minutes)
const TRANSACTION_TIMEOUT: Duration = Duration::from_secs(300);

// timeout before re-verifying block header inclusion
const BLOCK_WAIT_TIMEOUT: Duration = Duration::from_secs(6);

// number of storage entries to fetch at a time
const DEFAULT_PAGE_SIZE: u32 = 10;

/// Keys in storage maps are prefixed by two `twox_128` hashes: the pallet name and the
/// storage item names. Then, depending on the `hash_fn` hasher the map uses,  the layout
/// looks as follows:
/// `twox_128("PalletName") ++ twox_128("ItemName") ++ hash_fn(key) ++ key`
const BLAKE2_128_HASH_PREFIX_LENGTH: usize = 48;
const TWOX_64_HASH_PREFIX_LENGTH: usize = 40;

// sanity check to be sure that testing-utils is not accidentally selected
#[cfg(all(any(test, feature = "testing-utils"), not(feature = "parachain-metadata-kintsugi")))]
compile_error!("Tests are only supported for the kintsugi runtime");

cfg_if::cfg_if! {
    if #[cfg(feature = "parachain-metadata-interlay")] {
        const DEFAULT_SPEC_VERSION: Range<u32> = 1025000..1026000;
        pub const DEFAULT_SPEC_NAME: &str = "interlay-parachain";
        pub const SS58_PREFIX: u16 = 2032;
    } else if #[cfg(feature = "parachain-metadata-kintsugi")] {
        const DEFAULT_SPEC_VERSION: Range<u32> = 1025000..1026000;
        pub const DEFAULT_SPEC_NAME: &str = "kintsugi-parachain";
        pub const SS58_PREFIX: u16 = 2092;
    }
}

pub(crate) type FeeRateUpdateSender = tokio::sync::broadcast::Sender<FixedU128>;
pub type FeeRateUpdateReceiver = tokio::sync::broadcast::Receiver<FixedU128>;

#[derive(Clone)]
pub struct InterBtcParachain {
    api: Arc<OnlineClient<InterBtcRuntime>>,
    nonce: Arc<RwLock<u32>>,
    signer: InterBtcSigner,
    account_id: AccountId,
    shutdown_tx: ShutdownSender,
    fee_rate_update_tx: FeeRateUpdateSender,
    pub native_currency_id: CurrencyId,
    pub relay_chain_currency_id: CurrencyId,
    pub wrapped_currency_id: CurrencyId,
}

impl InterBtcParachain {
    pub async fn new<P: RpcClientT>(
        rpc_client: P,
        signer: InterBtcSigner,
        shutdown_tx: ShutdownSender,
    ) -> Result<Self, Error> {
        let account_id = signer.account_id.clone().0;
        let api = OnlineClient::from_rpc_client(Arc::new(rpc_client)).await?;

        let runtime_version = api.rpc().runtime_version(None).await?;
        let spec_name: String = runtime_version
            .other
            .get("specName")
            .and_then(|value| value.as_str())
            .map(ToString::to_string)
            .unwrap_or_default();
        if DEFAULT_SPEC_NAME == spec_name {
            log::info!("spec_name={}", spec_name);
        } else {
            return Err(Error::ParachainMetadataMismatch(DEFAULT_SPEC_NAME.into(), spec_name));
        }

        if DEFAULT_SPEC_VERSION.contains(&runtime_version.spec_version) {
            log::info!("spec_version={}", runtime_version.spec_version);
            log::info!("transaction_version={}", runtime_version.transaction_version);
        } else {
            return Err(Error::InvalidSpecVersion(
                DEFAULT_SPEC_VERSION.start,
                DEFAULT_SPEC_VERSION.end,
                runtime_version.spec_version,
            ));
        }

        let currency_constants = metadata::constants().currency();
        let native_currency_id = api.constants().at(&currency_constants.get_native_currency_id())?;
        let relay_chain_currency_id = api.constants().at(&currency_constants.get_relay_chain_currency_id())?;
        let wrapped_currency_id = api.constants().at(&currency_constants.get_wrapped_currency_id())?;

        // low capacity channel since we generally only care about the newest value, so it's ok
        // if we miss an event
        let (fee_rate_update_tx, _) = tokio::sync::broadcast::channel(2);

        let parachain_rpc = Self {
            api: Arc::new(api),
            nonce: Arc::new(RwLock::new(0)),
            signer,
            account_id: (*account_id).clone().into(),
            shutdown_tx,
            fee_rate_update_tx,
            native_currency_id,
            relay_chain_currency_id,
            wrapped_currency_id,
        };

        parachain_rpc.store_assets_metadata().await?;
        parachain_rpc.store_lend_tokens().await?;
        Ok(parachain_rpc)
    }

    #[cfg(feature = "testing-utils")]
    pub async fn manual_seal(&self) {
        // rather than adding a conditional dependency on substrate, just re-define the
        // struct. We don't really care about the contents anyway, and if this is ever
        // to change upstream we'll know from failing tests
        #[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
        pub struct ImportedAux {
            /// Only the header has been imported. Block body verification was skipped.
            pub header_only: bool,
            /// Clear all pending justification requests.
            pub clear_justification_requests: bool,
            /// Request a justification for the given block.
            pub needs_justification: bool,
            /// Received a bad justification.
            pub bad_justification: bool,
            /// Whether the block that was imported is the new best block.
            pub is_new_best: bool,
        }
        #[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
        pub struct CreatedBlock<Hash> {
            /// hash of the created block.
            pub hash: Hash,
            /// some extra details about the import operation
            pub aux: ImportedAux,
        }

        let _: CreatedBlock<primitives::Hash> = self
            .api
            .rpc()
            .request("engine_createBlock", rpc_params![true, true])
            .await
            .expect("failed to create block");
    }

    pub async fn from_url(url: &str, signer: InterBtcSigner, shutdown_tx: ShutdownSender) -> Result<Self, Error> {
        let ws_client = new_websocket_client(url, None, None).await?;
        Self::new(ws_client, signer, shutdown_tx).await
    }

    pub async fn from_url_with_retry(
        url: &str,
        signer: InterBtcSigner,
        connection_timeout: Duration,
        shutdown_tx: ShutdownSender,
    ) -> Result<Self, Error> {
        Self::from_url_and_config_with_retry(url, signer, None, None, connection_timeout, shutdown_tx).await
    }

    pub async fn from_url_and_config_with_retry(
        url: &str,
        signer: InterBtcSigner,
        max_concurrent_requests: Option<usize>,
        max_notifs_per_subscription: Option<usize>,
        connection_timeout: Duration,
        shutdown_tx: ShutdownSender,
    ) -> Result<Self, Error> {
        let ws_client = new_websocket_client_with_retry(
            url,
            max_concurrent_requests,
            max_notifs_per_subscription,
            connection_timeout,
        )
        .await?;
        Self::new(ws_client, signer, shutdown_tx).await
    }

    async fn get_fresh_nonce(&self) -> Result<u32, SubxtError> {
        // For getting the nonce, use latest, possibly non-finalized block.
        // TODO: we might want to wait until the latest block is actually finalized
        // query account info in order to get the nonce value used for communication
        let storage_key = metadata::storage().system().account(self.get_account_id().clone());
        let on_chain_nonce = self
            .api
            .storage()
            .at_latest()
            .await?
            .fetch(&storage_key)
            .await
            .transpose()
            .and_then(|x| x.ok())
            .map(|x| x.nonce)
            .unwrap_or_default();

        let mut next_nonce = self.nonce.write().await;

        let ret = if on_chain_nonce > *next_nonce {
            log::info!("Synced to on-chain nonce: {}", on_chain_nonce);
            on_chain_nonce
        } else {
            *next_nonce
        };

        *next_nonce = ret.saturating_add(1);

        Ok(ret)
    }

    async fn query_finalized<Address>(&self, address: Address) -> Result<Option<Address::Target>, Error>
    where
        Address: StorageAddress<IsFetchable = Yes>,
    {
        let hash = self.get_finalized_block_hash().await?;
        Ok(self.api.storage().at(hash).fetch(&address).await?)
    }

    async fn query_finalized_or_error<Address>(&self, address: Address) -> Result<Address::Target, Error>
    where
        Address: StorageAddress<IsFetchable = Yes>,
    {
        self.query_finalized(address).await?.ok_or(Error::StorageItemNotFound)
    }

    async fn query_finalized_or_default<Address>(&self, address: Address) -> Result<Address::Target, Error>
    where
        Address: StorageAddress<IsFetchable = Yes, IsDefaultable = Yes>,
    {
        let hash = self.get_finalized_block_hash().await?;
        Ok(self.api.storage().at(hash).fetch_or_default(&address).await?)
    }

    /// Gets a copy of the signer with a unique nonce
    async fn with_unique_signer<Call>(&self, call: Call) -> Result<ExtrinsicEvents<InterBtcRuntime>, Error>
    where
        Call: TxPayload,
    {
        notify_retry::<Error, _, _, _, _, _>(
            || async {
                match timeout(TRANSACTION_TIMEOUT, async {
                    let nonce = self.get_fresh_nonce().await?;
                    let tx_progress = self
                        .api
                        .tx()
                        .create_signed_with_nonce(&call, &self.signer, nonce, Default::default())?
                        .submit_and_watch()
                        .await?;

                    if cfg!(feature = "testing-utils") {
                        tx_progress.wait_for_in_block().await?.wait_for_success().await
                    } else {
                        tx_progress.wait_for_finalized_success().await
                    }
                })
                .await
                {
                    Err(_) => {
                        log::warn!("Timeout on transaction submission - restart required");
                        let _ = self.shutdown_tx.send(());
                        Err(Error::Timeout)
                    }
                    Ok(x) => Ok(x?),
                }
            },
            |result| async {
                match result.map_err(Into::<Error>::into) {
                    Ok(te) => Ok(te),
                    Err(err) => {
                        if let Some(data) = err.is_invalid_transaction() {
                            Err(RetryPolicy::Skip(Error::InvalidTransaction(data)))
                        } else if err.is_pool_too_low_priority().is_some() {
                            Err(RetryPolicy::Skip(Error::PoolTooLowPriority))
                        } else if err.is_block_hash_not_found_error() {
                            log::info!("Re-sending transaction after apparent fork");
                            Err(RetryPolicy::Skip(Error::BlockHashNotFound))
                        } else {
                            Err(RetryPolicy::Throw(err))
                        }
                    }
                }
            },
        )
        .await
    }

    pub async fn get_finalized_block_hash(&self) -> Result<H256, Error> {
        Ok(self.api.rpc().finalized_head().await?)
    }

    /// Subscribe to new parachain blocks.
    pub async fn on_block<F, R>(&self, on_block: F) -> Result<(), Error>
    where
        F: Fn(InterBtcHeader) -> R,
        R: Future<Output = Result<(), Error>>,
    {
        let mut sub = if cfg!(feature = "testing-utils") {
            self.api.blocks().subscribe_best().await?
        } else {
            self.api.blocks().subscribe_finalized().await?
        };
        loop {
            on_block(
                sub.next()
                    .await
                    .ok_or(Error::ChannelClosed)?
                    .map(|x| x.header().clone())?,
            )
            .await?;
        }
    }

    /// Wait for the block at the given height
    /// Note: will always wait at least one block.
    pub async fn wait_for_block(&self, height: u32) -> Result<(), Error> {
        let mut sub = if cfg!(feature = "testing-utils") {
            self.api.blocks().subscribe_best().await?
        } else {
            self.api.blocks().subscribe_finalized().await?
        };
        while let Some(block) = sub.next().await {
            if block?.number() >= height {
                return Ok(());
            }
        }
        Err(Error::ChannelClosed)
    }

    /// Sleep for `delay` parachain blocks
    pub async fn delay_for_blocks(&self, delay: u32) -> Result<(), Error> {
        if delay == 0 {
            return Ok(());
        }
        let starting_parachain_height = self.get_current_chain_height().await?;
        self.wait_for_block(starting_parachain_height + delay).await
    }

    async fn subscribe_events(
        &self,
    ) -> Result<impl Stream<Item = Result<subxt::events::Events<InterBtcRuntime>, SubxtError>> + Unpin, Error> {
        if cfg!(feature = "testing-utils") {
            Ok(self
                .api
                .blocks()
                .subscribe_best()
                .await?
                .then(|x| async move { x?.events().await })
                .boxed())
        } else {
            Ok(self
                .api
                .blocks()
                .subscribe_finalized()
                .await?
                .then(|x| async move { x?.events().await })
                .boxed())
        }
    }

    /// Subscription service that should listen forever, only returns if the initial subscription
    /// cannot be established. Calls `on_error` when an error event has been received, or when an
    /// event has been received that failed to be decoded into a raw event.
    ///
    /// # Arguments
    /// * `on_error` - callback for decoding errors, is not allowed to take too long
    pub async fn on_event_error<E: Fn(Error)>(&self, on_error: E) -> Result<(), Error> {
        let mut sub = self.subscribe_events().await?;

        loop {
            match sub.next().await {
                Some(Err(err)) => on_error(err.into()), // report error
                Some(Ok(_)) => {}                       // do nothing
                None => break Ok(()),                   // end of stream
            }
        }
    }

    /// Subscription service that should listen forever, only returns if the initial subscription
    /// cannot be established. This function uses two concurrent tasks: one for the event listener,
    /// and one that calls the given callback. This allows the callback to take a long time to
    /// complete without breaking the rpc communication, which could otherwise happen. Still, since
    /// the queue of callbacks is processed sequentially, some care should be taken that the queue
    /// does not overflow. `on_error` is called when the event has successfully been decoded into a
    /// raw_event, but failed to decode into an event of type `T`
    ///
    /// # Arguments
    /// * `on_event` - callback for events, is allowed to sometimes take a longer time
    /// * `on_error` - callback for decoding error, is not allowed to take too long
    pub async fn on_event<T, F, R, E>(&self, mut on_event: F, on_error: E) -> Result<(), Error>
    where
        T: StaticEvent + core::fmt::Debug,
        F: FnMut(T) -> R,
        R: Future<Output = ()>,
        E: Fn(Error),
    {
        let mut sub = self.subscribe_events().await?;
        let (tx, mut rx) = futures::channel::mpsc::channel::<T>(32);

        // two tasks: one for event listening and one for callback calling
        futures::future::try_join(
            async move {
                let tx = &tx;
                while let Some(result) = sub.next().fuse().await {
                    let event_stream = result
                        .iter()
                        .flat_map(|events| events.iter().map(|x| x.and_then(|y| y.as_event::<T>())))
                        .filter_map(|x| x.transpose());
                    for result in event_stream {
                        match result {
                            Ok(event) => {
                                log::trace!("event: {:?}", event);
                                if tx.clone().send(event).await.is_err() {
                                    break;
                                }
                            }
                            Err(err) => on_error(err.into()),
                        }
                    }
                }
                Result::<(), _>::Err(Error::ChannelClosed)
            },
            async move {
                loop {
                    // block until we receive an event from the other task
                    match rx.next().fuse().await {
                        Some(event) => {
                            on_event(event).await;
                        }
                        None => {
                            return Result::<(), _>::Err(Error::ChannelClosed);
                        }
                    }
                }
            },
        )
        .await?;
        Ok(())
    }

    async fn batch(&self, calls: Vec<EncodedCall>) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().utility().batch(calls)).await?;
        Ok(())
    }

    /// Emulate the POOL_INVALID_TX error using token transfer extrinsics.
    #[cfg(test)]
    pub async fn get_invalid_tx_error(&self, recipient: AccountId) -> Error {
        let call = metadata::tx().tokens().transfer(recipient, Token(DOT), 100);
        let nonce = self.get_fresh_nonce().await.unwrap();

        self.api
            .tx()
            .create_signed_with_nonce(&call, &self.signer, nonce, Default::default())
            .unwrap()
            .submit_and_watch()
            .await
            .unwrap();

        // now call with outdated nonce
        self.api
            .tx()
            .create_signed_with_nonce(&call, &self.signer, 0, Default::default())
            .unwrap()
            .submit_and_watch()
            .await
            .unwrap_err()
            .into()
    }

    /// Emulate the POOL_TOO_LOW_PRIORITY error using token transfer extrinsics.
    #[cfg(test)]
    pub async fn get_too_low_priority_error(&self, recipient: AccountId) -> Error {
        let call = metadata::tx().tokens().transfer(recipient, Token(DOT), 100);

        let nonce = self.get_fresh_nonce().await.unwrap();

        // submit tx but don't watch
        self.api
            .tx()
            .create_signed_with_nonce(&call, &self.signer, nonce, Default::default())
            .unwrap()
            .submit()
            .await
            .unwrap();

        // should call with the same nonce
        self.api
            .tx()
            .create_signed_with_nonce(&call, &self.signer, nonce, Default::default())
            .unwrap()
            .submit_and_watch()
            .await
            .unwrap_err()
            .into()
    }

    #[cfg(test)]
    pub async fn register_dummy_assets(&self) -> Result<(), Error> {
        let metadatas = ["ABC", "TEst", "QQQ"].map(|symbol| GenericAssetMetadata {
            decimals: 10,
            location: None,
            name: b"irrelevant".to_vec(),
            symbol: symbol.as_bytes().to_vec(),
            existential_deposit: 0,
            additional: metadata::runtime_types::interbtc_primitives::CustomMetadata {
                fee_per_second: 0,
                coingecko_id: vec![],
            },
        });

        let registration_calls = metadatas
            .map(|metadata| {
                EncodedCall::AssetRegistry(
                    metadata::runtime_types::orml_asset_registry::module::Call::register_asset {
                        metadata: metadata.clone(),
                        asset_id: None,
                    },
                )
            })
            .to_vec();

        let batch = EncodedCall::Utility(metadata::runtime_types::pallet_utility::pallet::Call::batch {
            calls: registration_calls,
        });

        self.with_unique_signer(metadata::tx().sudo().sudo(batch)).await?;
        Ok(())
    }
    #[cfg(test)]
    fn lending_mock_market_from_id(&self, id: u32) -> metadata::runtime_types::loans::types::Market<Balance> {
        use sp_runtime::{FixedPointNumber, FixedU128 as Rate, Permill as Ratio};

        metadata::runtime_types::loans::types::Market::<Balance> {
            close_factor: Static(Ratio::from_percent(50)),
            collateral_factor: Static(Ratio::from_percent(50)),
            liquidation_threshold: Static(Ratio::from_percent(55)),
            liquidate_incentive: Static(Rate::from_inner(Rate::DIV / 100 * 110)),
            state: metadata::runtime_types::loans::types::MarketState::Pending,
            rate_model: metadata::runtime_types::loans::rate_model::InterestRateModel::Jump(
                metadata::runtime_types::loans::rate_model::JumpModel {
                    base_rate: Static(Rate::from_inner(Rate::DIV / 100 * 2)),
                    jump_rate: Static(Rate::from_inner(Rate::DIV / 100 * 10)),
                    full_rate: Static(Rate::from_inner(Rate::DIV / 100 * 32)),
                    jump_utilization: Static(Ratio::from_percent(80)),
                },
            ),
            reserve_factor: Static(Ratio::from_percent(15)),
            liquidate_incentive_reserved_factor: Static(Ratio::from_percent(3)),
            supply_cap: 1_000_000_000_000_000_000_000u128,
            borrow_cap: 1_000_000_000_000_000_000_000u128,
            lend_token_id: LendToken(id),
        }
    }

    #[cfg(test)]
    pub async fn register_lending_markets(&self) -> Result<(), Error> {
        let add_market_txs = [ForeignAsset(1), Token(KINT)]
            .iter()
            .enumerate()
            .map(|(i, asset_id)| {
                EncodedCall::Loans(metadata::runtime_types::loans::pallet::Call::add_market {
                    asset_id: *asset_id,
                    market: self.lending_mock_market_from_id(i as u32),
                })
            })
            .collect();
        let batch = EncodedCall::Utility(metadata::runtime_types::pallet_utility::pallet::Call::batch {
            calls: add_market_txs,
        });
        self.with_unique_signer(metadata::tx().sudo().sudo(batch)).await?;
        Ok(())
    }

    pub async fn store_assets_metadata(&self) -> Result<(), Error> {
        AssetRegistry::extend(self.get_foreign_assets_metadata().await?)
    }

    pub async fn store_lend_tokens(&self) -> Result<(), Error> {
        let lend_tokens = self.get_lend_tokens().await?;
        LendingAssets::extend(lend_tokens)
    }

    /// Cache registered assets and updates
    pub async fn listen_for_registered_assets(&self) -> Result<(), Error> {
        futures::future::try_join(
            self.on_event::<RegisteredAssetEvent, _, _, _>(
                |event| async move {
                    if let Err(err) = AssetRegistry::insert(event.asset_id, event.metadata) {
                        log::error!("Failed to register asset {}: {}", event.asset_id, err);
                    }
                },
                |_| {},
            ),
            self.on_event::<UpdatedAssetEvent, _, _, _>(
                |event| async move {
                    if let Err(err) = AssetRegistry::insert(event.asset_id, event.metadata) {
                        log::error!("Failed to update asset {}: {}", event.asset_id, err);
                    }
                },
                |_| {},
            ),
        )
        .await?;
        Ok(())
    }

    /// Cache new markets and updates
    pub async fn listen_for_lending_markets(&self) -> Result<(), Error> {
        futures::future::try_join(
            self.on_event::<NewMarketEvent, _, _, _>(
                |event| async move {
                    if let Err(err) = LendingAssets::insert(event.underlying_currency_id, event.market.lend_token_id) {
                        log::error!(
                            "Failed to register lend token {:?}: {}",
                            event.underlying_currency_id,
                            err
                        );
                    }
                },
                |_| {},
            ),
            self.on_event::<UpdatedMarketEvent, _, _, _>(
                |event| async move {
                    if let Err(err) = LendingAssets::insert(event.underlying_currency_id, event.market.lend_token_id) {
                        log::error!(
                            "Failed to update lend token {:?}: {}",
                            event.underlying_currency_id,
                            err
                        );
                    }
                },
                |_| {},
            ),
        )
        .await?;
        Ok(())
    }

    /// Listen to fee_rate changes and broadcast new values on the fee_rate_update_tx channel
    pub async fn listen_for_fee_rate_changes(&self) -> Result<(), Error> {
        self.on_event::<FeedValuesEvent, _, _, _>(
            |event| async move {
                for (key, value) in event.values {
                    if let OracleKey::FeeEstimation = key {
                        let _ = self.fee_rate_update_tx.send(*value);
                    }
                }
            },
            |_error| {
                // Don't propagate error, it's unlikely to be useful.
                // We assume critical errors will cause the system to restart.
                // Note that we can't send the error itself due to the channel requiring
                // the type to be clonable, which Error isn't
            },
        )
        .await?;
        Ok(())
    }

    async fn get_decoded_storage_keys<T, U>(
        &self,
        key_addr: KeyStorageAddress<T>,
        hasher: StorageMapHasher,
    ) -> Result<Vec<(U, T)>, Error>
    where
        T: Decode + Send + 'static + DecodeWithMetadata,
        U: Decode + Send + 'static,
    {
        let head = self.get_finalized_block_hash().await?;
        let mut iter = self.api.storage().at(head).iter(key_addr, DEFAULT_PAGE_SIZE).await?;

        let mut ret = Vec::new();
        while let Some((key, value)) = iter.next().await? {
            let raw_key = key.0.clone();
            // last bytes are the raw key
            let mut key = match hasher {
                StorageMapHasher::Blake2_128 => Self::strip_blake2_key_prefix(raw_key.as_slice()),
                StorageMapHasher::Twox_64 => Self::strip_twox64_key_prefix(raw_key.as_slice()),
            };

            let decoded_key = U::decode(&mut key)?;
            ret.push((decoded_key, value));
        }
        Ok(ret)
    }

    fn strip_blake2_key_prefix(raw_key: &[u8]) -> &[u8] {
        &raw_key[BLAKE2_128_HASH_PREFIX_LENGTH..]
    }

    fn strip_twox64_key_prefix(raw_key: &[u8]) -> &[u8] {
        &raw_key[TWOX_64_HASH_PREFIX_LENGTH..]
    }

    async fn get_chain_counter(&self) -> Result<u32, Error> {
        self.query_finalized_or_default(metadata::storage().btc_relay().chain_counter())
            .await
    }
}

#[async_trait]
pub trait UtilFuncs {
    /// Gets the current height of the parachain
    async fn get_current_chain_height(&self) -> Result<u32, Error>;

    async fn get_rpc_properties(&self) -> Result<serde_json::Map<String, Value>, Error>;

    /// Gets the ID of the native currency.
    fn get_native_currency_id(&self) -> CurrencyId;

    /// Get the address of the configured signer.
    fn get_account_id(&self) -> &AccountId;

    fn is_this_vault(&self, vault_id: &VaultId) -> bool;

    async fn get_foreign_assets_metadata(&self) -> Result<Vec<(u32, AssetMetadata)>, Error>;

    async fn get_foreign_asset_metadata(&self, id: u32) -> Result<AssetMetadata, Error>;

    async fn get_lend_tokens(&self) -> Result<Vec<(CurrencyId, CurrencyId)>, Error>;
}

#[async_trait]
impl UtilFuncs for InterBtcParachain {
    async fn get_current_chain_height(&self) -> Result<u32, Error> {
        self.query_finalized_or_error(metadata::storage().system().number())
            .await
    }

    async fn get_rpc_properties(&self) -> Result<serde_json::Map<String, Value>, Error> {
        Ok(self.api.rpc().system_properties().await?)
    }

    fn get_native_currency_id(&self) -> CurrencyId {
        self.native_currency_id
    }

    fn get_account_id(&self) -> &AccountId {
        &self.account_id
    }

    fn is_this_vault(&self, vault_id: &VaultId) -> bool {
        &vault_id.account_id == self.get_account_id()
    }

    async fn get_foreign_assets_metadata(&self) -> Result<Vec<(u32, AssetMetadata)>, Error> {
        let key_addr = metadata::storage().asset_registry().metadata_root();
        self.get_decoded_storage_keys(key_addr, StorageMapHasher::Twox_64).await
    }

    async fn get_lend_tokens(&self) -> Result<Vec<(CurrencyId, CurrencyId)>, Error> {
        let key_addr = metadata::storage().loans().markets_root();
        let markets = self
            .get_decoded_storage_keys::<_, CurrencyId>(key_addr, StorageMapHasher::Blake2_128)
            .await?;
        let ret = markets
            .into_iter()
            .map(|(underlying_currency_id, market)| {
                let lend_token_id = market.lend_token_id;
                (underlying_currency_id, lend_token_id)
            })
            .collect();
        Ok(ret)
    }

    async fn get_foreign_asset_metadata(&self, id: u32) -> Result<AssetMetadata, Error> {
        self.query_finalized(metadata::storage().asset_registry().metadata(id))
            .await?
            .ok_or(Error::AssetNotFound)
    }
}

#[async_trait]
pub trait CollateralBalancesPallet {
    async fn get_free_balance(&self, currency_id: CurrencyId) -> Result<Balance, Error>;

    async fn get_free_balance_for_id(&self, id: AccountId, currency_id: CurrencyId) -> Result<Balance, Error>;

    async fn get_reserved_balance(&self, currency_id: CurrencyId) -> Result<Balance, Error>;

    async fn get_reserved_balance_for_id(&self, id: AccountId, currency_id: CurrencyId) -> Result<Balance, Error>;

    async fn transfer_to(&self, recipient: &AccountId, amounts: Vec<(u128, CurrencyId)>) -> Result<(), Error>;
}

#[async_trait]
impl CollateralBalancesPallet for InterBtcParachain {
    async fn get_free_balance(&self, currency_id: CurrencyId) -> Result<Balance, Error> {
        Ok(Self::get_free_balance_for_id(self, self.account_id.clone(), currency_id).await?)
    }

    async fn get_free_balance_for_id(&self, id: AccountId, currency_id: CurrencyId) -> Result<Balance, Error> {
        let storage_key = metadata::storage().tokens().accounts(id.clone(), currency_id);
        Ok(self.query_finalized_or_default(storage_key).await?.free)
    }

    async fn get_reserved_balance(&self, currency_id: CurrencyId) -> Result<Balance, Error> {
        Ok(Self::get_reserved_balance_for_id(self, self.account_id.clone(), currency_id).await?)
    }

    async fn get_reserved_balance_for_id(&self, id: AccountId, currency_id: CurrencyId) -> Result<Balance, Error> {
        let storage_key = metadata::storage().tokens().accounts(id.clone(), currency_id);
        Ok(self.query_finalized_or_default(storage_key).await?.reserved)
    }

    async fn transfer_to(&self, recipient: &AccountId, amounts: Vec<(u128, CurrencyId)>) -> Result<(), Error> {
        self.batch(
            amounts
                .into_iter()
                .map(|(amount, currency_id)| {
                    EncodedCall::Tokens(metadata::runtime_types::orml_tokens::module::Call::transfer {
                        dest: recipient.clone(),
                        currency_id,
                        amount,
                    })
                })
                .collect(),
        )
        .await
    }
}

#[async_trait]
pub trait ReplacePallet {
    /// Request the replacement of a new vault ownership
    ///
    /// # Arguments
    ///
    /// * `&self` - sender of the transaction
    /// * `amount` - amount of [Wrapped]
    async fn request_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

    /// Withdraw a request of vault replacement
    ///
    /// # Arguments
    ///
    /// * `&self` - sender of the transaction: the old vault
    /// * `amount` - the amount of [Wrapped] to replace
    async fn withdraw_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

    /// Accept request of vault replacement
    ///
    /// # Arguments
    ///
    /// * `&self` - the initiator of the transaction: the new vault
    /// * `old_vault` - the vault to replace
    /// * `amount_btc` - the amount of [Wrapped] to replace
    /// * `collateral` - the collateral for replacement
    /// * `btc_address` - the address to send funds to
    async fn accept_replace(
        &self,
        new_vault: &VaultId,
        old_vault: &VaultId,
        amount_btc: u128,
        collateral: u128,
        btc_address: BtcAddress,
    ) -> Result<(), Error>;

    /// Execute vault replacement
    ///
    /// # Arguments
    ///
    /// * `&self` - sender of the transaction: the old vault
    /// * `replace_id` - the ID of the replacement request
    /// * `raw_proof` - raw tx and proofs of coinbase and user tx
    async fn execute_replace(&self, replace_id: H256, raw_proof: &RawTransactionProof) -> Result<(), Error>;

    /// Cancel vault replacement
    ///
    /// # Arguments
    ///
    /// * `&self` - sender of the transaction: the new vault
    /// * `replace_id` - the ID of the replacement request
    async fn cancel_replace(&self, replace_id: H256) -> Result<(), Error>;

    /// Get all replace requests accepted by the given vault
    async fn get_new_vault_replace_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error>;

    /// Get all replace requests made by the given vault
    async fn get_old_vault_replace_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error>;

    /// Get the time difference in number of blocks between when a replace
    /// request is created and required completion time by a vault
    async fn get_replace_period(&self) -> Result<u32, Error>;

    /// Get a replace request from storage
    async fn get_replace_request(&self, replace_id: H256) -> Result<InterBtcReplaceRequest, Error>;

    /// Gets the minimum btc amount for replace requests
    async fn get_replace_dust_amount(&self) -> Result<u128, Error>;
}

#[async_trait]
impl ReplacePallet for InterBtcParachain {
    async fn request_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .replace()
                .request_replace(vault_id.currencies.clone(), amount),
        )
        .await?;
        Ok(())
    }

    async fn withdraw_replace(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .replace()
                .withdraw_replace(vault_id.currencies.clone(), amount),
        )
        .await?;
        Ok(())
    }

    async fn accept_replace(
        &self,
        new_vault: &VaultId,
        old_vault: &VaultId,
        amount_btc: u128,
        collateral: u128,
        btc_address: BtcAddress,
    ) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().replace().accept_replace(
            new_vault.currencies.clone(),
            old_vault.clone(),
            amount_btc,
            collateral,
            Static(btc_address),
        ))
        .await?;
        Ok(())
    }

    async fn execute_replace(&self, replace_id: H256, raw_proof: &RawTransactionProof) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .replace()
                .execute_replace(Static(replace_id), build_full_tx_proof(raw_proof)?),
        )
        .await?;
        Ok(())
    }

    async fn cancel_replace(&self, replace_id: H256) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().replace().cancel_replace(Static(replace_id)))
            .await?;
        Ok(())
    }

    /// Get all replace requests accepted by the given vault
    async fn get_new_vault_replace_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: Vec<H256> = self
            .api
            .rpc()
            .request("replace_getNewVaultReplaceRequests", rpc_params![account_id, head])
            .await?;
        join_all(
            result
                .into_iter()
                .map(|key| async move { self.get_replace_request(key).await.map(|value| (key, value)) }),
        )
        .await
        .into_iter()
        .collect()
    }

    /// Get all replace requests made by the given vault
    async fn get_old_vault_replace_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcReplaceRequest)>, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: Vec<H256> = self
            .api
            .rpc()
            .request("replace_getOldVaultReplaceRequests", rpc_params![account_id, head])
            .await?;
        join_all(
            result
                .into_iter()
                .map(|key| async move { self.get_replace_request(key).await.map(|value| (key, value)) }),
        )
        .await
        .into_iter()
        .collect()
    }

    async fn get_replace_period(&self) -> Result<u32, Error> {
        self.query_finalized_or_error(metadata::storage().replace().replace_period())
            .await
    }

    async fn get_replace_request(&self, replace_id: H256) -> Result<InterBtcReplaceRequest, Error> {
        self.query_finalized_or_error(metadata::storage().replace().replace_requests(Static::from(replace_id)))
            .await
    }

    async fn get_replace_dust_amount(&self) -> Result<u128, Error> {
        self.query_finalized_or_error(metadata::storage().replace().replace_btc_dust_value())
            .await
    }
}

#[async_trait]
pub trait TimestampPallet {
    async fn get_time_now(&self) -> Result<u64, Error>;
}

#[async_trait]
impl TimestampPallet for InterBtcParachain {
    /// Get the current time as defined by the `timestamp` pallet.
    async fn get_time_now(&self) -> Result<u64, Error> {
        self.query_finalized_or_error(metadata::storage().timestamp().now())
            .await
    }
}

#[async_trait]
pub trait OraclePallet {
    async fn get_exchange_rate(&self, currency_id: CurrencyId) -> Result<FixedU128, Error>;

    async fn feed_values(&self, values: Vec<(OracleKey, FixedU128)>) -> Result<(), Error>;

    async fn set_bitcoin_fees(&self, value: FixedU128) -> Result<(), Error>;

    async fn get_bitcoin_fees(&self) -> Result<FixedU128, Error>;

    async fn wrapped_to_collateral(&self, amount: u128, currency_id: CurrencyId) -> Result<u128, Error>;

    async fn collateral_to_wrapped(&self, amount: u128, currency_id: CurrencyId) -> Result<u128, Error>;

    async fn has_updated(&self, key: &OracleKey) -> Result<bool, Error>;

    fn on_fee_rate_change(&self) -> FeeRateUpdateReceiver;
}

#[async_trait]
impl OraclePallet for InterBtcParachain {
    /// Returns the last exchange rate in planck per satoshis, the time at which it was set
    /// and the configured max delay.
    async fn get_exchange_rate(&self, currency_id: CurrencyId) -> Result<FixedU128, Error> {
        Ok(*self
            .query_finalized_or_error(
                metadata::storage()
                    .oracle()
                    .aggregate(&OracleKey::ExchangeRate(currency_id)),
            )
            .await?)
    }

    /// Sets the current exchange rate (i.e. DOT/BTC)
    ///
    /// # Arguments
    /// * `value` - the current exchange rate
    async fn feed_values(&self, values: Vec<(OracleKey, FixedU128)>) -> Result<(), Error> {
        let converted_values: Vec<(OracleKey, Static<FixedU128>)> = values
            .iter()
            .map(|(key, value)| (key.clone(), Static::from(*value)))
            .collect();
        self.with_unique_signer(metadata::tx().oracle().feed_values(converted_values))
            .await?;
        Ok(())
    }

    /// Sets the estimated Satoshis per bytes required to get a Bitcoin transaction included in
    /// in the next block (~10 min)
    ///
    /// # Arguments
    /// * `value` - the estimated fee rate
    async fn set_bitcoin_fees(&self, value: FixedU128) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .oracle()
                .feed_values(vec![(OracleKey::FeeEstimation, Static::from(value))]),
        )
        .await?;
        Ok(())
    }

    /// Gets the estimated Satoshis per bytes required to get a Bitcoin transaction included in
    /// in the next x blocks
    async fn get_bitcoin_fees(&self) -> Result<FixedU128, Error> {
        Ok(*self
            .query_finalized_or_error(metadata::storage().oracle().aggregate(&OracleKey::FeeEstimation))
            .await?)
    }

    /// Converts the amount in btc to dot, based on the current set exchange rate.
    async fn wrapped_to_collateral(&self, amount: u128, currency_id: CurrencyId) -> Result<u128, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: BalanceWrapper<_> = self
            .api
            .rpc()
            .request(
                "oracle_wrappedToCollateral",
                rpc_params![BalanceWrapper { amount }, currency_id, head],
            )
            .await?;
        Ok(result.amount)
    }

    /// Converts the amount in dot to btc, based on the current set exchange rate.
    async fn collateral_to_wrapped(&self, amount: u128, currency_id: CurrencyId) -> Result<u128, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: BalanceWrapper<_> = self
            .api
            .rpc()
            .request(
                "oracle_collateralToWrapped",
                rpc_params![BalanceWrapper { amount }, currency_id, head],
            )
            .await?;

        Ok(result.amount)
    }

    async fn has_updated(&self, key: &OracleKey) -> Result<bool, Error> {
        Ok(self
            .query_finalized_or_error(metadata::storage().oracle().raw_values_updated(key))
            .await
            .unwrap_or(false))
    }

    fn on_fee_rate_change(&self) -> FeeRateUpdateReceiver {
        self.fee_rate_update_tx.subscribe()
    }
}

#[async_trait]
pub trait SecurityPallet {
    /// Gets the current active block number of the parachain
    async fn get_current_active_block_number(&self) -> Result<u32, Error>;
}

#[async_trait]
impl SecurityPallet for InterBtcParachain {
    /// Gets the current active block number of the parachain
    async fn get_current_active_block_number(&self) -> Result<u32, Error> {
        self.query_finalized_or_default(metadata::storage().security().active_block_count())
            .await
    }
}

#[async_trait]
pub trait IssuePallet {
    /// Request a new issue
    async fn request_issue(&self, amount: u128, vault_id: &VaultId) -> Result<RequestIssueEvent, Error>;

    /// Execute a issue request by providing a Bitcoin transaction inclusion proof
    async fn execute_issue(&self, issue_id: H256, raw_proof: &RawTransactionProof) -> Result<(), Error>;

    /// Cancel an ongoing issue request
    async fn cancel_issue(&self, issue_id: H256) -> Result<(), Error>;

    async fn get_issue_request(&self, issue_id: H256) -> Result<InterBtcIssueRequest, Error>;

    async fn get_vault_issue_requests(&self, account_id: AccountId)
        -> Result<Vec<(H256, InterBtcIssueRequest)>, Error>;

    async fn get_issue_period(&self) -> Result<u32, Error>;

    async fn get_all_active_issues(&self) -> Result<Vec<(H256, InterBtcIssueRequest)>, Error>;
}

#[async_trait]
impl IssuePallet for InterBtcParachain {
    async fn request_issue(&self, amount: u128, vault_id: &VaultId) -> Result<RequestIssueEvent, Error> {
        self.with_unique_signer(
            metadata::tx()
                .issue()
                .request_issue(amount, vault_id.clone(), self.native_currency_id),
        )
        .await?
        .find_first::<RequestIssueEvent>()?
        .ok_or(Error::RequestIssueIDNotFound)
    }

    async fn execute_issue(&self, issue_id: H256, raw_proof: &RawTransactionProof) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .issue()
                .execute_issue(Static(issue_id), build_full_tx_proof(raw_proof)?),
        )
        .await?;
        Ok(())
    }

    async fn cancel_issue(&self, issue_id: H256) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().issue().cancel_issue(Static(issue_id)))
            .await?;
        Ok(())
    }

    async fn get_issue_request(&self, issue_id: H256) -> Result<InterBtcIssueRequest, Error> {
        self.query_finalized_or_error(metadata::storage().issue().issue_requests(Static::from(issue_id)))
            .await
    }

    async fn get_vault_issue_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcIssueRequest)>, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: Vec<H256> = self
            .api
            .rpc()
            .request("issue_getVaultIssueRequests", rpc_params![account_id, head])
            .await?;
        join_all(
            result
                .into_iter()
                .map(|key| async move { self.get_issue_request(key).await.map(|value| (key, value)) }),
        )
        .await
        .into_iter()
        .collect()
    }

    async fn get_issue_period(&self) -> Result<u32, Error> {
        self.query_finalized_or_error(metadata::storage().issue().issue_period())
            .await
    }

    async fn get_all_active_issues(&self) -> Result<Vec<(H256, InterBtcIssueRequest)>, Error> {
        let current_height = self.get_current_active_block_number().await?;
        let issue_period = self.get_issue_period().await?;

        let mut issue_requests = Vec::new();

        let head = self.get_finalized_block_hash().await?;
        let key_addr = metadata::storage().issue().issue_requests_root();
        let mut iter = self.api.storage().at(head).iter(key_addr, DEFAULT_PAGE_SIZE).await?;

        while let Some((issue_id, request)) = iter.next().await? {
            // todo: we also need to check the bitcoin height
            if request.status == IssueRequestStatus::Pending && request.opentime + issue_period > current_height {
                let key_hash = issue_id.0.as_slice();
                // last bytes are the raw key
                let key = Self::strip_blake2_key_prefix(key_hash);
                issue_requests.push((H256::from_slice(key), request));
            }
        }
        Ok(issue_requests)
    }
}

#[async_trait]
pub trait RedeemPallet {
    /// Request a new redeem
    async fn request_redeem(&self, amount: u128, btc_address: BtcAddress, vault_id: &VaultId) -> Result<H256, Error>;

    /// Execute a redeem request by providing a Bitcoin transaction inclusion proof
    async fn execute_redeem(&self, redeem_id: H256, raw_proof: &RawTransactionProof) -> Result<(), Error>;

    /// Cancel an ongoing redeem request
    async fn cancel_redeem(&self, redeem_id: H256, reimburse: bool) -> Result<(), Error>;

    async fn get_redeem_request(&self, redeem_id: H256) -> Result<InterBtcRedeemRequest, Error>;

    /// Get all redeem requests requested of the given vault
    async fn get_vault_redeem_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error>;

    async fn get_redeem_period(&self) -> Result<BlockNumber, Error>;
}

#[async_trait]
impl RedeemPallet for InterBtcParachain {
    async fn request_redeem(&self, amount: u128, btc_address: BtcAddress, vault_id: &VaultId) -> Result<H256, Error> {
        let redeem_event = self
            .with_unique_signer(
                metadata::tx()
                    .redeem()
                    .request_redeem(amount, Static(btc_address), vault_id.clone()),
            )
            .await?
            .find_first::<RequestRedeemEvent>()?
            .ok_or(Error::RequestRedeemIDNotFound)?;
        Ok(*redeem_event.redeem_id)
    }

    async fn execute_redeem(&self, redeem_id: H256, raw_proof: &RawTransactionProof) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .redeem()
                .execute_redeem(Static(redeem_id), build_full_tx_proof(raw_proof)?),
        )
        .await?;
        Ok(())
    }

    async fn cancel_redeem(&self, redeem_id: H256, reimburse: bool) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().redeem().cancel_redeem(Static(redeem_id), reimburse))
            .await?;
        Ok(())
    }

    async fn get_redeem_request(&self, redeem_id: H256) -> Result<InterBtcRedeemRequest, Error> {
        self.query_finalized_or_error(metadata::storage().redeem().redeem_requests(Static::from(redeem_id)))
            .await
    }

    async fn get_vault_redeem_requests(
        &self,
        account_id: AccountId,
    ) -> Result<Vec<(H256, InterBtcRedeemRequest)>, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: Vec<H256> = self
            .api
            .rpc()
            .request("redeem_getVaultRedeemRequests", rpc_params![account_id, head])
            .await?;
        join_all(
            result
                .into_iter()
                .map(|key| async move { self.get_redeem_request(key).await.map(|value| (key, value)) }),
        )
        .await
        .into_iter()
        .collect()
    }

    async fn get_redeem_period(&self) -> Result<BlockNumber, Error> {
        self.query_finalized_or_error(metadata::storage().redeem().redeem_period())
            .await
    }
}

#[async_trait]
pub trait BtcRelayPallet {
    async fn get_best_block(&self) -> Result<H256Le, Error>;

    async fn get_best_block_height(&self) -> Result<u32, Error>;

    async fn get_block_hash(&self, height: u32) -> Result<H256Le, Error>;

    async fn get_block_header(&self, hash: H256Le) -> Result<InterBtcRichBlockHeader, Error>;

    async fn get_bitcoin_confirmations(&self) -> Result<u32, Error>;

    async fn get_parachain_confirmations(&self) -> Result<BlockNumber, Error>;

    async fn wait_for_block_in_relay(
        &self,
        block_hash: H256Le,
        _btc_confirmations: Option<BlockNumber>, // todo: can we remove this?
    ) -> Result<(), Error>;

    async fn verify_block_header_inclusion(&self, block_hash: H256Le) -> Result<(), Error>;

    async fn initialize_btc_relay(&self, header: RawBlockHeader, height: BitcoinBlockHeight) -> Result<(), Error>;

    async fn store_block_header(&self, header: RawBlockHeader) -> Result<(), Error>;

    async fn store_block_headers(&self, headers: Vec<RawBlockHeader>) -> Result<(), Error>;
}

#[async_trait]
impl BtcRelayPallet for InterBtcParachain {
    /// Get the hash of the current best tip.
    async fn get_best_block(&self) -> Result<H256Le, Error> {
        Ok(self
            .query_finalized_or_default(metadata::storage().btc_relay().best_block())
            .await?)
    }

    /// Get the current best known height.
    async fn get_best_block_height(&self) -> Result<u32, Error> {
        Ok(self
            .query_finalized_or_default(metadata::storage().btc_relay().best_block_height())
            .await?)
    }

    /// Get the block hash for the main chain at the specified height.
    ///
    /// # Arguments
    /// * `height` - chain height
    async fn get_block_hash(&self, height: u32) -> Result<H256Le, Error> {
        Ok(self
            .query_finalized_or_default(metadata::storage().btc_relay().chains_hashes(0, height))
            .await?)
    }

    /// Get the corresponding block header for the given hash.
    ///
    /// # Arguments
    /// * `hash` - little endian block hash
    async fn get_block_header(&self, hash: H256Le) -> Result<InterBtcRichBlockHeader, Error> {
        Ok(self
            .query_finalized_or_default(metadata::storage().btc_relay().block_headers(&hash))
            .await?)
    }

    /// Get the global security parameter k for stable Bitcoin transactions
    async fn get_bitcoin_confirmations(&self) -> Result<u32, Error> {
        self.query_finalized_or_error(metadata::storage().btc_relay().stable_bitcoin_confirmations())
            .await
    }

    /// Get the global security parameter for stable parachain confirmations
    async fn get_parachain_confirmations(&self) -> Result<BlockNumber, Error> {
        self.query_finalized_or_error(metadata::storage().btc_relay().stable_parachain_confirmations())
            .await
    }

    /// Wait until Bitcoin block is submitted to the relay
    async fn wait_for_block_in_relay(
        &self,
        block_hash: H256Le,
        _btc_confirmations: Option<BlockNumber>,
    ) -> Result<(), Error> {
        loop {
            match self.verify_block_header_inclusion(block_hash.clone()).await {
                Ok(_) => return Ok(()),
                Err(e) if e.is_invalid_chain_id() => return Err(e),
                _ => {
                    log::trace!(
                        "block {} not found or confirmed, waiting for {:?}",
                        Into::<RichH256Le>::into(block_hash.clone()),
                        BLOCK_WAIT_TIMEOUT
                    );
                    sleep(BLOCK_WAIT_TIMEOUT).await;
                }
            };
        }
    }

    /// check that the block with the given block is included in the main chain of the relay, with sufficient
    /// confirmations
    async fn verify_block_header_inclusion(&self, block_hash: H256Le) -> Result<(), Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: Result<(), metadata::DispatchError> = self
            .api
            .rpc()
            .request(
                "btcRelay_verifyBlockHeaderInclusion",
                rpc_params![Into::<RichH256Le>::into(block_hash), head],
            )
            .await?;

        result.map_err(|err| {
            let dispatch_error = subxt::error::DispatchError::decode_from(err.encode(), self.api.metadata())
                .unwrap_or(subxt::error::DispatchError::Other);
            Error::SubxtRuntimeError(SubxtError::Runtime(dispatch_error))
        })
    }

    /// Initializes the relay with the provided block header and height,
    /// should be called automatically by relayer subject to the
    /// result of `is_initialized`.
    ///
    /// # Arguments
    /// * `header` - raw block header
    /// * `height` - starting height
    async fn initialize_btc_relay(&self, header: RawBlockHeader, height: BitcoinBlockHeight) -> Result<(), Error> {
        // TODO: can we initialize the relay through the chain-spec?
        // we would also need to consider re-initialization per governance
        self.with_unique_signer(
            metadata::tx()
                .btc_relay()
                .initialize(Static(parse_block_header(&header.0)?), height),
        )
        .await?;
        Ok(())
    }

    /// Stores a block header in the BTC-Relay.
    ///
    /// # Arguments
    /// * `header` - raw block header
    async fn store_block_header(&self, header: RawBlockHeader) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().btc_relay().store_block_header(
            Static(parse_block_header(&header.0)?),
            self.get_chain_counter().await?.saturating_add(1),
        ))
        .await?;
        Ok(())
    }

    /// Stores multiple block headers in the BTC-Relay.
    ///
    /// # Arguments
    /// * `headers` - raw block headers
    async fn store_block_headers(&self, headers: Vec<RawBlockHeader>) -> Result<(), Error> {
        let headers = headers
            .iter()
            .map(|header| parse_block_header(&header.0))
            .collect::<Result<Vec<_>, _>>()?;
        let fork_bound = self.get_chain_counter().await?.saturating_add(1);
        self.batch(
            headers
                .into_iter()
                .map(|block_header| {
                    EncodedCall::BTCRelay(metadata::runtime_types::btc_relay::pallet::Call::store_block_header {
                        block_header: Static(block_header),
                        fork_bound,
                    })
                })
                .collect(),
        )
        .await
    }
}

#[async_trait]
pub trait VaultRegistryPallet {
    async fn get_vault(&self, vault_id: &VaultId) -> Result<InterBtcVault, Error>;

    async fn get_vaults_by_account_id(&self, account_id: &AccountId) -> Result<Vec<VaultId>, Error>;

    async fn get_all_vaults(&self) -> Result<Vec<InterBtcVault>, Error>;

    async fn register_vault(&self, vault_id: &VaultId, collateral: u128) -> Result<(), Error>;

    async fn deposit_collateral(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

    async fn withdraw_collateral(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error>;

    async fn get_public_key(&self) -> Result<Option<BtcPublicKey>, Error>;

    async fn register_public_key(&self, public_key: BtcPublicKey) -> Result<(), Error>;

    async fn get_required_collateral_for_wrapped(
        &self,
        amount_btc: u128,
        collateral_currency: CurrencyId,
    ) -> Result<u128, Error>;

    async fn get_required_collateral_for_vault(&self, vault_id: VaultId) -> Result<u128, Error>;

    async fn get_vault_total_collateral(&self, vault_id: VaultId) -> Result<u128, Error>;

    async fn get_collateralization_from_vault(&self, vault_id: VaultId, only_issued: bool) -> Result<u128, Error>;

    async fn set_current_client_release(&self, uri: &[u8], code_hash: &H256) -> Result<(), Error>;

    async fn set_pending_client_release(&self, uri: &[u8], code_hash: &H256) -> Result<(), Error>;
}

#[async_trait]
impl VaultRegistryPallet for InterBtcParachain {
    /// Fetch a specific vault by ID.
    ///
    /// # Arguments
    /// * `vault_id` - account ID of the vault
    ///
    /// # Errors
    /// * `VaultNotFound` - if the rpc returned a default value rather than the vault we want
    /// * `VaultLiquidated` - if the vault is liquidated
    async fn get_vault(&self, vault_id: &VaultId) -> Result<InterBtcVault, Error> {
        match self
            .query_finalized(metadata::storage().vault_registry().vaults(vault_id))
            .await?
        {
            Some(InterBtcVaultStatic {
                status: VaultStatus::Liquidated,
                ..
            }) => Err(Error::VaultLiquidated),
            Some(vault) if &vault.id == vault_id => Ok(vault.into()),
            _ => Err(Error::VaultNotFound),
        }
    }

    async fn get_vaults_by_account_id(&self, account_id: &AccountId) -> Result<Vec<VaultId>, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result = self
            .api
            .rpc()
            .request("vaultRegistry_getVaultsByAccountId", rpc_params![account_id, head])
            .await?;
        Ok(result)
    }

    /// Fetch all active vaults.
    async fn get_all_vaults(&self) -> Result<Vec<InterBtcVault>, Error> {
        let head = self.get_finalized_block_hash().await?;
        let key_addr = metadata::storage().vault_registry().vaults_root();
        let mut iter = self.api.storage().at(head).iter(key_addr, DEFAULT_PAGE_SIZE).await?;

        let mut vaults = Vec::new();
        while let Some((_, account)) = iter.next().await? {
            let account: InterBtcVault = account.into();
            if let VaultStatus::Active(..) = account.status {
                vaults.push(account);
            }
        }
        Ok(vaults)
    }

    /// Submit extrinsic to register a vault.
    ///
    /// # Arguments
    /// * `collateral` - deposit
    /// * `public_key` - Bitcoin public key
    async fn register_vault(&self, vault_id: &VaultId, collateral: u128) -> Result<(), Error> {
        // TODO: check MinimumDeposit
        if collateral == 0 {
            return Err(Error::InsufficientFunds);
        }

        self.with_unique_signer(
            metadata::tx()
                .vault_registry()
                .register_vault(vault_id.currencies.clone(), collateral),
        )
        .await?;
        Ok(())
    }

    /// Locks additional collateral as a security against stealing the
    /// Bitcoin locked with it.
    ///
    /// # Arguments
    /// * `amount` - the amount of extra collateral to lock
    async fn deposit_collateral(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().nomination().deposit_collateral(vault_id.clone(), amount))
            .await?;
        Ok(())
    }

    /// Withdraws `amount` of the collateral from the amount locked by
    /// the vault corresponding to the origin account
    /// The collateral left after withdrawal must be more than MinimumCollateralVault
    /// and above the SecureCollateralThreshold. Collateral that is currently
    /// being used to back issued tokens remains locked until the Vault
    /// is used for a redeem request (full release can take multiple redeem requests).
    ///
    /// # Arguments
    /// * `amount` - the amount of collateral to withdraw
    async fn withdraw_collateral(&self, vault_id: &VaultId, amount: u128) -> Result<(), Error> {
        self.with_unique_signer(
            metadata::tx()
                .nomination()
                .withdraw_collateral(vault_id.clone(), Some(amount), None),
        )
        .await?;
        Ok(())
    }

    async fn get_public_key(&self) -> Result<Option<BtcPublicKey>, Error> {
        self.query_finalized(
            metadata::storage()
                .vault_registry()
                .vault_bitcoin_public_key(self.get_account_id().clone()),
        )
        .await
    }

    /// Update the default BTC public key for the vault corresponding to the signer.
    ///
    /// # Arguments
    /// * `public_key` - the new public key of the vault
    async fn register_public_key(&self, public_key: BtcPublicKey) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().vault_registry().register_public_key(public_key))
            .await?;
        Ok(())
    }

    /// Custom RPC that calculates the exact collateral required to cover the BTC amount.
    ///
    /// # Arguments
    /// * `amount_btc` - amount of btc to cover
    async fn get_required_collateral_for_wrapped(
        &self,
        amount_btc: u128,
        collateral_currency: CurrencyId,
    ) -> Result<u128, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: BalanceWrapper<_> = self
            .api
            .rpc()
            .request(
                "vaultRegistry_getRequiredCollateralForWrapped",
                rpc_params![BalanceWrapper { amount: amount_btc }, collateral_currency, head],
            )
            .await?;

        Ok(result.amount)
    }

    /// Get the amount of collateral required for the given vault to be at the
    /// current SecureCollateralThreshold with the current exchange rate
    async fn get_required_collateral_for_vault(&self, vault_id: VaultId) -> Result<u128, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: BalanceWrapper<_> = self
            .api
            .rpc()
            .request(
                "vaultRegistry_getRequiredCollateralForVault",
                rpc_params![vault_id, head],
            )
            .await?;
        Ok(result.amount)
    }

    async fn get_vault_total_collateral(&self, vault_id: VaultId) -> Result<u128, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: BalanceWrapper<_> = self
            .api
            .rpc()
            .request("vaultRegistry_getVaultTotalCollateral", rpc_params![vault_id, head])
            .await?;

        Ok(result.amount)
    }

    async fn get_collateralization_from_vault(&self, vault_id: VaultId, only_issued: bool) -> Result<u128, Error> {
        let head = Some(self.get_finalized_block_hash().await?);
        let result: UnsignedFixedPoint = self
            .api
            .rpc()
            .request(
                "vaultRegistry_getCollateralizationFromVault",
                rpc_params![vault_id, only_issued, head],
            )
            .await?;
        Ok(result.into_inner())
    }

    /// For testing purposes only. Sets the current vault client release.
    ///
    /// # Arguments
    /// * `uri` - URI to the client release binary
    /// * `checksum` - The SHA256 checksum of the client binary
    async fn set_current_client_release(&self, uri: &[u8], checksum: &H256) -> Result<(), Error> {
        let release = ClientRelease {
            uri: BoundedVec(uri.to_vec()),
            checksum: Static(*checksum),
        };
        // TODO: uri should be client name
        self.with_unique_signer(
            metadata::tx()
                .clients_info()
                .set_current_client_release(BoundedVec(uri.to_vec()), release),
        )
        .await?;
        Ok(())
    }

    /// For testing purposes only. Sets the pending vault client release.
    ///
    /// # Arguments
    /// * `uri` - URI to the client release binary
    /// * `checksum` - The SHA256 checksum of the client binary
    async fn set_pending_client_release(&self, uri: &[u8], checksum: &H256) -> Result<(), Error> {
        let release = ClientRelease {
            uri: BoundedVec(uri.to_vec()),
            checksum: Static(*checksum),
        };
        self.with_unique_signer(
            metadata::tx()
                .clients_info()
                .set_pending_client_release(BoundedVec(uri.to_vec()), release),
        )
        .await?;
        Ok(())
    }
}

#[async_trait]
pub trait FeePallet {
    async fn get_issue_griefing_collateral(&self) -> Result<FixedU128, Error>;
    async fn get_issue_fee(&self) -> Result<FixedU128, Error>;
    async fn get_replace_griefing_collateral(&self) -> Result<FixedU128, Error>;
}

#[async_trait]
impl FeePallet for InterBtcParachain {
    async fn get_issue_griefing_collateral(&self) -> Result<FixedU128, Error> {
        Ok(*self
            .query_finalized_or_error(metadata::storage().fee().issue_griefing_collateral())
            .await?)
    }

    async fn get_issue_fee(&self) -> Result<FixedU128, Error> {
        Ok(*self
            .query_finalized_or_error(metadata::storage().fee().issue_fee())
            .await?)
    }

    async fn get_replace_griefing_collateral(&self) -> Result<FixedU128, Error> {
        Ok(*self
            .query_finalized_or_error(metadata::storage().fee().replace_griefing_collateral())
            .await?)
    }
}

#[async_trait]
pub trait SudoPallet {
    async fn sudo(&self, call: EncodedCall) -> Result<(), Error>;
    async fn set_storage<V: Encode + Send + Sync>(&self, module: &str, key: &str, value: V) -> Result<(), Error>;
    async fn set_redeem_period(&self, period: BlockNumber) -> Result<(), Error>;
    async fn set_parachain_confirmations(&self, value: BlockNumber) -> Result<(), Error>;
    async fn set_bitcoin_confirmations(&self, value: u32) -> Result<(), Error>;
    async fn disable_difficulty_check(&self) -> Result<(), Error>;
    async fn set_issue_period(&self, period: u32) -> Result<(), Error>;
    async fn insert_authorized_oracle(&self, account_id: AccountId, name: String) -> Result<(), Error>;
    async fn set_replace_period(&self, period: u32) -> Result<(), Error>;
    async fn set_balances(&self, amounts: Vec<(AccountId, u128, u128, CurrencyId)>) -> Result<(), Error>;
}

#[async_trait]
impl SudoPallet for InterBtcParachain {
    async fn sudo(&self, call: EncodedCall) -> Result<(), Error> {
        self.with_unique_signer(metadata::tx().sudo().sudo(call)).await?;
        Ok(())
    }

    async fn set_storage<V: Encode + Send + Sync>(&self, module: &str, key: &str, value: V) -> Result<(), Error> {
        let module = sp_core::twox_128(module.as_bytes());
        let item = sp_core::twox_128(key.as_bytes());
        Ok(self
            .sudo(EncodedCall::System(
                metadata::runtime_types::frame_system::pallet::Call::set_storage {
                    items: vec![([module, item].concat(), value.encode())],
                },
            ))
            .await?)
    }

    async fn set_redeem_period(&self, period: BlockNumber) -> Result<(), Error> {
        Ok(self
            .sudo(EncodedCall::Redeem(
                metadata::runtime_types::redeem::pallet::Call::set_redeem_period { period },
            ))
            .await?)
    }

    /// Set the global security parameter for stable parachain confirmations
    async fn set_parachain_confirmations(&self, value: BlockNumber) -> Result<(), Error> {
        self.set_storage(crate::BTC_RELAY_MODULE, crate::STABLE_PARACHAIN_CONFIRMATIONS, value)
            .await
    }

    /// Set the global security parameter k for stable Bitcoin transactions
    async fn set_bitcoin_confirmations(&self, value: u32) -> Result<(), Error> {
        self.set_storage(crate::BTC_RELAY_MODULE, crate::STABLE_BITCOIN_CONFIRMATIONS, value)
            .await
    }

    async fn disable_difficulty_check(&self) -> Result<(), Error> {
        self.set_storage(crate::BTC_RELAY_MODULE, crate::DISABLE_DIFFICULTY_CHECK, true)
            .await
    }

    async fn set_issue_period(&self, period: u32) -> Result<(), Error> {
        Ok(self
            .sudo(EncodedCall::Issue(
                metadata::runtime_types::issue::pallet::Call::set_issue_period { period },
            ))
            .await?)
    }

    /// Adds a new authorized oracle with the given name and the signer's AccountId
    ///
    /// # Arguments
    /// * `account_id` - The Account ID of the new oracle
    /// * `name` - The name of the new oracle
    async fn insert_authorized_oracle(&self, account_id: AccountId, name: String) -> Result<(), Error> {
        Ok(self
            .sudo(EncodedCall::Oracle(
                metadata::runtime_types::oracle::pallet::Call::insert_authorized_oracle {
                    account_id,
                    name: BoundedVec(name.into_bytes()),
                },
            ))
            .await?)
    }

    async fn set_replace_period(&self, period: u32) -> Result<(), Error> {
        Ok(self
            .sudo(EncodedCall::Replace(
                metadata::runtime_types::replace::pallet::Call::set_replace_period { period },
            ))
            .await?)
    }

    async fn set_balances(&self, amounts: Vec<(AccountId, u128, u128, CurrencyId)>) -> Result<(), Error> {
        self.sudo(EncodedCall::Utility(
            metadata::runtime_types::pallet_utility::pallet::Call::batch {
                calls: amounts
                    .into_iter()
                    .map(|(recipient, free, reserved, currency_id)| {
                        EncodedCall::Tokens(metadata::runtime_types::orml_tokens::module::Call::set_balance {
                            who: recipient,
                            currency_id,
                            new_free: free,
                            new_reserved: reserved,
                        })
                    })
                    .collect(),
            },
        ))
        .await
    }
}

pub fn build_full_tx_proof(raw_proof: &RawTransactionProof) -> Result<Static<FullTransactionProof>, Error> {
    Ok(Static(FullTransactionProof {
        user_tx_proof: PartialTransactionProof {
            transaction: parse_transaction(&raw_proof.raw_user_tx[..])?,
            tx_encoded_len: raw_proof.raw_user_tx.len().try_into()?,
            merkle_proof: MerkleProof::parse(&raw_proof.user_tx_proof[..])?,
        },
        coinbase_proof: PartialTransactionProof {
            transaction: parse_transaction(&raw_proof.raw_coinbase_tx[..])?,
            tx_encoded_len: raw_proof.raw_coinbase_tx.len().try_into()?,
            merkle_proof: MerkleProof::parse(&raw_proof.coinbase_tx_proof[..])?,
        },
    }))
}