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
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
// Copyright 2022 Interlay.
// This file is part of Interlay.

// Copyright 2021 Parallel Finance Developer.
// This file is part of Parallel Finance.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Loans pallet
//!
//! ## Overview
//!
//! The loans pallet implements a Compound V2-style lending protocol by using a pool-based strategy
//! that aggregates each user's supplied assets. The interest rate is dynamically
//! determined by the supply and demand. Lending positions are tokenized and can thus have their
//! ownership transferred.

#![cfg_attr(not(feature = "std"), no_std)]

pub use crate::rate_model::*;
use crate::types::AccountLiquidity;

use currency::{Amount, Rounding};
use frame_support::{
    log,
    pallet_prelude::*,
    require_transactional,
    traits::{
        tokens::{fungibles::Inspect, Fortitude, Preservation},
        UnixTime,
    },
    transactional, PalletId,
};
use frame_system::pallet_prelude::*;
use num_traits::cast::ToPrimitive;
use orml_traits::{MultiCurrency, MultiReservableCurrency};
pub use pallet::*;
use primitives::{Balance, Rate, Ratio, Timestamp};
use sp_runtime::{
    traits::{
        AccountIdConversion, CheckedAdd, CheckedDiv, CheckedMul, One, SaturatedConversion, Saturating, StaticLookup,
        Zero,
    },
    ArithmeticError, FixedPointNumber, FixedU128,
};
use sp_std::{marker, result::Result};

use traits::{
    ConvertToBigUint, LoansApi as LoansTrait, LoansMarketDataProvider, MarketInfo, MarketStatus, OnExchangeRateChange,
};

pub use default_weights::WeightInfo;
pub use orml_traits::currency::{OnDeposit, OnSlash, OnTransfer};
pub use types::{BorrowSnapshot, EarnedSnapshot, Market, MarketState, RewardMarketState};

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

mod farming;
mod interest;
#[cfg(test)]
mod lend_token;
mod rate_model;
mod types;

mod default_weights;

#[cfg(test)]
use mocktopus::macros::mockable;

pub const REWARD_SUB_ACCOUNT: &[u8; 7] = b"farming";
pub const INCENTIVE_SUB_ACCOUNT: &[u8; 9] = b"incentive";

pub const DEFAULT_MAX_EXCHANGE_RATE: u128 = 1_000_000_000_000_000_000; // 1
pub const DEFAULT_MIN_EXCHANGE_RATE: u128 = 20_000_000_000_000_000; // 0.02

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type CurrencyId<T> = <T as orml_tokens::Config>::CurrencyId;
type BalanceOf<T> = <T as currency::Config>::Balance;

/// Lending-specific methods on Amount
#[cfg_attr(test, mockable)]
trait LendingAmountExt {
    fn to_lend_token(&self) -> Result<Self, DispatchError>
    where
        Self: Sized;
    fn to_underlying(&self) -> Result<Self, DispatchError>
    where
        Self: Sized;
}

#[cfg_attr(test, mockable)]
impl<T: Config> LendingAmountExt for Amount<T> {
    fn to_lend_token(&self) -> Result<Self, DispatchError> {
        let lend_token_id = Pallet::<T>::lend_token_id(self.currency())?;
        self.convert_to(lend_token_id)
    }

    fn to_underlying(&self) -> Result<Self, DispatchError> {
        let underlying_id = Pallet::<T>::underlying_id(self.currency())?;
        self.convert_to(underlying_id)
    }
}

pub struct OnSlashHook<T>(marker::PhantomData<T>);
// This implementation is not allowed to fail, so erors are logged instead of being propagated.
// If the slash-related FRAME traits are allowed to fail, this can be fixed.
// Opened a GitHub issue for this in the Substrate repo: https://github.com/paritytech/substrate/issues/12533
// TODO: Propagate error once the issue is resolved upstream
impl<T: Config> OnSlash<T::AccountId, CurrencyId<T>, BalanceOf<T>> for OnSlashHook<T> {
    /// Whenever a lend_token balance is mutated, the supplier incentive rewards accumulated up to that point
    /// have to be distributed.
    fn on_slash(currency_id: CurrencyId<T>, account_id: &T::AccountId, amount: BalanceOf<T>) {
        if currency_id.is_lend_token() {
            // Note that wherever `on_slash` is called in the lending pallet, and the `account_id` has non-zero
            // `AccountDeposits`, the storage item needs to be decreased by `amount` to reflect the reduction
            // in collateral.
            let f = || -> DispatchResult {
                let underlying_id = Pallet::<T>::underlying_id(currency_id)?;
                Pallet::<T>::update_reward_supply_index(underlying_id)?;
                Pallet::<T>::distribute_supplier_reward(underlying_id, account_id)?;
                Ok(())
            };
            if let Err(e) = f() {
                log::trace!(
                    target: "loans::on_slash",
                    "error: {:?}, currency_id: {:?}, account_id: {:?}, amount: {:?}",
                    e,
                    currency_id,
                    account_id,
                    amount,
                );
            }
        }
    }
}

pub struct PreDeposit<T>(marker::PhantomData<T>);
impl<T: Config> OnDeposit<T::AccountId, CurrencyId<T>, BalanceOf<T>> for PreDeposit<T> {
    /// Whenever a lend_token balance is mutated, the supplier incentive rewards accumulated up to that point
    /// have to be distributed.
    fn on_deposit(currency_id: CurrencyId<T>, account_id: &T::AccountId, _amount: BalanceOf<T>) -> DispatchResult {
        if currency_id.is_lend_token() {
            let underlying_id = Pallet::<T>::underlying_id(currency_id)?;
            Pallet::<T>::update_reward_supply_index(underlying_id)?;
            Pallet::<T>::distribute_supplier_reward(underlying_id, account_id)?;
        }
        Ok(())
    }
}

pub struct PostDeposit<T>(marker::PhantomData<T>);
impl<T: Config> OnDeposit<T::AccountId, CurrencyId<T>, BalanceOf<T>> for PostDeposit<T> {
    fn on_deposit(currency_id: CurrencyId<T>, account_id: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
        if currency_id.is_lend_token() {
            Pallet::<T>::lock_if_account_deposited(account_id, &Amount::new(amount, currency_id))?;
        }
        Ok(())
    }
}

pub struct PreTransfer<T>(marker::PhantomData<T>);
impl<T: Config> OnTransfer<T::AccountId, CurrencyId<T>, BalanceOf<T>> for PreTransfer<T> {
    /// Whenever a lend_token balance is mutated, the supplier incentive rewards accumulated up to that point
    /// have to be distributed.
    fn on_transfer(
        currency_id: CurrencyId<T>,
        from: &T::AccountId,
        to: &T::AccountId,
        _amount: BalanceOf<T>,
    ) -> DispatchResult {
        if currency_id.is_lend_token() {
            let underlying_id = Pallet::<T>::underlying_id(currency_id)?;
            Pallet::<T>::update_reward_supply_index(underlying_id)?;
            Pallet::<T>::distribute_supplier_reward(underlying_id, from)?;
            Pallet::<T>::distribute_supplier_reward(underlying_id, to)?;
        }
        Ok(())
    }
}

pub struct PostTransfer<T>(marker::PhantomData<T>);
impl<T: Config> OnTransfer<T::AccountId, CurrencyId<T>, BalanceOf<T>> for PostTransfer<T> {
    /// If an account has locked their lend_token balance as collateral, any incoming lend_tokens
    /// have to be automatically locked as well, in order to enforce a "collateral toggle" that
    /// offers the same UX as Compound V2's lending protocol implementation.
    fn on_transfer(
        currency_id: CurrencyId<T>,
        _from: &T::AccountId,
        to: &T::AccountId,
        amount: BalanceOf<T>,
    ) -> DispatchResult {
        if currency_id.is_lend_token() {
            Pallet::<T>::lock_if_account_deposited(to, &Amount::new(amount, currency_id))?;
        }
        Ok(())
    }
}

/// Utility type for managing upgrades/migrations.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum Versions {
    V0,
}

#[frame_support::pallet]
pub mod pallet {

    use super::*;

    #[pallet::config]
    pub trait Config:
        frame_system::Config + currency::Config<Balance = Balance, UnsignedFixedPoint = FixedU128>
    {
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// The loan's module id, used to derive the account that holds the liquidity in all markets.
        #[pallet::constant]
        type PalletId: Get<PalletId>;

        /// The origin which can add/reduce reserves.
        type ReserveOrigin: EnsureOrigin<Self::RuntimeOrigin>;

        /// The origin which can add or update markets (including interest rate model, liquidate incentive and
        /// collateral ratios).
        type UpdateOrigin: EnsureOrigin<Self::RuntimeOrigin>;

        /// Weight information for extrinsics in this pallet.
        type WeightInfo: WeightInfo;

        /// Unix time
        type UnixTime: UnixTime;

        /// Incentive reward asset id.
        #[pallet::constant]
        type RewardAssetId: Get<CurrencyId<Self>>;

        /// Reference currency for expressing asset prices. Example: USD, IBTC.
        #[pallet::constant]
        type ReferenceAssetId: Get<CurrencyId<Self>>;

        /// Hook for exchangerate changes.
        type OnExchangeRateChange: OnExchangeRateChange<CurrencyId<Self>>;
    }

