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
// This file is part of the SORA network and Polkaswap app.

// Copyright (c) 2020, 2021, Polka Biome Ltd. All rights reserved.
// SPDX-License-Identifier: BSD-4-Clause

// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:

// Redistributions of source code must retain the above copyright notice, this list
// of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// All advertising materials mentioning features or use of this software must display
// the following acknowledgement: This product includes software developed by Polka Biome
// Ltd., SORA, and Polkaswap.
//
// Neither the name of the Polka Biome Ltd. nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY Polka Biome Ltd. AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Polka Biome Ltd. BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/*!
# Multi-network Ethereum Bridge pallet

## Description
Provides functionality for cross-chain transfers of assets between Sora and Ethereum-based networks.

## Overview
Bridge can be divided into 3 main parts:
1. Bridge pallet. A Substrate pallet (this one).
2. Middle-layer. A part of the bridge pallet, more precisely, off-chain workers.
3. [Bridge contracts](https://github.com/sora-xor/sora2-evm-contracts). A set of smart-contracts
deployed on Ethereum-based networks.

## Definitions
_Thischain_ - working chain/network.
_Sidechain_ - external chain/network.
_Network_ - an ethereum-based network with a bridge contract.

## Bridge pallet
Stores basic information about networks: peers' accounts, requests and registered assets/tokens.
Networks can be added and managed through requests. Requests can be [incoming](`IncomingRequest`)
(came from sidechain) or [outgoing](`OutgoingRequest`) (to sidechain). Each request has it's own
hash (differs from extrinsic hash), status (`RequestStatus`), some specific data and additional information.
The requests life-cycle consists of 3 stages: validation, preparation and finalization.
Requests are registered by accounts and finalized by _bridge peers_.

## Middle-layer
Works through off-chain workers. Any substrate node can be a bridge peer with its own
secret key (differs from validator's key) and participate in bridge consensus (after election).
The bridge peer set (`Peers` in storage) forms an n-of-m-multisignature account (`BridgeAccount`
in storage), which is used to finalize all requests.

## Bridge contract
Persists the same multi-sig account (+- 1 signatory) for validating all its incoming requests.
*/

#![cfg_attr(not(feature = "std"), no_std)]
// TODO #167: fix clippy warnings
#![allow(clippy::all)]

#[macro_use]
extern crate alloc;
extern crate jsonrpc_core as jsonrpc;

use crate::offchain::SignatureParams;
use crate::util::majority;
use alloc::string::String;
use bridge_types::traits::BridgeApp;
use bridge_types::GenericNetworkId;
use codec::{Decode, Encode};
use common::prelude::Balance;
use common::{
    AssetInfoProvider, AssetName, AssetSymbol, BalancePrecision, DEFAULT_BALANCE_PRECISION,
};
use core::stringify;
use frame_support::dispatch::{DispatchError, DispatchResult};
use frame_support::log::{debug, error, info, warn};
use frame_support::sp_runtime::app_crypto::{ecdsa, sp_core};
use frame_support::sp_runtime::offchain::storage::StorageValueRef;
use frame_support::sp_runtime::offchain::storage_lock::{StorageLock, Time};
use frame_support::sp_runtime::traits::{
    AtLeast32Bit, BlockNumberProvider, MaybeSerializeDeserialize, Member, One, UniqueSaturatedInto,
};
use frame_support::sp_runtime::KeyTypeId;
use frame_support::traits::Get;
use frame_support::weights::Weight;
use frame_support::{ensure, fail, Parameter, RuntimeDebug};
use frame_system::offchain::{AppCrypto, CreateSignedTransaction};
use frame_system::pallet_prelude::OriginFor;
use frame_system::{ensure_root, ensure_signed};
use hex_literal::hex;
pub use pallet::*;
use permissions::{Scope, BURN, MINT};
use requests::*;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::{H160, H256};
use sp_std::borrow::Cow;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::fmt::{self, Debug};
use sp_std::marker::PhantomData;
use sp_std::prelude::*;
#[cfg(feature = "std")]
use std::collections::HashMap;
pub use weights::WeightInfo;

type EthAddress = H160;

pub mod weights;

mod benchmarking;
mod contract;
mod macros;
pub mod migration;
pub mod offchain;
pub mod requests;
mod rpc;
#[cfg(test)]
mod tests;
pub mod types;
mod util;

/// Substrate node RPC URL.
const SUB_NODE_URL: &str = "http://127.0.0.1:9954";
const HTTP_REQUEST_TIMEOUT_SECS: u64 = 10;
/// Substrate maximum amount of blocks for which an extrinsic is expecting to be finalized.
const SUBSTRATE_MAX_BLOCK_NUM_EXPECTING_UNTIL_FINALIZATION: u32 = 50;
/// Maximum substrate blocks can be handled during single offchain procedure.
const SUBSTRATE_HANDLE_BLOCK_COUNT_PER_BLOCK: u32 = 20;
#[cfg(not(test))]
const MAX_FAILED_SEND_SIGNED_TX_RETRIES: u16 = 2000;
#[cfg(test)]
const MAX_FAILED_SEND_SIGNED_TX_RETRIES: u16 = 10;
const MAX_SUCCESSFUL_SENT_SIGNED_TX_PER_ONCE: u8 = 5;

pub const TECH_ACCOUNT_PREFIX: &[u8] = b"bridge";
pub const TECH_ACCOUNT_MAIN: &[u8] = b"main";
pub const TECH_ACCOUNT_AUTHORITY: &[u8] = b"authority";

pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"ethb");
/// A number of sidechain blocks needed to consider transaction as confirmed.
pub const CONFIRMATION_INTERVAL: u64 = 30;
/// Maximum number of `Log` items per `eth_getLogs` request.
pub const MAX_GET_LOGS_ITEMS: u64 = 50;

// Off-chain worker storage paths.
pub const STORAGE_SUB_NODE_URL_KEY: &[u8] = b"eth-bridge-ocw::sub-node-url";
pub const STORAGE_PEER_SECRET_KEY: &[u8] = b"eth-bridge-ocw::secret-key";
pub const STORAGE_ETH_NODE_PARAMS: &str = "eth-bridge-ocw::node-params";
pub const STORAGE_NETWORK_IDS_KEY: &[u8] = b"eth-bridge-ocw::network-ids";
pub const STORAGE_PENDING_TRANSACTIONS_KEY: &[u8] = b"eth-bridge-ocw::pending-transactions";
pub const STORAGE_FAILED_PENDING_TRANSACTIONS_KEY: &[u8] =
    b"eth-bridge-ocw::failed-pending-transactions";
pub const STORAGE_SUB_TO_HANDLE_FROM_HEIGHT_KEY: &[u8] =
    b"eth-bridge-ocw::sub-to-handle-from-height";

/// Contract's `Deposit(bytes32,uint256,address,bytes32)` event topic.
pub const DEPOSIT_TOPIC: H256 = H256(hex!(
    "85c0fa492ded927d3acca961da52b0dda1debb06d8c27fe189315f06bb6e26c8"
));
pub const OFFCHAIN_TRANSACTION_WEIGHT_LIMIT: Weight =
    Weight::from_parts(10_000_000_000_000_000u64, 10_000_000_000_000_000u64);
const MAX_PENDING_TX_BLOCKS_PERIOD: u32 = 100;
const RE_HANDLE_TXS_PERIOD: u32 = 200;
/// Minimum peers required to start bridge migration
pub const MINIMUM_PEERS_FOR_MIGRATION: usize = 3;

type AssetIdOf<T> = <T as assets::Config>::AssetId;
type Timepoint<T> = bridge_multisig::BridgeTimepoint<<T as frame_system::Config>::BlockNumber>;
type BridgeTimepoint<T> = Timepoint<T>;
type BridgeNetworkId<T> = <T as Config>::NetworkId;

/// Ethereum node parameters (url, credentials).
#[derive(
    Encode, Decode, Eq, PartialEq, Clone, PartialOrd, Ord, RuntimeDebug, scale_info::TypeInfo,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct NodeParams {
    url: String,
    credentials: Option<String>,
}

/// Local peer config. Contains a set of networks that the peer is responsible for.
#[cfg(feature = "std")]
#[derive(Clone, RuntimeDebug, Serialize, Deserialize)]
pub struct PeerConfig<NetworkId: std::hash::Hash + Eq> {
    pub networks: HashMap<NetworkId, NodeParams>,
}

/// Network-specific parameters.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)]
pub struct NetworkParams<AccountId: Ord> {
    pub bridge_contract_address: EthAddress,
    pub initial_peers: BTreeSet<AccountId>,
}

/// Network configuration.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct NetworkConfig<T: Config> {
    pub initial_peers: BTreeSet<T::AccountId>,
    pub bridge_account_id: T::AccountId,
    pub assets: Vec<AssetConfig<T::AssetId>>,
    pub bridge_contract_address: EthAddress,
    pub reserves: Vec<(T::AssetId, Balance)>,
}

