kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
Documentation
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
//! Bonding curve implementations for token pricing

use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

/// Trait for bonding curve implementations
pub trait BondingCurve: Send + Sync {
    /// Calculate the cost to buy `amount` tokens at current `supply`
    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal;

    /// Calculate the proceeds from selling `amount` tokens at current `supply`
    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal;

    /// Get the current spot price at given supply
    fn spot_price(&self, current_supply: Decimal) -> Decimal;

    /// Calculate price impact percentage for a buy order
    fn buy_price_impact(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount == dec!(0) {
            return dec!(0);
        }
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);
        if spot_before == dec!(0) {
            return dec!(0);
        }
        ((spot_after - spot_before) / spot_before) * dec!(100)
    }

    /// Calculate price impact percentage for a sell order
    fn sell_price_impact(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount == dec!(0) || current_supply <= amount {
            return dec!(0);
        }
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply - amount);
        if spot_before == dec!(0) {
            return dec!(0);
        }
        ((spot_before - spot_after) / spot_before) * dec!(100)
    }

    /// Calculate average price per token for a buy order
    fn avg_buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount == dec!(0) {
            return dec!(0);
        }
        self.buy_price(current_supply, amount) / amount
    }

    /// Calculate average price per token for a sell order
    fn avg_sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount == dec!(0) {
            return dec!(0);
        }
        self.sell_price(current_supply, amount) / amount
    }

    /// Check if price is within slippage tolerance
    fn check_buy_slippage(
        &self,
        current_supply: Decimal,
        amount: Decimal,
        max_price: Decimal,
    ) -> bool {
        let avg_price = self.avg_buy_price(current_supply, amount);
        avg_price <= max_price
    }

    /// Check if sell price is within slippage tolerance
    fn check_sell_slippage(
        &self,
        current_supply: Decimal,
        amount: Decimal,
        min_price: Decimal,
    ) -> bool {
        let avg_price = self.avg_sell_price(current_supply, amount);
        avg_price >= min_price
    }
}

/// Price quote for an order
#[derive(Debug, Clone, Serialize)]
pub struct PriceQuote {
    /// Amount of tokens
    pub amount: Decimal,
    /// Total cost/proceeds in BTC
    pub total_btc: Decimal,
    /// Average price per token
    pub avg_price_btc: Decimal,
    /// Current spot price before trade
    pub spot_price_before: Decimal,
    /// Spot price after trade
    pub spot_price_after: Decimal,
    /// Price impact percentage
    pub price_impact_percent: Decimal,
}

impl PriceQuote {
    /// Check if price impact exceeds a threshold
    pub fn is_high_impact(&self, threshold_percent: Decimal) -> bool {
        self.price_impact_percent.abs() >= threshold_percent
    }

    /// Get effective spread (difference between spot before and average price)
    pub fn effective_spread_percent(&self) -> Decimal {
        if self.spot_price_before == dec!(0) {
            return dec!(0);
        }
        ((self.avg_price_btc - self.spot_price_before) / self.spot_price_before).abs() * dec!(100)
    }
}

/// Curve parameter validator
pub struct CurveValidator;

impl CurveValidator {
    /// Validate initial price (must be positive and reasonable)
    pub fn validate_initial_price(price: Decimal) -> Result<(), String> {
        if price <= dec!(0) {
            return Err("Initial price must be positive".to_string());
        }
        if price < dec!(0.00000001) {
            return Err("Initial price is too small (min 0.00000001 BTC)".to_string());
        }
        if price > dec!(100) {
            return Err("Initial price is too high (max 100 BTC)".to_string());
        }
        Ok(())
    }