    #[pallet::error]
    pub enum Error<T> {
        /// Insufficient liquidity to borrow more or disable collateral
        InsufficientLiquidity,
        /// Insufficient deposit to redeem
        InsufficientDeposit,
        /// Repay amount greater than allowed (either repays more than the existing debt, or
        /// exceeds the close factor)
        TooMuchRepay,
        /// Repay amount more than collateral amount
        InsufficientCollateral,
        /// Liquidator is same as borrower
        LiquidatorIsBorrower,
        /// Deposits are not used as a collateral
        DepositsAreNotCollateral,
        /// Insufficient shortfall to repay
        InsufficientShortfall,
        /// Insufficient reserves
        InsufficientReserves,
        /// Invalid rate model params
        InvalidRateModelParam,
        /// Market not activated
        MarketNotActivated,
        /// Invalid lend_token id
        InvalidLendTokenId,
        /// Market does not exist
        MarketDoesNotExist,
        /// Market already activated
        MarketAlreadyActivated,
        /// Market already exists
        MarketAlreadyExists,
        /// New markets must have a pending state
        NewMarketMustHavePendingState,
        /// Upper bound of supplying is exceeded
        SupplyCapacityExceeded,
        /// Upper bound of borrowing is exceeded
        BorrowCapacityExceeded,
        /// Insufficient cash in the pool
        InsufficientCash,
        /// The factor should be greater than 0% and less than 100%
        InvalidFactor,
        /// The supply cap cannot be zero
        InvalidSupplyCap,
        /// The exchange rate should be a value between `MinExchangeRate` and `MaxExchangeRate`
        InvalidExchangeRate,
        /// Amount cannot be zero
        InvalidAmount,
        /// Locking collateral failed. The account has no `free` tokens.
        DepositAllCollateralFailed,
        /// Unlocking collateral failed. The account has no `reserved` tokens.
        WithdrawAllCollateralFailed,
        /// Tokens already locked for a different purpose than borrow collateral
        TokensAlreadyLocked,
        /// Only free lend tokens are redeemable
        LockedTokensCannotBeRedeemed,
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub (crate) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Enable collateral for certain asset
        DepositCollateral {
            account_id: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Disable collateral for certain asset
        WithdrawCollateral {
            account_id: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Event emitted when assets are deposited
        Deposited {
            account_id: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Event emitted when assets are redeemed
        Redeemed {
            account_id: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Event emitted when cash is borrowed
        Borrowed {
            account_id: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Event emitted when a borrow is repaid
        RepaidBorrow {
            account_id: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Event emitted when a borrow is liquidated
        LiquidatedBorrow {
            liquidator: T::AccountId,
            borrower: T::AccountId,
            liquidation_currency_id: CurrencyId<T>,
            collateral_currency_id: CurrencyId<T>,
            repay_amount: BalanceOf<T>,
            collateral_underlying_amount: BalanceOf<T>,
        },
        /// Event emitted when the reserves are reduced
        ReservesReduced {
            receiver: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
            new_reserve_amount: BalanceOf<T>,
        },
        /// Event emitted when the reserves are added
        ReservesAdded {
            payer: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
            new_reserve_amount: BalanceOf<T>,
        },
        /// New market is set
        NewMarket {
            underlying_currency_id: CurrencyId<T>,
            market: Market<BalanceOf<T>>,
        },
        /// Event emitted when a market is activated
        ActivatedMarket { underlying_currency_id: CurrencyId<T> },
        /// New market parameters is updated
        UpdatedMarket {
            underlying_currency_id: CurrencyId<T>,
            market: Market<BalanceOf<T>>,
        },
        /// Reward added
        RewardAdded { payer: T::AccountId, amount: BalanceOf<T> },
        /// Reward withdrawed
        RewardWithdrawn {
            receiver: T::AccountId,
            amount: BalanceOf<T>,
        },
        /// Event emitted when market reward speed updated.
        MarketRewardSpeedUpdated {
            underlying_currency_id: CurrencyId<T>,
            supply_reward_per_block: BalanceOf<T>,
            borrow_reward_per_block: BalanceOf<T>,
        },
        /// Deposited when Reward is distributed to a supplier
        DistributedSupplierReward {
            underlying_currency_id: CurrencyId<T>,
            supplier: T::AccountId,
            reward_delta: BalanceOf<T>,
            supply_reward_index: BalanceOf<T>,
        },
        /// Deposited when Reward is distributed to a borrower
        DistributedBorrowerReward {
            underlying_currency_id: CurrencyId<T>,
            borrower: T::AccountId,
            reward_delta: BalanceOf<T>,
            borrow_reward_index: BalanceOf<T>,
        },
        /// Reward Paid for user
        RewardPaid {
            receiver: T::AccountId,
            amount: BalanceOf<T>,
        },
        /// Event emitted when the incentive reserves are redeemed and transfer to receiver's account
        IncentiveReservesReduced {
            receiver: T::AccountId,
            currency_id: CurrencyId<T>,
            amount: BalanceOf<T>,
        },
        /// Event emitted when interest has been accrued for a market
        InterestAccrued {
            underlying_currency_id: CurrencyId<T>,
            total_borrows: BalanceOf<T>,
            total_reserves: BalanceOf<T>,
            borrow_index: FixedU128,
            utilization_ratio: Ratio,
            borrow_rate: Rate,
            supply_rate: Rate,
            exchange_rate: Rate,
        },
    }

    /// The timestamp of the last calculation of accrued interest
    #[pallet::storage]
    #[pallet::getter(fn last_accrued_interest_time)]
    pub type LastAccruedInterestTime<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Timestamp, ValueQuery>;

    /// Total amount of outstanding borrows of the underlying in this market
    /// CurrencyId -> Balance
    #[pallet::storage]
    pub type TotalBorrows<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, BalanceOf<T>, ValueQuery>;

    /// Total amount of reserves of the underlying held in this market
    /// CurrencyId -> Balance
    #[pallet::storage]
    pub type TotalReserves<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, BalanceOf<T>, ValueQuery>;

    /// Mapping of account addresses to outstanding borrow balances
    /// CurrencyId -> Owner -> BorrowSnapshot
    #[pallet::storage]
    #[pallet::getter(fn account_borrows)]
    pub type AccountBorrows<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        CurrencyId<T>,
        Blake2_128Concat,
        T::AccountId,
        BorrowSnapshot<BalanceOf<T>>,
        ValueQuery,
    >;

    /// Mapping of account addresses to collateral deposit details
    /// CollateralType -> Owner -> Collateral Deposits
    ///
    /// # Remarks
    ///
    /// Differently from Parallel Finance's implementation of lending, `AccountDeposits` only
    /// represents Lend Tokens locked as collateral rather than the entire Lend Token balance of an account.
    /// If an account minted without also locking their balance as collateral, their corresponding entry
    /// in this map will be zero.
    #[pallet::storage]
    pub type AccountDeposits<T: Config> =
        StorageDoubleMap<_, Blake2_128Concat, CurrencyId<T>, Blake2_128Concat, T::AccountId, BalanceOf<T>, ValueQuery>;

    /// Accumulator of the total earned interest rate since the opening of the market
    /// CurrencyId -> u128
    #[pallet::storage]
    #[pallet::getter(fn borrow_index)]
    pub type BorrowIndex<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Rate, ValueQuery>;

    /// The internal exchange rate from the associated lend token to the underlying currency.
    #[pallet::storage]
    #[pallet::getter(fn exchange_rate)]
    pub type ExchangeRate<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Rate, ValueQuery>;

    /// Mapping of borrow rate to currency type
    #[pallet::storage]
    #[pallet::getter(fn borrow_rate)]
    pub type BorrowRate<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Rate, ValueQuery>;

    /// Mapping of supply rate to currency type
    #[pallet::storage]
    #[pallet::getter(fn supply_rate)]
    pub type SupplyRate<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Rate, ValueQuery>;

    /// Borrow utilization ratio
    #[pallet::storage]
    #[pallet::getter(fn utilization_ratio)]
    pub type UtilizationRatio<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Ratio, ValueQuery>;

    /// Mapping of underlying currency id to its market
    #[pallet::storage]
    pub type Markets<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, Market<BalanceOf<T>>>;

    /// Mapping of lend_token id to underlying currency id
    /// `lend_token id`: voucher token id
    /// `asset id`: underlying token id
    #[pallet::storage]
    pub type UnderlyingAssetId<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, CurrencyId<T>>;

    /// Mapping of underlying currency id to supply reward speed
    #[pallet::storage]
    #[pallet::getter(fn reward_supply_speed)]
    pub type RewardSupplySpeed<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, BalanceOf<T>, ValueQuery>;

    /// Mapping of underlying currency id to borrow reward speed
    #[pallet::storage]
    #[pallet::getter(fn reward_borrow_speed)]
    pub type RewardBorrowSpeed<T: Config> = StorageMap<_, Blake2_128Concat, CurrencyId<T>, BalanceOf<T>, ValueQuery>;

    /// The Reward market supply state for each market
    #[pallet::storage]
    #[pallet::getter(fn reward_supply_state)]
    pub type RewardSupplyState<T: Config> =
        StorageMap<_, Blake2_128Concat, CurrencyId<T>, RewardMarketState<BlockNumberFor<T>, BalanceOf<T>>, ValueQuery>;

    /// The Reward market borrow state for each market
    #[pallet::storage]
    #[pallet::getter(fn reward_borrow_state)]
    pub type RewardBorrowState<T: Config> =
        StorageMap<_, Blake2_128Concat, CurrencyId<T>, RewardMarketState<BlockNumberFor<T>, BalanceOf<T>>, ValueQuery>;

    /// The incentive reward index for each market for each supplier as of the last time they accrued Reward
    #[pallet::storage]
    #[pallet::getter(fn reward_supplier_index)]
    pub type RewardSupplierIndex<T: Config> =
        StorageDoubleMap<_, Blake2_128Concat, CurrencyId<T>, Blake2_128Concat, T::AccountId, BalanceOf<T>, ValueQuery>;

    /// The incentive reward index for each market for each borrower as of the last time they accrued Reward
    #[pallet::storage]
    #[pallet::getter(fn reward_borrower_index)]
    pub type RewardBorrowerIndex<T: Config> =
        StorageDoubleMap<_, Blake2_128Concat, CurrencyId<T>, Blake2_128Concat, T::AccountId, BalanceOf<T>, ValueQuery>;

    /// The incentive reward accrued but not yet transferred to each user.
    #[pallet::storage]
    #[pallet::getter(fn reward_accrued)]
    pub type RewardAccrued<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf<T>, ValueQuery>;

    /// The maximum allowed exchange rate for a market.
    #[pallet::storage]
    #[pallet::getter(fn max_exchange_rate)]
    pub type MaxExchangeRate<T: Config> = StorageValue<_, Rate, ValueQuery>;

    /// The minimum allowed exchange rate for a market. This is the starting rate when a market is first set up.
    #[pallet::storage]
    #[pallet::getter(fn min_exchange_rate)]
    pub type MinExchangeRate<T: Config> = StorageValue<_, Rate, ValueQuery>;

    /// DefaultVersion is using for initialize the StorageVersion
    #[pallet::type_value]
    pub(super) fn DefaultVersion<T: Config>() -> Versions {
        Versions::V0
    }

    /// Storage version of the pallet.
    #[pallet::storage]
    pub(crate) type StorageVersion<T: Config> = StorageValue<_, Versions, ValueQuery, DefaultVersion<T>>;

    #[pallet::genesis_config]
    #[derive(frame_support::DefaultNoBound)]
    pub struct GenesisConfig<T: Config> {
        pub max_exchange_rate: Rate,
        pub min_exchange_rate: Rate,
        pub _marker: PhantomData<T>,
    }

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            MaxExchangeRate::<T>::put(&self.max_exchange_rate);
            MinExchangeRate::<T>::put(&self.min_exchange_rate);
        }
    }

    #[pallet::pallet]
    pub struct Pallet<T>(PhantomData<T>);

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Creates a new lending market for a given currency. Returns `Err` if a market already
        /// exists for the given currency.
        ///
        /// All provided market states must be `Pending`, otherwise an error will be returned.
        ///
        /// The lend_token id specified in the Market struct has to be unique, and cannot be later reused
        /// when creating a new market.
        ///
        /// - `asset_id`: Currency to enable lending and borrowing for.
        /// - `market`: Configuration of the new lending market
        #[pallet::call_index(0)]
        #[pallet::weight(<T as Config>::WeightInfo::add_market())]
        #[transactional]
        pub fn add_market(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            market: Market<BalanceOf<T>>,
        ) -> DispatchResultWithPostInfo {
            T::UpdateOrigin::ensure_origin(origin)?;
            ensure!(!Markets::<T>::contains_key(asset_id), Error::<T>::MarketAlreadyExists);
            ensure!(
                market.state == MarketState::Pending,
                Error::<T>::NewMarketMustHavePendingState
            );
            ensure!(market.rate_model.check_model(), Error::<T>::InvalidRateModelParam);
            ensure!(
                market.collateral_factor >= Ratio::zero() && market.collateral_factor < Ratio::one(),
                Error::<T>::InvalidFactor,
            );
            ensure!(
                market.liquidation_threshold < Ratio::one() && market.liquidation_threshold >= market.collateral_factor,
                Error::<T>::InvalidFactor
            );
            ensure!(
                market.reserve_factor > Ratio::zero() && market.reserve_factor < Ratio::one(),
                Error::<T>::InvalidFactor,
            );
            ensure!(
                market.liquidate_incentive_reserved_factor < Ratio::one(),
                Error::<T>::InvalidFactor,
            );
            ensure!(market.supply_cap > Zero::zero(), Error::<T>::InvalidSupplyCap,);

            // Ensures a given `lend_token_id` does not exist either in a `Market` or as an `UnderlyingAssetId`.
            Self::ensure_lend_token(market.lend_token_id)?;
            // Update storage of `Market` and `UnderlyingAssetId`
            Markets::<T>::insert(asset_id, market.clone());
            UnderlyingAssetId::<T>::insert(market.lend_token_id, asset_id);

            // Init the ExchangeRate and BorrowIndex for asset
            let initial_exchange_rate = Self::min_exchange_rate();
            let initial_borrow_index = Rate::one();
            ExchangeRate::<T>::insert(asset_id, initial_exchange_rate);
            BorrowIndex::<T>::insert(asset_id, initial_borrow_index);

            // Emit an `InterestAccrued` event so event subscribers can see the
            // initial exchange rate.
            Self::deposit_event(Event::<T>::InterestAccrued {
                underlying_currency_id: asset_id,
                total_borrows: Balance::zero(),
                total_reserves: Balance::zero(),
                borrow_index: initial_borrow_index,
                utilization_ratio: Ratio::zero(),
                borrow_rate: Rate::zero(),
                supply_rate: Rate::zero(),
                exchange_rate: initial_exchange_rate,
            });
            Self::deposit_event(Event::<T>::NewMarket {
                underlying_currency_id: asset_id,
                market,
            });
            Ok(().into())
        }

        /// Activates a market. Returns `Err` if the market does not exist.
        ///
        /// If the market is already active, does nothing.
        ///
        /// - `asset_id`: Currency to enable lending and borrowing for.
        #[pallet::call_index(1)]
        #[pallet::weight(<T as Config>::WeightInfo::activate_market())]
        #[transactional]
        pub fn activate_market(origin: OriginFor<T>, asset_id: CurrencyId<T>) -> DispatchResultWithPostInfo {
            T::UpdateOrigin::ensure_origin(origin)?;
            Self::mutate_market(asset_id, |stored_market| {
                // Ensure that market is not already active
                if let MarketState::Active = stored_market.state {
                    return Err(Error::<T>::MarketAlreadyActivated.into());
                }
                stored_market.state = MarketState::Active;
                Ok(stored_market.clone())
            })?;
            Self::deposit_event(Event::<T>::ActivatedMarket {
                underlying_currency_id: asset_id,
            });
            Ok(().into())
        }

        /// Updates the rate model of a stored market. Returns `Err` if the market
        /// currency does not exist or the rate model is invalid.
        ///
        /// - `asset_id`: Market currency
        /// - `rate_model`: The new rate model to set
        #[pallet::call_index(2)]
        #[pallet::weight(<T as Config>::WeightInfo::update_rate_model())]
        #[transactional]
        pub fn update_rate_model(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            rate_model: InterestRateModel,
        ) -> DispatchResultWithPostInfo {
            T::UpdateOrigin::ensure_origin(origin)?;
            ensure!(rate_model.check_model(), Error::<T>::InvalidRateModelParam);
            let market = Self::mutate_market(asset_id, |stored_market| {
                stored_market.rate_model = rate_model;
                Ok(stored_market.clone())
            })?;
            Self::deposit_event(Event::<T>::UpdatedMarket {
                underlying_currency_id: asset_id,
                market,
            });

            Ok(().into())
        }

        /// Updates a stored market. Returns `Err` if the market currency does not exist.
        ///
        /// - `asset_id`: market related currency
        /// - `collateral_factor`: the collateral utilization ratio
        /// - `liquidation_threshold`: The collateral ratio when a borrower can be liquidated
        /// - `reserve_factor`: fraction of interest set aside for reserves
        /// - `close_factor`: max percentage of debt that can be liquidated in a single transaction
        /// - `liquidate_incentive_reserved_factor`: liquidation share set aside for reserves
        /// - `liquidate_incentive`: liquidation incentive ratio
        /// - `supply_cap`: Upper bound of supplying
        /// - `borrow_cap`: Upper bound of borrowing
        #[pallet::call_index(3)]
        #[pallet::weight(<T as Config>::WeightInfo::update_market())]
        #[transactional]
        pub fn update_market(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            collateral_factor: Option<Ratio>,
            liquidation_threshold: Option<Ratio>,
            reserve_factor: Option<Ratio>,
            close_factor: Option<Ratio>,
            liquidate_incentive_reserved_factor: Option<Ratio>,
            liquidate_incentive: Option<Rate>,
            supply_cap: Option<BalanceOf<T>>,
            borrow_cap: Option<BalanceOf<T>>,
        ) -> DispatchResultWithPostInfo {
            T::UpdateOrigin::ensure_origin(origin)?;

            let market = Self::market(asset_id)?;

            let collateral_factor = collateral_factor.unwrap_or(market.collateral_factor);
            let liquidation_threshold = liquidation_threshold.unwrap_or(market.liquidation_threshold);
            let reserve_factor = reserve_factor.unwrap_or(market.reserve_factor);
            let close_factor = close_factor.unwrap_or(market.close_factor);
            let liquidate_incentive_reserved_factor =
                liquidate_incentive_reserved_factor.unwrap_or(market.liquidate_incentive_reserved_factor);
            let liquidate_incentive = liquidate_incentive.unwrap_or(market.liquidate_incentive);
            let supply_cap = supply_cap.unwrap_or(market.supply_cap);
            let borrow_cap = borrow_cap.unwrap_or(market.borrow_cap);

            ensure!(
                collateral_factor >= Ratio::zero() && collateral_factor < Ratio::one(),
                Error::<T>::InvalidFactor
            );
            ensure!(
                liquidation_threshold >= collateral_factor && liquidation_threshold < Ratio::one(),
                Error::<T>::InvalidFactor
            );
            ensure!(
                reserve_factor > Ratio::zero() && reserve_factor < Ratio::one(),
                Error::<T>::InvalidFactor
            );
            ensure!(
                market.liquidate_incentive_reserved_factor < Ratio::one(),
                Error::<T>::InvalidFactor,
            );
            ensure!(supply_cap > Zero::zero(), Error::<T>::InvalidSupplyCap);

            let market = Self::mutate_market(asset_id, |stored_market| {
                *stored_market = Market {
                    state: stored_market.state,
                    lend_token_id: stored_market.lend_token_id,
                    rate_model: stored_market.rate_model,
                    collateral_factor,
                    liquidation_threshold,
                    reserve_factor,
                    close_factor,
                    liquidate_incentive,
                    liquidate_incentive_reserved_factor,
                    supply_cap,
                    borrow_cap,
                };
                Ok(stored_market.clone())
            })?;
            Self::deposit_event(Event::<T>::UpdatedMarket {
                underlying_currency_id: asset_id,
                market,
            });

            Ok(().into())
        }

        /// Force updates a stored market. Returns `Err` if the market currency
        /// does not exist.
        ///
        /// - `asset_id`: market related currency
        /// - `market`: Configuration of the new lending market
        #[pallet::call_index(4)]
        #[pallet::weight(<T as Config>::WeightInfo::force_update_market())]
        #[transactional]
        pub fn force_update_market(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            market: Market<BalanceOf<T>>,
        ) -> DispatchResultWithPostInfo {
            T::UpdateOrigin::ensure_origin(origin)?;
            ensure!(market.rate_model.check_model(), Error::<T>::InvalidRateModelParam);
            if UnderlyingAssetId::<T>::contains_key(market.lend_token_id) {
                ensure!(
                    Self::underlying_id(market.lend_token_id)? == asset_id,
                    Error::<T>::InvalidLendTokenId
                );
            }
            UnderlyingAssetId::<T>::insert(market.lend_token_id, asset_id);
            let updated_market = Self::mutate_market(asset_id, |stored_market| {
                *stored_market = market;
                Ok(stored_market.clone())
            })?;

            Self::deposit_event(Event::<T>::UpdatedMarket {
                underlying_currency_id: asset_id,
                market: updated_market,
            });
            Ok(().into())
        }

        /// Deposit incentive reward currency into the pallet account.
        ///
        /// - `amount`: Reward amount added
        #[pallet::call_index(5)]
        #[pallet::weight(<T as Config>::WeightInfo::add_reward())]
        #[transactional]
        pub fn add_reward(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            ensure!(!amount.is_zero(), Error::<T>::InvalidAmount);

            let reward_asset = T::RewardAssetId::get();
            let pool_account = Self::reward_account_id();

            let amount_to_transfer: Amount<T> = Amount::new(amount, reward_asset);
            amount_to_transfer.transfer(&who, &pool_account)?;

            Self::deposit_event(Event::<T>::RewardAdded { payer: who, amount });

            Ok(().into())
        }

        /// Updates reward speed for the specified market
        ///
        /// The origin must conform to `UpdateOrigin`.
        ///
        /// - `asset_id`: Market related currency
        /// - `supply_reward_per_block`: supply reward amount per block.
        /// - `borrow_reward_per_block`: borrow reward amount per block.
        #[pallet::call_index(6)]
        #[pallet::weight(<T as Config>::WeightInfo::update_market_reward_speed())]
        #[transactional]
        pub fn update_market_reward_speed(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            supply_reward_per_block: Option<BalanceOf<T>>,
            borrow_reward_per_block: Option<BalanceOf<T>>,
        ) -> DispatchResultWithPostInfo {
            T::UpdateOrigin::ensure_origin(origin)?;
            Self::ensure_active_market(asset_id)?;

            let current_supply_speed = RewardSupplySpeed::<T>::get(asset_id);
            let current_borrow_speed = RewardBorrowSpeed::<T>::get(asset_id);

            let supply_reward_per_block = supply_reward_per_block.unwrap_or(current_supply_speed);
            let borrow_reward_per_block = borrow_reward_per_block.unwrap_or(current_borrow_speed);

            if supply_reward_per_block != current_supply_speed {
                Self::update_reward_supply_index(asset_id)?;
                RewardSupplySpeed::<T>::try_mutate(asset_id, |current_speed| -> DispatchResult {
                    *current_speed = supply_reward_per_block;
                    Ok(())
                })?;
            }

            if borrow_reward_per_block != current_borrow_speed {
                Self::update_reward_borrow_index(asset_id)?;
                RewardBorrowSpeed::<T>::try_mutate(asset_id, |current_speed| -> DispatchResult {
                    *current_speed = borrow_reward_per_block;
                    Ok(())
                })?;
            }

            Self::deposit_event(Event::<T>::MarketRewardSpeedUpdated {
                underlying_currency_id: asset_id,
                supply_reward_per_block,
                borrow_reward_per_block,
            });
            Ok(().into())
        }

        /// Claim incentive rewards for all markets.
        #[pallet::call_index(7)]
        #[pallet::weight(<T as Config>::WeightInfo::claim_reward())]
        #[transactional]
        pub fn claim_reward(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            for asset_id in Markets::<T>::iter_keys() {
                Self::collect_market_reward(asset_id, &who)?;
            }

            Self::pay_reward(&who)?;

            Ok(().into())
        }

        /// Claim inceitve reward for the specified market.
        ///
        /// - `asset_id`: Market related currency
        #[pallet::call_index(8)]
        #[pallet::weight(<T as Config>::WeightInfo::claim_reward_for_market())]
        #[transactional]
        pub fn claim_reward_for_market(origin: OriginFor<T>, asset_id: CurrencyId<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            Self::collect_market_reward(asset_id, &who)?;

            Self::pay_reward(&who)?;

            Ok(().into())
        }

        /// The caller supplies (lends) assets into the market and receives a corresponding amount
        /// of lend tokens, at the current internal exchange rate.
        ///
        /// - `asset_id`: the asset to be deposited.
        /// - `mint_amount`: the amount to be deposited.
        #[pallet::call_index(9)]
        #[pallet::weight(<T as Config>::WeightInfo::mint())]
        #[transactional]
        pub fn mint(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            #[pallet::compact] mint_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            ensure!(!mint_amount.is_zero(), Error::<T>::InvalidAmount);
            Self::do_mint(&who, &Amount::new(mint_amount, asset_id))?;

            Ok(().into())
        }

        /// The caller redeems lend tokens for the underlying asset, at the current
        /// internal exchange rate.
        ///
        /// - `asset_id`: the asset to be redeemed
        /// - `redeem_amount`: the amount to be redeemed, expressed in the underyling currency (`asset_id`)
        #[pallet::call_index(10)]
        #[pallet::weight(<T as Config>::WeightInfo::redeem())]
        #[transactional]
        pub fn redeem(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            #[pallet::compact] redeem_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            ensure!(!redeem_amount.is_zero(), Error::<T>::InvalidAmount);

            let underlying = Amount::<T>::new(redeem_amount, asset_id);
            let voucher = underlying.to_lend_token()?;

            Self::do_redeem(&who, &underlying, &voucher)?;

            Ok(().into())
        }

        /// The caller redeems their entire lend token balance in exchange for the underlying asset.
        /// Note: this will fail if the account needs some of the collateral for backing open borrows,
        /// or if any of the lend tokens are used by other pallets (e.g. used as vault collateral)
        ///
        /// - `asset_id`: the asset to be redeemed.
        #[pallet::call_index(11)]
        #[pallet::weight(<T as Config>::WeightInfo::redeem_all())]
        #[transactional]
        pub fn redeem_all(origin: OriginFor<T>, asset_id: CurrencyId<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin.clone())?;

            let voucher = Self::balance(Self::lend_token_id(asset_id)?, &who);
            let underlying = &voucher.to_underlying()?;

            // note: we use total balance rather than underlying.to_lend_token, s.t. the account
            // is left neatly with 0 balance
            Self::do_redeem(&who, &underlying, &voucher)?;

            Ok(().into())
        }

        /// The caller borrows `borrow_amount` of `asset_id` from the protocol, using their
        /// supplied assets as collateral.
        ///
        /// - `asset_id`: the asset to be borrowed.
        /// - `borrow_amount`: the amount to be borrowed.
        #[pallet::call_index(12)]
        #[pallet::weight(<T as Config>::WeightInfo::borrow())]
        #[transactional]
        pub fn borrow(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            #[pallet::compact] borrow_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            ensure!(!borrow_amount.is_zero(), Error::<T>::InvalidAmount);
            Self::do_borrow(&who, &Amount::new(borrow_amount, asset_id))?;

            Ok(().into())
        }

        /// The caller repays some of their debts.
        ///
        /// - `asset_id`: the asset to be repaid.
        /// - `repay_amount`: the amount to be repaid, in the underlying currency (`asset_id`).
        #[pallet::call_index(13)]
        #[pallet::weight(<T as Config>::WeightInfo::repay_borrow())]
        #[transactional]
        pub fn repay_borrow(
            origin: OriginFor<T>,
            asset_id: CurrencyId<T>,
            #[pallet::compact] repay_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            ensure!(!repay_amount.is_zero(), Error::<T>::InvalidAmount);
            Self::do_repay_borrow(&who, &Amount::new(repay_amount, asset_id))?;

            Ok(().into())
        }

        /// The caller repays all of their debts.
        ///
        /// - `asset_id`: the asset to be repaid.
        #[pallet::call_index(14)]
        #[pallet::weight(<T as Config>::WeightInfo::repay_borrow_all())]
        #[transactional]
        pub fn repay_borrow_all(origin: OriginFor<T>, asset_id: CurrencyId<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            Self::ensure_active_market(asset_id)?;
            Self::accrue_interest(asset_id)?;
            let account_borrows = Self::current_borrow_balance(&who, asset_id)?;
            ensure!(!account_borrows.is_zero(), Error::<T>::InvalidAmount);
            Self::do_repay_borrow(&who, &account_borrows)?;

            Ok(().into())
        }

        /// Caller enables their lend token balance as borrow collateral. This operation locks
        /// the lend tokens, so they are no longer transferrable.
        /// Any incoming lend tokens into the caller's account (either by direct transfer or minting)
        /// are automatically locked as well, such that locking and unlocking borrow collateral is
        /// an atomic state (a "collateral toggle").
        /// If any of the caller's lend token balance is locked elsewhere (for instance, as bridge vault
        /// collateral), this operation will fail.
        /// If this operation is successful, the caller's maximum allowed debt increases.
        ///
        /// - `asset_id`: the underlying asset denoting the market whose lend tokens are to be
        /// enabled as collateral.
        #[pallet::call_index(15)]
        #[pallet::weight(<T as Config>::WeightInfo::deposit_all_collateral())]
        #[transactional]
        pub fn deposit_all_collateral(origin: OriginFor<T>, asset_id: CurrencyId<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            let free_lend_tokens = Self::free_lend_tokens(asset_id, &who)?;
            ensure!(!free_lend_tokens.is_zero(), Error::<T>::DepositAllCollateralFailed);
            let reserved_lend_tokens = Self::reserved_lend_tokens(asset_id, &who)?;
            // This check could fail if `withdraw_all_collateral()` leaves leftover lend_tokens locked.
            // However the current implementation is guaranteed to withdraw everything.
            ensure!(reserved_lend_tokens.is_zero(), Error::<T>::TokensAlreadyLocked);
            Self::do_deposit_collateral(&who, &free_lend_tokens)?;
            Ok(().into())
        }

        /// Caller disables their lend token balance as borrow collateral. This operation unlocks
        /// the lend tokens, so they become transferrable.
        /// This operation can only succeed if the caller's debt is backed by sufficient collateral
        /// excluding this currency.
        ///
        /// - `asset_id`: the underlying asset denoting the market whose lend tokens are to be
        /// disabled as collateral.
        #[pallet::call_index(16)]
        #[pallet::weight(<T as Config>::WeightInfo::withdraw_all_collateral())]
        #[transactional]
        pub fn withdraw_all_collateral(origin: OriginFor<T>, asset_id: CurrencyId<T>) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;

            let lend_token_id = Self::lend_token_id(asset_id)?;
            let collateral = Self::account_deposits(lend_token_id, &who.clone());
            ensure!(!collateral.is_zero(), Error::<T>::WithdrawAllCollateralFailed);
            Self::do_withdraw_collateral(&who, &collateral)?;
            Ok(().into())
        }

        /// The caller liquidates the borrower's collateral. This extrinsic may need to be called multiple
        /// times to completely clear the borrower's bad debt, because of the `close_factor` parameter in
        /// the market. See the `close_factor_may_require_multiple_liquidations_to_clear_bad_debt` unit
        /// test for an example of this.
        ///
        /// - `borrower`: the borrower to be liquidated.
        /// - `liquidation_asset_id`: the underlying asset to be liquidated.
        /// - `repay_amount`: the amount of `liquidation_asset_id` to be repaid. This parameter can only
        /// be as large as the `close_factor` market parameter allows
        /// (`close_factor * borrower_debt_in_liquidation_asset`).
        /// - `collateral_asset_id`: The underlying currency whose lend tokens to seize from the borrower.
        /// Note that the liquidator has to redeem the received lend tokens from the market to convert them
        /// to `collateral_asset_id`.
        #[pallet::call_index(17)]
        #[pallet::weight(<T as Config>::WeightInfo::liquidate_borrow())]
        #[transactional]
        pub fn liquidate_borrow(
            origin: OriginFor<T>,
            borrower: T::AccountId,
            liquidation_asset_id: CurrencyId<T>,
            #[pallet::compact] repay_amount: BalanceOf<T>,
            collateral_asset_id: CurrencyId<T>,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            Self::accrue_interest(liquidation_asset_id)?;
            Self::accrue_interest(collateral_asset_id)?;
            ensure!(!repay_amount.is_zero(), Error::<T>::InvalidAmount);
            let liquidation = Amount::new(repay_amount, liquidation_asset_id);
            Self::do_liquidate_borrow(who, borrower, &liquidation, collateral_asset_id)?;
            Ok(().into())
        }

        /// Add reserves by transferring from payer.
        /// TODO: This extrinsic currently does nothing useful. See the TODO comment
        /// of the `ensure_enough_cash` function for more details. Based on that
        /// TODO, decide whether this extrinsic should be kept.
        ///
        /// May only be called from `T::ReserveOrigin`.
        ///
        /// - `payer`: the payer account.
        /// - `asset_id`: the assets to be added.
        /// - `add_amount`: the amount to be added.
        #[pallet::call_index(18)]
        #[pallet::weight(<T as Config>::WeightInfo::add_reserves())]
        #[transactional]
        pub fn add_reserves(
            origin: OriginFor<T>,
            payer: <T::Lookup as StaticLookup>::Source,
            asset_id: CurrencyId<T>,
            #[pallet::compact] add_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            let amount_to_transfer = Amount::new(add_amount, asset_id);
            T::ReserveOrigin::ensure_origin(origin)?;
            let payer = T::Lookup::lookup(payer)?;
            Self::ensure_active_market(asset_id)?;
            Self::accrue_interest(asset_id)?;

            ensure!(!amount_to_transfer.is_zero(), Error::<T>::InvalidAmount);
            amount_to_transfer.transfer(&payer, &Self::account_id())?;
            let total_reserves = Self::total_reserves(asset_id);
            let total_reserves_new = total_reserves.checked_add(&amount_to_transfer)?;
            TotalReserves::<T>::insert(asset_id, total_reserves_new.amount());

            Self::deposit_event(Event::<T>::ReservesAdded {
                payer,
                currency_id: asset_id,
                amount: amount_to_transfer.amount(),
                new_reserve_amount: total_reserves_new.amount(),
            });

            Ok(().into())
        }

        /// Reduces reserves (treasury's share of accrued interest) by transferring to receiver.
        ///
        /// May only be called from `T::ReserveOrigin`.
        ///
        /// - `receiver`: the receiver account.
        /// - `asset_id`: the assets to be reduced.
        /// - `reduce_amount`: the amount to be reduced.
        #[pallet::call_index(19)]
        #[pallet::weight(<T as Config>::WeightInfo::reduce_reserves())]
        #[transactional]
        pub fn reduce_reserves(
            origin: OriginFor<T>,
            receiver: <T::Lookup as StaticLookup>::Source,
            asset_id: CurrencyId<T>,
            #[pallet::compact] reduce_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            T::ReserveOrigin::ensure_origin(origin)?;
            let receiver = T::Lookup::lookup(receiver)?;
            Self::ensure_active_market(asset_id)?;
            Self::accrue_interest(asset_id)?;

            let amount_to_transfer = Amount::new(reduce_amount, asset_id);

            ensure!(!amount_to_transfer.is_zero(), Error::<T>::InvalidAmount);
            let total_reserves = Self::total_reserves(asset_id);
            if amount_to_transfer.gt(&total_reserves)? {
                return Err(Error::<T>::InsufficientReserves.into());
            }
            let total_reserves_new = total_reserves.checked_sub(&amount_to_transfer)?;
            TotalReserves::<T>::insert(asset_id, total_reserves_new.amount());

            amount_to_transfer.transfer(&Self::account_id(), &receiver)?;

            Self::deposit_event(Event::<T>::ReservesReduced {
                receiver,
                currency_id: asset_id,
                amount: amount_to_transfer.amount(),
                new_reserve_amount: total_reserves_new.amount(),
            });

            Ok(().into())
        }

        /// Sender redeems some of internal supplies in exchange for the underlying asset.
        ///
        /// - `asset_id`: the asset to be redeemed.
        /// - `redeem_amount`: the amount to be redeemed.
        #[pallet::call_index(20)]
        #[pallet::weight(<T as Config>::WeightInfo::reduce_incentive_reserves())]
        #[transactional]
        pub fn reduce_incentive_reserves(
            origin: OriginFor<T>,
            receiver: <T::Lookup as StaticLookup>::Source,
            asset_id: CurrencyId<T>,
            #[pallet::compact] redeem_amount: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            T::ReserveOrigin::ensure_origin(origin)?;
            ensure!(!redeem_amount.is_zero(), Error::<T>::InvalidAmount);
            let receiver = T::Lookup::lookup(receiver)?;
            let from = Self::incentive_reward_account_id();
            Self::ensure_active_market(asset_id)?;
            Self::accrue_interest(asset_id)?;

            let underlying = Amount::new(redeem_amount, asset_id);

            Self::do_redeem(&from, &underlying, &underlying.to_lend_token()?)?;

            underlying.transfer(&from, &receiver)?;

            Self::deposit_event(Event::<T>::IncentiveReservesReduced {
                receiver,
                currency_id: asset_id,
                amount: underlying.amount(),
            });
            Ok(().into())
        }
    }
}

impl<T: Config> Pallet<T> {
    #[cfg_attr(any(test, feature = "integration-tests"), visibility::make(pub))]
    fn account_deposits(lend_token_id: CurrencyId<T>, supplier: &T::AccountId) -> Amount<T> {
        Amount::new(AccountDeposits::<T>::get(lend_token_id, supplier), lend_token_id)
    }

