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
//! Events and helper methods for fees.
use crate::prelude::*;

use super::config::Config;

impl Config {
    /// Calculate the trade fee based on the given old and new position parameters.
    ///
    /// When opening a new position, you can use [Config::calculate_trade_fee_open].
    pub fn calculate_trade_fee(
        &self,
        old_notional_size_in_collateral: Signed<Collateral>,
        new_notional_size_in_collateral: Signed<Collateral>,
        old_counter_collateral: Collateral,
        new_counter_collateral: Collateral,
    ) -> Result<Collateral> {
        debug_assert!(
            old_notional_size_in_collateral.is_zero()
                || (old_notional_size_in_collateral.is_negative()
                    == new_notional_size_in_collateral.is_negative())
        );
        let old_notional_size_in_collateral = old_notional_size_in_collateral.abs_unsigned();
        let new_notional_size_in_collateral = new_notional_size_in_collateral.abs_unsigned();
        let notional_size_fee = match new_notional_size_in_collateral
            .checked_sub(old_notional_size_in_collateral)
            .ok()
        {
            Some(delta) => {
                debug_assert!(old_notional_size_in_collateral <= new_notional_size_in_collateral);
                delta.checked_mul_dec(self.trading_fee_notional_size)?
            }
            None => {
                debug_assert!(old_notional_size_in_collateral > new_notional_size_in_collateral);
                Collateral::zero()
            }
        };
        let counter_collateral_fee = match new_counter_collateral
            .checked_sub(old_counter_collateral)
            .ok()
        {
            Some(delta) => {
                debug_assert!(old_counter_collateral <= new_counter_collateral);
                delta.checked_mul_dec(self.trading_fee_counter_collateral)?
            }
            None => {
                debug_assert!(old_counter_collateral > new_counter_collateral);
                Collateral::zero()
            }
        };
        notional_size_fee
            .checked_add(counter_collateral_fee)
            .context("Overflow when calculating trading fee")
    }

    /// Same as [Config::calculate_trade_fee] but for opening a new position.
    pub fn calculate_trade_fee_open(
        &self,
        notional_size_in_collateral: Signed<Collateral>,
        counter_collateral: Collateral,
    ) -> Result<Collateral> {
        self.calculate_trade_fee(
            Signed::zero(),
            notional_size_in_collateral,
            Collateral::zero(),
            counter_collateral,
        )
    }
}

/// Events for fees.
pub mod events {
    use super::*;
    use crate::contracts::market::order::OrderId;
    use crate::contracts::market::position::PositionId;
    use crate::{constants::event_key, contracts::market::deferred_execution::DeferredExecId};
    use cosmwasm_std::{Decimal256, Event};

    /// Represents either a [PositionId] or an [OrderId]
    #[derive(Debug, Clone)]
    pub enum TradeId {
        /// An open position
        Position(PositionId),
        /// A pending limit order
        LimitOrder(OrderId),
        /// A deferred execution item not connected to a position or order
        Deferred(DeferredExecId),
    }

    /// The type of fee that was paid out
    #[derive(Debug, Clone, Copy)]
    pub enum FeeSource {
        /// Trading fees
        Trading,
        /// Borrow fees
        Borrow,
        /// Delta neutrality fee
        DeltaNeutrality,
    }