#[derive(Clone, Encode)]
pub struct BridgeAssetData<T: Config> {
    pub asset_id: T::AssetId,
    pub name: AssetName,
    pub symbol: AssetSymbol,
    pub sidechain_precision: BalancePrecision,
    pub sidechain_asset_id: sp_core::H160,
}

impl<T: Config> BridgeAssetData<T> {
    pub fn new(
        name: &'static str,
        symbol: &'static str,
        sidechain_precision: BalancePrecision,
        sidechain_asset_id: sp_core::H160,
    ) -> Self {
        let name: Cow<'_, str> = if name.contains(".") {
            name.replacen(".", " ", 2).into()
        } else {
            name.into()
        };
        let mut data = Self {
            asset_id: T::AssetId::from(H256::zero()),
            name: AssetName(name.as_bytes().to_vec()),
            symbol: AssetSymbol(symbol.as_bytes().to_vec()),
            sidechain_precision,
            sidechain_asset_id,
        };
        let asset_id =
            assets::Pallet::<T>::gen_asset_id_from_any(&("BridgeAssetData", data.clone()));
        data.asset_id = asset_id;
        data
    }
}

/// Bridge status.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug, scale_info::TypeInfo)]
pub enum BridgeStatus {
    Initialized,
    Migrating,
}

impl Default for BridgeStatus {
    fn default() -> Self {
        Self::Initialized
    }
}

/// Bridge asset parameters.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)]
pub enum AssetConfig<AssetId> {
    Thischain {
        id: AssetId,
    },
    Sidechain {
        id: AssetId,
        sidechain_id: sp_core::H160,
        owned: bool,
        precision: BalancePrecision,
    },
}

impl<AssetId> AssetConfig<AssetId> {
    pub fn sidechain(
        id: AssetId,
        sidechain_id: sp_core::H160,
        precision: BalancePrecision,
    ) -> Self {
        Self::Sidechain {
            id,
            sidechain_id,
            owned: false,
            precision,
        }
    }

    pub fn asset_id(&self) -> &AssetId {
        match self {
            AssetConfig::Thischain { id, .. } => id,
            AssetConfig::Sidechain { id, .. } => id,
        }
    }

    pub fn kind(&self) -> AssetKind {
        match self {
            AssetConfig::Thischain { .. } => AssetKind::Thischain,
            AssetConfig::Sidechain { owned: false, .. } => AssetKind::Sidechain,
            AssetConfig::Sidechain { owned: true, .. } => AssetKind::SidechainOwned,
        }
    }
}

