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
use super::{OracleAccountInfos, OracleConfig, TokenIndex, TokenPosition};
use crate::accounts_zerocopy::KeyedAccountReader;
use crate::error::*;
use crate::i80f48::ClampToInt;
use crate::state::{oracle, StablePriceModel};
use crate::util;

use anchor_lang::prelude::*;
use derivative::Derivative;
use fixed::types::I80F48;
use oracle::oracle_log_context;
use static_assertions::const_assert_eq;

use std::mem::size_of;

pub const HOUR: i64 = 3600;
pub const DAY: i64 = 86400;
pub const DAY_I80F48: I80F48 = I80F48::from_bits(86_400 * I80F48::ONE.to_bits());
pub const ONE_BPS: I80F48 = I80F48::from_bits(28147497671);
pub const YEAR_I80F48: I80F48 = I80F48::from_bits(31_536_000 * I80F48::ONE.to_bits());

#[derive(Derivative)]
#[derivative(Debug)]
#[account(zero_copy)]
pub struct Bank {
    // ABI: Clients rely on this being at offset 8
    pub group: Pubkey,

    #[derivative(Debug(format_with = "util::format_zero_terminated_utf8_bytes"))]
    pub name: [u8; 16],

    pub mint: Pubkey,
    pub vault: Pubkey,
    pub oracle: Pubkey,

    pub oracle_config: OracleConfig,
    pub stable_price_model: StablePriceModel,

    /// the index used to scale the value of an IndexedPosition
    /// TODO: should always be >= 0, add checks?
    pub deposit_index: I80F48,
    pub borrow_index: I80F48,

    /// deposits/borrows for this bank
    ///
    /// Note that these may become negative. It's perfectly fine for users to borrow one one bank
    /// (increasing indexed_borrows there) and paying back on another (possibly decreasing indexed_borrows
    /// below zero).
    ///
    /// The vault amount is not deducable from these values.
    ///
    /// These become meaningful when summed over all banks (like in update_index_and_rate).
    pub indexed_deposits: I80F48,
    pub indexed_borrows: I80F48,

    pub index_last_updated: u64,
    pub bank_rate_last_updated: u64,

    pub avg_utilization: I80F48,

    pub adjustment_factor: I80F48,

    /// The unscaled borrow interest curve is defined as continuous piecewise linear with the points:
    ///
    /// - 0% util: zero_util_rate
    /// - util0% util: rate0
    /// - util1% util: rate1
    /// - 100% util: max_rate
    ///
    /// The final rate is this unscaled curve multiplied by interest_curve_scaling.
    pub util0: I80F48,
    pub rate0: I80F48,
    pub util1: I80F48,
    pub rate1: I80F48,

    /// the 100% utilization rate
    ///
    /// This isn't the max_rate, since this still gets scaled by interest_curve_scaling,
    /// which is >=1.
    pub max_rate: I80F48,

    /// Fees collected over the lifetime of the bank
    ///
    /// See fees_withdrawn for how much of the fees was withdrawn.
    /// See collected_liquidation_fees for the (included) subtotal for liquidation related fees.
    pub collected_fees_native: I80F48,

    pub loan_origination_fee_rate: I80F48,
    pub loan_fee_rate: I80F48,

    // This is a _lot_ of bytes (64) - seems unnecessary
    // (could maybe store them in one byte each, as an informal U1F7?
    // that could store values between 0-2 and converting to I80F48 would be a cheap expand+shift)
    pub maint_asset_weight: I80F48,
    pub init_asset_weight: I80F48,
    pub maint_liab_weight: I80F48,
    pub init_liab_weight: I80F48,

    /// Liquidation fee that goes to the liqor.
    ///
    /// Liquidation always involves two tokens, and the sum of the two configured fees is used.
    ///
    /// A fraction of the price, like 0.05 for a 5% fee during liquidation.
    ///
    /// See also platform_liquidation_fee.
    pub liquidation_fee: I80F48,

    // Collection of all fractions-of-native-tokens that got rounded away
    pub dust: I80F48,

    pub flash_loan_token_account_initial: u64,
    pub flash_loan_approved_amount: u64,

    // Index into TokenInfo on the group
    pub token_index: TokenIndex,

    pub bump: u8,

    pub mint_decimals: u8,

    pub bank_num: u32,

    /// The maximum utilization allowed when borrowing is 1-this value
    /// WARNING: Outdated name, kept for IDL compatibility
    pub min_vault_to_deposits_ratio: f64,

    /// Size in seconds of a net borrows window
    pub net_borrow_limit_window_size_ts: u64,
    /// Timestamp at which the last net borrows window started
    pub last_net_borrows_window_start_ts: u64,
    /// Net borrow limit per window in quote native; set to -1 to disable.
    pub net_borrow_limit_per_window_quote: i64,
    /// Sum of all deposits and borrows in the last window, in native units.
    pub net_borrows_in_window: i64,

    /// Soft borrow limit in native quote
    ///
    /// Once the borrows on the bank exceed this quote value, init_liab_weight is scaled up.
    /// Set to f64::MAX to disable.
    ///
    /// See scaled_init_liab_weight().
    pub borrow_weight_scale_start_quote: f64,

    /// Limit for collateral of deposits in native quote
    ///
    /// Once the deposits in the bank exceed this quote value, init_asset_weight is scaled
    /// down to keep the total collateral value constant.
    /// Set to f64::MAX to disable.
    ///
    /// See scaled_init_asset_weight().
    pub deposit_weight_scale_start_quote: f64,

    // We have 3 modes
    // 0 - Off,
    // 1 - ReduceDepositsReduceBorrows - standard
    // 2 - ReduceBorrows - borrows can only be reduced, but deposits have no restriction, special case for
    //                 force close mode, where liqor should first acquire deposits before closing liqee's borrows
    pub reduce_only: u8,
    pub force_close: u8,

    /// If set to 1, deposits cannot be liquidated when an account is liquidatable.
    /// That means bankrupt accounts may still have assets of this type deposited.
    pub disable_asset_liquidation: u8,

    pub force_withdraw: u8,

    #[derivative(Debug = "ignore")]
    pub padding: [u8; 4],

    // Do separate bookkeping for how many tokens were withdrawn
    // This ensures that collected_fees_native is strictly increasing for stats gathering purposes
    pub fees_withdrawn: u64,

    /// Fees for the token conditional swap feature
    pub token_conditional_swap_taker_fee_rate: f32,
    pub token_conditional_swap_maker_fee_rate: f32,

    pub flash_loan_swap_fee_rate: f32,

    /// Target utilization: If actual utilization is higher, scale up interest.
    /// If it's lower, scale down interest (if possible)
    pub interest_target_utilization: f32,

    /// Current interest curve scaling, always >= 1.0
    ///
    /// Except when first migrating to having this field, then 0.0
    pub interest_curve_scaling: f64,

    /// Largest amount of tokens that might be added the bank based on
    /// serum open order execution.
    pub potential_serum_tokens: u64,

    /// Start timestamp in seconds at which maint weights should start to change away
    /// from maint_asset_weight, maint_liab_weight towards _asset_target and _liab_target.
    /// If _start and _end and _duration_inv are 0, no shift is configured.
    pub maint_weight_shift_start: u64,
    /// End timestamp in seconds until which the maint weights should reach the configured targets.
    pub maint_weight_shift_end: u64,
    /// Cache of the inverse of maint_weight_shift_end - maint_weight_shift_start,
    /// or zero if no shift is configured
    pub maint_weight_shift_duration_inv: I80F48,
    /// Maint asset weight to reach at _shift_end.
    pub maint_weight_shift_asset_target: I80F48,
    pub maint_weight_shift_liab_target: I80F48,

    /// Oracle that may be used if the main oracle is stale or not confident enough.
    /// If this is Pubkey::default(), no fallback is available.
    pub fallback_oracle: Pubkey,

    /// zero means none, in token native
    pub deposit_limit: u64,

    /// The unscaled borrow interest curve point for zero utilization.
    ///
    /// See util0, rate0, util1, rate1, max_rate
    pub zero_util_rate: I80F48,