    #[cfg_attr(any(test, feature = "integration-tests"), visibility::make(pub))]
    fn total_borrows(asset_id: CurrencyId<T>) -> Amount<T> {
        Amount::new(TotalBorrows::<T>::get(asset_id), asset_id)
    }

    #[cfg_attr(any(test, feature = "integration-tests"), visibility::make(pub))]
    fn total_reserves(asset_id: CurrencyId<T>) -> Amount<T> {
        Amount::new(TotalReserves::<T>::get(asset_id), asset_id)
    }

    pub fn account_id() -> T::AccountId {
        T::PalletId::get().into_account_truncating()
    }

    pub fn get_account_liquidity(account: &T::AccountId) -> Result<AccountLiquidity<T>, DispatchError> {
        let total_collateral_value = Self::total_collateral_value(account)?;
        let total_borrow_value = Self::total_borrowed_value(account)?;
        log::trace!(
            target: "loans::get_account_liquidity",
            "account: {:?}, total_borrow_value: {:?}, total_collateral_value: {:?}",
            account,
            total_borrow_value.amount(),
            total_collateral_value.amount(),
        );
        AccountLiquidity::from_collateral_and_debt(total_collateral_value, total_borrow_value)
    }

    pub fn get_account_liquidation_threshold_liquidity(
        account: &T::AccountId,
    ) -> Result<AccountLiquidity<T>, DispatchError> {
        let total_collateral_value = Self::total_liquidation_threshold_value(account)?;
        let total_borrow_value = Self::total_borrowed_value(account)?;
        log::trace!(
            target: "loans::get_account_liquidation_threshold_liquidity",
            "account: {:?}, total_borrow_value: {:?}, total_collateral_value: {:?}",
            account,
            total_borrow_value.amount(),
            total_collateral_value.amount(),
        );
        AccountLiquidity::from_collateral_and_debt(total_collateral_value, total_borrow_value)
    }