/// Bridge function signature version
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Encode, Decode, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)]
pub enum BridgeSignatureVersion {
    V1,
    // Fix signature overlapping for addPeer, removePeer and prepareForMigration
    // Add bridge contract address to the signature
    V2,
    // Use abi.encode instead of abi.encodePacked and add prefix for each function
    V3,
}

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use crate::offchain::SignatureParams;
    use crate::util::get_bridge_account;
    use bridge_types::traits::{BridgeAssetLockChecker, MessageStatusNotifier};
    use codec::Codec;
    use common::prelude::constants::EXTRINSIC_FIXED_WEIGHT;
    use common::weights::{err_pays_no, pays_no, pays_no_with_maybe_weight};
    use frame_support::log;
    use frame_support::pallet_prelude::*;
    use frame_support::traits::{GetCallMetadata, StorageVersion};
    use frame_support::transactional;
    use frame_support::weights::WeightToFeePolynomial;
    use frame_system::pallet_prelude::*;
    use frame_system::RawOrigin;

    // TODO: #395 use AssetInfoProvider instead of assets pallet
    #[pallet::config]
    pub trait Config:
        frame_system::Config
        + CreateSignedTransaction<Call<Self>>
        + CreateSignedTransaction<bridge_multisig::Call<Self>>
        + assets::Config
        + bridge_multisig::Config<RuntimeCall = <Self as Config>::RuntimeCall>
        + fmt::Debug
    {
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
        /// The identifier type for an offchain worker.
        type PeerId: AppCrypto<Self::Public, Self::Signature>;
        /// The overarching dispatch call type.
        type RuntimeCall: From<Call<Self>>
            + From<bridge_multisig::Call<Self>>
            + Codec
            + Clone
            + GetCallMetadata;
        /// Sidechain network ID.
        type NetworkId: Parameter
            + Member
            + AtLeast32Bit
            + Copy
            + MaybeSerializeDeserialize
            + Ord
            + Default
            + Debug;
        type GetEthNetworkId: Get<Self::NetworkId>;
        /// Weight information for extrinsics in this pallet.
        type WeightInfo: WeightInfo;
        #[cfg(test)]
        type Mock: tests::mock::Mock;

        type MessageStatusNotifier: MessageStatusNotifier<Self::AssetId, Self::AccountId, Balance>;

        type BridgeAssetLockChecker: BridgeAssetLockChecker<Self::AssetId, Balance>;

        type WeightToFee: WeightToFeePolynomial<Balance = Balance>;
    }

    /// The current storage version.
    const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    #[pallet::storage_version(STORAGE_VERSION)]
    #[pallet::without_storage_info]
    pub struct Pallet<T>(PhantomData<T>);

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
    where
        T: CreateSignedTransaction<<T as Config>::RuntimeCall>,
    {
        /// Main off-chain worker procedure.
        ///
        /// Note: only one worker is expected to be used.
        fn offchain_worker(block_number: T::BlockNumber) {
            debug!("Entering off-chain workers {:?}", block_number);
            let value_ref = StorageValueRef::persistent(STORAGE_PEER_SECRET_KEY);
            if value_ref.get::<Vec<u8>>().ok().flatten().is_none() {
                debug!("Peer secret key not found. Skipping off-chain procedure.");
                return;
            }

            let mut lock = StorageLock::<'_, Time>::with_deadline(
                b"eth-bridge-ocw::lock",
                sp_core::offchain::Duration::from_millis(100000),
            );
            let guard = lock.try_lock();
            if let Ok(_guard) = guard {
                Self::offchain();
            } else {
                log::debug!("Skip worker {:?}", block_number);
            }
        }
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Register a new bridge.
        ///
        /// Parameters:
        /// - `bridge_contract_address` - address of smart-contract deployed on a corresponding
        /// network.
        /// - `initial_peers` - a set of initial network peers.
        #[transactional]
        #[pallet::call_index(0)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn register_bridge(
            origin: OriginFor<T>,
            bridge_contract_address: EthAddress,
            initial_peers: Vec<T::AccountId>,
            signature_version: BridgeSignatureVersion,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            let net_id = NextNetworkId::<T>::get();
            ensure!(!initial_peers.is_empty(), Error::<T>::NotEnoughPeers);
            let peers_account_id = bridge_multisig::Pallet::<T>::register_multisig_inner(
                initial_peers[0].clone(),
                initial_peers.clone(),
            )?;
            BridgeContractAddress::<T>::insert(net_id, bridge_contract_address);
            BridgeAccount::<T>::insert(net_id, peers_account_id);
            BridgeStatuses::<T>::insert(net_id, BridgeStatus::Initialized);
            BridgeSignatureVersions::<T>::insert(net_id, signature_version);
            Peers::<T>::insert(net_id, initial_peers.into_iter().collect::<BTreeSet<_>>());
            NextNetworkId::<T>::set(net_id + T::NetworkId::one());
            Ok(().into())
        }

        /// Add a Thischain asset to the bridge whitelist.
        ///
        /// Can only be called by root.
        ///
        /// Parameters:
        /// - `asset_id` - Thischain asset identifier.
        /// - `network_id` - network identifier to which the asset should be added.
        #[transactional]
        #[pallet::call_index(1)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn add_asset(
            origin: OriginFor<T>,
            asset_id: AssetIdOf<T>,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            let from = Self::authority_account().ok_or(Error::<T>::AuthorityAccountNotSet)?;
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::AddAsset(
                OutgoingAddAsset {
                    author: from.clone(),
                    asset_id,
                    nonce,
                    network_id,
                    timepoint,
                },
            )))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            Ok(().into())
        }

        /// Add a Sidechain token to the bridge whitelist.
        ///
        /// Parameters:
        /// - `token_address` - token contract address.
        /// - `symbol` - token symbol (ticker).
        /// - `name` - token name.
        /// - `decimals` -  token precision.
        /// - `network_id` - network identifier.
        #[transactional]
        #[pallet::call_index(2)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn add_sidechain_token(
            origin: OriginFor<T>,
            token_address: EthAddress,
            symbol: String,
            name: String,
            decimals: u8,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called add_sidechain_token");
            ensure_root(origin)?;
            let from = Self::authority_account().ok_or(Error::<T>::AuthorityAccountNotSet)?;
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::AddToken(
                OutgoingAddToken {
                    author: from.clone(),
                    token_address,
                    symbol,
                    name,
                    decimals,
                    nonce,
                    network_id,
                    timepoint,
                },
            )))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            Ok(().into())
        }

        /// Transfer some amount of the given asset to Sidechain address.
        ///
        /// Note: if the asset kind is `Sidechain`, the amount should fit in the asset's precision
        /// on sidechain (`SidechainAssetPrecision`) without extra digits. For example, assume
        /// some ERC-20 (`T`) token has `decimals=6`, and the corresponding asset on substrate has
        /// `7`. Alice's balance on thischain is `0.1000009`. If Alice would want to transfer all
        /// the amount, she will get an error `NonZeroDust`, because of the `9` at the end, so, the
        /// correct amount would be `0.100000` (only 6 digits after the decimal point).
        ///
        /// Parameters:
        /// - `asset_id` - thischain asset id.
        /// - `to` - sidechain account id.
        /// - `amount` - amount of the asset.
        /// - `network_id` - network identifier.
        #[transactional]
        #[pallet::call_index(3)]
        #[pallet::weight(<T as Config>::WeightInfo::transfer_to_sidechain())]
        pub fn transfer_to_sidechain(
            origin: OriginFor<T>,
            asset_id: AssetIdOf<T>,
            to: EthAddress,
            amount: Balance,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called transfer_to_sidechain");
            let from = ensure_signed(origin)?;
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::Transfer(
                OutgoingTransfer {
                    from: from.clone(),
                    to,
                    asset_id,
                    amount,
                    nonce,
                    network_id,
                    timepoint,
                },
            )))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            Ok(().into())
        }

        /// Load incoming request from Sidechain by the given transaction hash.
        ///
        /// Parameters:
        /// - `eth_tx_hash` - transaction hash on Sidechain.
        /// - `kind` - incoming request type.
        /// - `network_id` - network identifier.

        #[transactional]
        #[pallet::call_index(4)]
        #[pallet::weight(<T as Config>::WeightInfo::request_from_sidechain())]
        pub fn request_from_sidechain(
            origin: OriginFor<T>,
            eth_tx_hash: H256,
            kind: IncomingRequestKind,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called request_from_sidechain");
            let from = ensure_signed(origin)?;
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            match kind {
                IncomingRequestKind::Transaction(kind) => {
                    Self::add_request(&OffchainRequest::LoadIncoming(
                        LoadIncomingRequest::Transaction(LoadIncomingTransactionRequest::new(
                            from,
                            eth_tx_hash,
                            timepoint,
                            kind,
                            network_id,
                        )),
                    ))?;
                    Ok(().into())
                }
                IncomingRequestKind::Meta(kind) => {
                    if kind == IncomingMetaRequestKind::CancelOutgoingRequest {
                        fail!(Error::<T>::Unavailable);
                    }
                    let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
                    Self::add_request(&OffchainRequest::load_incoming_meta(
                        LoadIncomingMetaRequest::new(
                            from,
                            eth_tx_hash,
                            timepoint,
                            kind,
                            network_id,
                        ),
                    ))?;
                    Ok(().into())
                }
            }
        }

        /// Finalize incoming request (see `Pallet::finalize_incoming_request_inner`).
        ///
        /// Can be only called from a bridge account.
        ///
        /// Parameters:
        /// - `request` - an incoming request.
        /// - `network_id` - network identifier.
        #[pallet::call_index(5)]
        #[pallet::weight(<T as Config>::WeightInfo::finalize_incoming_request())]
        pub fn finalize_incoming_request(
            origin: OriginFor<T>,
            hash: H256,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called finalize_incoming_request");
            let _ = Self::ensure_bridge_account(origin, network_id)?;
            let request = Requests::<T>::get(network_id, &hash)
                .ok_or_else(|| err_pays_no(Error::<T>::UnknownRequest))?;
            let (request, hash) = request
                .as_incoming()
                .ok_or_else(|| err_pays_no(Error::<T>::ExpectedIncomingRequest))?;
            pays_no(Self::finalize_incoming_request_inner(
                request, hash, network_id,
            ))
        }

        /// Add a new peer to the bridge peers set.
        ///
        /// Parameters:
        /// - `account_id` - account id on thischain.
        /// - `address` - account id on sidechain.
        /// - `network_id` - network identifier.

        #[transactional]
        #[pallet::call_index(6)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn add_peer(
            origin: OriginFor<T>,
            account_id: T::AccountId,
            address: EthAddress,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called change_peers_out");
            ensure_root(origin)?;
            let from = Self::authority_account().ok_or(Error::<T>::AuthorityAccountNotSet)?;
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::AddPeer(
                OutgoingAddPeer {
                    author: from.clone(),
                    peer_account_id: account_id.clone(),
                    peer_address: address,
                    nonce,
                    network_id,
                    timepoint,
                },
            )))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            if network_id == T::GetEthNetworkId::get() {
                let nonce = frame_system::Pallet::<T>::account_nonce(&from);
                Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::AddPeerCompat(
                    OutgoingAddPeerCompat {
                        author: from.clone(),
                        peer_account_id: account_id,
                        peer_address: address,
                        nonce,
                        network_id,
                        timepoint,
                    },
                )))?;
                frame_system::Pallet::<T>::inc_account_nonce(&from);
            }
            Ok(().into())
        }

        /// Remove peer from the the bridge peers set.
        ///
        /// Parameters:
        /// - `account_id` - account id on thischain.
        /// - `network_id` - network identifier.

        #[transactional]
        #[pallet::call_index(7)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn remove_peer(
            origin: OriginFor<T>,
            account_id: T::AccountId,
            peer_address: Option<EthAddress>,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called change_peers_out");
            ensure_root(origin)?;
            let from = Self::authority_account().ok_or(Error::<T>::AuthorityAccountNotSet)?;
            let peer_address = if PeerAddress::<T>::contains_key(network_id, &account_id) {
                if let Some(peer_address) = peer_address {
                    ensure!(
                        peer_address == Self::peer_address(network_id, &account_id),
                        Error::<T>::UnknownPeerId
                    );
                    peer_address
                } else {
                    Self::peer_address(network_id, &account_id)
                }
            } else {
                peer_address.ok_or(Error::<T>::UnknownPeerId)?
            };
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            let compat_hash = if network_id == T::GetEthNetworkId::get() {
                let nonce = frame_system::Pallet::<T>::account_nonce(&from);
                let request = OffchainRequest::outgoing(OutgoingRequest::RemovePeerCompat(
                    OutgoingRemovePeerCompat {
                        author: from.clone(),
                        peer_account_id: account_id.clone(),
                        peer_address,
                        nonce,
                        network_id,
                        timepoint,
                    },
                ));
                Self::add_request(&request)?;
                frame_system::Pallet::<T>::inc_account_nonce(&from);
                Some(request.hash())
            } else {
                None
            };
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::RemovePeer(
                OutgoingRemovePeer {
                    author: from.clone(),
                    peer_account_id: account_id,
                    peer_address,
                    nonce,
                    network_id,
                    timepoint,
                    compat_hash,
                },
            )))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            Ok(().into())
        }

        /// Prepare the given bridge for migration.
        ///
        /// Can only be called by an authority.
        ///
        /// Parameters:
        /// - `network_id` - bridge network identifier.

        #[transactional]
        #[pallet::call_index(8)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn prepare_for_migration(
            origin: OriginFor<T>,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called prepare_for_migration");
            ensure_root(origin)?;
            if BridgeSignatureVersions::<T>::get(network_id) == BridgeSignatureVersion::V1
                && Peers::<T>::get(network_id).len() < MINIMUM_PEERS_FOR_MIGRATION
            {
                return Err(Error::<T>::UnsafeMigration.into());
            }
            let from = Self::authority_account().ok_or(Error::<T>::AuthorityAccountNotSet)?;
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            Self::add_request(&OffchainRequest::outgoing(
                OutgoingRequest::PrepareForMigration(OutgoingPrepareForMigration {
                    author: from.clone(),
                    nonce,
                    network_id,
                    timepoint,
                }),
            ))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            Ok(().into())
        }

        /// Migrate the given bridge to a new smart-contract.
        ///
        /// Can only be called by an authority.
        ///
        /// Parameters:
        /// - `new_contract_address` - new sidechain ocntract address.
        /// - `erc20_native_tokens` - migrated assets ids.
        /// - `network_id` - bridge network identifier.

        #[transactional]
        #[pallet::call_index(9)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn migrate(
            origin: OriginFor<T>,
            new_contract_address: EthAddress,
            erc20_native_tokens: Vec<EthAddress>,
            network_id: BridgeNetworkId<T>,
            new_signature_version: BridgeSignatureVersion,
        ) -> DispatchResultWithPostInfo {
            debug!("called prepare_for_migration");
            ensure_root(origin)?;
            let from = Self::authority_account().ok_or(Error::<T>::AuthorityAccountNotSet)?;
            let nonce = frame_system::Pallet::<T>::account_nonce(&from);
            let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
            Self::add_request(&OffchainRequest::outgoing(OutgoingRequest::Migrate(
                OutgoingMigrate {
                    author: from.clone(),
                    new_contract_address,
                    erc20_native_tokens,
                    nonce,
                    network_id,
                    timepoint,
                    new_signature_version,
                },
            )))?;
            frame_system::Pallet::<T>::inc_account_nonce(&from);
            Ok(().into())
        }

        /// Register the given incoming request and add it to the queue.
        ///
        /// Calls `validate` and `prepare` on the request, adds it to the queue and maps it with the
        /// corresponding load-incoming-request and removes the load-request from the queue.
        ///
        /// Can only be called by a bridge account.
        #[pallet::call_index(10)]
        #[pallet::weight(<T as Config>::WeightInfo::register_incoming_request())]
        pub fn register_incoming_request(
            origin: OriginFor<T>,
            incoming_request: IncomingRequest<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called register_incoming_request");
            let net_id = incoming_request.network_id();
            let _ = Self::ensure_bridge_account(origin, net_id)?;
            pays_no(Self::register_incoming_request_inner(
                &OffchainRequest::incoming(incoming_request),
                net_id,
            ))
        }

        /// Import the given incoming request.
        ///
        /// Register's the load request, then registers and finalizes the incoming request if it
        /// succeeded, otherwise aborts the load request.
        ///
        /// Can only be called by a bridge account.
        #[pallet::call_index(11)]
        #[pallet::weight({
            <T as Config>::WeightInfo::register_incoming_request().saturating_add(
                if incoming_request_result.is_ok() {
                    <T as Config>::WeightInfo::finalize_incoming_request()
                } else {
                    <T as Config>::WeightInfo::abort_request()
                }
            )
        })]
        pub fn import_incoming_request(
            origin: OriginFor<T>,
            load_incoming_request: LoadIncomingRequest<T>,
            incoming_request_result: Result<IncomingRequest<T>, DispatchError>,
        ) -> DispatchResultWithPostInfo {
            debug!("called import_incoming_request");
            let net_id = load_incoming_request.network_id();
            let _ = Self::ensure_bridge_account(origin, net_id)?;
            pays_no(Self::inner_import_incoming_request(
                net_id,
                load_incoming_request,
                incoming_request_result,
            ))
        }

        /// Approve the given outgoing request. The function is used by bridge peers.
        ///
        /// Verifies the peer signature of the given request and adds it to `RequestApprovals`.
        /// Once quorum is collected, the request gets finalized and removed from request queue.
        #[pallet::call_index(12)]
        #[pallet::weight(<T as Config>::WeightInfo::approve_request())]
        pub fn approve_request(
            origin: OriginFor<T>,
            ocw_public: ecdsa::Public,
            hash: H256,
            signature_params: SignatureParams,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!("called approve_request");
            let author = ensure_signed(origin)?;
            let net_id = network_id;
            Self::ensure_peer(&author, net_id)?;
            pays_no_with_maybe_weight(Self::inner_approve_request(
                ocw_public,
                hash,
                signature_params,
                author,
                net_id,
            ))
        }

        /// Cancels a registered request.
        ///
        /// Loads request by the given `hash`, cancels it, changes its status to `Failed` and
        /// removes it from the request queues.
        ///
        /// Can only be called from a bridge account.
        #[pallet::call_index(13)]
        #[pallet::weight(<T as Config>::WeightInfo::abort_request())]
        pub fn abort_request(
            origin: OriginFor<T>,
            hash: H256,
            error: DispatchError,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            debug!(
                "called abort_request. Hash: {:?}, reason: {:?}",
                hash, error
            );
            let _ = Self::ensure_bridge_account(origin, network_id)?;
            let request = Requests::<T>::get(network_id, hash)
                .ok_or_else(|| err_pays_no(Error::<T>::UnknownRequest))?;
            pays_no(Self::inner_abort_request(&request, hash, error, network_id))
        }

        /// Add the given peer to the peers set without additional checks.
        ///
        /// Can only be called by a root account.

        #[transactional]
        #[pallet::call_index(14)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn force_add_peer(
            origin: OriginFor<T>,
            who: T::AccountId,
            address: EthAddress,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            let _ = ensure_root(origin)?;
            if !Self::is_peer(&who, network_id) {
                bridge_multisig::Pallet::<T>::add_signatory(
                    RawOrigin::Signed(get_bridge_account::<T>(network_id)).into(),
                    who.clone(),
                )
                .map_err(|e| e.error)?;
                PeerAddress::<T>::insert(network_id, &who, address);
                PeerAccountId::<T>::insert(network_id, &address, who.clone());
                <Peers<T>>::mutate(network_id, |l| l.insert(who));
            }
            Ok(().into())
        }

        /// Remove asset
        ///
        /// Can only be called by root.
        #[pallet::call_index(15)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn remove_sidechain_asset(
            origin: OriginFor<T>,
            asset_id: AssetIdOf<T>,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            log::debug!("called remove_sidechain_asset. asset_id: {:?}", asset_id);
            ensure_root(origin)?;
            assets::Pallet::<T>::ensure_asset_exists(&asset_id)?;
            let token_address = RegisteredSidechainToken::<T>::get(network_id, &asset_id)
                .ok_or(Error::<T>::UnknownAssetId)?;
            RegisteredAsset::<T>::remove(network_id, &asset_id);
            RegisteredSidechainAsset::<T>::remove(network_id, &token_address);
            RegisteredSidechainToken::<T>::remove(network_id, &asset_id);
            SidechainAssetPrecision::<T>::remove(network_id, &asset_id);
            Ok(().into())
        }

        /// Register existing asset
        ///
        /// Can only be called by root.
        #[pallet::call_index(16)]
        // eth-bridge pallet will be deprecated soon, so we set const weight here
        #[pallet::weight(EXTRINSIC_FIXED_WEIGHT)]
        pub fn register_existing_sidechain_asset(
            origin: OriginFor<T>,
            asset_id: AssetIdOf<T>,
            token_address: EthAddress,
            network_id: BridgeNetworkId<T>,
        ) -> DispatchResultWithPostInfo {
            log::debug!(
                "called register_existing_sidechain_asset. asset_id: {:?}",
                asset_id
            );
            ensure_root(origin)?;
            assets::Pallet::<T>::ensure_asset_exists(&asset_id)?;
            ensure!(
                !RegisteredAsset::<T>::contains_key(network_id, &asset_id),
                Error::<T>::TokenIsAlreadyAdded
            );

            // TODO: #395 use AssetInfoProvider instead of assets pallet
            let (_, _, precision, ..) = assets::Pallet::<T>::get_asset_info(&asset_id);
            RegisteredAsset::<T>::insert(network_id, &asset_id, AssetKind::Sidechain);
            RegisteredSidechainAsset::<T>::insert(network_id, &token_address, asset_id);
            RegisteredSidechainToken::<T>::insert(network_id, &asset_id, token_address);
            SidechainAssetPrecision::<T>::insert(network_id, &asset_id, precision);
            Ok(().into())
        }
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// New request has been registered. [Request Hash]
        RequestRegistered(H256),
        /// The request's approvals have been collected. [Encoded Outgoing Request, Signatures]
        ApprovalsCollected(H256),
        /// The request finalization has been failed. [Request Hash]
        RequestFinalizationFailed(H256),
        /// The incoming request finalization has been failed. [Request Hash]
        IncomingRequestFinalizationFailed(H256),
        /// The incoming request has been finalized. [Request Hash]
        IncomingRequestFinalized(H256),
        /// The request was aborted and cancelled. [Request Hash]
        RequestAborted(H256),
        /// The request wasn't finalized nor cancelled. [Request Hash]
        CancellationFailed(H256),
        /// The request registration has been failed. [Request Hash, Error]
        RegisterRequestFailed(H256, DispatchError),
    }

    #[cfg_attr(test, derive(PartialEq, Eq))]
    #[pallet::error]
    pub enum Error<T> {
        /// Error fetching HTTP.
        HttpFetchingError,
        /// Account not found.
        AccountNotFound,
        /// Forbidden.
        Forbidden,
        /// Request is already registered.
        RequestIsAlreadyRegistered,
        /// Failed to load sidechain transaction.
        FailedToLoadTransaction,
        /// Failed to load token precision.
        FailedToLoadPrecision,
        /// Unknown method ID.
        UnknownMethodId,
        /// Invalid contract function input.
        InvalidFunctionInput,
        /// Invalid peer signature.
        InvalidSignature,
        /// Invalid uint value.
        InvalidUint,
        /// Invalid amount value.
        InvalidAmount,
        /// Invalid balance value.
        InvalidBalance,
        /// Invalid string value.
        InvalidString,
        /// Invalid byte value.
        InvalidByte,
        /// Invalid address value.
        InvalidAddress,
        /// Invalid asset id value.
        InvalidAssetId,
        /// Invalid account id value.
        InvalidAccountId,
        /// Invalid bool value.
        InvalidBool,
        /// Invalid h256 value.
        InvalidH256,
        /// Invalid array value.
        InvalidArray,
        /// Unknown contract event.
        UnknownEvent,
        /// Unknown token address.
        UnknownTokenAddress,
        /// No local account for signing available.
        NoLocalAccountForSigning,
        /// Unsupported asset id.
        UnsupportedAssetId,
        /// Failed to sign message.
        FailedToSignMessage,
        /// Failed to send signed message.
        FailedToSendSignedTransaction,
        /// Token is not owned by the author.
        TokenIsNotOwnedByTheAuthor,
        /// Token is already added.
        TokenIsAlreadyAdded,
        /// Duplicated request.
        DuplicatedRequest,
        /// Token is unsupported.
        UnsupportedToken,
        /// Unknown peer address.
        UnknownPeerAddress,
        /// Ethereum ABI encoding error.
        EthAbiEncodingError,
        /// Ethereum ABI decoding error.
        EthAbiDecodingError,
        /// Ethereum transaction is failed.
        EthTransactionIsFailed,
        /// Ethereum transaction is succeeded.
        EthTransactionIsSucceeded,
        /// Ethereum transaction is pending.
        EthTransactionIsPending,
        /// Ethereum log was removed.
        EthLogWasRemoved,
        /// No pending peer.
        NoPendingPeer,
        /// Wrong pending peer.
        WrongPendingPeer,
        /// Too many pending peers.
        TooManyPendingPeers,
        /// Failed to get an asset by id.
        FailedToGetAssetById,
        /// Can't add more peers.
        CantAddMorePeers,
        /// Can't remove more peers.
        CantRemoveMorePeers,
        /// Peer is already added.
        PeerIsAlreadyAdded,
        /// Unknown peer id.
        UnknownPeerId,
        /// Can't reserve funds.
        CantReserveFunds,
        /// Funds are already claimed.
        AlreadyClaimed,
        /// Failed to load substrate block header.
        FailedToLoadBlockHeader,
        /// Failed to load substrate finalized head.
        FailedToLoadFinalizedHead,
        /// Unknown contract address.
        UnknownContractAddress,
        /// Invalid contract input.
        InvalidContractInput,
        /// Request is not owned by the author.
        RequestIsNotOwnedByTheAuthor,
        /// Failed to parse transaction hash in a call.
        FailedToParseTxHashInCall,
        /// Request is not ready.
        RequestIsNotReady,
        /// Unknown request.
        UnknownRequest,
        /// Request is not finalized on Sidechain.
        RequestNotFinalizedOnSidechain,
        /// Unknown network.
        UnknownNetwork,
        /// Contract is in migration stage.
        ContractIsInMigrationStage,
        /// Contract is not on migration stage.
        ContractIsNotInMigrationStage,
        /// Contract is already in migration stage.
        ContractIsAlreadyInMigrationStage,
        /// Functionality is unavailable.
        Unavailable,
        /// Failed to unreserve asset.
        FailedToUnreserve,
        /// The sidechain asset is alredy registered.
        SidechainAssetIsAlreadyRegistered,
        /// Expected an outgoing request.
        ExpectedOutgoingRequest,
        /// Expected an incoming request.
        ExpectedIncomingRequest,
        /// Unknown asset id.
        UnknownAssetId,
        /// Failed to serialize JSON.
        JsonSerializationError,
        /// Failed to deserialize JSON.
        JsonDeserializationError,
        /// Failed to load sidechain node parameters.
        FailedToLoadSidechainNodeParams,
        /// Failed to load current sidechain height.
        FailedToLoadCurrentSidechainHeight,
        /// Failed to query sidechain 'used' variable.
        FailedToLoadIsUsed,
        /// Sidechain transaction might have failed due to gas limit.
        TransactionMightHaveFailedDueToGasLimit,
        /// A transfer of XOR was expected.
        ExpectedXORTransfer,
        /// Unable to pay transfer fees.
        UnableToPayFees,
        /// The request was purposely cancelled.
        Cancelled,
        /// Unsupported asset precision.
        UnsupportedAssetPrecision,
        /// Non-zero dust.
        NonZeroDust,
        /// Increment account reference error.
        IncRefError,
        /// Unknown error.
        Other,
        /// Expected pending request.
        ExpectedPendingRequest,
        /// Expected Ethereum network.
        ExpectedEthNetwork,
        /// Request was removed and refunded.
        RemovedAndRefunded,
        /// Authority account is not set.
        AuthorityAccountNotSet,
        /// Not enough peers provided, need at least 1
        NotEnoughPeers,
        /// Failed to read value from offchain storage.
        ReadStorageError,
        /// Bridge needs to have at least 3 peers for migration. Add new peer
        UnsafeMigration,
    }

    impl<T: Config> Error<T> {
        pub fn should_retry(&self) -> bool {
            match self {
                Self::HttpFetchingError
                | Self::NoLocalAccountForSigning
                | Self::FailedToSignMessage
                | Self::JsonDeserializationError => true,
                _ => false,
            }
        }

        pub fn should_abort(&self) -> bool {
            match self {
                Self::FailedToSendSignedTransaction => false,
                _ => true,
            }
        }
    }

    /// Registered requests queue handled by off-chain workers.
    #[pallet::storage]
    #[pallet::getter(fn requests_queue)]
    pub type RequestsQueue<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, Vec<H256>, ValueQuery>;

    /// Registered requests.
    #[pallet::storage]
    #[pallet::getter(fn request)]
    pub type Requests<T: Config> =
        StorageDoubleMap<_, Twox64Concat, BridgeNetworkId<T>, Identity, H256, OffchainRequest<T>>;

    /// Used to identify an incoming request by the corresponding load request.
    #[pallet::storage]
    #[pallet::getter(fn load_to_incoming_request_hash)]
    pub type LoadToIncomingRequestHash<T: Config> =
        StorageDoubleMap<_, Twox64Concat, BridgeNetworkId<T>, Identity, H256, H256, ValueQuery>;

    /// Requests statuses.
    #[pallet::storage]
    #[pallet::getter(fn request_status)]
    pub type RequestStatuses<T: Config> =
        StorageDoubleMap<_, Twox64Concat, BridgeNetworkId<T>, Identity, H256, RequestStatus>;

    /// Requests submission height map (on substrate).
    #[pallet::storage]
    #[pallet::getter(fn request_submission_height)]
    pub type RequestSubmissionHeight<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Identity,
        H256,
        T::BlockNumber,
        ValueQuery,
    >;

    /// Outgoing requests approvals.
    #[pallet::storage]
    #[pallet::getter(fn approvals)]
    pub(super) type RequestApprovals<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Identity,
        H256,
        BTreeSet<SignatureParams>,
        ValueQuery,
    >;

    /// Requests made by an account.
    #[pallet::storage]
    #[pallet::getter(fn account_requests)]
    pub(super) type AccountRequests<T: Config> =
        StorageMap<_, Blake2_128Concat, T::AccountId, Vec<(BridgeNetworkId<T>, H256)>, ValueQuery>;

    /// Registered asset kind.
    #[pallet::storage]
    #[pallet::getter(fn registered_asset)]
    pub(super) type RegisteredAsset<T: Config> =
        StorageDoubleMap<_, Twox64Concat, BridgeNetworkId<T>, Identity, T::AssetId, AssetKind>;

    /// Precision (decimals) of a registered sidechain asset. Should be the same as in the ERC-20
    /// contract.
    #[pallet::storage]
    #[pallet::getter(fn sidechain_asset_precision)]
    pub(super) type SidechainAssetPrecision<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Identity,
        T::AssetId,
        BalancePrecision,
        ValueQuery,
    >;

    /// Registered token `AssetId` on Thischain.
    #[pallet::storage]
    #[pallet::getter(fn registered_sidechain_asset)]
    pub(super) type RegisteredSidechainAsset<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Blake2_128Concat,
        EthAddress,
        T::AssetId,
    >;

    /// Registered asset address on Sidechain.
    #[pallet::storage]
    #[pallet::getter(fn registered_sidechain_token)]
    pub(super) type RegisteredSidechainToken<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Blake2_128Concat,
        T::AssetId,
        EthAddress,
    >;

    /// Network peers set.
    #[pallet::storage]
    #[pallet::getter(fn peers)]
    pub(super) type Peers<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, BTreeSet<T::AccountId>, ValueQuery>;

    /// Network pending (being added/removed) peer.
    #[pallet::storage]
    #[pallet::getter(fn pending_peer)]
    pub(super) type PendingPeer<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, T::AccountId>;

    /// Used for compatibility with XOR and VAL contracts.
    #[pallet::storage]
    pub(super) type PendingEthPeersSync<T: Config> = StorageValue<_, EthPeersSync, ValueQuery>;

    /// Peer account ID on Thischain.
    #[pallet::storage]
    #[pallet::getter(fn peer_account_id)]
    pub(super) type PeerAccountId<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Blake2_128Concat,
        EthAddress,
        T::AccountId,
        OptionQuery,
    >;

    /// Peer address on Sidechain.
    #[pallet::storage]
    #[pallet::getter(fn peer_address)]
    pub(super) type PeerAddress<T: Config> = StorageDoubleMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        Blake2_128Concat,
        T::AccountId,
        EthAddress,
        ValueQuery,
    >;

    /// Multi-signature bridge peers' account. `None` if there is no account and network with the given ID.
    #[pallet::storage]
    #[pallet::getter(fn bridge_account)]
    pub type BridgeAccount<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, T::AccountId>;

    /// Thischain authority account.
    #[pallet::storage]
    #[pallet::getter(fn authority_account)]
    pub(super) type AuthorityAccount<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;

    /// Bridge status.
    #[pallet::storage]
    #[pallet::getter(fn bridge_contract_status)]
    pub(super) type BridgeStatuses<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, BridgeStatus>;

    /// Smart-contract address on Sidechain.
    #[pallet::storage]
    #[pallet::getter(fn bridge_contract_address)]
    pub(super) type BridgeContractAddress<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, EthAddress, ValueQuery>;

    /// Sora XOR master contract address.
    #[pallet::storage]
    #[pallet::getter(fn xor_master_contract_address)]
    pub(super) type XorMasterContractAddress<T: Config> = StorageValue<_, EthAddress, ValueQuery>;

    /// Sora VAL master contract address.
    #[pallet::storage]
    #[pallet::getter(fn val_master_contract_address)]
    pub(super) type ValMasterContractAddress<T: Config> = StorageValue<_, EthAddress, ValueQuery>;

    /// Next Network ID counter.
    #[pallet::storage]
    pub(super) type NextNetworkId<T: Config> = StorageValue<_, BridgeNetworkId<T>, ValueQuery>;

    /// Requests migrating from version '0.1.0' to '0.2.0'. These requests should be removed from
    /// `RequestsQueue` before migration procedure started.
    #[pallet::storage]
    pub(super) type MigratingRequests<T: Config> = StorageValue<_, Vec<H256>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn bridge_signature_version)]
    pub(super) type BridgeSignatureVersions<T: Config> = StorageMap<
        _,
        Twox64Concat,
        BridgeNetworkId<T>,
        BridgeSignatureVersion,
        ValueQuery,
        DefaultForBridgeSignatureVersion,
    >;

    #[pallet::type_value]
    pub fn DefaultForBridgeSignatureVersion() -> BridgeSignatureVersion {
        BridgeSignatureVersion::V3
    }

    #[pallet::storage]
    #[pallet::getter(fn pending_bridge_signature_version)]
    pub(super) type PendingBridgeSignatureVersions<T: Config> =
        StorageMap<_, Twox64Concat, BridgeNetworkId<T>, BridgeSignatureVersion>;

    #[pallet::genesis_config]
    pub struct GenesisConfig<T: Config> {
        pub authority_account: Option<T::AccountId>,
        pub xor_master_contract_address: EthAddress,
        pub val_master_contract_address: EthAddress,
        pub networks: Vec<NetworkConfig<T>>,
    }

    #[cfg(feature = "std")]
    impl<T: Config> Default for GenesisConfig<T> {
        fn default() -> Self {
            Self {
                authority_account: Default::default(),
                xor_master_contract_address: Default::default(),
                val_master_contract_address: Default::default(),
                networks: Default::default(),
            }
        }
    }

    #[pallet::genesis_build]
    impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
        fn build(&self) {
            AuthorityAccount::<T>::put(&self.authority_account.as_ref().unwrap());
            XorMasterContractAddress::<T>::put(&self.xor_master_contract_address);
            ValMasterContractAddress::<T>::put(&self.val_master_contract_address);
            for network in &self.networks {
                let net_id = NextNetworkId::<T>::get();
                let peers_account_id = &network.bridge_account_id;
                BridgeContractAddress::<T>::insert(net_id, network.bridge_contract_address);
                frame_system::Pallet::<T>::inc_consumers(&peers_account_id).unwrap();
                BridgeAccount::<T>::insert(net_id, peers_account_id.clone());
                BridgeStatuses::<T>::insert(net_id, BridgeStatus::Initialized);
                Peers::<T>::insert(net_id, network.initial_peers.clone());
                for asset_config in &network.assets {
                    let kind = asset_config.kind();
                    let asset_id = asset_config.asset_id();
                    if let AssetConfig::Sidechain {
                        sidechain_id,
                        precision,
                        ..
                    } = &asset_config
                    {
                        let token_address = EthAddress::from(sidechain_id.0);
                        RegisteredSidechainAsset::<T>::insert(net_id, token_address, *asset_id);
                        RegisteredSidechainToken::<T>::insert(net_id, asset_id, token_address);
                        SidechainAssetPrecision::<T>::insert(net_id, asset_id, precision);
                    }
                    RegisteredAsset::<T>::insert(net_id, asset_id, kind);
                }
                // TODO: consider to change to Limited.
                let scope = Scope::Unlimited;
                let permission_ids = [MINT, BURN];
                for permission_id in &permission_ids {
                    permissions::Pallet::<T>::assign_permission(
                        peers_account_id.clone(),
                        &peers_account_id,
                        *permission_id,
                        scope,
                    )
                    .expect("failed to assign permissions for a bridge account");
                }
                for (asset_id, balance) in &network.reserves {
                    assets::Pallet::<T>::mint_to(
                        asset_id,
                        &peers_account_id,
                        &peers_account_id,
                        *balance,
                    )
                    .unwrap();
                }
                NextNetworkId::<T>::set(net_id + T::NetworkId::one());
            }
        }
    }
}