    /// Additional to liquidation_fee, but goes to the group owner instead of the liqor
    pub platform_liquidation_fee: I80F48,

    /// Platform fees that were collected during liquidation (in native tokens)
    ///
    /// See also collected_fees_native and fees_withdrawn.
    pub collected_liquidation_fees: I80F48,

    /// Collateral fees that have been collected (in native tokens)
    ///
    /// See also collected_fees_native and fees_withdrawn.
    pub collected_collateral_fees: I80F48,

    /// The daily collateral fees rate for fully utilized collateral.
    pub collateral_fee_per_day: f32,

    #[derivative(Debug = "ignore")]
    pub padding2: [u8; 4],

    /// Largest amount of tokens that might be added the bank based on
    /// oenbook open order execution.
    pub potential_openbook_tokens: u64,

    #[derivative(Debug = "ignore")]
    pub reserved: [u8; 1888],
}
const_assert_eq!(
    size_of::<Bank>(),
    32 + 16
        + 32 * 3
        + 96
        + 288
        + 16 * 2
        + 16 * 2
        + 8 * 2
        + 16
        + 16 * 6
        + 16 * 3
        + 16 * 4
        + 16
        + 16
        + 8
        + 8
        + 2
        + 1
        + 1
        + 4
        + 8
        + 8 * 4
        + 8
        + 8
        + 1
        + 1
        + 6
        + 8
        + 4 * 4
        + 8 * 2
        + 8 * 2
        + 16 * 3
        + 32
        + 8
        + 16 * 4
        + 4 * 2
        + 8
        + 1888
);
const_assert_eq!(size_of::<Bank>(), 3064);
const_assert_eq!(size_of::<Bank>() % 8, 0);

pub struct WithdrawResult {
    pub position_is_active: bool,
    pub loan_origination_fee: I80F48,
    pub loan_amount: I80F48,
}

impl WithdrawResult {
    pub fn has_loan(&self) -> bool {
        self.loan_amount.is_positive()
    }
}

pub struct TransferResult {
    pub source_is_active: bool,
    pub target_is_active: bool,
    pub loan_origination_fee: I80F48,
    pub loan_amount: I80F48,
}

impl TransferResult {
    pub fn has_loan(&self) -> bool {
        self.loan_amount.is_positive()
    }
}

impl Bank {
    pub fn from_existing_bank(
        existing_bank: &Bank,
        vault: Pubkey,
        bank_num: u32,
        bump: u8,
    ) -> Self {
        Self {
            // values that must be reset/changed
            vault,
            indexed_deposits: I80F48::ZERO,
            indexed_borrows: I80F48::ZERO,
            collected_fees_native: I80F48::ZERO,
            collected_liquidation_fees: I80F48::ZERO,
            collected_collateral_fees: I80F48::ZERO,
            fees_withdrawn: 0,
            dust: I80F48::ZERO,
            flash_loan_approved_amount: 0,
            flash_loan_token_account_initial: u64::MAX,
            net_borrows_in_window: 0,
            potential_serum_tokens: 0,
            potential_openbook_tokens: 0,
            bump,
            bank_num,

            // values that can be copied
            // these are listed explicitly, so someone must make the decision when a
            // new field is added!
            name: existing_bank.name,
            group: existing_bank.group,
            mint: existing_bank.mint,
            oracle: existing_bank.oracle,
            deposit_index: existing_bank.deposit_index,
            borrow_index: existing_bank.borrow_index,
            index_last_updated: existing_bank.index_last_updated,
            bank_rate_last_updated: existing_bank.bank_rate_last_updated,
            avg_utilization: existing_bank.avg_utilization,
            adjustment_factor: existing_bank.adjustment_factor,
            util0: existing_bank.util0,
            rate0: existing_bank.rate0,
            util1: existing_bank.util1,
            rate1: existing_bank.rate1,
            max_rate: existing_bank.max_rate,
            loan_origination_fee_rate: existing_bank.loan_origination_fee_rate,
            loan_fee_rate: existing_bank.loan_fee_rate,
            maint_asset_weight: existing_bank.maint_asset_weight,
            init_asset_weight: existing_bank.init_asset_weight,
            maint_liab_weight: existing_bank.maint_liab_weight,
            init_liab_weight: existing_bank.init_liab_weight,
            liquidation_fee: existing_bank.liquidation_fee,
            token_index: existing_bank.token_index,
            mint_decimals: existing_bank.mint_decimals,
            oracle_config: existing_bank.oracle_config,
            stable_price_model: existing_bank.stable_price_model,
            min_vault_to_deposits_ratio: existing_bank.min_vault_to_deposits_ratio,
            net_borrow_limit_per_window_quote: existing_bank.net_borrow_limit_per_window_quote,
            net_borrow_limit_window_size_ts: existing_bank.net_borrow_limit_window_size_ts,
            last_net_borrows_window_start_ts: existing_bank.last_net_borrows_window_start_ts,
            borrow_weight_scale_start_quote: existing_bank.borrow_weight_scale_start_quote,
            deposit_weight_scale_start_quote: existing_bank.deposit_weight_scale_start_quote,
            reduce_only: existing_bank.reduce_only,
            force_close: existing_bank.force_close,
            disable_asset_liquidation: existing_bank.disable_asset_liquidation,
            force_withdraw: existing_bank.force_withdraw,
            padding: [0; 4],
            token_conditional_swap_taker_fee_rate: existing_bank
                .token_conditional_swap_taker_fee_rate,
            token_conditional_swap_maker_fee_rate: existing_bank
                .token_conditional_swap_maker_fee_rate,
            flash_loan_swap_fee_rate: existing_bank.flash_loan_swap_fee_rate,
            interest_target_utilization: existing_bank.interest_target_utilization,
            interest_curve_scaling: existing_bank.interest_curve_scaling,
            maint_weight_shift_start: existing_bank.maint_weight_shift_start,
            maint_weight_shift_end: existing_bank.maint_weight_shift_end,
            maint_weight_shift_duration_inv: existing_bank.maint_weight_shift_duration_inv,
            maint_weight_shift_asset_target: existing_bank.maint_weight_shift_asset_target,
            maint_weight_shift_liab_target: existing_bank.maint_weight_shift_liab_target,
            fallback_oracle: existing_bank.oracle,
            deposit_limit: existing_bank.deposit_limit,
            zero_util_rate: existing_bank.zero_util_rate,
            platform_liquidation_fee: existing_bank.platform_liquidation_fee,
            collateral_fee_per_day: existing_bank.collateral_fee_per_day,
            padding2: [0; 4],
            reserved: [0; 1888],
        }
    }

    pub fn verify(&self) -> Result<()> {
        require_gte!(self.oracle_config.conf_filter, 0.0);
        require_gte!(self.util0, I80F48::ZERO);
        require_gte!(self.util1, self.util0);
        require_gte!(I80F48::ONE, self.util1);
        require_gte!(self.rate0, I80F48::ZERO);
        require_gte!(self.rate1, I80F48::ZERO);
        require_gte!(self.max_rate, I80F48::ZERO);
        require_gte!(self.adjustment_factor, 0.0);
        require_gte!(self.loan_fee_rate, 0.0);
        require_gte!(self.loan_origination_fee_rate, 0.0);
        require_gte!(self.stable_price_model.delay_growth_limit, 0.0);
        require_gte!(self.stable_price_model.stable_growth_limit, 0.0);
        require_gte!(self.init_asset_weight, 0.0);
        require_gte!(self.maint_asset_weight, self.init_asset_weight);
        require_gte!(self.maint_liab_weight, 0.0);
        require_gte!(self.init_liab_weight, self.maint_liab_weight);
        require_gte!(self.liquidation_fee, 0.0);
        require_gte!(self.min_vault_to_deposits_ratio, 0.0);
        require_gte!(1.0, self.min_vault_to_deposits_ratio);
        require_gte!(self.net_borrow_limit_per_window_quote, -1);
        require_gt!(self.borrow_weight_scale_start_quote, 0.0);
        require_gt!(self.deposit_weight_scale_start_quote, 0.0);
        require_gte!(2, self.reduce_only);
        require_gte!(self.token_conditional_swap_taker_fee_rate, 0.0);
        require_gte!(self.token_conditional_swap_maker_fee_rate, 0.0);
        require_gte!(self.flash_loan_swap_fee_rate, 0.0);
        require_gte!(self.interest_curve_scaling, 1.0);
        require_gte!(self.interest_target_utilization, 0.0);
        require_gte!(1.0, self.interest_target_utilization);
        require_gte!(self.maint_weight_shift_duration_inv, 0.0);
        require_gte!(self.maint_weight_shift_asset_target, 0.0);
        require_gte!(self.maint_weight_shift_liab_target, 0.0);
        require_gte!(self.zero_util_rate, I80F48::ZERO);
        require_gte!(self.platform_liquidation_fee, 0.0);
        if !self.allows_asset_liquidation() {
            require!(self.are_borrows_reduce_only(), MangoError::SomeError);
            require_eq!(self.maint_asset_weight, I80F48::ZERO);
        }
        require_gte!(self.collateral_fee_per_day, 0.0);
        if self.is_force_withdraw() {
            require!(self.are_deposits_reduce_only(), MangoError::SomeError);
            require!(!self.allows_asset_liquidation(), MangoError::SomeError);
            require_eq!(self.maint_asset_weight, I80F48::ZERO);
        }
        Ok(())
    }