    fn total_borrowed_value(borrower: &T::AccountId) -> Result<Amount<T>, DispatchError> {
        let mut total_borrow_value = Amount::<T>::zero(T::ReferenceAssetId::get());
        for (asset_id, _) in Self::active_markets() {
            let borrow = Self::current_borrow_balance(borrower, asset_id)?;
            if borrow.is_zero() {
                continue;
            }
            let value = Self::get_asset_value(&borrow)?;
            total_borrow_value.checked_accrue(&value)?;
        }

        Ok(total_borrow_value)
    }

    fn collateral_amount_value(voucher: &Amount<T>) -> Result<Amount<T>, DispatchError> {
        let underlying = voucher.to_underlying()?;
        let market = Self::market(underlying.currency())?;
        let effects = underlying.map(|x| market.collateral_factor.mul_ceil(x));

        Self::get_asset_value(&effects)
    }

    fn collateral_asset_value(supplier: &T::AccountId, asset_id: CurrencyId<T>) -> Result<Amount<T>, DispatchError> {
        let lend_token_id = Self::lend_token_id(asset_id)?;
        let deposits = Self::account_deposits(lend_token_id, supplier);
        if deposits.is_zero() {
            return Ok(Amount::<T>::zero(T::ReferenceAssetId::get()));
        }
        Self::collateral_amount_value(&deposits)
    }