    /// Validate increment/growth rate parameters
    pub fn validate_growth_rate(rate: Decimal, curve_type: &str) -> Result<(), String> {
        match curve_type {
            "linear" => {
                if rate < dec!(0) {
                    return Err("Linear increment cannot be negative".to_string());
                }
                if rate > dec!(1) {
                    return Err("Linear increment is too high (max 1 BTC)".to_string());
                }
            }
            "exponential" => {
                if rate <= dec!(0) {
                    return Err("Exponential growth rate must be positive".to_string());
                }
                if rate > dec!(0.1) {
                    return Err("Exponential growth rate is too high (max 0.1)".to_string());
                }
            }
            "bancor" => {
                if rate <= dec!(0) || rate > dec!(1) {
                    return Err("Reserve ratio must be between 0 and 1".to_string());
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Validate scale factor (must be positive)
    pub fn validate_scale_factor(factor: Decimal) -> Result<(), String> {
        if factor <= dec!(0) {
            return Err("Scale factor must be positive".to_string());
        }
        if factor < dec!(0.1) {
            return Err("Scale factor is too small (min 0.1)".to_string());
        }
        Ok(())
    }
}

/// Curve comparison utilities
pub struct CurveComparator;

impl CurveComparator {
    /// Compare two curves at a given supply level
    pub fn compare_spot_prices<C1: BondingCurve, C2: BondingCurve>(
        curve1: &C1,
        curve2: &C2,
        supply: Decimal,
    ) -> CurveComparison {
        let price1 = curve1.spot_price(supply);
        let price2 = curve2.spot_price(supply);

        let difference = price1 - price2;
        let percent_difference = if price2 != dec!(0) {
            (difference / price2) * dec!(100)
        } else {
            dec!(0)
        };

        CurveComparison {
            supply,
            price1,
            price2,
            difference,
            percent_difference,
        }
    }

    /// Calculate price elasticity (% change in price / % change in supply)
    pub fn calculate_elasticity<C: BondingCurve>(
        curve: &C,
        supply: Decimal,
        delta_percent: Decimal,
    ) -> Decimal {
        let delta = supply * delta_percent / dec!(100);
        let new_supply = supply + delta;

        let price_before = curve.spot_price(supply);
        let price_after = curve.spot_price(new_supply);

        if price_before == dec!(0) {
            return dec!(0);
        }

        let price_change_percent = ((price_after - price_before) / price_before) * dec!(100);
        if delta_percent != dec!(0) {
            price_change_percent / delta_percent
        } else {
            dec!(0)
        }
    }

    /// Find the supply level where a curve reaches a target price
    pub fn find_price_target<C: BondingCurve>(
        curve: &C,
        target_price: Decimal,
        max_supply: Decimal,
        tolerance: Decimal,
    ) -> Option<Decimal> {
        // Binary search for supply level
        let mut low = dec!(0);
        let mut high = max_supply;
        let mut iterations = 0;
        let max_iterations = 100;

        while iterations < max_iterations && (high - low) > tolerance {
            let mid = (low + high) / dec!(2);
            let price = curve.spot_price(mid);

            if (price - target_price).abs() < tolerance {
                return Some(mid);
            }

            if price < target_price {
                low = mid;
            } else {
                high = mid;
            }

            iterations += 1;
        }

        None
    }
}

/// Result of comparing two curves
#[derive(Debug, Clone, Serialize)]
pub struct CurveComparison {
    /// Supply level at which the comparison was made
    pub supply: Decimal,
    /// Spot price from the first curve
    pub price1: Decimal,
    /// Spot price from the second curve
    pub price2: Decimal,
    /// Absolute price difference (price1 - price2)
    pub difference: Decimal,
    /// Percentage difference relative to price2
    pub percent_difference: Decimal,
}

/// Linear bonding curve: P(n) = initial_price + (n * increment)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinearBondingCurve {
    /// Starting price at zero supply
    pub initial_price: Decimal,
    /// Price increase per unit of additional supply
    pub increment: Decimal,
}

impl LinearBondingCurve {
    /// Create a new linear bonding curve
    pub fn new(initial_price: Decimal, increment: Decimal) -> Self {
        Self {
            initial_price,
            increment,
        }
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply - amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }
}

impl BondingCurve for LinearBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        self.initial_price + (current_supply * self.increment)
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        // Integral of P(n) from current_supply to current_supply + amount
        // = amount * initial_price + increment * (amount * (2*supply + amount - 1) / 2)
        let base_cost = amount * self.initial_price;
        let incremental_cost =
            self.increment * amount * (dec!(2) * current_supply + amount - dec!(1)) / dec!(2);
        base_cost + incremental_cost
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        // Same as buy but from (supply - amount) to supply, with 5% spread
        let new_supply = current_supply - amount;
        let base_value = amount * self.initial_price;
        let incremental_value =
            self.increment * amount * (dec!(2) * new_supply + amount - dec!(1)) / dec!(2);
        (base_value + incremental_value) * dec!(0.95) // 5% spread
    }
}

/// Bancor bonding curve using reserve ratio formula
/// P(n) = reserve_balance / (supply * reserve_ratio)
/// This is an approximation using polynomial expansion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BancorBondingCurve {
    /// Reserve balance in BTC (virtual reserve)
    pub reserve_balance: Decimal,
    /// Reserve ratio (0-1), typically 0.1-0.5
    /// Higher ratio = lower price volatility
    pub reserve_ratio: Decimal,
    /// Initial supply for calculations
    pub initial_supply: Decimal,
}

impl BancorBondingCurve {
    /// Create a new Bancor bonding curve
    pub fn new(reserve_balance: Decimal, reserve_ratio: Decimal, initial_supply: Decimal) -> Self {
        Self {
            reserve_balance,
            reserve_ratio: reserve_ratio.clamp(dec!(0.05), dec!(1)),
            initial_supply,
        }
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = if current_supply > amount {
            self.spot_price(current_supply - amount)
        } else {
            dec!(0)
        };

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }

    /// Calculate power approximation: (1 + x)^n using Taylor series
    /// For small x, this gives a good approximation
    fn power_approx(base: Decimal, exponent: Decimal) -> Decimal {
        // For (1+x)^n where x is small, use binomial expansion
        // (1+x)^n ≈ 1 + nx + n(n-1)x²/2 + ...
        let x = base - dec!(1);
        if x.abs() < dec!(0.5) {
            let term1 = dec!(1);
            let term2 = exponent * x;
            let term3 = exponent * (exponent - dec!(1)) * x * x / dec!(2);
            let term4 =
                exponent * (exponent - dec!(1)) * (exponent - dec!(2)) * x * x * x / dec!(6);
            (term1 + term2 + term3 + term4).max(dec!(0.001))
        } else {
            // For larger values, use simpler approximation
            // Convert to f64 for pow, then back
            let base_f64: f64 = base.try_into().unwrap_or(1.0);
            let exp_f64: f64 = exponent.try_into().unwrap_or(1.0);
            let result = base_f64.powf(exp_f64);
            Decimal::try_from(result).unwrap_or(dec!(1))
        }
    }
}