impl<T: Config> Pallet<T> {
    /// Registers the given off-chain request.
    ///
    /// Conditions for registering:
    /// 1. Network ID should be valid.
    /// 2. If the bridge is migrating and request is outgoing, it should be allowed during migration.
    /// 3. Request status should be empty or `Failed` (for resubmission).
    /// 4. There is no registered request with the same hash.
    /// 5. The request's `validate` and `prepare` should pass.
    fn add_request(request: &OffchainRequest<T>) -> Result<(), DispatchError> {
        let net_id = request.network_id();
        let bridge_status = BridgeStatuses::<T>::get(net_id).ok_or(Error::<T>::UnknownNetwork)?;
        if request.is_incoming() {
            Self::register_incoming_request_inner(request, net_id)?;
            return Ok(());
        }
        if let Some((outgoing_req, _)) = request.as_outgoing() {
            ensure!(
                bridge_status != BridgeStatus::Migrating
                    || outgoing_req.is_allowed_during_migration(),
                Error::<T>::ContractIsInMigrationStage
            );
        }
        let hash = request.hash();
        let can_resubmit = RequestStatuses::<T>::get(net_id, &hash)
            .map(|status| matches!(status, RequestStatus::Failed(_)))
            .unwrap_or(false);
        if !can_resubmit {
            ensure!(
                Requests::<T>::get(net_id, &hash).is_none(),
                Error::<T>::DuplicatedRequest
            );
        }
        request.validate()?;
        request.prepare()?;
        AccountRequests::<T>::mutate(&request.author(), |vec| vec.push((net_id, hash)));
        Requests::<T>::insert(net_id, &hash, request);
        RequestsQueue::<T>::mutate(net_id, |v| v.push(hash));
        RequestStatuses::<T>::insert(net_id, &hash, RequestStatus::Pending);
        let block_number = frame_system::Pallet::<T>::current_block_number();
        RequestSubmissionHeight::<T>::insert(net_id, &hash, block_number);
        Self::deposit_event(Event::RequestRegistered(hash));
        Ok(())
    }