    fn liquidation_threshold_asset_value(
        borrower: &T::AccountId,
        asset_id: CurrencyId<T>,
    ) -> Result<Amount<T>, DispatchError> {
        let lend_token_id = Self::lend_token_id(asset_id)?;
        if !AccountDeposits::<T>::contains_key(lend_token_id, borrower) {
            return Ok(Amount::<T>::zero(T::ReferenceAssetId::get()));
        }
        let deposits = Self::account_deposits(lend_token_id, borrower);
        if deposits.is_zero() {
            return Ok(Amount::<T>::zero(T::ReferenceAssetId::get()));
        }
        let underlying_amount = deposits.to_underlying()?;
        let market = Self::market(asset_id)?;
        let effects_amount = underlying_amount.map(|x| market.liquidation_threshold.mul_ceil(x));

        Self::get_asset_value(&effects_amount)
    }

    fn total_collateral_value(supplier: &T::AccountId) -> Result<Amount<T>, DispatchError> {
        let mut total_asset_value = Amount::<T>::zero(T::ReferenceAssetId::get());
        for (asset_id, _market) in Self::active_markets() {
            total_asset_value = total_asset_value.checked_add(&Self::collateral_asset_value(supplier, asset_id)?)?;
        }

        Ok(total_asset_value)
    }

    fn total_liquidation_threshold_value(borrower: &T::AccountId) -> Result<Amount<T>, DispatchError> {
        let mut total_asset_value = Amount::<T>::zero(T::ReferenceAssetId::get());
        for (asset_id, _market) in Self::active_markets() {
            total_asset_value =
                total_asset_value.checked_add(&Self::liquidation_threshold_asset_value(borrower, asset_id)?)?;
        }

        Ok(total_asset_value)
    }