impl BondingCurve for BancorBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        let effective_supply = self.initial_supply + current_supply;
        if effective_supply == dec!(0) || self.reserve_ratio == dec!(0) {
            return dec!(0);
        }
        self.reserve_balance / (effective_supply * self.reserve_ratio)
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) {
            return dec!(0);
        }
        let effective_supply = self.initial_supply + current_supply;
        if effective_supply == dec!(0) {
            return dec!(0);
        }

        // Bancor formula: cost = reserve * ((1 + amount/supply)^(1/rr) - 1)
        let supply_ratio = dec!(1) + amount / effective_supply;
        let exponent = dec!(1) / self.reserve_ratio;
        let power = Self::power_approx(supply_ratio, exponent);

        self.reserve_balance * (power - dec!(1))
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || amount > current_supply {
            return dec!(0);
        }
        let effective_supply = self.initial_supply + current_supply;

        // Bancor formula: proceeds = reserve * (1 - (1 - amount/supply)^(1/rr))
        let supply_ratio = dec!(1) - amount / effective_supply;
        let exponent = dec!(1) / self.reserve_ratio;
        let power = Self::power_approx(supply_ratio, exponent);

        // Apply 5% spread
        self.reserve_balance * (dec!(1) - power) * dec!(0.95)
    }
}

/// Exponential bonding curve: P(n) = initial_price * e^(k * n)
/// Uses polynomial approximation for e^x
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExponentialBondingCurve {
    /// Starting price at zero supply
    pub initial_price: Decimal,
    /// Growth rate (k). Smaller = gentler curve.
    pub growth_rate: Decimal,
}

impl ExponentialBondingCurve {
    /// Create a new exponential bonding curve
    pub fn new(initial_price: Decimal, growth_rate: Decimal) -> Self {
        Self {
            initial_price,
            growth_rate: growth_rate.clamp(dec!(0.0001), dec!(0.1)),
        }
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = if current_supply > amount {
            self.spot_price(current_supply - amount)
        } else {
            dec!(0)
        };

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }

    /// Approximate e^x using Taylor series
    fn exp_approx(x: Decimal) -> Decimal {
        // e^x ≈ 1 + x + x²/2 + x³/6 + x⁴/24
        let x2 = x * x;
        let x3 = x2 * x;
        let x4 = x3 * x;
        let x5 = x4 * x;

        let result = dec!(1) + x + x2 / dec!(2) + x3 / dec!(6) + x4 / dec!(24) + x5 / dec!(120);
        result.max(dec!(0.0001))
    }
}

impl BondingCurve for ExponentialBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        let exponent = self.growth_rate * current_supply;
        // Clamp exponent to avoid overflow
        let clamped_exp = exponent.clamp(dec!(-10), dec!(10));
        self.initial_price * Self::exp_approx(clamped_exp)
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || self.growth_rate == dec!(0) {
            return self.initial_price * amount;
        }

        // Integral of P * e^(k*n) from supply to supply+amount
        // = (P/k) * (e^(k*(supply+amount)) - e^(k*supply))
        let k = self.growth_rate;
        let exp_start = Self::exp_approx((k * current_supply).clamp(dec!(-10), dec!(10)));
        let exp_end = Self::exp_approx((k * (current_supply + amount)).clamp(dec!(-10), dec!(10)));

        (self.initial_price / k) * (exp_end - exp_start)
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || amount > current_supply || self.growth_rate == dec!(0) {
            return self.initial_price * amount * dec!(0.95);
        }

        let new_supply = current_supply - amount;
        let k = self.growth_rate;
        let exp_start = Self::exp_approx((k * new_supply).clamp(dec!(-10), dec!(10)));
        let exp_end = Self::exp_approx((k * current_supply).clamp(dec!(-10), dec!(10)));

        // Apply 5% spread
        (self.initial_price / k) * (exp_end - exp_start) * dec!(0.95)
    }
}

/// Sigmoid bonding curve: P(n) = max_price / (1 + e^(-k*(n - midpoint)))
/// Creates S-curve pricing that caps at max_price
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SigmoidBondingCurve {
    /// Maximum price the curve approaches
    pub max_price: Decimal,
    /// Growth rate (steepness of curve)
    pub growth_rate: Decimal,
    /// Supply midpoint where price = max_price/2
    pub midpoint: Decimal,
}

impl SigmoidBondingCurve {
    /// Create a new sigmoid bonding curve
    pub fn new(max_price: Decimal, growth_rate: Decimal, midpoint: Decimal) -> Self {
        Self {
            max_price,
            growth_rate: growth_rate.clamp(dec!(0.001), dec!(0.1)),
            midpoint,
        }
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = if current_supply > amount {
            self.spot_price(current_supply - amount)
        } else {
            dec!(0)
        };

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }

    /// Sigmoid function: 1 / (1 + e^-x)
    fn sigmoid(x: Decimal) -> Decimal {
        let clamped = x.clamp(dec!(-10), dec!(10));
        let exp_neg_x = ExponentialBondingCurve::exp_approx(-clamped);
        dec!(1) / (dec!(1) + exp_neg_x)
    }
}