    pub fn name(&self) -> &str {
        std::str::from_utf8(&self.name)
            .unwrap()
            .trim_matches(char::from(0))
    }

    pub fn are_deposits_reduce_only(&self) -> bool {
        self.reduce_only == 1
    }

    pub fn are_borrows_reduce_only(&self) -> bool {
        self.reduce_only == 1 || self.reduce_only == 2
    }

    pub fn is_force_close(&self) -> bool {
        self.force_close == 1
    }

    pub fn is_force_withdraw(&self) -> bool {
        self.force_withdraw == 1
    }

    pub fn allows_asset_liquidation(&self) -> bool {
        self.disable_asset_liquidation == 0
    }

    #[inline(always)]
    pub fn native_borrows(&self) -> I80F48 {
        self.borrow_index * self.indexed_borrows
    }

    #[inline(always)]
    pub fn native_deposits(&self) -> I80F48 {
        self.deposit_index * self.indexed_deposits
    }

    pub fn maint_weights(&self, now_ts: u64) -> (I80F48, I80F48) {
        if self.maint_weight_shift_duration_inv.is_zero() || now_ts <= self.maint_weight_shift_start
        {
            (self.maint_asset_weight, self.maint_liab_weight)
        } else if now_ts >= self.maint_weight_shift_end {
            (
                self.maint_weight_shift_asset_target,
                self.maint_weight_shift_liab_target,
            )
        } else {
            let scale = I80F48::from(now_ts - self.maint_weight_shift_start)
                * self.maint_weight_shift_duration_inv;
            let asset = self.maint_asset_weight
                + scale * (self.maint_weight_shift_asset_target - self.maint_asset_weight);
            let liab = self.maint_liab_weight
                + scale * (self.maint_weight_shift_liab_target - self.maint_liab_weight);
            (asset, liab)
        }
    }

    pub fn enforce_borrows_lte_deposits(&self) -> Result<()> {
        self.enforce_max_utilization(I80F48::ONE)
    }

    /// Prevent borrowing away the full bank vault.
    /// Keep some in reserve to satisfy non-borrow withdraws.
    pub fn enforce_max_utilization_on_borrow(&self) -> Result<()> {
        self.enforce_max_utilization(
            I80F48::ONE - I80F48::from_num(self.min_vault_to_deposits_ratio),
        )
    }

    /// Prevent borrowing away the full bank vault.
    /// Keep some in reserve to satisfy non-borrow withdraws.
    fn enforce_max_utilization(&self, max_utilization: I80F48) -> Result<()> {
        let bank_native_deposits = self.native_deposits();
        let bank_native_borrows = self.native_borrows();

        if bank_native_borrows > max_utilization * bank_native_deposits {
            return err!(MangoError::BankBorrowLimitReached).with_context(|| {
                format!(
                    "deposits {}, borrows {}, max utilization {}",
                    bank_native_deposits, bank_native_borrows, max_utilization,
                )
            });
        };

        Ok(())
    }