    /// Checks if the redeemer should be allowed to redeem tokens in given market.
    /// Takes into account both `free` and `locked` (i.e. deposited as collateral) lend_tokens of the redeemer.
    fn redeem_allowed(redeemer: &T::AccountId, voucher: &Amount<T>) -> DispatchResult {
        let asset_id = Self::underlying_id(voucher.currency())?;
        log::trace!(
            target: "loans::redeem_allowed",
            "asset_id: {:?}, redeemer: {:?}, voucher_amount: {:?}",
            asset_id,
            redeemer,
            voucher.amount(),
        );

        ensure!(!voucher.is_zero(), Error::<T>::InvalidAmount);

        if Self::balance(voucher.currency(), redeemer).lt(&voucher)? {
            return Err(Error::<T>::InsufficientDeposit.into());
        }

        // Ensure there is enough cash in the market
        let redeem_amount = voucher.to_underlying()?;
        Self::ensure_enough_cash(&redeem_amount)?;

        // Only free tokens are redeemable. If the account has enough liquidity, the lend tokens
        // must first be withdrawn from collateral (this happens automatically in the `redeem` and
        // `redeem_all` extrinsics)
        if voucher.gt(&Self::free_lend_tokens(asset_id, redeemer)?)? {
            return Err(Error::<T>::LockedTokensCannotBeRedeemed.into());
        }
        Ok(())
    }

    /// Borrower shouldn't borrow more than their total collateral value allows
    fn borrow_allowed(borrower: &T::AccountId, borrow: &Amount<T>) -> DispatchResult {
        Self::ensure_under_borrow_cap(borrow)?;
        Self::ensure_enough_cash(borrow)?;
        let borrow_value = Self::get_asset_value(borrow)?;
        Self::ensure_liquidity(borrower, borrow_value)?;

        Ok(())
    }

    #[require_transactional]
    fn do_repay_borrow_with_amount(
        borrower: &T::AccountId,
        asset_id: CurrencyId<T>,
        account_borrows: &Amount<T>,
        repay_amount: &Amount<T>,
    ) -> DispatchResult {
        if account_borrows.lt(repay_amount)? {
            return Err(Error::<T>::TooMuchRepay.into());
        }
        Self::update_reward_borrow_index(asset_id)?;
        Self::distribute_borrower_reward(asset_id, borrower)?;

        repay_amount.transfer(borrower, &Self::account_id())?;

        let account_borrows_new = account_borrows.checked_sub(&repay_amount)?;
        let total_borrows = Self::total_borrows(asset_id);
        // Use `saturating_sub` here, because it's intended for `total_borrows` to be rounded down,
        // such that it is less than or equal to the actual borrower debt.
        let total_borrows_new = total_borrows.saturating_sub(&repay_amount)?;
        AccountBorrows::<T>::insert(
            asset_id,
            borrower,
            BorrowSnapshot {
                principal: account_borrows_new.amount(),
                borrow_index: Self::borrow_index(asset_id),
            },
        );
        TotalBorrows::<T>::insert(asset_id, total_borrows_new.amount());

        Ok(())
    }

    // Calculates and returns the most recent amount of borrowed balance of `currency_id`
    // for `who`.
    pub fn current_borrow_balance(who: &T::AccountId, asset_id: CurrencyId<T>) -> Result<Amount<T>, DispatchError> {
        let snapshot: BorrowSnapshot<BalanceOf<T>> = Self::account_borrows(asset_id, who);
        if snapshot.principal.is_zero() || snapshot.borrow_index.is_zero() {
            return Ok(Amount::zero(asset_id));
        }
        let principal_amount = Amount::<T>::new(snapshot.principal, asset_id);
        // Round borrower debt up to avoid interest-free loans
        Self::borrow_balance_from_old_and_new_index(
            &snapshot.borrow_index,
            &Self::borrow_index(asset_id),
            principal_amount,
            Rounding::Up,
        )
    }

    pub fn borrow_balance_from_old_and_new_index(
        old_index: &FixedU128,
        new_index: &FixedU128,
        amount: Amount<T>,
        rounding: Rounding,
    ) -> Result<Amount<T>, DispatchError> {
        // Calculate new borrow balance using the interest index:
        // recent_borrow_balance = snapshot.principal * borrow_index / snapshot.borrow_index
        let borrow_index_increase = new_index.checked_div(&old_index).ok_or(ArithmeticError::Underflow)?;
        amount.checked_rounded_mul(&borrow_index_increase, rounding)
    }

    /// Checks if the liquidation should be allowed to occur
    fn liquidate_borrow_allowed(
        borrower: &T::AccountId,
        underlying: &Amount<T>,
        market: &Market<BalanceOf<T>>,
    ) -> DispatchResult {
        log::trace!(
            target: "loans::liquidate_borrow_allowed",
            "borrower: {:?}, liquidation_asset_id {:?}, repay_amount {:?}, market: {:?}",
            borrower,
            underlying.currency(),
            underlying.amount(),
            market
        );
        // The account's shortfall, as calculated using the liquidation threshold, should be non-zero
        if Self::get_account_liquidation_threshold_liquidity(borrower)?
            .shortfall()
            .is_zero()
        {
            return Err(Error::<T>::InsufficientShortfall.into());
        }

        // The liquidator may not repay more than 50% (close_factor) of the borrower's borrow balance.
        let account_borrows = Self::current_borrow_balance(borrower, underlying.currency())?;
        let account_borrows_value = Self::get_asset_value(&account_borrows)?;
        let repay_value = Self::get_asset_value(&underlying)?;

        if account_borrows_value
            .map(|x| market.close_factor.mul_ceil(x))
            .lt(&repay_value)?
        {
            return Err(Error::<T>::TooMuchRepay.into());
        }

        Ok(())
    }

    /// Note:
    /// - `liquidation_asset_id` is borrower's debt asset.
    /// - `collateral_asset_id` is borrower's collateral asset.
    /// - `repay_amount` is amount of liquidation_asset_id
    ///
    /// The liquidator will repay a certain amount of liquidation_asset_id from own
    /// account for borrower. Then the protocol will reduce borrower's debt
    /// and liquidator will receive collateral_asset_id (as voucher amount) from
    /// borrower.
    #[require_transactional]
    pub fn do_liquidate_borrow(
        liquidator: T::AccountId,
        borrower: T::AccountId,
        repayment_underlying: &Amount<T>,
        collateral_asset_id: CurrencyId<T>,
    ) -> DispatchResult {
        let liquidation_asset_id = repayment_underlying.currency();
        Self::ensure_active_market(liquidation_asset_id)?;
        Self::ensure_active_market(collateral_asset_id)?;

        let market = Self::market(liquidation_asset_id)?;

        if borrower == liquidator {
            return Err(Error::<T>::LiquidatorIsBorrower.into());
        }
        Self::liquidate_borrow_allowed(&borrower, repayment_underlying, &market)?;

        let lend_token_id = Self::lend_token_id(collateral_asset_id)?;
        let deposits = Self::account_deposits(lend_token_id, &borrower);
        ensure!(!deposits.is_zero(), Error::<T>::DepositsAreNotCollateral);
        let borrower_deposits = deposits.to_underlying()?;

        let collateral_value = Self::get_asset_value(&borrower_deposits)?;
        // liquidate_value includes the premium of the liquidator
        let liquidate_value = Self::get_asset_value(repayment_underlying)?.checked_mul(&market.liquidate_incentive)?;
        if collateral_value.lt(&liquidate_value)? {
            return Err(Error::<T>::InsufficientCollateral.into());
        }

        // Calculate the collateral amount to seize from the borrower
        let real_collateral_underlying_amount = liquidate_value.convert_to(collateral_asset_id)?;
        Self::liquidated_transfer(
            &liquidator,
            &borrower,
            &repayment_underlying,
            &real_collateral_underlying_amount,
            &market,
        )?;

        Ok(())
    }

    #[require_transactional]
    fn liquidated_transfer(
        liquidator: &T::AccountId,
        borrower: &T::AccountId,
        repayment: &Amount<T>,
        collateral_underlying: &Amount<T>,
        market: &Market<BalanceOf<T>>,
    ) -> DispatchResult {
        let liquidation_asset_id = repayment.currency();
        let collateral_asset_id = collateral_underlying.currency();

        log::error!(
            target: "loans::liquidated_transfer",
            "liquidator: {:?}, borrower: {:?}, liquidation_asset_id: {:?},
                collateral_asset_id: {:?}, repay_amount: {:?}, collateral_underlying.amount(): {:?}",
            liquidator,
            borrower,
            repayment.currency(),
            collateral_underlying.currency(),
            repayment.amount(),
            collateral_underlying.amount()
        );

        // update borrow index after accrue interest.
        Self::update_reward_borrow_index(liquidation_asset_id)?;
        Self::distribute_borrower_reward(liquidation_asset_id, liquidator)?;

        // 1.liquidator repays borrower's debt,
        // transfer from liquidator to module account
        repayment.transfer(liquidator, &Self::account_id())?;

        // 2.the system reduces borrower's debt
        let account_borrows_new =
            Self::current_borrow_balance(borrower, liquidation_asset_id)?.checked_sub(&repayment)?;
        let total_borrows_new = Self::total_borrows(liquidation_asset_id).checked_sub(&repayment)?;
        AccountBorrows::<T>::insert(
            liquidation_asset_id,
            borrower,
            BorrowSnapshot {
                principal: account_borrows_new.amount(),
                borrow_index: Self::borrow_index(liquidation_asset_id),
            },
        );
        TotalBorrows::<T>::insert(liquidation_asset_id, total_borrows_new.amount());

        // update supply index before modify supply balance.
        Self::update_reward_supply_index(collateral_asset_id)?;
        Self::distribute_supplier_reward(collateral_asset_id, liquidator)?;
        Self::distribute_supplier_reward(collateral_asset_id, borrower)?;
        Self::distribute_supplier_reward(collateral_asset_id, &Self::incentive_reward_account_id())?;

        // 3.the liquidator will receive voucher token from borrower
        let lend_token_id = Self::lend_token_id(collateral_asset_id)?;
        let amount_to_liquidate = collateral_underlying.to_lend_token()?;
        // Decrease the amount of collateral the borrower deposited
        AccountDeposits::<T>::try_mutate_exists(lend_token_id, borrower, |deposits| -> DispatchResult {
            let d = deposits
                .unwrap_or_default()
                .checked_sub(amount_to_liquidate.amount())
                .ok_or(ArithmeticError::Underflow)?;
            if d.is_zero() {
                // remove deposits storage if zero balance
                *deposits = None;
            } else {
                *deposits = Some(d);
            }
            Ok(())
        })?;
        // Unlock this balance to make it transferrable
        amount_to_liquidate.unlock_on(borrower)?;

        let incentive_reserved = amount_to_liquidate
            .checked_div(&market.liquidate_incentive)?
            .mul_ratio_floor(market.liquidate_incentive_reserved_factor);

        // increase liquidator's voucher_balance
        let liquidator_amount = amount_to_liquidate.checked_sub(&incentive_reserved)?;
        liquidator_amount.transfer(borrower, liquidator)?;

        // increase reserve's voucher_balance
        incentive_reserved.transfer(borrower, &Self::incentive_reward_account_id())?;

        Self::deposit_event(Event::<T>::LiquidatedBorrow {
            liquidator: liquidator.clone(),
            borrower: borrower.clone(),
            liquidation_currency_id: liquidation_asset_id,
            collateral_currency_id: collateral_asset_id,
            repay_amount: repayment.amount(),
            collateral_underlying_amount: collateral_underlying.amount(),
        });

        Ok(())
    }