impl BondingCurve for SigmoidBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        let x = self.growth_rate * (current_supply - self.midpoint);
        self.max_price * Self::sigmoid(x)
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) {
            return dec!(0);
        }

        // Numerical integration using trapezoidal rule
        let steps = 100i32;
        let step_size = amount / Decimal::from(steps);
        let mut total = dec!(0);

        for i in 0..steps {
            let n = current_supply + step_size * Decimal::from(i);
            let price_start = self.spot_price(n);
            let price_end = self.spot_price(n + step_size);
            total += (price_start + price_end) / dec!(2) * step_size;
        }

        total
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || amount > current_supply {
            return dec!(0);
        }

        let new_supply = current_supply - amount;

        // Numerical integration
        let steps = 100i32;
        let step_size = amount / Decimal::from(steps);
        let mut total = dec!(0);

        for i in 0..steps {
            let n = new_supply + step_size * Decimal::from(i);
            let price_start = self.spot_price(n);
            let price_end = self.spot_price(n + step_size);
            total += (price_start + price_end) / dec!(2) * step_size;
        }

        // Apply 5% spread
        total * dec!(0.95)
    }
}

/// Phase of the adaptive bonding curve
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum CurvePhase {
    /// Early adopter phase: exponential growth (rewards early buyers)
    EarlyAdopter,
    /// Growth phase: linear growth (fair distribution)
    Growth,
    /// Maturity phase: sigmoid (price stabilization)
    Maturity,
}

/// Adaptive bonding curve that transitions through phases
/// - Early Adopter (0 - phase1_threshold): Exponential growth
/// - Growth (phase1_threshold - phase2_threshold): Linear growth
/// - Maturity (> phase2_threshold): Sigmoid approach to max
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveBondingCurve {
    /// Base initial price
    pub initial_price: Decimal,
    /// Maximum price the curve can reach
    pub max_price: Decimal,
    /// Supply threshold to transition from Early to Growth
    pub phase1_threshold: Decimal,
    /// Supply threshold to transition from Growth to Maturity
    pub phase2_threshold: Decimal,
    /// Total supply cap
    pub total_supply: Decimal,
}

impl AdaptiveBondingCurve {
    /// Create a new adaptive bonding curve with explicit phase thresholds
    pub fn new(
        initial_price: Decimal,
        max_price: Decimal,
        phase1_threshold: Decimal,
        phase2_threshold: Decimal,
        total_supply: Decimal,
    ) -> Self {
        Self {
            initial_price,
            max_price,
            phase1_threshold: phase1_threshold.max(dec!(1)),
            phase2_threshold: phase2_threshold.max(phase1_threshold + dec!(1)),
            total_supply,
        }
    }

    /// Create with default thresholds based on total supply
    /// Phase 1 at 10%, Phase 2 at 50%
    pub fn with_defaults(
        initial_price: Decimal,
        max_price: Decimal,
        total_supply: Decimal,
    ) -> Self {
        Self::new(
            initial_price,
            max_price,
            total_supply * dec!(0.1), // 10% for early adopter
            total_supply * dec!(0.5), // 50% for growth phase
            total_supply,
        )
    }

    /// Determine current phase based on supply
    pub fn get_phase(&self, current_supply: Decimal) -> CurvePhase {
        if current_supply < self.phase1_threshold {
            CurvePhase::EarlyAdopter
        } else if current_supply < self.phase2_threshold {
            CurvePhase::Growth
        } else {
            CurvePhase::Maturity
        }
    }

    /// Get price at phase 1 threshold (for continuity)
    fn price_at_phase1(&self) -> Decimal {
        self.early_adopter_price(self.phase1_threshold)
    }

    /// Get price at phase 2 threshold (for continuity)
    fn price_at_phase2(&self) -> Decimal {
        self.growth_price(self.phase2_threshold)
    }

    /// Early adopter phase pricing: exponential growth
    fn early_adopter_price(&self, supply: Decimal) -> Decimal {
        // Use simple exponential growth: P(n) = initial_price * (1 + growth_rate)^n
        // This provides reliable increasing prices
        if supply <= dec!(0) {
            return self.initial_price;
        }

        // Calculate growth rate to reach 3x at phase1_threshold
        // (1 + r)^threshold = 3, so r = 3^(1/threshold) - 1
        // For small r, approximate: r ≈ ln(3) / threshold ≈ 1.1 / threshold
        let growth_per_unit = dec!(1.1) / self.phase1_threshold.max(dec!(1));

        // P(n) = initial * (1 + n * growth_per_unit)
        // This is linearized exponential growth that's simpler and always increasing
        let multiplier = dec!(1) + supply * growth_per_unit;
        self.initial_price * multiplier.min(dec!(3))
    }

    /// Growth phase pricing: linear growth
    fn growth_price(&self, supply: Decimal) -> Decimal {
        // Linear from price_at_phase1 to a target price at phase2
        let start_price = self.price_at_phase1();
        let target_price = self.max_price * dec!(0.7); // Target 70% of max at end of growth

        let supply_in_phase = supply - self.phase1_threshold;
        let phase_length = self.phase2_threshold - self.phase1_threshold;

        if phase_length <= dec!(0) {
            return start_price;
        }

        let progress = supply_in_phase / phase_length;
        start_price + (target_price - start_price) * progress.min(dec!(1))
    }