    /// Prepares and validates the request, then adds it to the queue and maps it with the
    /// corresponding load request and removes the load request from the queue.
    fn register_incoming_request_inner(
        incoming_request: &OffchainRequest<T>,
        network_id: T::NetworkId,
    ) -> Result<H256, DispatchError> {
        let sidechain_tx_hash = incoming_request
            .as_incoming()
            .expect("request is always 'incoming'; qed")
            .0
            .hash();
        let incoming_request_hash = incoming_request.hash();
        let request_author = incoming_request.author().clone();
        ensure!(
            !Requests::<T>::contains_key(network_id, incoming_request_hash),
            Error::<T>::RequestIsAlreadyRegistered
        );
        Self::remove_request_from_queue(network_id, &sidechain_tx_hash);
        RequestStatuses::<T>::insert(network_id, sidechain_tx_hash, RequestStatus::Done);
        LoadToIncomingRequestHash::<T>::insert(
            network_id,
            sidechain_tx_hash,
            incoming_request_hash,
        );
        if let Err(e) = incoming_request
            .validate()
            .and_then(|_| incoming_request.prepare())
        {
            RequestStatuses::<T>::insert(
                network_id,
                incoming_request_hash,
                RequestStatus::Failed(e),
            );
            warn!("{:?}", e);
            Self::deposit_event(Event::RegisterRequestFailed(incoming_request_hash, e));
            return Ok(incoming_request_hash);
        }
        Requests::<T>::insert(network_id, &incoming_request_hash, incoming_request);
        RequestsQueue::<T>::mutate(network_id, |v| v.push(incoming_request_hash));
        RequestStatuses::<T>::insert(network_id, incoming_request_hash, RequestStatus::Pending);
        AccountRequests::<T>::mutate(request_author, |v| {
            v.push((network_id, incoming_request_hash))
        });
        Ok(incoming_request_hash)
    }