    pub fn lock_if_account_deposited(account_id: &T::AccountId, lend_tokens: &Amount<T>) -> DispatchResult {
        // if the receiver already has their collateral deposited
        let deposit = Pallet::<T>::account_deposits(lend_tokens.currency(), account_id);
        if !deposit.is_zero() {
            // then any incoming `lend_tokens` must automatically be deposited as collateral
            // to enforce the "collateral toggle"
            Self::do_deposit_collateral(account_id, &lend_tokens)?;
        }
        Ok(())
    }

    // Ensures a given `asset_id` is an active market.
    fn ensure_active_market(asset_id: CurrencyId<T>) -> Result<Market<BalanceOf<T>>, DispatchError> {
        Self::active_markets()
            .find(|(id, _)| id == &asset_id)
            .map(|(_, market)| market)
            .ok_or_else(|| Error::<T>::MarketNotActivated.into())
    }

    /// Ensure supplying `amount` asset does not exceed the market's supply cap.
    fn ensure_under_supply_cap(asset: &Amount<T>) -> DispatchResult {
        let asset_id = asset.currency();

        let market = Self::market(asset_id)?;
        // Assets holded by market currently.
        let current_cash = Self::balance(asset_id, &Self::account_id());
        let total_cash = current_cash.checked_add(&asset)?;
        ensure!(
            total_cash.amount() <= market.supply_cap,
            Error::<T>::SupplyCapacityExceeded
        );

        Ok(())
    }

    /// Ensure borrowing `amount` asset does not exceed the market's borrow cap.
    fn ensure_under_borrow_cap(asset: &Amount<T>) -> DispatchResult {
        let asset_id = asset.currency();
        let market = Self::market(asset_id)?;
        let total_borrows = Self::total_borrows(asset_id);
        let new_total_borrows = total_borrows.checked_add(&asset)?;
        ensure!(
            new_total_borrows.amount() <= market.borrow_cap,
            Error::<T>::BorrowCapacityExceeded
        );

        Ok(())
    }

    /// Make sure there is enough cash available in the pool
    /// TODO: Compared to Compound's implementation, it seems like
    /// this function should not subtract the total reserves from the total cash,
    /// possibly to allow the treasury to deposit liquidity and enable users to exit
    /// their lend token positions in case of 100% utilization in the market.
    /// Decide if this function needs to be modified as such, or remove the `add_reserves`
    /// extrinsic since it currently does nothing useful.
    ///
    /// See the redeem check in Compound V2 (also the borrow check):
    /// - `getCashPrior() > redeemAmount`:
    /// https://github.com/compound-finance/compound-protocol/blob/a3214f67b73310d547e00fc578e8355911c9d376/contracts/CToken.sol#L518
    /// - but getCashPrior is the entire balance of the contract:
    /// https://github.com/compound-finance/compound-protocol/blob/a3214f67b73310d547e00fc578e8355911c9d376/contracts/CToken.sol#L1125
    fn ensure_enough_cash(amount: &Amount<T>) -> DispatchResult {
        let reducible_cash =
            Self::get_total_cash(amount.currency()).checked_sub(&Self::total_reserves(amount.currency()))?;
        if reducible_cash.lt(&amount)? {
            return Err(Error::<T>::InsufficientCash.into());
        }

        Ok(())
    }

    /// Ensures a given `lend_token_id` is unique in `Markets` and `UnderlyingAssetId`.
    fn ensure_lend_token(lend_token_id: CurrencyId<T>) -> DispatchResult {
        // The lend_token id is unique, cannot be repeated
        ensure!(
            !UnderlyingAssetId::<T>::contains_key(lend_token_id),
            Error::<T>::InvalidLendTokenId
        );

        // The lend_token id should not be the same as the id of any asset in markets
        ensure!(
            !Markets::<T>::contains_key(lend_token_id),
            Error::<T>::InvalidLendTokenId
        );

        Ok(())
    }

    /// Ensures that `account` has sufficient liquidity to cover a withdrawal
    /// Returns `Err` If InsufficientLiquidity
    /// `account`: account that needs a liquidity check
    /// `reduce_amount`: amount to reduce the liquidity (collateral) of the `account` by
    fn ensure_liquidity(account: &T::AccountId, reduce_amount: Amount<T>) -> DispatchResult {
        if Self::get_account_liquidity(account)?.liquidity().ge(&reduce_amount)? {
            return Ok(());
        }
        Err(Error::<T>::InsufficientLiquidity.into())
    }

    /// Transferrable balance in the pallet account (`free - frozen`)
    fn get_total_cash(asset_id: CurrencyId<T>) -> Amount<T> {
        // for the reducible_balance arguments, see:
        // - https://docs.rs/frame-support/latest/frame_support/traits/tokens/enum.Preservation.html
        // - https://docs.rs/frame-support/latest/frame_support/traits/tokens/enum.Fortitude.html
        Amount::new(
            orml_tokens::Pallet::<T>::reducible_balance(
                asset_id,
                &Self::account_id(),
                Preservation::Expendable, // We don’t care if the account gets killed by this operation.
                Fortitude::Polite,        // "The operation should execute with regular privilege", i.e. not forced
            ),
            asset_id,
        )
    }

    /// Get the total balance of `who`.
    /// Ignores any frozen balance of this account (`free + reserved`)
    fn balance(asset_id: CurrencyId<T>, who: &T::AccountId) -> Amount<T> {
        let balance = <orml_tokens::Pallet<T> as MultiCurrency<T::AccountId>>::total_balance(asset_id, who);
        Amount::new(balance, asset_id)
    }

    /// Total issuance of lending tokens (lend_tokens), given the underlying
    pub fn total_supply(asset_id: CurrencyId<T>) -> Result<Amount<T>, DispatchError> {
        let lend_token_id = Self::lend_token_id(asset_id)?;
        let issuance = orml_tokens::Pallet::<T>::total_issuance(lend_token_id);
        Ok(Amount::new(issuance, lend_token_id))
    }

    /// Free lending tokens (lend_tokens) of an account, given the underlying
    pub fn free_lend_tokens(asset_id: CurrencyId<T>, account_id: &T::AccountId) -> Result<Amount<T>, DispatchError> {
        let lend_token_id = Self::lend_token_id(asset_id)?;
        let amount = Amount::new(
            orml_tokens::Pallet::<T>::free_balance(lend_token_id, account_id),
            lend_token_id,
        );
        Ok(amount)
    }

    /// Reserved lending tokens (lend_tokens) of an account, given the underlying
    pub fn reserved_lend_tokens(
        asset_id: CurrencyId<T>,
        account_id: &T::AccountId,
    ) -> Result<Amount<T>, DispatchError> {
        let lend_token_id = Self::lend_token_id(asset_id)?;
        let amount = Amount::new(
            orml_tokens::Pallet::<T>::reserved_balance(lend_token_id, account_id),
            lend_token_id,
        );
        Ok(amount)
    }

    // Returns the value of the asset, in the reference currency.
    // Returns `Err` if oracle price not ready or arithmetic error.
    pub fn get_asset_value(asset: &Amount<T>) -> Result<Amount<T>, DispatchError> {
        asset.convert_to(T::ReferenceAssetId::get())
    }

    // Returns a stored Market.
    //
    // Returns `Err` if market does not exist.
    pub fn market(asset_id: CurrencyId<T>) -> Result<Market<BalanceOf<T>>, DispatchError> {
        Markets::<T>::try_get(asset_id).map_err(|_err| Error::<T>::MarketDoesNotExist.into())
    }

    // Mutates a stored Market.
    //
    // Returns `Err` if market does not exist.
    pub(crate) fn mutate_market<F>(asset_id: CurrencyId<T>, cb: F) -> Result<Market<BalanceOf<T>>, DispatchError>
    where
        F: FnOnce(&mut Market<BalanceOf<T>>) -> Result<Market<BalanceOf<T>>, DispatchError>,
    {
        Markets::<T>::try_mutate(asset_id, |opt| -> Result<Market<BalanceOf<T>>, DispatchError> {
            if let Some(market) = opt {
                return cb(market);
            }
            Err(Error::<T>::MarketDoesNotExist.into())
        })
    }

    // All markets that are `MarketStatus::Active`.
    fn active_markets() -> impl Iterator<Item = (CurrencyId<T>, Market<BalanceOf<T>>)> {
        Markets::<T>::iter().filter(|(_, market)| market.state == MarketState::Active)
    }

    // Returns the lend_token_id of the related asset
    //
    // Returns `Err` if market does not exist.
    pub fn lend_token_id(asset_id: CurrencyId<T>) -> Result<CurrencyId<T>, DispatchError> {
        if let Ok(market) = Self::market(asset_id) {
            Ok(market.lend_token_id)
        } else {
            Err(Error::<T>::MarketDoesNotExist.into())
        }
    }