    /// Maturity phase pricing: sigmoid approach to max
    fn maturity_price(&self, supply: Decimal) -> Decimal {
        // Sigmoid from price_at_phase2 approaching max_price
        let start_price = self.price_at_phase2();
        let remaining_supply = self.total_supply - self.phase2_threshold;

        if remaining_supply <= dec!(0) {
            return start_price;
        }

        let supply_in_phase = supply - self.phase2_threshold;
        let progress = supply_in_phase / remaining_supply;

        // Sigmoid transformation for smooth approach
        // Using a simple S-curve approximation
        let sigmoid_progress = if progress < dec!(0.5) {
            // First half: accelerating
            dec!(2) * progress * progress
        } else {
            // Second half: decelerating
            dec!(1) - dec!(2) * (dec!(1) - progress) * (dec!(1) - progress)
        };

        start_price + (self.max_price - start_price) * sigmoid_progress.min(dec!(1))
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = if current_supply > amount {
            self.spot_price(current_supply - amount)
        } else {
            dec!(0)
        };

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }
}

impl BondingCurve for AdaptiveBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        match self.get_phase(current_supply) {
            CurvePhase::EarlyAdopter => self.early_adopter_price(current_supply),
            CurvePhase::Growth => self.growth_price(current_supply),
            CurvePhase::Maturity => self.maturity_price(current_supply),
        }
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) {
            return dec!(0);
        }

        // Numerical integration using trapezoidal rule
        // This handles phase transitions smoothly
        let steps = 100i32;
        let step_size = amount / Decimal::from(steps);
        let mut total = dec!(0);

        for i in 0..steps {
            let n = current_supply + step_size * Decimal::from(i);
            let price_start = self.spot_price(n);
            let price_end = self.spot_price(n + step_size);
            total += (price_start + price_end) / dec!(2) * step_size;
        }

        total
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || amount > current_supply {
            return dec!(0);
        }

        let new_supply = current_supply - amount;

        // Numerical integration
        let steps = 100i32;
        let step_size = amount / Decimal::from(steps);
        let mut total = dec!(0);

        for i in 0..steps {
            let n = new_supply + step_size * Decimal::from(i);
            let price_start = self.spot_price(n);
            let price_end = self.spot_price(n + step_size);
            total += (price_start + price_end) / dec!(2) * step_size;
        }

        // Apply 5% spread
        total * dec!(0.95)
    }
}

/// Square root bonding curve: P(n) = initial_price * sqrt(1 + n/scale_factor)
/// Provides gentler price increases than exponential but still rewards early adopters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SquareRootBondingCurve {
    /// Starting price at zero supply
    pub initial_price: Decimal,
    /// Scale factor controlling curve steepness. Higher = gentler curve.
    pub scale_factor: Decimal,
}

impl SquareRootBondingCurve {
    /// Create a new square root bonding curve
    pub fn new(initial_price: Decimal, scale_factor: Decimal) -> Self {
        Self {
            initial_price,
            scale_factor: scale_factor.max(dec!(1)),
        }
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = if current_supply > amount {
            self.spot_price(current_supply - amount)
        } else {
            dec!(0)
        };

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }

    /// Approximate square root using Newton's method
    fn sqrt_approx(x: Decimal) -> Decimal {
        if x <= dec!(0) {
            return dec!(0);
        }
        if x == dec!(1) {
            return dec!(1);
        }

        // Convert to f64 for sqrt, then back
        let x_f64: f64 = x.try_into().unwrap_or(1.0);
        let result = x_f64.sqrt();
        Decimal::try_from(result).unwrap_or(dec!(1))
    }

    /// Closed-form integral of P(s) = init * sqrt(1 + s/k) from `supply` to `supply + amount`.
    ///
    /// Antiderivative: F(s) = init * (2k/3) * (1 + s/k)^(3/2)
    /// Definite integral = F(s0 + amount) - F(s0)
    fn sqrt_integral(&self, supply: Decimal, amount: Decimal) -> Decimal {
        let init: f64 = self.initial_price.try_into().unwrap_or(0.0);
        let k: f64 = self.scale_factor.try_into().unwrap_or(1.0);
        let s0: f64 = supply.try_into().unwrap_or(0.0);
        let amt: f64 = amount.try_into().unwrap_or(0.0);

        if k <= 0.0 || amt <= 0.0 {
            return dec!(0);
        }

        let f_upper = (1.0 + (s0 + amt) / k).powf(1.5);
        let f_lower = (1.0 + s0 / k).powf(1.5);
        let total = init * (2.0 * k / 3.0) * (f_upper - f_lower);

        Decimal::try_from(total.max(0.0)).unwrap_or(dec!(0))
    }
}

impl BondingCurve for SquareRootBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        let normalized = dec!(1) + current_supply / self.scale_factor;
        self.initial_price * Self::sqrt_approx(normalized)
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) {
            return dec!(0);
        }

        // Exact closed-form antiderivative of P(s) = init * sqrt(1 + s/k)
        self.sqrt_integral(current_supply, amount)
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || amount > current_supply {
            return dec!(0);
        }

        // Integrate from (current_supply - amount) to current_supply, then apply 5% spread
        self.sqrt_integral(current_supply - amount, amount) * dec!(0.95)
    }
}

