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
//! History data types
use crate::prelude::*;

/// History events
pub mod events {
    use super::*;
    use crate::constants::event_key;
    use crate::contracts::market::entry::{
        LpAction, LpActionKind, PositionAction, PositionActionKind,
    };
    use crate::contracts::market::position::PositionId;

    /// Trade volume increased by the given amount
    pub struct TradeVolumeEvent {
        /// Additional trade volume
        pub volume_usd: Usd,
    }

    impl From<TradeVolumeEvent> for Event {
        fn from(src: TradeVolumeEvent) -> Self {
            Event::new("history-trade-volume")
                .add_attribute("volume-usd", src.volume_usd.to_string())
        }
    }
    impl TryFrom<Event> for TradeVolumeEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(TradeVolumeEvent {
                volume_usd: evt.decimal_attr("volume-usd")?,
            })
        }
    }

    /// Realized PnL
    pub struct PnlEvent {
        /// In collateral
        pub pnl: Signed<Collateral>,
        /// In USD
        pub pnl_usd: Signed<Usd>,
    }

    impl From<PnlEvent> for Event {
        fn from(src: PnlEvent) -> Self {
            Event::new("history-pnl")
                .add_attribute("pnl", src.pnl.to_string())
                .add_attribute("pnl-usd", src.pnl_usd.to_string())
        }
    }
    impl TryFrom<Event> for PnlEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(PnlEvent {
                pnl: evt.number_attr("pnl")?,
                pnl_usd: evt.number_attr("pnl-usd")?,
            })
        }
    }

    /// Action taken on a position
    pub struct PositionActionEvent {
        /// Which position
        pub pos_id: PositionId,
        /// Action
        pub action: PositionAction,
    }

    impl From<PositionActionEvent> for Event {
        fn from(src: PositionActionEvent) -> Self {
            let evt = Event::new("history-position-action")
                .add_attribute(event_key::POS_ID, src.pos_id.to_string())
                .add_attribute(
                    event_key::POSITION_ACTION_KIND,
                    match src.action.kind {
                        PositionActionKind::Open => "open",
                        PositionActionKind::Update => "update",
                        PositionActionKind::Close => "close",
                        PositionActionKind::Transfer => "transfer",
                    },
                )
                .add_attribute(
                    event_key::POSITION_ACTION_TIMESTAMP,
                    src.action.timestamp.to_string(),
                )
                .add_attribute(
                    event_key::POSITION_ACTION_COLLATERAL,
                    src.action.collateral.to_string(),
                )
                .add_attribute(
                    event_key::POSITION_ACTION_TRANSFER,
                    src.action.transfer_collateral.to_string(),
                );

            let evt = match src.action.price_timestamp {
                Some(price_timestamp) => evt.add_attribute(
                    event_key::POSITION_ACTION_PRICE_TIMESTAMP,
                    price_timestamp.to_string(),
                ),
                None => evt,
            };

            let evt = match src.action.leverage {
                Some(leverage) => {
                    evt.add_attribute(event_key::POSITION_ACTION_LEVERAGE, leverage.to_string())
                }
                None => evt,
            };
            let evt = match src.action.max_gains {
                Some(max_gains) => {
                    evt.add_attribute(event_key::POSITION_ACTION_MAX_GAINS, max_gains.to_string())
                }
                None => evt,
            };

            let evt = match src.action.trade_fee {
                None => evt,
                Some(trade_fee) => {
                    evt.add_attribute(event_key::POSITION_ACTION_TRADE_FEE, trade_fee.to_string())
                }
            };

            let evt = match src.action.delta_neutrality_fee {
                None => evt,
                Some(delta_neutrality_fee) => evt.add_attribute(
                    event_key::POSITION_ACTION_DELTA_NEUTRALITY_FEE,
                    delta_neutrality_fee.to_string(),
                ),
            };

            let evt = match src.action.old_owner {
                None => evt,
                Some(old_owner) => evt.add_attribute(
                    event_key::POSITION_ACTION_OLD_OWNER,
                    old_owner.into_string(),
                ),
            };

            let evt = match src.action.take_profit_trader {
                None => evt,
                Some(take_profit_trader) => evt.add_attribute(
                    event_key::TAKE_PROFIT_OVERRIDE,
                    take_profit_trader.to_string(),
                ),
            };

            let evt = match src.action.stop_loss_override {
                None => evt,
                Some(stop_loss_override) => evt.add_attribute(
                    event_key::STOP_LOSS_OVERRIDE,
                    stop_loss_override.to_string(),
                ),
            };

            match src.action.new_owner {
                None => evt,
                Some(new_owner) => evt.add_attribute(
                    event_key::POSITION_ACTION_NEW_OWNER,
                    new_owner.into_string(),
                ),
            }
        }
    }

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

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            let pos_id = PositionId::new(evt.u64_attr(event_key::POS_ID)?);
            Ok(PositionActionEvent {
                pos_id,
                action: PositionAction {
                    id: Some(pos_id),
                    kind: evt.map_attr_result(event_key::POSITION_ACTION_KIND, |s| match s {
                        "open" => Ok(PositionActionKind::Open),
                        "update" => Ok(PositionActionKind::Update),
                        "close" => Ok(PositionActionKind::Close),
                        "transfer" => Ok(PositionActionKind::Transfer),
                        _ => Err(PerpError::unimplemented().into()),
                    })?,
                    timestamp: evt.timestamp_attr(event_key::POSITION_ACTION_TIMESTAMP)?,
                    price_timestamp: evt
                        .try_timestamp_attr(event_key::POSITION_ACTION_PRICE_TIMESTAMP)?,
                    collateral: evt.decimal_attr(event_key::POSITION_ACTION_COLLATERAL)?,
                    transfer_collateral: evt.signed_attr(event_key::POSITION_ACTION_TRANSFER)?,
                    leverage: evt.try_leverage_to_base_attr(event_key::POSITION_ACTION_LEVERAGE)?,
                    max_gains: evt
                        .try_map_attr(event_key::POSITION_ACTION_MAX_GAINS, |value| {
                            MaxGainsInQuote::try_from(value)
                        })
                        .transpose()?,
                    trade_fee: evt.try_decimal_attr(event_key::POSITION_ACTION_TRADE_FEE)?,
                    delta_neutrality_fee: evt
                        .try_number_attr(event_key::POSITION_ACTION_DELTA_NEUTRALITY_FEE)?,
                    old_owner: evt.try_unchecked_addr_attr(event_key::POSITION_ACTION_OLD_OWNER)?,
                    new_owner: evt.try_unchecked_addr_attr(event_key::POSITION_ACTION_NEW_OWNER)?,
                    take_profit_trader: evt
                        .try_map_attr(event_key::TAKE_PROFIT_OVERRIDE, |s| {
                            TakeProfitTrader::try_from(s)
                        })
                        .transpose()?,
                    stop_loss_override: evt
                        .try_price_base_in_quote(event_key::STOP_LOSS_OVERRIDE)?,
                },
            })
        }
    }

    /// Event when a new action is added to the liquidity provider history.
    pub struct LpActionEvent {
        /// Liquidity provider
        pub addr: Addr,
        /// Action that occurred
        pub action: LpAction,
        /// Identifier for the action
        pub action_id: u64,
    }

    impl From<LpActionEvent> for Event {
        fn from(src: LpActionEvent) -> Self {
            let event = Event::new("history-lp-action")
                .add_attribute(event_key::LP_ACTION_ADDRESS, src.addr.to_string())
                .add_attribute(event_key::LP_ACTION_ID, src.action_id.to_string())
                .add_attribute(
                    event_key::LP_ACTION_KIND,
                    match src.action.kind {
                        LpActionKind::DepositLp => "deposit-lp",
                        LpActionKind::DepositXlp => "deposit-xlp",
                        LpActionKind::ReinvestYieldLp => "reinvest-yield-lp",
                        LpActionKind::ReinvestYieldXlp => "reinvest-yield-xlp",
                        LpActionKind::UnstakeXlp => "unstake-xlp",
                        LpActionKind::Withdraw => "withdraw",
                        LpActionKind::ClaimYield => "claim-yield",
                        LpActionKind::CollectLp => "collect-lp",
                    },
                )
                .add_attribute(
                    event_key::LP_ACTION_TIMESTAMP,
                    src.action.timestamp.to_string(),
                )
                .add_attribute(
                    event_key::LP_ACTION_COLLATERAL,
                    src.action.collateral.to_string(),
                )
                .add_attribute(
                    event_key::LP_ACTION_COLLATERAL_USD,
                    src.action.collateral_usd.to_string(),
                );
            match src.action.tokens {
                Some(tokens) => {
                    event.add_attribute(event_key::LP_ACTION_TOKENS, tokens.to_string())
                }
                None => event,
            }
        }
    }

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

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(LpActionEvent {
                addr: evt.unchecked_addr_attr(event_key::LP_ACTION_ADDRESS)?,
                action_id: evt.u64_attr(event_key::LP_ACTION_ID)?,
                action: LpAction {
                    kind: evt.map_attr_result(event_key::LP_ACTION_KIND, |s| match s {
                        "deposit-lp" => Ok(LpActionKind::DepositLp),
                        "deposit-xlp" => Ok(LpActionKind::DepositXlp),
                        "reinvest-yield-lp" => Ok(LpActionKind::ReinvestYieldLp),
                        "reinvest-yield-xlp" => Ok(LpActionKind::ReinvestYieldXlp),
                        "unstake-xlp" => Ok(LpActionKind::UnstakeXlp),
                        "withdraw" => Ok(LpActionKind::Withdraw),
                        "claim-yield" => Ok(LpActionKind::ClaimYield),
                        "collect-lp" => Ok(LpActionKind::CollectLp),
                        _ => Err(PerpError::unimplemented().into()),
                    })?,
                    timestamp: evt.timestamp_attr(event_key::LP_ACTION_TIMESTAMP)?,
                    tokens: evt.try_decimal_attr(event_key::LP_ACTION_TOKENS)?,
                    collateral: evt.decimal_attr(event_key::LP_ACTION_COLLATERAL)?,
                    collateral_usd: evt.decimal_attr(event_key::LP_ACTION_COLLATERAL_USD)?,
                },
            })
        }
    }

    /// Liquidity deposited into the pool
    pub struct LpDepositEvent {
        /// Deposited amount in collateral
        pub deposit: Collateral,
        /// Current value of deposit in USD
        pub deposit_usd: Usd,
    }

    impl From<LpDepositEvent> for Event {
        fn from(src: LpDepositEvent) -> Self {
            Event::new("history-lp-deposit")
                .add_attribute("deposit", src.deposit.to_string())
                .add_attribute("deposit-usd", src.deposit_usd.to_string())
        }
    }
    impl TryFrom<Event> for LpDepositEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(LpDepositEvent {
                deposit: evt.decimal_attr("deposit")?,
                deposit_usd: evt.decimal_attr("deposit-usd")?,
            })
        }
    }

    /// LP yield was claimed
    pub struct LpYieldEvent {
        /// Liquidity provider
        pub addr: Addr,
        /// Yield amount in collateral
        pub r#yield: Collateral,
        /// Yield value in USD at current rate
        pub yield_usd: Usd,
    }

    impl From<LpYieldEvent> for Event {
        fn from(
            LpYieldEvent {
                addr,
                r#yield,
                yield_usd,
            }: LpYieldEvent,
        ) -> Self {
            Event::new("history-lp-yield")
                .add_attribute("addr", addr.to_string())
                .add_attribute("yield", r#yield.to_string())
                .add_attribute("yield-usd", yield_usd.to_string())
        }
    }
    impl TryFrom<Event> for LpYieldEvent {
        type Error = anyhow::Error;

        fn try_from(evt: Event) -> anyhow::Result<Self> {
            Ok(LpYieldEvent {
                addr: evt.unchecked_addr_attr("addr")?,
                r#yield: evt.decimal_attr("yield")?,
                yield_usd: evt.decimal_attr("yield-usd")?,
            })
        }
    }
}