    /// At first, `finalize` is called on the request, if it fails, the `cancel` function
    /// gets called. Request status changes depending on the result (`Done` or `Failed`), and
    /// finally the request gets removed from the queue.
    fn finalize_incoming_request_inner(
        request: &IncomingRequest<T>,
        hash: H256,
        network_id: T::NetworkId,
    ) -> DispatchResult {
        ensure!(
            RequestStatuses::<T>::get(network_id, hash).ok_or(Error::<T>::UnknownRequest)?
                == RequestStatus::Pending,
            Error::<T>::ExpectedPendingRequest
        );
        let error_opt = request.finalize().err();
        if let Some(e) = error_opt {
            error!("Incoming request failed {:?} {:?}", hash, e);
            Self::deposit_event(Event::IncomingRequestFinalizationFailed(hash));
            RequestStatuses::<T>::insert(network_id, hash, RequestStatus::Failed(e));
            cancel!(request, hash, network_id, e);
        } else {
            warn!("Incoming request finalized {:?}", hash);
            RequestStatuses::<T>::insert(network_id, hash, RequestStatus::Done);
            Self::deposit_event(Event::IncomingRequestFinalized(hash));
        }
        Self::remove_request_from_queue(network_id, &hash);
        Ok(())
    }

    /// Finds and removes request from `RequestsQueue` by its hash and network id.
    fn remove_request_from_queue(network_id: T::NetworkId, hash: &H256) {
        RequestsQueue::<T>::mutate(network_id, |queue| {
            if let Some(pos) = queue.iter().position(|x| x == hash) {
                queue.remove(pos);
            }
        });
    }