/// Logarithmic bonding curve: P(n) = initial_price * (1 + log_base(1 + n/scale_factor))
/// Provides diminishing price growth - good for long-term stability
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogarithmicBondingCurve {
    /// Starting price at zero supply
    pub initial_price: Decimal,
    /// Scale factor controlling curve steepness
    pub scale_factor: Decimal,
    /// Logarithm base (typically e=2.71828 or 10)
    pub log_base: Decimal,
}

impl LogarithmicBondingCurve {
    /// Create a new logarithmic bonding curve
    pub fn new(initial_price: Decimal, scale_factor: Decimal, log_base: Decimal) -> Self {
        Self {
            initial_price,
            scale_factor: scale_factor.max(dec!(1)),
            log_base: log_base.clamp(dec!(1.1), dec!(10)),
        }
    }

    /// Create with natural logarithm (base e)
    pub fn new_natural_log(initial_price: Decimal, scale_factor: Decimal) -> Self {
        // Use Euler's number (e ≈ 2.71828182845904523536...)
        let e = Decimal::try_from(std::f64::consts::E).unwrap_or(dec!(2.71828182845904523536));
        Self::new(initial_price, scale_factor, e)
    }

    /// Get a price quote for buying tokens
    pub fn get_buy_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.buy_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = self.spot_price(current_supply + amount);

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.buy_price_impact(current_supply, amount),
        }
    }

    /// Get a price quote for selling tokens
    pub fn get_sell_quote(&self, current_supply: Decimal, amount: Decimal) -> PriceQuote {
        let total = self.sell_price(current_supply, amount);
        let spot_before = self.spot_price(current_supply);
        let spot_after = if current_supply > amount {
            self.spot_price(current_supply - amount)
        } else {
            dec!(0)
        };

        PriceQuote {
            amount,
            total_btc: total,
            avg_price_btc: if amount > dec!(0) {
                total / amount
            } else {
                dec!(0)
            },
            spot_price_before: spot_before,
            spot_price_after: spot_after,
            price_impact_percent: self.sell_price_impact(current_supply, amount),
        }
    }

    /// Approximate logarithm: log_base(x) = ln(x) / ln(base)
    fn log_approx(x: Decimal, base: Decimal) -> Decimal {
        if x <= dec!(0) {
            return dec!(0);
        }
        if x == dec!(1) {
            return dec!(0);
        }

        // Convert to f64 for log calculation
        let x_f64: f64 = x.try_into().unwrap_or(1.0);
        let base_f64: f64 = base.try_into().unwrap_or(std::f64::consts::E);
        let result = x_f64.ln() / base_f64.ln();
        Decimal::try_from(result).unwrap_or(dec!(0))
    }

    /// Closed-form integral of P(s) = init * (1 + log_b(1 + s/k)) from `supply` to `supply + amount`.
    ///
    /// Antiderivative: F(s) = init * [s + (s+k)/ln(b) * (ln(1 + s/k) - 1)]
    /// Definite integral = init * { amount + (1/ln(b)) *
    ///     [(s0+amt+k)*(ln(1+(s0+amt)/k) - 1) - (s0+k)*(ln(1+s0/k) - 1)] }
    fn log_integral(&self, supply: Decimal, amount: Decimal) -> Decimal {
        let init: f64 = self.initial_price.try_into().unwrap_or(0.0);
        let k: f64 = self.scale_factor.try_into().unwrap_or(1.0);
        let s0: f64 = supply.try_into().unwrap_or(0.0);
        let amt: f64 = amount.try_into().unwrap_or(0.0);
        let log_b: f64 = self.log_base.try_into().unwrap_or(std::f64::consts::E);

        if k <= 0.0 || amt <= 0.0 || log_b <= 0.0 || log_b == 1.0 {
            return dec!(0);
        }

        let ln_b = log_b.ln();

        // Antiderivative evaluated at s: s + (s+k)/ln_b * (ln(1 + s/k) - 1)
        let antiderivative = |s: f64| -> f64 {
            let arg = 1.0 + s / k;
            if arg <= 0.0 {
                return 0.0;
            }
            s + (s + k) / ln_b * (arg.ln() - 1.0)
        };

        let total = init * (antiderivative(s0 + amt) - antiderivative(s0));
        Decimal::try_from(total.max(0.0)).unwrap_or(dec!(0))
    }
}

impl BondingCurve for LogarithmicBondingCurve {
    fn spot_price(&self, current_supply: Decimal) -> Decimal {
        let normalized = dec!(1) + current_supply / self.scale_factor;
        let log_term = Self::log_approx(normalized, self.log_base);
        self.initial_price * (dec!(1) + log_term)
    }