    impl FeeSource {
        fn as_str(self) -> &'static str {
            match self {
                FeeSource::Trading => "trading",
                FeeSource::Borrow => "borrow",
                FeeSource::DeltaNeutrality => "delta-neutrality",
            }
        }
    }

    impl FromStr for FeeSource {
        type Err = anyhow::Error;

        fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
            match s {
                "trading" => Ok(FeeSource::Trading),
                "borrow" => Ok(FeeSource::Borrow),
                "delta-neutrality" => Ok(FeeSource::DeltaNeutrality),
                _ => Err(anyhow::anyhow!("Unknown FeeSource {s}")),
            }
        }
    }

    /// Event fired whenever a fee is collected
    #[derive(Debug, Clone)]
    pub struct FeeEvent {
        /// Position that triggered the fee
        pub trade_id: TradeId,
        /// Source of the fee
        pub fee_source: FeeSource,
        /// Amount paid to LP holders, in collateral
        pub lp_amount: Collateral,
        /// Amount paid to LP holders, in USD
        pub lp_amount_usd: Usd,
        /// Amount paid to xLP holders, in collateral
        pub xlp_amount: Collateral,
        /// Amount paid to xLP holders, in USD
        pub xlp_amount_usd: Usd,
        /// Amount paid to the protocol/DAO, in collateral
        pub protocol_amount: Collateral,
        /// Amount paid to the protocol/DAO, in USD
        pub protocol_amount_usd: Usd,
    }

    impl From<FeeEvent> for Event {
        fn from(
            FeeEvent {
                trade_id,
                fee_source,
                lp_amount,
                lp_amount_usd,
                xlp_amount,
                xlp_amount_usd,
                protocol_amount,
                protocol_amount_usd,
            }: FeeEvent,
        ) -> Self {
            let (trade_id_key, trade_id_val) = match trade_id {
                TradeId::Position(pos_id) => ("pos-id", pos_id.to_string()),
                TradeId::LimitOrder(order_id) => ("order-id", order_id.to_string()),
                TradeId::Deferred(deferred_id) => ("deferred-id", deferred_id.to_string()),
            };

            Event::new("fee")
                .add_attribute(trade_id_key, trade_id_val)
                .add_attribute("source", fee_source.as_str())
                .add_attribute("lp-amount", lp_amount.to_string())
                .add_attribute("lp-amount-usd", lp_amount_usd.to_string())
                .add_attribute("xlp-amount", xlp_amount.to_string())
                .add_attribute("xlp-amount-usd", xlp_amount_usd.to_string())
                .add_attribute("protocol-amount", protocol_amount.to_string())
                .add_attribute("protocol-amount-usd", protocol_amount_usd.to_string())
        }
    }
    impl TryFrom<Event> for FeeEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            let trade_id = match evt.try_u64_attr("pos-id")? {
                Some(pos_id) => TradeId::Position(PositionId::new(pos_id)),
                None => match evt.try_u64_attr("order-id")? {
                    Some(order_id) => TradeId::LimitOrder(OrderId::new(order_id)),
                    None => {
                        let deferred_id = evt.u64_attr("deferred-id")?;
                        TradeId::Deferred(DeferredExecId::from_u64(deferred_id))
                    }
                },
            };

            Ok(FeeEvent {
                trade_id,
                fee_source: evt.string_attr("source")?.parse()?,
                lp_amount: evt.decimal_attr("lp-amount")?,
                lp_amount_usd: evt.decimal_attr("lp-amount-usd")?,
                xlp_amount: evt.decimal_attr("xlp-amount")?,
                xlp_amount_usd: evt.decimal_attr("xlp-amount-usd")?,
                protocol_amount: evt.decimal_attr("protocol-amount")?,
                protocol_amount_usd: evt.decimal_attr("protocol-amount-usd")?,
            })
        }
    }

    /// Event when a funding payment is made
    pub struct FundingPaymentEvent {
        /// Position that paid (or received) the payment
        pub pos_id: PositionId,
        /// Size of the payment, negative means paid to the posiiton
        pub amount: Signed<Collateral>,
        /// Amount expressed in USD
        pub amount_usd: Signed<Usd>,
        /// Whether the position is long or short
        pub direction: DirectionToBase,
    }

    impl From<&FundingPaymentEvent> for Event {
        fn from(src: &FundingPaymentEvent) -> Self {
            Event::new("funding-payment")
                .add_attribute("pos-id", src.pos_id.to_string())
                .add_attribute("amount", src.amount.to_string())
                .add_attribute("amount-usd", src.amount_usd.to_string())
                .add_attribute(event_key::DIRECTION, src.direction.as_str())
        }
    }
    impl From<FundingPaymentEvent> for Event {
        fn from(src: FundingPaymentEvent) -> Self {
            (&src).into()
        }
    }

    impl TryFrom<Event> for FundingPaymentEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(FundingPaymentEvent {
                pos_id: PositionId::new(evt.u64_attr("pos-id")?),
                amount: evt.number_attr("amount")?,
                amount_usd: evt.number_attr("amount-usd")?,
                direction: evt.direction_attr(event_key::DIRECTION)?,
            })
        }
    }

    /// The funding rate was changed
    pub struct FundingRateChangeEvent {
        /// When the change happened
        pub time: Timestamp,
        /// Long is in terms of base, not notional
        pub long_rate_base: Number,
        /// Short is in terms of base, not notional
        pub short_rate_base: Number,
    }

    impl From<FundingRateChangeEvent> for Event {
        fn from(
            FundingRateChangeEvent {
                time,
                long_rate_base,
                short_rate_base,
            }: FundingRateChangeEvent,
        ) -> Self {
            Event::new("funding-rate-change")
                .add_attribute("time", time.to_string())
                .add_attribute("long-rate", long_rate_base.to_string())
                .add_attribute("short-rate", short_rate_base.to_string())
        }
    }

    impl TryFrom<Event> for FundingRateChangeEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(FundingRateChangeEvent {
                time: evt.timestamp_attr("time")?,
                long_rate_base: evt.number_attr("long-rate-base")?,
                short_rate_base: evt.number_attr("short-rate-base")?,
            })
        }
    }

    /// The borrow fee was changed
    pub struct BorrowFeeChangeEvent {
        /// When it was changed
        pub time: Timestamp,
        /// Sum of LP and xLP rate
        pub total_rate: Decimal256,
        /// Amount paid to LP holders
        pub lp_rate: Decimal256,
        /// Amount paid to xLP holders
        pub xlp_rate: Decimal256,
    }

    impl From<BorrowFeeChangeEvent> for Event {
        fn from(
            BorrowFeeChangeEvent {
                time,
                total_rate,
                lp_rate,
                xlp_rate,
            }: BorrowFeeChangeEvent,
        ) -> Self {
            Event::new("borrow-fee-change")
                .add_attribute("time", time.to_string())
                .add_attribute("total", total_rate.to_string())
                .add_attribute("lp", lp_rate.to_string())
                .add_attribute("xlp", xlp_rate.to_string())
        }
    }

    impl TryFrom<Event> for BorrowFeeChangeEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> Result<Self, Self::Error> {
            Ok(BorrowFeeChangeEvent {
                time: evt.timestamp_attr("time")?,
                total_rate: evt.decimal_attr("total")?,
                lp_rate: evt.decimal_attr("lp")?,
                xlp_rate: evt.decimal_attr("xlp")?,
            })
        }
    }

    /// A crank fee was collected
    pub struct CrankFeeEvent {
        /// Position that paid the fee
        pub trade_id: TradeId,
        /// Amount paid, in collateral
        pub amount: Collateral,
        /// Amount paid, in USD
        pub amount_usd: Usd,
        /// Old crank fee fund balance
        pub old_balance: Collateral,
        /// New crank fee fund balance
        pub new_balance: Collateral,
    }

    impl From<CrankFeeEvent> for Event {
        fn from(
            CrankFeeEvent {
                trade_id,
                amount,
                amount_usd,
                old_balance,
                new_balance,
            }: CrankFeeEvent,
        ) -> Self {
            let (trade_id_key, trade_id_val) = match trade_id {
                TradeId::Position(pos_id) => ("pos-id", pos_id.to_string()),
                TradeId::LimitOrder(order_id) => ("order-id", order_id.to_string()),
                TradeId::Deferred(deferred_id) => ("deferred-id", deferred_id.to_string()),
            };

            Event::new("crank-fee")
                .add_attribute(trade_id_key, trade_id_val)
                .add_attribute("amount", amount.to_string())
                .add_attribute("amount-usd", amount_usd.to_string())
                .add_attribute("old-balance", old_balance.to_string())
                .add_attribute("new-balance", new_balance.to_string())
        }
    }
    impl TryFrom<Event> for CrankFeeEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            let trade_id = match evt.try_u64_attr("pos-id")? {
                Some(pos_id) => TradeId::Position(PositionId::new(pos_id)),
                None => match evt.try_u64_attr("order-id")? {
                    Some(order_id) => TradeId::LimitOrder(OrderId::new(order_id)),
                    None => {
                        let deferred_id = evt.u64_attr("deferred-id")?;
                        TradeId::Deferred(DeferredExecId::from_u64(deferred_id))
                    }
                },
            };

            Ok(CrankFeeEvent {
                trade_id,
                amount: evt.decimal_attr("amount")?,
                amount_usd: evt.decimal_attr("amount-usd")?,
                old_balance: evt.decimal_attr("old-balance")?,
                new_balance: evt.decimal_attr("new-balance")?,
            })
        }
    }

    /// Crank reward was earned by a cranker
    pub struct CrankFeeEarnedEvent {
        /// Which wallet received the fee
        pub recipient: Addr,
        /// Amount allocated to the wallet, in collateral
        pub amount: NonZero<Collateral>,
        /// Amount allocated to the wallet, in USD
        pub amount_usd: NonZero<Usd>,
    }

    impl From<CrankFeeEarnedEvent> for Event {
        fn from(
            CrankFeeEarnedEvent {
                recipient,
                amount,
                amount_usd,
            }: CrankFeeEarnedEvent,
        ) -> Self {
            Event::new("crank-fee-claimed")
                .add_attribute("recipient", recipient.to_string())
                .add_attribute("amount", amount.to_string())
                .add_attribute("amount-usd", amount_usd.to_string())
        }
    }
    impl TryFrom<Event> for CrankFeeEarnedEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(CrankFeeEarnedEvent {
                recipient: evt.unchecked_addr_attr("recipient")?,
                amount: evt.non_zero_attr("amount")?,
                amount_usd: evt.non_zero_attr("amount-usd")?,
            })
        }
    }

    /// Emitted when there is insufficient liquidation margin for a fee
    pub struct InsufficientMarginEvent {
        /// Position that had insufficient margin
        pub pos: PositionId,
        /// Type of fee that couldn't be covered
        pub fee_type: FeeType,
        /// Funds available
        pub available: Signed<Collateral>,
        /// Fee amount requested
        pub requested: Signed<Collateral>,
        /// Description of what happened
        pub desc: Option<String>,
    }
    impl From<&InsufficientMarginEvent> for Event {
        fn from(
            InsufficientMarginEvent {
                pos,
                fee_type,
                available,
                requested,
                desc,
            }: &InsufficientMarginEvent,
        ) -> Self {
            let evt = Event::new(event_key::INSUFFICIENT_MARGIN)
                .add_attribute(event_key::POS_ID, pos.to_string())
                .add_attribute(event_key::FEE_TYPE, fee_type.as_str())
                .add_attribute(event_key::AVAILABLE, available.to_string())
                .add_attribute(event_key::REQUESTED, requested.to_string());
            match desc {
                Some(desc) => evt.add_attribute(event_key::DESC, desc),
                None => evt,
            }
        }
    }
    impl From<InsufficientMarginEvent> for Event {
        fn from(event: InsufficientMarginEvent) -> Self {
            (&event).into()
        }
    }

    /// Fee type which can have insufficient margin available
    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    pub enum FeeType {
        /// There is insufficient active collateral for the liquidation margin.
        Overall,
        /// Insufficient borrow fee portion of the liquidation margin.
        Borrow,
        /// Insufficient delta neutrality fee portion of the liquidation margin.
        DeltaNeutrality,
        /// Insufficient funding payment portion of the liquidation margin.
        Funding,
        /// Insufficient crank fee portion of the liquidation margin.
        Crank,
        /// Protocol-wide insufficient funding payments.
        ///
        /// This means that the protocol itself would reach insolvency if we
        /// paid the funding payments this payment expects.
        FundingTotal,
    }

    impl FeeType {
        /// Represent as a string
        pub fn as_str(self) -> &'static str {
            match self {
                FeeType::Overall => "overall",
                FeeType::Borrow => "borrow",
                FeeType::DeltaNeutrality => "delta-neutrality",
                FeeType::Funding => "funding",
                FeeType::FundingTotal => "funding-total",
                FeeType::Crank => "crank",
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::contracts::market::spot_price::SpotPriceConfig;

    use super::*;

    #[test]
    fn trade_fee_open() {
        let config = Config {
            trading_fee_notional_size: "0.01".parse().unwrap(),
            trading_fee_counter_collateral: "0.02".parse().unwrap(),
            ..Config::new(SpotPriceConfig::Manual {
                admin: Addr::unchecked("foo"),
            })
        };
        assert_eq!(
            config
                .calculate_trade_fee_open("-500".parse().unwrap(), "200".parse().unwrap())
                .unwrap(),
            "9".parse().unwrap()
        )
    }

    #[test]
    fn trade_fee_update() {
        let config = Config {
            trading_fee_notional_size: "0.01".parse().unwrap(),
            trading_fee_counter_collateral: "0.02".parse().unwrap(),
            ..Config::new(SpotPriceConfig::Manual {
                admin: Addr::unchecked("foo"),
            })
        };
        assert_eq!(
            config
                .calculate_trade_fee(
                    "-100".parse().unwrap(),
                    "-500".parse().unwrap(),
                    "100".parse().unwrap(),
                    "200".parse().unwrap()
                )
                .unwrap(),
            "6".parse().unwrap()
        );
        assert_eq!(
            config
                .calculate_trade_fee(
                    "-100".parse().unwrap(),
                    "-500".parse().unwrap(),
                    "300".parse().unwrap(),
                    "200".parse().unwrap()
                )
                .unwrap(),
            "4".parse().unwrap()
        );
        assert_eq!(
            config
                .calculate_trade_fee(
                    "-600".parse().unwrap(),
                    "-500".parse().unwrap(),
                    "300".parse().unwrap(),
                    "200".parse().unwrap()
                )
                .unwrap(),
            "0".parse().unwrap()
        );
        assert_eq!(
            config
                .calculate_trade_fee(
                    "-600".parse().unwrap(),
                    "-500".parse().unwrap(),
                    "100".parse().unwrap(),
                    "200".parse().unwrap()
                )
                .unwrap(),
            "2".parse().unwrap()
        );
    }
}