    /// Registers new sidechain asset and grants mint permission to the bridge account.
    fn register_sidechain_asset(
        token_address: EthAddress,
        precision: BalancePrecision,
        symbol: AssetSymbol,
        name: AssetName,
        network_id: T::NetworkId,
    ) -> Result<T::AssetId, DispatchError> {
        ensure!(
            RegisteredSidechainAsset::<T>::get(network_id, &token_address).is_none(),
            Error::<T>::TokenIsAlreadyAdded
        );
        ensure!(
            precision <= DEFAULT_BALANCE_PRECISION,
            Error::<T>::UnsupportedAssetPrecision
        );
        let bridge_account =
            Self::bridge_account(network_id).expect("networks can't be removed; qed");
        let asset_id = assets::Pallet::<T>::register_from(
            &bridge_account,
            symbol,
            name,
            DEFAULT_BALANCE_PRECISION,
            Balance::from(0u32),
            true,
            None,
            None,
        )?;
        RegisteredAsset::<T>::insert(network_id, &asset_id, AssetKind::Sidechain);
        RegisteredSidechainAsset::<T>::insert(network_id, &token_address, asset_id);
        RegisteredSidechainToken::<T>::insert(network_id, &asset_id, token_address);
        SidechainAssetPrecision::<T>::insert(network_id, &asset_id, precision);
        let scope = Scope::Limited(common::hash(&asset_id));
        let permission_ids = [MINT, BURN];
        for permission_id in &permission_ids {
            let permission_owner = permissions::Owners::<T>::get(permission_id, &scope)
                .pop()
                .unwrap_or_else(|| bridge_account.clone());
            permissions::Pallet::<T>::grant_permission_with_scope(
                permission_owner,
                bridge_account.clone(),
                *permission_id,
                scope,
            )?;
        }

        Ok(asset_id)
    }

    fn inner_abort_request(
        request: &OffchainRequest<T>,
        hash: H256,
        error: DispatchError,
        network_id: T::NetworkId,
    ) -> Result<(), DispatchError> {
        ensure!(
            RequestStatuses::<T>::get(network_id, hash).ok_or(Error::<T>::UnknownRequest)?
                == RequestStatus::Pending,
            Error::<T>::ExpectedPendingRequest
        );
        cancel!(request, hash, network_id, error);
        Self::remove_request_from_queue(network_id, &hash);
        Self::deposit_event(Event::RequestAborted(hash));
        Ok(())
    }