    fn buy_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) {
            return dec!(0);
        }

        // Exact closed-form antiderivative of P(s) = init * (1 + log_b(1 + s/k))
        self.log_integral(current_supply, amount)
    }

    fn sell_price(&self, current_supply: Decimal, amount: Decimal) -> Decimal {
        if amount <= dec!(0) || amount > current_supply {
            return dec!(0);
        }

        // Integrate from (current_supply - amount) to current_supply, then apply 5% spread
        self.log_integral(current_supply - amount, amount) * dec!(0.95)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_linear_spot_price() {
        let curve = LinearBondingCurve::new(dec!(0.0001), dec!(0.00001));

        assert_eq!(curve.spot_price(dec!(0)), dec!(0.0001));
        assert_eq!(curve.spot_price(dec!(100)), dec!(0.0011));
    }

    #[test]
    fn test_linear_buy_price() {
        let curve = LinearBondingCurve::new(dec!(0.0001), dec!(0.00001));

        // Buy 10 tokens starting from supply 0
        let cost = curve.buy_price(dec!(0), dec!(10));

        // Expected: 10 * 0.0001 + 0.00001 * 10 * (0 + 10 - 1) / 2
        // = 0.001 + 0.00001 * 10 * 4.5
        // = 0.001 + 0.00045
        // = 0.00145
        assert_eq!(cost, dec!(0.00145));
    }

    #[test]
    fn test_linear_sell_price_less_than_buy() {
        let curve = LinearBondingCurve::new(dec!(0.0001), dec!(0.00001));

        let buy_cost = curve.buy_price(dec!(0), dec!(10));
        let sell_proceeds = curve.sell_price(dec!(10), dec!(10));

        // Sell proceeds should be less than buy cost due to spread
        assert!(sell_proceeds < buy_cost);
        assert!(sell_proceeds > buy_cost * dec!(0.9)); // But not too much less
    }

    #[test]
    fn test_bancor_spot_price() {
        let curve = BancorBondingCurve::new(dec!(0.1), dec!(0.5), dec!(1000));

        // At supply 0: price = 0.1 / (1000 * 0.5) = 0.0002
        let price = curve.spot_price(dec!(0));
        assert_eq!(price, dec!(0.0002));
    }

    #[test]
    fn test_exponential_increasing_price() {
        let curve = ExponentialBondingCurve::new(dec!(0.0001), dec!(0.001));

        let price_0 = curve.spot_price(dec!(0));
        let price_100 = curve.spot_price(dec!(100));
        let price_1000 = curve.spot_price(dec!(1000));

        // Price should increase as supply increases
        assert!(price_100 > price_0);
        assert!(price_1000 > price_100);
    }

    #[test]
    fn test_sigmoid_approaches_max() {
        let curve = SigmoidBondingCurve::new(dec!(1), dec!(0.01), dec!(500));

        let price_0 = curve.spot_price(dec!(0));
        let price_500 = curve.spot_price(dec!(500));
        let price_1000 = curve.spot_price(dec!(1000));

        // At midpoint, price should be ~0.5 of max
        assert!(price_500 > dec!(0.4));
        assert!(price_500 < dec!(0.6));

        // At high supply, should approach max
        assert!(price_1000 > price_500);
        assert!(price_1000 < dec!(1));

        // At 0, should be low
        assert!(price_0 < dec!(0.1));
    }

    #[test]
    fn test_adaptive_phase_transitions() {
        // Supply 1000, phases at 100 (10%) and 500 (50%)
        let curve = AdaptiveBondingCurve::with_defaults(
            dec!(0.0001), // initial price
            dec!(0.01),   // max price
            dec!(1000),   // total supply
        );

        // Check phases
        assert_eq!(curve.get_phase(dec!(50)), CurvePhase::EarlyAdopter);
        assert_eq!(curve.get_phase(dec!(100)), CurvePhase::Growth);
        assert_eq!(curve.get_phase(dec!(300)), CurvePhase::Growth);
        assert_eq!(curve.get_phase(dec!(500)), CurvePhase::Maturity);
        assert_eq!(curve.get_phase(dec!(800)), CurvePhase::Maturity);
    }

    #[test]
    fn test_adaptive_price_increases() {
        let curve = AdaptiveBondingCurve::with_defaults(
            dec!(0.0001), // initial price
            dec!(0.01),   // max price
            dec!(1000),   // total supply
        );

        let price_0 = curve.spot_price(dec!(0));
        let price_50 = curve.spot_price(dec!(50));
        let price_100 = curve.spot_price(dec!(100));
        let price_300 = curve.spot_price(dec!(300));
        let price_500 = curve.spot_price(dec!(500));
        let price_900 = curve.spot_price(dec!(900));

        // Price should generally increase
        assert!(price_50 > price_0);
        assert!(price_100 > price_50);
        assert!(price_300 > price_100);
        assert!(price_500 > price_300);
        assert!(price_900 > price_500);

        // Maturity phase should approach but not exceed max
        assert!(price_900 < dec!(0.01));
    }

    #[test]
    fn test_adaptive_sell_less_than_buy() {
        let curve = AdaptiveBondingCurve::with_defaults(dec!(0.0001), dec!(0.01), dec!(1000));

        let buy_cost = curve.buy_price(dec!(100), dec!(50));
        let sell_proceeds = curve.sell_price(dec!(150), dec!(50));

        // Sell proceeds should be less due to 5% spread
        assert!(sell_proceeds < buy_cost);
        assert!(sell_proceeds > buy_cost * dec!(0.9));
    }

    #[test]
    fn test_sqrt_curve_gentler_growth() {
        let curve = SquareRootBondingCurve::new(dec!(0.0001), dec!(100));

        let price_0 = curve.spot_price(dec!(0));
        let price_100 = curve.spot_price(dec!(100));
        let price_400 = curve.spot_price(dec!(400));

        // Price should increase but more gently than exponential
        assert!(price_100 > price_0);
        assert!(price_400 > price_100);

        // sqrt(1) = 1, sqrt(2) ≈ 1.414, sqrt(5) ≈ 2.236
        // So price growth should be sublinear
        let growth_100 = price_100 / price_0;
        let growth_400 = price_400 / price_0;

        // sqrt relationship: 400/100 = 4, but sqrt(5)/sqrt(2) ≈ 1.58
        assert!(growth_400 < growth_100 * dec!(2));
    }

    #[test]
    fn test_sqrt_sell_less_than_buy() {
        let curve = SquareRootBondingCurve::new(dec!(0.0001), dec!(100));

        let buy_cost = curve.buy_price(dec!(100), dec!(50));
        let sell_proceeds = curve.sell_price(dec!(150), dec!(50));

        // Sell should be less than buy due to spread
        assert!(sell_proceeds < buy_cost);
        assert!(sell_proceeds > buy_cost * dec!(0.9));
    }

    #[test]
    fn test_log_curve_basic_properties() {
        let curve = LogarithmicBondingCurve::new_natural_log(dec!(0.0001), dec!(100));

        let price_0 = curve.spot_price(dec!(0));
        let price_100 = curve.spot_price(dec!(100));
        let price_1000 = curve.spot_price(dec!(1000));

        // Price should increase monotonically
        assert!(price_100 > price_0);
        assert!(price_1000 > price_100);

        // All prices should be positive
        assert!(price_0 > dec!(0));
        assert!(price_100 > dec!(0));
        assert!(price_1000 > dec!(0));
    }

    #[test]
    fn test_log_sell_less_than_buy() {
        let curve = LogarithmicBondingCurve::new_natural_log(dec!(0.0001), dec!(100));

        let buy_cost = curve.buy_price(dec!(100), dec!(50));
        let sell_proceeds = curve.sell_price(dec!(150), dec!(50));

        // Sell should be less than buy due to spread
        assert!(sell_proceeds < buy_cost);
        assert!(sell_proceeds > buy_cost * dec!(0.9));
    }

    #[test]
    fn test_sqrt_curve_basic_properties() {
        let curve = SquareRootBondingCurve::new(dec!(0.0001), dec!(100));

        let price_0 = curve.spot_price(dec!(0));
        let price_100 = curve.spot_price(dec!(100));
        let price_400 = curve.spot_price(dec!(400));

        // Price should increase monotonically
        assert!(price_100 > price_0);
        assert!(price_400 > price_100);

        // All prices should be positive
        assert!(price_0 > dec!(0));
        assert!(price_100 > dec!(0));
        assert!(price_400 > dec!(0));
    }

    /// Verify closed-form SquareRoot integral matches 100-step trapezoidal rule within 0.01%.
    #[test]
    fn test_closed_form_matches_numerical_sqrt() {
        let curve = SquareRootBondingCurve::new(dec!(0.0001), dec!(100));

        let test_cases: &[(Decimal, Decimal)] = &[
            (dec!(0), dec!(50)),
            (dec!(100), dec!(50)),
            (dec!(500), dec!(100)),
            (dec!(1000), dec!(200)),
        ];

        for &(supply, amount) in test_cases {
            // Reference: 100-step trapezoidal rule
            let steps = 100i32;
            let step_size = amount / Decimal::from(steps);
            let mut numerical = dec!(0);
            for i in 0..steps {
                let n = supply + step_size * Decimal::from(i);
                let price_start = curve.spot_price(n);
                let price_end = curve.spot_price(n + step_size);
                numerical += (price_start + price_end) / dec!(2) * step_size;
            }

            let closed_form = curve.buy_price(supply, amount);

            // Relative error must be under 0.01%
            let relative_error = if numerical > dec!(0) {
                ((closed_form - numerical) / numerical).abs()
            } else {
                dec!(0)
            };
            assert!(
                relative_error < dec!(0.0001),
                "sqrt closed-form vs numerical: supply={supply}, amount={amount}, \
                 closed={closed_form}, numerical={numerical}, rel_err={relative_error}"
            );
        }
    }

    /// Verify closed-form Logarithmic integral matches 100-step trapezoidal rule within 0.01%.
    #[test]
    fn test_closed_form_matches_numerical_log() {
        let curve = LogarithmicBondingCurve::new_natural_log(dec!(0.0001), dec!(100));

        let test_cases: &[(Decimal, Decimal)] = &[
            (dec!(0), dec!(50)),
            (dec!(100), dec!(50)),
            (dec!(500), dec!(100)),
            (dec!(1000), dec!(200)),
        ];

        for &(supply, amount) in test_cases {
            // Reference: 100-step trapezoidal rule
            let steps = 100i32;
            let step_size = amount / Decimal::from(steps);
            let mut numerical = dec!(0);
            for i in 0..steps {
                let n = supply + step_size * Decimal::from(i);
                let price_start = curve.spot_price(n);
                let price_end = curve.spot_price(n + step_size);
                numerical += (price_start + price_end) / dec!(2) * step_size;
            }

            let closed_form = curve.buy_price(supply, amount);

            // Relative error must be under 0.01%
            let relative_error = if numerical > dec!(0) {
                ((closed_form - numerical) / numerical).abs()
            } else {
                dec!(0)
            };
            assert!(
                relative_error < dec!(0.0001),
                "log closed-form vs numerical: supply={supply}, amount={amount}, \
                 closed={closed_form}, numerical={numerical}, rel_err={relative_error}"
            );
        }
    }
}