    // Returns the incentive reward account
    pub fn incentive_reward_account_id() -> T::AccountId {
        T::PalletId::get().into_sub_account_truncating(INCENTIVE_SUB_ACCOUNT)
    }
}

impl<T: Config> LoansTrait<CurrencyId<T>, AccountIdOf<T>, Amount<T>> for Pallet<T> {
    fn do_mint(supplier: &AccountIdOf<T>, amount: &Amount<T>) -> Result<(), DispatchError> {
        let asset_id = amount.currency();
        Self::ensure_active_market(asset_id)?;
        Self::ensure_under_supply_cap(&amount)?;

        Self::accrue_interest(asset_id)?;

        // update supply index before modify supply balance.
        Self::update_reward_supply_index(asset_id)?;
        Self::distribute_supplier_reward(asset_id, supplier)?;

        let voucher = amount.to_lend_token()?;
        ensure!(!voucher.is_zero(), Error::<T>::InvalidExchangeRate);

        amount.transfer(supplier, &Self::account_id())?;

        voucher.mint_to(supplier)?;

        Self::deposit_event(Event::<T>::Deposited {
            account_id: supplier.clone(),
            currency_id: asset_id,
            amount: amount.amount(),
        });
        Ok(())
    }

    fn do_borrow(borrower: &AccountIdOf<T>, borrow: &Amount<T>) -> Result<(), DispatchError> {
        let asset_id = borrow.currency();
        Self::ensure_active_market(asset_id)?;

        Self::accrue_interest(asset_id)?;
        Self::borrow_allowed(borrower, &borrow)?;

        // update borrow index after accrue interest.
        Self::update_reward_borrow_index(asset_id)?;
        Self::distribute_borrower_reward(asset_id, borrower)?;

        let account_borrows = Self::current_borrow_balance(borrower, asset_id)?;
        let account_borrows_new = account_borrows.checked_add(borrow)?;
        let total_borrows = Self::total_borrows(asset_id);
        let total_borrows_new = total_borrows.checked_add(&borrow)?;
        AccountBorrows::<T>::insert(
            asset_id,
            borrower,
            BorrowSnapshot {
                principal: account_borrows_new.amount(),
                borrow_index: Self::borrow_index(asset_id),
            },
        );
        TotalBorrows::<T>::insert(asset_id, total_borrows_new.amount());
        borrow.transfer(&Self::account_id(), borrower)?;

        Self::deposit_event(Event::<T>::Borrowed {
            account_id: borrower.clone(),
            currency_id: asset_id,
            amount: borrow.amount(),
        });
        Ok(())
    }

    fn do_deposit_collateral(supplier: &AccountIdOf<T>, lend_token_amount: &Amount<T>) -> Result<(), DispatchError> {
        // If the given asset_id is not a valid lend_token, fetching the underlying will fail
        let underlying_id = Self::underlying_id(lend_token_amount.currency())?;
        Self::ensure_active_market(underlying_id)?;

        // Will fail if supplier has insufficient free tokens
        lend_token_amount.lock_on(supplier)?;

        // Increase the amount of collateral deposited
        let deposit = Self::account_deposits(lend_token_amount.currency(), supplier);
        let new_deposit = deposit.checked_add(&lend_token_amount)?;
        AccountDeposits::<T>::insert(lend_token_amount.currency(), supplier, new_deposit.amount());

        Self::deposit_event(Event::<T>::DepositCollateral {
            account_id: supplier.clone(),
            currency_id: lend_token_amount.currency(),
            amount: lend_token_amount.amount(),
        });
        Ok(())
    }

    fn do_withdraw_collateral(supplier: &AccountIdOf<T>, voucher: &Amount<T>) -> Result<(), DispatchError> {
        // If the given asset_id is not a valid lend_token, fetching the underlying will fail
        let underlying_id = Self::underlying_id(voucher.currency())?;
        Self::ensure_active_market(underlying_id)?;

        let total_collateral_value = Self::total_collateral_value(supplier)?;
        let collateral_amount_value = Self::collateral_amount_value(&voucher)?;
        let total_borrowed_value = Self::total_borrowed_value(supplier)?;
        log::trace!(
            target: "loans::collateral_asset",
            "total_collateral_value: {:?}, collateral_asset_value: {:?}, total_borrowed_value: {:?}",
            total_collateral_value.amount(),
            collateral_amount_value.amount(),
            total_borrowed_value.amount(),
        );

        if total_collateral_value.lt(&total_borrowed_value.checked_add(&collateral_amount_value)?)? {
            return Err(Error::<T>::InsufficientLiquidity.into());
        }

        voucher.unlock_on(supplier)?;

        // Decrease the amount of collateral deposited
        AccountDeposits::<T>::try_mutate_exists(voucher.currency(), supplier, |deposits| -> DispatchResult {
            let d = deposits
                .unwrap_or_default()
                .checked_sub(voucher.amount())
                .ok_or(ArithmeticError::Underflow)?;
            if d.is_zero() {
                // remove deposits storage if zero balance
                *deposits = None;
            } else {
                *deposits = Some(d);
            }
            Ok(())
        })?;

        Self::deposit_event(Event::<T>::WithdrawCollateral {
            account_id: supplier.clone(),
            currency_id: voucher.currency(),
            amount: voucher.amount(),
        });
        Ok(())
    }

    fn do_repay_borrow(borrower: &AccountIdOf<T>, borrow: &Amount<T>) -> Result<(), DispatchError> {
        let asset_id = borrow.currency();
        Self::ensure_active_market(asset_id)?;
        Self::accrue_interest(asset_id)?;
        let account_borrows = Self::current_borrow_balance(borrower, asset_id)?;
        Self::do_repay_borrow_with_amount(borrower, asset_id, &account_borrows, &borrow)?;
        Self::deposit_event(Event::<T>::RepaidBorrow {
            account_id: borrower.clone(),
            currency_id: asset_id,
            amount: borrow.amount(),
        });
        Ok(())
    }

    fn do_redeem(supplier: &AccountIdOf<T>, underlying: &Amount<T>, voucher: &Amount<T>) -> Result<(), DispatchError> {
        let asset_id = underlying.currency();

        // if the receiver has collateral locked
        if !Pallet::<T>::account_deposits(voucher.currency(), &supplier).is_zero() {
            // Withdraw the `lend_tokens` from the borrow collateral, so they are redeemable.
            // This assumes that a user cannot have both `free` and `locked` lend tokens at
            // the same time (for the purposes of lending and borrowing).
            Self::do_withdraw_collateral(&supplier, &voucher)?;
        }

        Self::ensure_active_market(asset_id)?;
        Self::accrue_interest(asset_id)?;

        Self::redeem_allowed(supplier, &voucher)?;

        Self::update_reward_supply_index(asset_id)?;
        Self::distribute_supplier_reward(asset_id, supplier)?;

        // Need to first `lock_on` in order to `burn_from` because:
        // 1) only the `free` lend_tokens are redeemable
        // 2) `burn_from` can only be called on locked tokens.
        voucher.lock_on(supplier)?;
        voucher.burn_from(supplier)?;

        underlying
            .transfer(&Self::account_id(), supplier)
            .map_err(|_| Error::<T>::InsufficientCash)?;

        Self::deposit_event(Event::<T>::Redeemed {
            account_id: supplier.clone(),
            currency_id: asset_id,
            amount: underlying.amount(),
        });
        Ok(())
    }

    // NOTE: used in OracleApi, so don't use oracle calls here or it'll recurse forever
    fn recompute_underlying_amount(lend_tokens: &Amount<T>) -> Result<Amount<T>, DispatchError> {
        // This function could be called externally to this pallet, with interest
        // possibly not having accrued for a few blocks. This would result in using an
        // outdated exchange rate. Call `accrue_interest` to avoid this.
        let underlying_id = Self::underlying_id(lend_tokens.currency())?;
        Self::ensure_active_market(underlying_id)?;
        Self::accrue_interest(underlying_id)?;
        let exchange_rate = Self::exchange_rate_stored(underlying_id)?;
        Ok(lend_tokens.checked_mul(&exchange_rate)?.set_currency(underlying_id))
    }

    // Returns a stored asset_id
    //
    // Returns `Err` if asset_id does not exist, it also means that lend_token_id is invalid.
    fn underlying_id(lend_token_id: CurrencyId<T>) -> Result<CurrencyId<T>, DispatchError> {
        UnderlyingAssetId::<T>::try_get(lend_token_id).map_err(|_err| Error::<T>::InvalidLendTokenId.into())
    }

    // NOTE: used in OracleApi, so don't use oracle calls here or it'll recurse forever
    fn recompute_collateral_amount(underlying: &Amount<T>) -> Result<Amount<T>, DispatchError> {
        // This function could be called externally to this pallet, with interest
        // possibly not having accrued for a few blocks. This would result in using an
        // outdated exchange rate. Call `accrue_interest` to avoid this.
        Self::ensure_active_market(underlying.currency())?;
        Self::accrue_interest(underlying.currency())?;
        let exchange_rate = Self::exchange_rate_stored(underlying.currency())?;

        let lend_token_id = Self::lend_token_id(underlying.currency())?;

        Ok(underlying.checked_div(&exchange_rate)?.set_currency(lend_token_id))
    }
}

impl<T: Config> LoansMarketDataProvider<CurrencyId<T>, BalanceOf<T>> for Pallet<T> {
    fn get_market_info(asset_id: CurrencyId<T>) -> Result<MarketInfo, DispatchError> {
        let market = Self::market(asset_id)?;
        let full_rate = Self::get_full_interest_rate(asset_id).ok_or(Error::<T>::InvalidRateModelParam)?;
        Ok(MarketInfo {
            collateral_factor: market.collateral_factor,
            liquidation_threshold: market.liquidation_threshold,
            reserve_factor: market.reserve_factor,
            close_factor: market.close_factor,
            full_rate,
        })
    }

    fn get_market_status(asset_id: CurrencyId<T>) -> Result<MarketStatus<BalanceOf<T>>, DispatchError> {
        let (borrow_rate, supply_rate, exchange_rate, utilization, total_borrows, total_reserves, borrow_index) =
            Self::get_market_status(asset_id)?;
        Ok(MarketStatus {
            borrow_rate,
            supply_rate,
            exchange_rate,
            utilization,
            total_borrows,
            total_reserves,
            borrow_index,
        })
    }

    fn get_full_interest_rate(asset_id: CurrencyId<T>) -> Option<Rate> {
        if let Ok(market) = Self::market(asset_id) {
            let rate = match market.rate_model {
                InterestRateModel::Jump(jump) => Some(jump.full_rate),
                _ => None,
            };
            return rate;
        }
        None
    }
}

impl<T: Config> OnExchangeRateChange<CurrencyId<T>> for Pallet<T> {
    fn on_exchange_rate_change(currency_id: &CurrencyId<T>) {
        // todo: propagate error
        if let Ok(lend_token_id) = Pallet::<T>::lend_token_id(*currency_id) {
            T::OnExchangeRateChange::on_exchange_rate_change(&lend_token_id)
        }
    }
}