    /// Deposits `native_amount`.
    ///
    /// If the token position ends up positive but below one native token and this token
    /// position isn't marked as in-use, the token balance will be dusted, the position
    /// will be set to zero and this function returns Ok(false).
    ///
    /// native_amount must be >= 0
    /// fractional deposits can be relevant during liquidation, for example
    pub fn deposit(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<bool> {
        self.deposit_internal_wrapper(position, native_amount, !position.is_in_use(), now_ts)
    }

    /// Like `deposit()`, but allows dusting of in-use accounts.
    ///
    /// Returns Ok(false) if the position was dusted and was not in-use.
    pub fn deposit_with_dusting(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<bool> {
        self.deposit_internal_wrapper(position, native_amount, true, now_ts)
            .map(|not_dusted| not_dusted || position.is_in_use())
    }

    pub fn deposit_internal_wrapper(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        allow_dusting: bool,
        now_ts: u64,
    ) -> Result<bool> {
        let opening_indexed_position = position.indexed_position;
        let result = self.deposit_internal(position, native_amount, allow_dusting, now_ts)?;
        self.update_cumulative_interest(position, opening_indexed_position);
        Ok(result)
    }

    /// Internal function to deposit funds
    pub fn deposit_internal(
        &mut self,
        position: &mut TokenPosition,
        mut native_amount: I80F48,
        allow_dusting: bool,
        now_ts: u64,
    ) -> Result<bool> {
        require_gte!(native_amount, 0);

        let native_position = position.native(self);

        // Adding DELTA to amount/index helps because (amount/index)*index <= amount, but
        // we want to ensure that users can withdraw the same amount they have deposited, so
        // (amount/index + delta)*index >= amount is a better guarantee.
        // Additionally, we require that we don't adjust values if
        // (native / index) * index == native, because we sometimes call this function with
        // values that are products of index.
        let div_rounding_up = |native: I80F48, index: I80F48| {
            let indexed = native / index;
            if (indexed * index) < native {
                indexed + I80F48::DELTA
            } else {
                indexed
            }
        };

        if native_position.is_negative() {
            // Only account for the borrows we are repaying
            self.update_net_borrows(native_position.max(-native_amount), now_ts);

            let new_native_position = native_position + native_amount;
            let indexed_change = div_rounding_up(native_amount, self.borrow_index);
            // this is only correct if it's not positive, because it scales the whole amount by borrow_index
            let new_indexed_value = position.indexed_position + indexed_change;
            if new_indexed_value.is_negative() {
                // pay back borrows only, leaving a negative position
                self.indexed_borrows -= indexed_change;
                position.indexed_position = new_indexed_value;
                return Ok(true);
            } else if new_native_position < I80F48::ONE && allow_dusting {
                // if there's less than one token deposited, zero the position
                self.dust += new_native_position;
                self.indexed_borrows += position.indexed_position;
                position.indexed_position = I80F48::ZERO;
                return Ok(false);
            }

            // pay back all borrows
            self.indexed_borrows += position.indexed_position; // position.value is negative
            position.indexed_position = I80F48::ZERO;
            // deposit the rest
            // note: .max(0) because there's a scenario where new_indexed_value == 0 and new_native_position < 0
            native_amount = new_native_position.max(I80F48::ZERO);
        }

        // add to deposits
        let indexed_change = div_rounding_up(native_amount, self.deposit_index);
        self.indexed_deposits += indexed_change;
        position.indexed_position += indexed_change;

        Ok(true)
    }

    /// Withdraws `native_amount` without applying the loan origination fee.
    ///
    /// If the token position ends up positive but below one native token and this token
    /// position isn't marked as in-use, the token balance will be dusted, the position
    /// will be set to zero and this function returns Ok(false).
    ///
    /// native_amount must be >= 0
    /// fractional withdraws can be relevant during liquidation, for example
    pub fn withdraw_without_fee(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<bool> {
        let position_is_active = self
            .withdraw_internal_wrapper(
                position,
                native_amount,
                false,
                !position.is_in_use(),
                now_ts,
            )?
            .position_is_active;

        Ok(position_is_active)
    }

    /// Like `withdraw_without_fee()` but allows dusting of in-use token accounts.
    ///
    /// Returns Ok(false) on dusted positions that weren't in-use.
    pub fn withdraw_without_fee_with_dusting(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<bool> {
        self.withdraw_internal_wrapper(position, native_amount, false, true, now_ts)
            .map(|withdraw_result| withdraw_result.position_is_active || position.is_in_use())
    }

    /// Withdraws `native_amount` while applying the loan origination fee if a borrow is created.
    ///
    /// If the token position ends up positive but below one native token and this token
    /// position isn't marked as in-use, the token balance will be dusted, the position
    /// will be set to zero and this function returns Ok(false).
    ///
    /// native_amount must be >= 0
    /// fractional withdraws can be relevant during liquidation, for example
    pub fn withdraw_with_fee(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<WithdrawResult> {
        self.withdraw_internal_wrapper(position, native_amount, true, !position.is_in_use(), now_ts)
    }

    /// Internal function to withdraw funds
    fn withdraw_internal_wrapper(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        with_loan_origination_fee: bool,
        allow_dusting: bool,
        now_ts: u64,
    ) -> Result<WithdrawResult> {
        let opening_indexed_position = position.indexed_position;
        let res = self.withdraw_internal(
            position,
            native_amount,
            with_loan_origination_fee,
            allow_dusting,
            now_ts,
        );
        self.update_cumulative_interest(position, opening_indexed_position);
        res
    }

    /// Internal function to withdraw funds
    fn withdraw_internal(
        &mut self,
        position: &mut TokenPosition,
        mut native_amount: I80F48,
        with_loan_origination_fee: bool,
        allow_dusting: bool,
        now_ts: u64,
    ) -> Result<WithdrawResult> {
        require_gte!(native_amount, 0);
        let native_position = position.native(self);

        if !native_position.is_negative() {
            let new_native_position = native_position - native_amount;
            if !new_native_position.is_negative() {
                // withdraw deposits only
                if new_native_position < I80F48::ONE && allow_dusting {
                    // zero the account collecting the leftovers in `dust`
                    self.dust += new_native_position;
                    self.indexed_deposits -= position.indexed_position;
                    position.indexed_position = I80F48::ZERO;
                    return Ok(WithdrawResult {
                        position_is_active: false,
                        loan_origination_fee: I80F48::ZERO,
                        loan_amount: I80F48::ZERO,
                    });
                } else {
                    // withdraw some deposits leaving a positive balance
                    let indexed_change = native_amount / self.deposit_index;
                    self.indexed_deposits -= indexed_change;
                    position.indexed_position -= indexed_change;
                    return Ok(WithdrawResult {
                        position_is_active: true,
                        loan_origination_fee: I80F48::ZERO,
                        loan_amount: I80F48::ZERO,
                    });
                }
            }

            // withdraw all deposits
            self.indexed_deposits -= position.indexed_position;
            position.indexed_position = I80F48::ZERO;
            // borrow the rest
            native_amount = -new_native_position;
        }

        let mut loan_origination_fee = I80F48::ZERO;
        if with_loan_origination_fee {
            loan_origination_fee = self.loan_origination_fee_rate * native_amount;
            self.collected_fees_native += loan_origination_fee;
            native_amount += loan_origination_fee;
        }

        // add to borrows
        let indexed_change = native_amount / self.borrow_index;
        self.indexed_borrows += indexed_change;
        position.indexed_position -= indexed_change;

        // net borrows requires updating in only this case, since other branches of the method deal with
        // withdraws and not borrows
        self.update_net_borrows(native_amount, now_ts);

        Ok(WithdrawResult {
            position_is_active: true,
            loan_origination_fee,
            loan_amount: native_amount,
        })
    }

    // withdraw the loan origination fee for a borrow that happened earlier
    pub fn withdraw_loan_origination_fee(
        &mut self,
        position: &mut TokenPosition,
        already_borrowed_native_amount: I80F48,
        now_ts: u64,
    ) -> Result<WithdrawResult> {
        let loan_origination_fee = self.loan_origination_fee_rate * already_borrowed_native_amount;
        self.collected_fees_native += loan_origination_fee;

        let position_is_active = self
            .withdraw_internal_wrapper(
                position,
                loan_origination_fee,
                false,
                !position.is_in_use(),
                now_ts,
            )?
            .position_is_active;

        Ok(WithdrawResult {
            position_is_active,
            loan_origination_fee,
            // To avoid double counting of loans return loan_amount of 0 here (as the loan_amount has already been returned earlier with loan_origination_fee == 0)
            loan_amount: I80F48::ZERO,
        })
    }

    /// Returns true if the position remains active
    pub fn dust_if_possible(&mut self, position: &mut TokenPosition, now_ts: u64) -> Result<bool> {
        if position.is_in_use() {
            return Ok(true);
        }
        let native = position.native(self);
        if native >= 0 && native < 1 {
            // Withdrawing 0 triggers the dusting check
            return self.withdraw_without_fee(position, I80F48::ZERO, now_ts);
        }
        Ok(true)
    }

    /// Change a position without applying the loan origination fee
    pub fn change_without_fee(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<bool> {
        if native_amount >= 0 {
            self.deposit(position, native_amount, now_ts)
        } else {
            self.withdraw_without_fee(position, -native_amount, now_ts)
        }
    }

    /// Change a position, while taking the loan origination fee into account
    pub fn change_with_fee(
        &mut self,
        position: &mut TokenPosition,
        native_amount: I80F48,
        now_ts: u64,
    ) -> Result<WithdrawResult> {
        if native_amount >= 0 {
            Ok(WithdrawResult {
                position_is_active: self.deposit(position, native_amount, now_ts)?,
                loan_origination_fee: I80F48::ZERO,
                loan_amount: I80F48::ZERO,
            })
        } else {
            self.withdraw_with_fee(position, -native_amount, now_ts)
        }
    }

    /// Generic "transfer" from source to target.
    ///
    /// Amounts for source and target can differ and can be zero.
    /// Checks reduce-only, net borrow limits and deposit limits.
    pub fn checked_transfer_with_fee(
        &mut self,
        source: &mut TokenPosition,
        source_amount: I80F48,
        target: &mut TokenPosition,
        target_amount: I80F48,
        now_ts: u64,
        oracle_price: I80F48,
    ) -> Result<TransferResult> {
        let before_borrows = self.indexed_borrows;
        let before_deposits = self.indexed_deposits;

        let withdraw_result = if !source_amount.is_zero() {
            let withdraw_result = self.withdraw_with_fee(source, source_amount, now_ts)?;
            require!(
                source.indexed_position >= 0 || !self.are_borrows_reduce_only(),
                MangoError::TokenInReduceOnlyMode
            );
            withdraw_result
        } else {
            WithdrawResult {
                position_is_active: true,
                loan_amount: I80F48::ZERO,
                loan_origination_fee: I80F48::ZERO,
            }
        };

        let target_is_active = if !target_amount.is_zero() {
            let active = self.deposit(target, target_amount, now_ts)?;
            require!(
                target.indexed_position <= 0 || !self.are_deposits_reduce_only(),
                MangoError::TokenInReduceOnlyMode
            );
            active
        } else {
            true
        };

        // Adding DELTA here covers the case where we add slightly more than we withdraw
        if self.indexed_borrows > before_borrows + I80F48::DELTA {
            self.check_net_borrows(oracle_price)?;
        }
        if self.indexed_deposits > before_deposits + I80F48::DELTA {
            self.check_deposit_and_oo_limit()?;
        }

        Ok(TransferResult {
            source_is_active: withdraw_result.position_is_active,
            target_is_active,
            loan_origination_fee: withdraw_result.loan_origination_fee,
            loan_amount: withdraw_result.loan_amount,
        })
    }

    /// Update the bank's net_borrows fields.
    ///
    /// If oracle_price is set, also do a net borrows check and error if the threshold is exceeded.
    pub fn update_net_borrows(&mut self, native_amount: I80F48, now_ts: u64) {
        let in_new_window =
            now_ts >= self.last_net_borrows_window_start_ts + self.net_borrow_limit_window_size_ts;

        let amount = native_amount.ceil().clamp_to_i64();

        self.net_borrows_in_window = if in_new_window {
            // reset to latest window
            self.last_net_borrows_window_start_ts = now_ts / self.net_borrow_limit_window_size_ts
                * self.net_borrow_limit_window_size_ts;
            amount
        } else {
            self.net_borrows_in_window + amount
        };
    }

    pub fn remaining_net_borrows_quote(&self, oracle_price: I80F48) -> I80F48 {
        if self.net_borrows_in_window < 0 || self.net_borrow_limit_per_window_quote < 0 {
            return I80F48::MAX;
        }

        let price = oracle_price.max(self.stable_price());
        let net_borrows_quote = price
            .checked_mul_int(self.net_borrows_in_window.into())
            .unwrap();

        I80F48::from(self.net_borrow_limit_per_window_quote) - net_borrows_quote
    }

    pub fn check_net_borrows(&self, oracle_price: I80F48) -> Result<()> {
        let remaining_quote = self.remaining_net_borrows_quote(oracle_price);
        if remaining_quote < 0 {
            return Err(error_msg_typed!(MangoError::BankNetBorrowsLimitReached,
                    "net_borrows_in_window: {:?}, remaining quote: {:?}, net_borrow_limit_per_window_quote: {:?}, last_net_borrows_window_start_ts: {:?}",
                    self.net_borrows_in_window, remaining_quote, self.net_borrow_limit_per_window_quote, self.last_net_borrows_window_start_ts

            ));
        }

        Ok(())
    }

    pub fn remaining_deposits_until_limit(&self) -> I80F48 {
        if self.deposit_limit == 0 {
            return I80F48::MAX;
        }

        // Assuming slightly higher deposits than true allows the returned value
        // to be deposit()ed safely into this bank without triggering limits.
        // (because deposit() will round up in favor of the user)
        let deposits = self.deposit_index * (self.indexed_deposits + I80F48::DELTA);

        let serum = I80F48::from(self.potential_serum_tokens);
        let openbook = I80F48::from(self.potential_openbook_tokens);
        let total = deposits + serum + openbook;

        I80F48::from(self.deposit_limit) - total
    }

    pub fn check_deposit_and_oo_limit(&self) -> Result<()> {
        if self.deposit_limit == 0 {
            return Ok(());
        }

        // Intentionally does not use remaining_deposits_until_limit(): That function
        // returns slightly less than the true limit to make sure depositing that amount
        // will not cause a limit overrun.
        let deposits = self.native_deposits();
        let serum = I80F48::from(self.potential_serum_tokens);
        let openbook = I80F48::from(self.potential_openbook_tokens);
        let total = deposits + serum + openbook;
        let remaining = I80F48::from(self.deposit_limit) - total;
        if remaining < 0 {
            return Err(error_msg_typed!(
                MangoError::BankDepositLimit,
                "deposit limit exceeded: remaining: {}, total: {}, limit: {}, deposits: {}, serum: {}, openbook: {}",
                remaining,
                total,
                self.deposit_limit,
                deposits,
                serum,
                openbook,
            ));
        }

        Ok(())
    }

    pub fn update_cumulative_interest(
        &self,
        position: &mut TokenPosition,
        opening_indexed_position: I80F48,
    ) {
        if opening_indexed_position.is_positive() {
            let interest = ((self.deposit_index - position.previous_index)
                * opening_indexed_position)
                .to_num::<f64>();
            position.cumulative_deposit_interest += interest;
        } else {
            let interest = ((self.borrow_index - position.previous_index)
                * opening_indexed_position)
                .to_num::<f64>();
            position.cumulative_borrow_interest -= interest;
        }

        if position.indexed_position.is_positive() {
            position.previous_index = self.deposit_index
        } else {
            position.previous_index = self.borrow_index
        }
    }

    pub fn compute_index(
        &self,
        indexed_total_deposits: I80F48,
        indexed_total_borrows: I80F48,
        diff_ts: I80F48,
    ) -> Result<(I80F48, I80F48, I80F48, I80F48, I80F48)> {
        // compute index based on utilization
        let native_total_deposits = self.deposit_index * indexed_total_deposits;
        let native_total_borrows = self.borrow_index * indexed_total_borrows;

        let instantaneous_utilization =
            Self::instantaneous_utilization(native_total_deposits, native_total_borrows);

        let borrow_rate = self.compute_interest_rate(instantaneous_utilization);

        // We want to grant depositors a rate that exactly matches the amount that is
        // taken from borrowers. That means:
        //   (new_deposit_index - old_deposit_index) * indexed_deposits
        //      = (new_borrow_index - old_borrow_index) * indexed_borrows
        // with
        //   new_deposit_index = old_deposit_index * (1 + deposit_rate) and
        //   new_borrow_index = old_borrow_index * (1 * borrow_rate)
        // we have
        //   deposit_rate = borrow_rate * (old_borrow_index * indexed_borrows) / (old_deposit_index * indexed_deposits)
        // and the latter factor is exactly instantaneous_utilization.
        let deposit_rate = borrow_rate * instantaneous_utilization;

        // The loan fee rate is not distributed to depositors.
        let borrow_rate_with_fees = borrow_rate + self.loan_fee_rate;
        let borrow_fees = native_total_borrows * self.loan_fee_rate * diff_ts / YEAR_I80F48;

        let borrow_index =
            (self.borrow_index * borrow_rate_with_fees * diff_ts) / YEAR_I80F48 + self.borrow_index;
        let deposit_index =
            (self.deposit_index * deposit_rate * diff_ts) / YEAR_I80F48 + self.deposit_index;

        Ok((
            deposit_index,
            borrow_index,
            borrow_fees,
            borrow_rate,
            deposit_rate,
        ))
    }

    /// Current utilization, clamped to 0..1
    ///
    /// Above 100% utilization can happen natually when utilization is 100% and interest is paid out,
    /// increasing borrows more than deposits.
    fn instantaneous_utilization(
        native_total_deposits: I80F48,
        native_total_borrows: I80F48,
    ) -> I80F48 {
        if native_total_deposits == I80F48::ZERO {
            I80F48::ZERO
        } else {
            (native_total_borrows / native_total_deposits)
                .max(I80F48::ZERO)
                .min(I80F48::ONE)
        }
    }

    /// returns the current interest rate in APR
    #[inline(always)]
    pub fn compute_interest_rate(&self, utilization: I80F48) -> I80F48 {
        Bank::interest_rate_curve_calculator(
            utilization,
            self.zero_util_rate,
            self.util0,
            self.rate0,
            self.util1,
            self.rate1,
            self.max_rate,
            self.interest_curve_scaling,
        )
    }

    /// calculator function that can be used to compute an interest
    /// rate based on the given parameters
    #[inline(always)]
    pub fn interest_rate_curve_calculator(
        utilization: I80F48,
        zero_util_rate: I80F48,
        util0: I80F48,
        rate0: I80F48,
        util1: I80F48,
        rate1: I80F48,
        max_rate: I80F48,
        scaling: f64,
    ) -> I80F48 {
        // Clamp to avoid negative or extremely high interest
        let utilization = utilization.max(I80F48::ZERO).min(I80F48::ONE);

        let v = if utilization <= util0 {
            let slope = (rate0 - zero_util_rate) / util0;
            zero_util_rate + slope * utilization
        } else if utilization <= util1 {
            let extra_util = utilization - util0;
            let slope = (rate1 - rate0) / (util1 - util0);
            rate0 + slope * extra_util
        } else {
            let extra_util = utilization - util1;
            let slope = (max_rate - rate1) / (I80F48::ONE - util1);
            rate1 + slope * extra_util
        };

        // scaling will be 0 when it's introduced
        if scaling == 0.0 {
            v
        } else {
            v * I80F48::from_num(scaling)
        }
    }

    // compute new avg utilization
    pub fn compute_new_avg_utilization(
        &self,
        indexed_total_deposits: I80F48,
        indexed_total_borrows: I80F48,
        now_ts: u64,
    ) -> I80F48 {
        if now_ts == 0 {
            return I80F48::ZERO;
        }

        let native_total_deposits = self.deposit_index * indexed_total_deposits;
        let native_total_borrows = self.borrow_index * indexed_total_borrows;
        let instantaneous_utilization =
            Self::instantaneous_utilization(native_total_deposits, native_total_borrows);

        // Compute a time-weighted average since bank_rate_last_updated.
        let previous_avg_time =
            I80F48::from_num(self.index_last_updated - self.bank_rate_last_updated);
        let diff_ts = I80F48::from_num(now_ts - self.index_last_updated);
        let new_avg_time = I80F48::from_num(now_ts - self.bank_rate_last_updated);
        if new_avg_time <= 0 {
            return instantaneous_utilization;
        }
        (self.avg_utilization * previous_avg_time + instantaneous_utilization * diff_ts)
            / new_avg_time
    }

    // computes new optimal rates and max rate
    pub fn update_interest_rate_scaling(&mut self) {
        // Interest increases above target_util, decreases below
        let target_util = self.interest_target_utilization as f64;

        // use avg_utilization and not instantaneous_utilization so that rates cannot be manipulated easily
        // also clamp to avoid unusually quick interest rate curve changes
        let avg_util = self.avg_utilization.to_num::<f64>().max(0.0).min(1.0);

        // move rates up when utilization is above optimal utilization, and vice versa
        // util factor is between -1 (avg util = 0) and +1 (avg util = 100%)
        let util_factor = if avg_util > target_util {
            (avg_util - target_util) / (1.0 - target_util)
        } else {
            (avg_util - target_util) / target_util
        };
        let adjustment = 1.0 + self.adjustment_factor.to_num::<f64>() * util_factor;

        self.interest_curve_scaling = (self.interest_curve_scaling * adjustment).max(1.0)
    }

    /// Tries to return the primary oracle price, and if there is a confidence or staleness issue returns the fallback oracle price if possible.
    pub fn oracle_price<T: KeyedAccountReader>(
        &self,
        oracle_acc_infos: &OracleAccountInfos<T>,
        staleness_slot: Option<u64>,
    ) -> Result<I80F48> {
        require_keys_eq!(self.oracle, *oracle_acc_infos.oracle.key());
        let primary_state = oracle::oracle_state_unchecked(oracle_acc_infos, self.mint_decimals)?;
        let primary_ok =
            primary_state.check_confidence_and_maybe_staleness(&self.oracle_config, staleness_slot);
        if primary_ok.is_oracle_error() && oracle_acc_infos.fallback_opt.is_some() {
            let fallback_oracle_acc = oracle_acc_infos.fallback_opt.unwrap();
            require_keys_eq!(self.fallback_oracle, *fallback_oracle_acc.key());
            let fallback_state =
                oracle::fallback_oracle_state_unchecked(&oracle_acc_infos, self.mint_decimals)?;
            let fallback_ok = fallback_state
                .check_confidence_and_maybe_staleness(&self.oracle_config, staleness_slot);
            fallback_ok.with_context(|| {
                format!(
                    "{} {}",
                    oracle_log_context(
                        self.name(),
                        &primary_state,
                        &self.oracle_config,
                        staleness_slot
                    ),
                    oracle_log_context(
                        self.name(),
                        &fallback_state,
                        &self.oracle_config,
                        staleness_slot
                    )
                )
            })?;
            Ok(fallback_state.price)
        } else {
            primary_ok.with_context(|| {
                oracle_log_context(
                    self.name(),
                    &primary_state,
                    &self.oracle_config,
                    staleness_slot,
                )
            })?;
            Ok(primary_state.price)
        }
    }

    pub fn stable_price(&self) -> I80F48 {
        I80F48::from_num(self.stable_price_model.stable_price)
    }

    /// Returns the init asset weight, adjusted for the number of deposits on the bank.
    ///
    /// If max_collateral is 0, then the scaled init weight will be 0.
    /// Otherwise the weight is unadjusted until max_collateral and then scaled down
    /// such that scaled_init_weight * deposits remains constant.
    #[inline(always)]
    pub fn scaled_init_asset_weight(&self, price: I80F48) -> I80F48 {
        if self.deposit_weight_scale_start_quote == f64::MAX {
            return self.init_asset_weight;
        }
        let all_deposits = self.native_deposits().to_num::<f64>()
            + self.potential_serum_tokens as f64
            + self.potential_openbook_tokens as f64;
        let deposits_quote = all_deposits * price.to_num::<f64>();
        if deposits_quote <= self.deposit_weight_scale_start_quote {
            self.init_asset_weight
        } else {
            // The next line is around 500 CU
            let scale = self.deposit_weight_scale_start_quote / deposits_quote;
            self.init_asset_weight * I80F48::from_num(scale)
        }
    }

    #[inline(always)]
    pub fn scaled_init_liab_weight(&self, price: I80F48) -> I80F48 {
        if self.borrow_weight_scale_start_quote == f64::MAX {
            return self.init_liab_weight;
        }
        let borrows_quote = self.native_borrows().to_num::<f64>() * price.to_num::<f64>();
        if borrows_quote <= self.borrow_weight_scale_start_quote {
            self.init_liab_weight
        } else if self.borrow_weight_scale_start_quote == 0.0 {
            // TODO: will certainly cause overflow, so it's not exactly what is needed; health should be -MAX?
            // maybe handling this case isn't super helpful?
            I80F48::MAX
        } else {
            // The next line is around 500 CU
            let scale = borrows_quote / self.borrow_weight_scale_start_quote;
            self.init_liab_weight * I80F48::from_num(scale)
        }
    }

    /// Grows potential_serum_tokens if new > old, shrinks it otherwise
    #[inline(always)]
    pub fn update_potential_serum_tokens(&mut self, old: u64, new: u64) {
        if new >= old {
            self.potential_serum_tokens += new - old;
        } else {
            self.potential_serum_tokens = self.potential_serum_tokens.saturating_sub(old - new);
        }
    }

    /// Grows potential_openbook_tokens if new > old, shrinks it otherwise
    #[inline(always)]
    pub fn update_potential_openbook_tokens(&mut self, old: u64, new: u64) {
        if new >= old {
            self.potential_openbook_tokens += new - old;
        } else {
            self.potential_openbook_tokens =
                self.potential_openbook_tokens.saturating_sub(old - new);
        }
    }
}

#[macro_export]
macro_rules! bank_seeds {
    ( $bank:expr ) => {
        &[
            b"Bank".as_ref(),
            $bank.group.as_ref(),
            $bank.token_index.to_le_bytes(),
            &bank.bank_num.to_le_bytes(),
            &[$bank.bump],
        ]
    };
}

pub use bank_seeds;

#[cfg(test)]
mod tests {
    use bytemuck::Zeroable;
    use std::cmp::min;

    use super::*;

    fn bank_change_runner(start: f64, change: i32, is_in_use: bool, use_withdraw: bool) {
        println!(
            "testing: in use: {is_in_use}, start: {start}, change: {change}, use_withdraw: {use_withdraw}",
        );

        let epsilon = I80F48::from_bits(1);

        //
        // SETUP
        //

        let mut bank = Bank::zeroed();
        bank.net_borrow_limit_window_size_ts = 1; // dummy
        bank.net_borrow_limit_per_window_quote = i64::MAX; // max since we don't want this to interfere
        bank.deposit_index = I80F48::from_num(100.0);
        bank.borrow_index = I80F48::from_num(10.0);
        bank.loan_origination_fee_rate = I80F48::from_num(0.1);
        let indexed = |v: I80F48, b: &Bank| {
            if v > 0 {
                let i = v / b.deposit_index;
                if i * b.deposit_index < v {
                    i + I80F48::DELTA
                } else {
                    i
                }
            } else {
                v / b.borrow_index
            }
        };

        let mut account = TokenPosition {
            indexed_position: I80F48::ZERO,
            token_index: 0,
            in_use_count: u16::from(is_in_use),
            cumulative_deposit_interest: 0.0,
            cumulative_borrow_interest: 0.0,
            previous_index: I80F48::ZERO,
            padding: Default::default(),
            reserved: [0; 128],
        };

        account.indexed_position = indexed(I80F48::from_num(start), &bank);
        if start >= 0.0 {
            bank.indexed_deposits = account.indexed_position;
        } else {
            bank.indexed_borrows = -account.indexed_position;
        }

        // get the rounded start value
        let start_native = account.native(&bank);

        //
        // TEST
        //

        let change = I80F48::from(change);
        let dummy_now_ts = 1 as u64;
        let dummy_price = I80F48::ZERO;
        let is_active = if use_withdraw {
            bank.withdraw_with_fee(&mut account, change, dummy_now_ts)
                .unwrap()
                .position_is_active
        } else {
            bank.change_with_fee(&mut account, change, dummy_now_ts)
                .unwrap()
                .position_is_active
        };

        let mut expected_native = start_native + change;
        let is_deposit_into_nonnegative = start >= 0.0 && change >= 0 && !use_withdraw;
        if expected_native >= 0.0
            && expected_native < 1.0
            && !is_in_use
            && !is_deposit_into_nonnegative
        {
            assert!(!is_active);
            assert_eq!(bank.dust, expected_native);
            expected_native = I80F48::ZERO;
        } else {
            assert!(is_active);
            assert_eq!(bank.dust, I80F48::ZERO);
        }
        if change < 0 && expected_native < 0 {
            let new_borrow = -(expected_native - min(start_native, I80F48::ZERO));
            expected_native -= new_borrow * bank.loan_origination_fee_rate;
        }
        let expected_indexed = indexed(expected_native, &bank);

        // at most one epsilon error in the resulting indexed value
        assert!((account.indexed_position - expected_indexed).abs() <= epsilon);

        if account.indexed_position.is_positive() {
            assert_eq!(bank.indexed_deposits, account.indexed_position);
            assert_eq!(bank.indexed_borrows, I80F48::ZERO);
        } else {
            assert_eq!(bank.indexed_deposits, I80F48::ZERO);
            assert_eq!(bank.indexed_borrows, -account.indexed_position);
        }
    }

    #[test]
    pub fn bank_change() -> Result<()> {
        let cases = [
            (-10.1, 1),
            (-10.1, 10),
            (-10.1, 11),
            (-10.1, 50),
            (-10.0, 10),
            (-10.0, 11),
            (-2.0, 2),
            (-2.0, 3),
            (-0.1, 1),
            (0.0, 1),
            (0.1, 1),
            (10.1, -1),
            (10.1, -9),
            (10.1, -10),
            (10.1, -11),
            (10.0, -10),
            (10.0, -9),
            (1.0, -1),
            (0.1, -1),
            (0.0, -1),
            (-0.1, -1),
            (-1.1, -10),
            (10.0, 0),
            (1.0, 0),
            (0.1, 0),
            (0.0, 0),
            (-0.1, 0),
        ];

        for is_in_use in [false, true] {
            for (start, change) in cases {
                bank_change_runner(start, change, is_in_use, false);
                if change == 0 {
                    // check withdrawing 0
                    bank_change_runner(start, change, is_in_use, true);
                }
            }
        }
        Ok(())
    }

    #[test]
    fn bank_transfer() {
        //
        // SETUP
        //

        let mut bank_proto = Bank::zeroed();
        bank_proto.net_borrow_limit_window_size_ts = 1; // dummy
        bank_proto.net_borrow_limit_per_window_quote = i64::MAX; // max since we don't want this to interfere
        bank_proto.deposit_index = I80F48::from(1_234_567);
        bank_proto.borrow_index = I80F48::from(1_234_567);
        bank_proto.loan_origination_fee_rate = I80F48::from_num(0.1);

        let account_proto = TokenPosition {
            indexed_position: I80F48::ZERO,
            token_index: 0,
            in_use_count: 1,
            cumulative_deposit_interest: 0.0,
            cumulative_borrow_interest: 0.0,
            previous_index: I80F48::ZERO,
            padding: Default::default(),
            reserved: [0; 128],
        };

        //
        // TESTS
        //

        // simple transfer
        {
            let mut bank = bank_proto.clone();
            let mut a1 = account_proto.clone();
            let mut a2 = account_proto.clone();

            let amount = I80F48::from(100);
            bank.deposit(&mut a1, amount, 0).unwrap();
            let damount = a1.native(&bank);
            let r = bank
                .checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                .unwrap();
            assert_eq!(a2.native(&bank), damount);
            assert!(r.source_is_active);
            assert!(r.target_is_active);
        }

        // borrow limits
        {
            let mut bank = bank_proto.clone();
            bank.net_borrow_limit_per_window_quote = 100;
            bank.loan_origination_fee_rate = I80F48::ZERO;
            let mut a1 = account_proto.clone();
            let mut a2 = account_proto.clone();

            {
                let mut b = bank.clone();
                let amount = I80F48::from(101);
                assert!(b
                    .checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .is_err());
            }

            {
                let mut b = bank.clone();
                let amount = I80F48::from(100);
                b.checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .unwrap();
            }

            {
                let mut b = bank.clone();
                let amount = b.remaining_net_borrows_quote(I80F48::ONE);
                b.checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .unwrap();
            }
        }

        // deposit limits
        {
            let mut bank = bank_proto.clone();
            bank.deposit_limit = 100;
            let mut a1 = account_proto.clone();
            let mut a2 = account_proto.clone();

            {
                let mut b = bank.clone();
                let amount = I80F48::from(101);
                assert!(b
                    .checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .is_err());
            }

            {
                // still bad because deposit() adds DELTA more than requested
                let mut b = bank.clone();
                let amount = I80F48::from(100);
                assert!(b
                    .checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .is_err());
            }

            {
                let mut b = bank.clone();
                let amount = I80F48::from_num(99.999);
                b.checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .unwrap();
            }

            {
                let mut b = bank.clone();
                let amount = b.remaining_deposits_until_limit();
                b.checked_transfer_with_fee(&mut a1, amount, &mut a2, amount, 0, I80F48::ONE)
                    .unwrap();
            }
        }

        // reducing transfer while limits exceeded
        {
            let mut bank = bank_proto.clone();
            bank.loan_origination_fee_rate = I80F48::ZERO;

            let amount = I80F48::from(100);
            let mut a1 = account_proto.clone();
            bank.deposit(&mut a1, amount, 0).unwrap();
            let mut a2 = account_proto.clone();
            bank.withdraw_with_fee(&mut a2, amount, 0).unwrap();

            bank.net_borrow_limit_per_window_quote = 100;
            bank.net_borrows_in_window = 200;
            bank.deposit_limit = 100;
            bank.potential_serum_tokens = 100;
            bank.potential_openbook_tokens = 100;

            let half = I80F48::from(50);
            bank.checked_transfer_with_fee(&mut a1, half, &mut a2, half, 0, I80F48::ONE)
                .unwrap();
            bank.checked_transfer_with_fee(&mut a1, half, &mut a2, half, 0, I80F48::ONE)
                .unwrap();
            assert!(bank
                .checked_transfer_with_fee(&mut a1, half, &mut a2, half, 0, I80F48::ONE)
                .is_err());
        }
    }

    #[test]
    fn test_compute_new_avg_utilization() {
        let mut bank = Bank::zeroed();
        bank.deposit_index = I80F48::from_num(1.0);
        bank.borrow_index = I80F48::from_num(1.0);
        bank.bank_rate_last_updated = 1000;
        bank.index_last_updated = 1000;

        let compute_new_avg_utilization_runner =
            |bank: &mut Bank, utilization: I80F48, now_ts: u64| {
                bank.avg_utilization =
                    bank.compute_new_avg_utilization(I80F48::ONE, utilization, now_ts);
                bank.index_last_updated = now_ts;
            };

        compute_new_avg_utilization_runner(&mut bank, I80F48::ZERO, 1000);
        assert_eq!(bank.avg_utilization, I80F48::ZERO);

        compute_new_avg_utilization_runner(&mut bank, I80F48::from_num(0.5), 1010);
        assert!((bank.avg_utilization - I80F48::from_num(0.5)).abs() < 0.0001);

        compute_new_avg_utilization_runner(&mut bank, I80F48::from_num(0.8), 1015);
        assert!((bank.avg_utilization - I80F48::from_num(0.6)).abs() < 0.0001);

        compute_new_avg_utilization_runner(&mut bank, I80F48::ONE, 1020);
        assert!((bank.avg_utilization - I80F48::from_num(0.7)).abs() < 0.0001);

        bank.bank_rate_last_updated = 1020;
        compute_new_avg_utilization_runner(&mut bank, I80F48::ONE, 1040);
        assert_eq!(bank.avg_utilization, I80F48::ONE);
    }

    #[test]
    pub fn test_net_borrows() -> Result<()> {
        let mut bank = Bank::zeroed();
        bank.net_borrow_limit_window_size_ts = 100;
        bank.net_borrow_limit_per_window_quote = 1000;
        bank.deposit_index = I80F48::from_num(100.0);
        bank.borrow_index = I80F48::from_num(100.0);

        let price = I80F48::from(2);

        let mut account = TokenPosition::default();

        bank.change_without_fee(&mut account, I80F48::from(100), 0)
            .unwrap();
        assert_eq!(bank.net_borrows_in_window, 0);
        bank.change_without_fee(&mut account, I80F48::from(-100), 0)
            .unwrap();
        assert_eq!(bank.net_borrows_in_window, 0);

        account = TokenPosition::default();
        bank.change_without_fee(&mut account, I80F48::from(10), 0)
            .unwrap();
        bank.change_without_fee(&mut account, I80F48::from(-110), 0)
            .unwrap();
        assert_eq!(bank.net_borrows_in_window, 100);
        bank.change_without_fee(&mut account, I80F48::from(50), 0)
            .unwrap();
        assert_eq!(bank.net_borrows_in_window, 50);
        bank.change_without_fee(&mut account, I80F48::from(100), 0)
            .unwrap();
        assert_eq!(bank.net_borrows_in_window, 1); // rounding

        account = TokenPosition::default();
        bank.net_borrows_in_window = 0;
        bank.change_without_fee(&mut account, I80F48::from(-450), 0)
            .unwrap();
        bank.change_without_fee(&mut account, I80F48::from(-51), 0)
            .unwrap();
        bank.check_net_borrows(price).unwrap_err();

        account = TokenPosition::default();
        bank.net_borrows_in_window = 0;
        bank.change_without_fee(&mut account, I80F48::from(-450), 0)
            .unwrap();
        bank.change_without_fee(&mut account, I80F48::from(-50), 0)
            .unwrap();
        bank.change_without_fee(&mut account, I80F48::from(-50), 101)
            .unwrap();

        Ok(())
    }

    #[test]
    pub fn test_bank_maint_weight_shift() -> Result<()> {
        let mut bank = Bank::zeroed();
        bank.maint_asset_weight = I80F48::ONE;
        bank.maint_liab_weight = I80F48::ZERO;
        bank.maint_weight_shift_start = 100;
        bank.maint_weight_shift_end = 1100;
        bank.maint_weight_shift_duration_inv = I80F48::ONE / I80F48::from(1000);
        bank.maint_weight_shift_asset_target = I80F48::from(2);
        bank.maint_weight_shift_liab_target = I80F48::from(10);

        let (a, l) = bank.maint_weights(0);
        assert_eq!(a, 1.0);
        assert_eq!(l, 0.0);

        let (a, l) = bank.maint_weights(100);
        assert_eq!(a, 1.0);
        assert_eq!(l, 0.0);

        let (a, l) = bank.maint_weights(1100);
        assert_eq!(a, 2.0);
        assert_eq!(l, 10.0);

        let (a, l) = bank.maint_weights(2000);
        assert_eq!(a, 2.0);
        assert_eq!(l, 10.0);

        let abs_diff = |x: I80F48, y: f64| (x.to_num::<f64>() - y).abs();

        let (a, l) = bank.maint_weights(600);
        assert!(abs_diff(a, 1.5) < 1e-8);
        assert!(abs_diff(l, 5.0) < 1e-8);

        let (a, l) = bank.maint_weights(200);
        assert!(abs_diff(a, 1.1) < 1e-8);
        assert!(abs_diff(l, 1.0) < 1e-8);

        let (a, l) = bank.maint_weights(1000);
        assert!(abs_diff(a, 1.9) < 1e-8);
        assert!(abs_diff(l, 9.0) < 1e-8);

        Ok(())
    }

    #[test]
    pub fn test_bank_interest() -> Result<()> {
        let index_start = I80F48::from(1_000_000);

        let mut bank = Bank::zeroed();
        bank.util0 = I80F48::from_num(0.5);
        bank.rate0 = I80F48::from_num(0.02);
        bank.util1 = I80F48::from_num(0.75);
        bank.rate1 = I80F48::from_num(0.05);
        bank.max_rate = I80F48::from_num(0.5);
        bank.interest_curve_scaling = 4.0;
        bank.deposit_index = index_start;
        bank.borrow_index = index_start;
        bank.net_borrow_limit_window_size_ts = 1;

        let mut position0 = TokenPosition::default();
        let mut position1 = TokenPosition::default();

        // create 100% utilization, meaning 0.5 * 4 = 200% interest
        bank.deposit(&mut position0, I80F48::from(1_000_000_000), 0)
            .unwrap();
        bank.withdraw_without_fee(&mut position1, I80F48::from(1_000_000_000), 0)
            .unwrap();

        // accumulate interest for a day at 5s intervals
        let interval = 5;
        for i in 0..24 * 60 * 60 / interval {
            let (deposit_index, borrow_index, borrow_fees, borrow_rate, deposit_rate) = bank
                .compute_index(
                    bank.indexed_deposits,
                    bank.indexed_borrows,
                    I80F48::from(interval),
                )
                .unwrap();
            bank.deposit_index = deposit_index;
            bank.borrow_index = borrow_index;
        }

        // the 5s rate is 2/(365*24*60*60/5), so
        // expected is (1+five_sec_rate)^(24*60*60/5)
        assert!(
            ((bank.deposit_index / index_start).to_num::<f64>() - 1.0054944908).abs() < 0.0000001
        );
        assert!(
            ((bank.borrow_index / index_start).to_num::<f64>() - 1.0054944908).abs() < 0.0000001
        );

        Ok(())
    }

    #[test]
    fn test_bank_interest_rate_curve() {
        let mut bank = Bank::zeroed();
        bank.zero_util_rate = I80F48::from(1);
        bank.rate0 = I80F48::from(3);
        bank.rate1 = I80F48::from(7);
        bank.max_rate = I80F48::from(13);

        bank.util0 = I80F48::from_num(0.5);
        bank.util1 = I80F48::from_num(0.75);

        let interest = |v: f64| {
            bank.compute_interest_rate(I80F48::from_num(v))
                .to_num::<f64>()
        };
        let d = |a: f64, b: f64| (a - b).abs();

        // the points
        let eps = 0.0001;
        assert!(d(interest(-0.5), 1.0) <= eps);
        assert!(d(interest(0.0), 1.0) <= eps);
        assert!(d(interest(0.5), 3.0) <= eps);
        assert!(d(interest(0.75), 7.0) <= eps);
        assert!(d(interest(1.0), 13.0) <= eps);
        assert!(d(interest(1.5), 13.0) <= eps);

        // midpoints
        assert!(d(interest(0.25), 2.0) <= eps);
        assert!(d(interest((0.5 + 0.75) / 2.0), 5.0) <= eps);
        assert!(d(interest((0.75 + 1.0) / 2.0), 10.0) <= eps);

        // around the points
        let delta = 0.000001;
        assert!(d(interest(0.0 + delta), 1.0) <= eps);
        assert!(d(interest(0.5 - delta), 3.0) <= eps);
        assert!(d(interest(0.5 + delta), 3.0) <= eps);
        assert!(d(interest(0.75 - delta), 7.0) <= eps);
        assert!(d(interest(0.75 + delta), 7.0) <= eps);
        assert!(d(interest(1.0 - delta), 13.0) <= eps);
    }
}