    fn inner_import_incoming_request(
        net_id: T::NetworkId,
        load_incoming_request: LoadIncomingRequest<T>,
        incoming_request_result: Result<IncomingRequest<T>, DispatchError>,
    ) -> Result<(), DispatchError> {
        let sidechain_tx_hash = load_incoming_request.hash();
        let load_incoming = OffchainRequest::LoadIncoming(load_incoming_request);
        Self::add_request(&load_incoming)?;
        match incoming_request_result {
            Ok(incoming_request) => {
                assert_eq!(net_id, incoming_request.network_id());
                let incoming = OffchainRequest::incoming(incoming_request.clone());
                let incoming_request_hash = incoming.hash();
                Self::add_request(&incoming)?;
                Self::finalize_incoming_request_inner(
                    &incoming_request,
                    incoming_request_hash,
                    net_id,
                )?;
            }
            Err(e) => {
                Self::inner_abort_request(&load_incoming, sidechain_tx_hash, e, net_id)?;
            }
        }
        Ok(().into())
    }

    fn inner_approve_request(
        ocw_public: ecdsa::Public,
        hash: H256,
        signature_params: SignatureParams,
        author: T::AccountId,
        net_id: T::NetworkId,
    ) -> Result<Option<Weight>, DispatchError> {
        let request = Requests::<T>::get(net_id, hash)
            .and_then(|x| x.into_outgoing().map(|x| x.0))
            .ok_or(Error::<T>::UnknownRequest)?;
        let request_encoded = request.to_eth_abi(hash)?;
        if !Self::verify_message(
            request_encoded.as_raw(),
            &signature_params,
            &ocw_public,
            &author,
        ) {
            // TODO: punish the peer.
            return Err(Error::<T>::InvalidSignature.into());
        }
        info!("Verified request approve {:?}", request_encoded);
        let mut approvals = RequestApprovals::<T>::get(net_id, &hash);
        let pending_peers_len = if Self::is_additional_signature_needed(net_id, &request) {
            1
        } else {
            0
        };
        let need_sigs = majority(Self::peers(net_id).len()) + pending_peers_len;
        let current_status =
            RequestStatuses::<T>::get(net_id, &hash).ok_or(Error::<T>::UnknownRequest)?;
        approvals.insert(signature_params);
        RequestApprovals::<T>::insert(net_id, &hash, &approvals);
        if current_status == RequestStatus::Pending && approvals.len() == need_sigs {
            if let Err(err) = request.finalize(hash) {
                error!("Outgoing request finalization failed: {:?}", err);
                RequestStatuses::<T>::insert(net_id, hash, RequestStatus::Failed(err));
                Self::deposit_event(Event::RequestFinalizationFailed(hash));
                cancel!(request, hash, net_id, err);
            } else {
                debug!("Outgoing request approvals collected {:?}", hash);
                RequestStatuses::<T>::insert(net_id, hash, RequestStatus::ApprovalsReady);
                Self::deposit_event(Event::ApprovalsCollected(hash));
            }
            Self::remove_request_from_queue(net_id, &hash);
            let weight_info = <T as Config>::WeightInfo::approve_request_finalize();
            return Ok(Some(weight_info));
        }
        Ok(None)
    }

    fn is_additional_signature_needed(net_id: T::NetworkId, request: &OutgoingRequest<T>) -> bool {
        PendingPeer::<T>::get(net_id).is_some()
            && !matches!(
                &request,
                OutgoingRequest::AddPeer(..) | OutgoingRequest::AddPeerCompat(..)
            )
    }

    fn ensure_generic_network(
        generic_network_id: GenericNetworkId,
    ) -> Result<T::NetworkId, DispatchError> {
        let network_id = T::GetEthNetworkId::get();
        if generic_network_id != GenericNetworkId::EVMLegacy(network_id.unique_saturated_into()) {
            return Err(Error::<T>::UnknownNetwork.into());
        }
        Ok(network_id)
    }
}

impl<T: Config> BridgeApp<T::AccountId, EthAddress, T::AssetId, Balance> for Pallet<T> {
    fn is_asset_supported(network_id: GenericNetworkId, asset_id: T::AssetId) -> bool {
        let Ok(network_id) = Self::ensure_generic_network(network_id) else {
            return false;
        };
        RegisteredAsset::<T>::contains_key(network_id, &asset_id)
    }

    fn transfer(
        network_id: GenericNetworkId,
        asset_id: T::AssetId,
        sender: T::AccountId,
        recipient: EthAddress,
        amount: Balance,
    ) -> Result<H256, DispatchError> {
        debug!("called BridgeApp::transfer");
        let network_id = Self::ensure_generic_network(network_id)?;
        let nonce = frame_system::Pallet::<T>::account_nonce(&sender);
        let timepoint = bridge_multisig::Pallet::<T>::thischain_timepoint();
        let request = OffchainRequest::outgoing(OutgoingRequest::Transfer(OutgoingTransfer {
            from: sender.clone(),
            to: recipient,
            asset_id,
            amount,
            nonce,
            network_id,
            timepoint,
        }));
        let tx_hash = request.hash();
        Self::add_request(&request)?;
        frame_system::Pallet::<T>::inc_account_nonce(&sender);
        Ok(tx_hash)
    }

    fn refund(
        _network_id: GenericNetworkId,
        _message_id: H256,
        _recipient: T::AccountId,
        _asset_id: T::AssetId,
        _amount: Balance,
    ) -> DispatchResult {
        Err(Error::<T>::Unavailable.into())
    }

    fn list_supported_assets(
        network_id: GenericNetworkId,
    ) -> Vec<bridge_types::types::BridgeAssetInfo> {
        use bridge_types::evm::{EVMAppKind, EVMLegacyAssetInfo};
        use bridge_types::types::BridgeAssetInfo;
        let Ok(network_id) = Self::ensure_generic_network(network_id) else {
            return vec![];
        };
        RegisteredAsset::<T>::iter_prefix(network_id)
            .map(|(asset_id, _kind)| {
                let evm_address =
                    RegisteredSidechainToken::<T>::get(network_id, &asset_id).map(|x| H160(x.0));
                let precision = evm_address.map(|_address| {
                    let precision = SidechainAssetPrecision::<T>::get(network_id, &asset_id);
                    precision
                });

                let app_kind = if asset_id == common::XOR.into() {
                    EVMAppKind::XorMaster
                } else if asset_id == common::VAL.into() {
                    EVMAppKind::ValMaster
                } else {
                    EVMAppKind::HashiBridge
                };

                BridgeAssetInfo::EVMLegacy(EVMLegacyAssetInfo {
                    asset_id: asset_id.into(),
                    app_kind,
                    evm_address,
                    precision,
                })
            })
            .collect()
    }

    fn list_apps() -> Vec<bridge_types::types::BridgeAppInfo> {
        use bridge_types::evm::{EVMAppInfo, EVMAppKind};
        use bridge_types::types::BridgeAppInfo;
        let mut apps = vec![];
        let network_id = T::GetEthNetworkId::get();
        let generic_network_id = GenericNetworkId::EVMLegacy(network_id.unique_saturated_into());
        if let Ok(bridge_address) = BridgeContractAddress::<T>::try_get(network_id) {
            let app = BridgeAppInfo::EVM(
                generic_network_id,
                EVMAppInfo {
                    evm_address: bridge_address,
                    app_kind: EVMAppKind::HashiBridge,
                },
            );
            apps.push(app);
        }
        if let Ok(xor_master) = XorMasterContractAddress::<T>::try_get() {
            let app = BridgeAppInfo::EVM(
                generic_network_id,
                EVMAppInfo {
                    evm_address: xor_master,
                    app_kind: EVMAppKind::XorMaster,
                },
            );
            apps.push(app);
        }
        if let Ok(val_master) = ValMasterContractAddress::<T>::try_get() {
            let app = BridgeAppInfo::EVM(
                generic_network_id,
                EVMAppInfo {
                    evm_address: val_master,
                    app_kind: EVMAppKind::ValMaster,
                },
            );
            apps.push(app);
        }
        apps
    }

    fn is_asset_supported_weight() -> Weight {
        T::DbWeight::get().reads(1)
    }

    fn refund_weight() -> Weight {
        Default::default()
    }

    fn transfer_weight() -> Weight {
        <T as Config>::WeightInfo::transfer_to_sidechain()
    }
}