hyperdrive-math 0.18.1

API for simulating Hyperdrive smart contract transactions.
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
use std::{cmp::min, collections::btree_map::Entry};

use ethers::{prelude::EthLogDecode, signers::LocalWallet, types::U256};
use eyre::Result;
use fixedpointmath::{fixed, uint256, FixedPoint};
use hyperdrive_test_utils::{
    agent::{Agent, ContractCall, TxOptions},
    chain::ChainClient,
};
use hyperdrive_wrappers::wrappers::ihyperdrive::{Checkpoint, IHyperdriveEvents, Options};
use rand_chacha::ChaCha8Rng;
use tracing::instrument;

use crate::State;

pub trait HyperdriveMathAgent {
    /// Gets the current state of the pool.
    async fn get_state(&self) -> Result<State>;

    /// Gets the latest checkpoint.
    async fn latest_checkpoint(&self) -> Result<U256>;

    /// Calculates the spot price.
    async fn calculate_spot_price(&self) -> Result<FixedPoint<U256>>;

    /// Calculates the long amount that will be opened for a given base amount.
    async fn calculate_open_long(&self, base_amount: FixedPoint<U256>) -> Result<FixedPoint<U256>>;

    /// Calculates the deposit required to short a given amount of bonds with the
    /// current market state.
    async fn calculate_open_short(&self, bond_amount: FixedPoint<U256>)
        -> Result<FixedPoint<U256>>;

    /// Calculates the max long that can be opened in the current checkpoint.
    async fn calculate_max_long(
        &self,
        maybe_max_iterations: Option<usize>,
    ) -> Result<FixedPoint<U256>>;

    /// Gets the long that moves the fixed rate to a target value.
    async fn calculate_targeted_long(
        &self,
        target_rate: FixedPoint<U256>,
        maybe_max_iterations: Option<usize>,
        maybe_allowable_error: Option<FixedPoint<U256>>,
    ) -> Result<FixedPoint<U256>>;

    /// Calculates the max short that can be opened in the current checkpoint.
    ///
    /// Since interest can accrue between the time the calculation is made and
    /// the transaction is submitted, it's convenient to have a slippage
    /// tolerance to lower the revert rate.
    async fn calculate_max_short(
        &self,
        maybe_slippage_tolerance: Option<FixedPoint<U256>>,
    ) -> Result<FixedPoint<U256>>;

    async fn open_long(
        &mut self,
        base_paid: FixedPoint<U256>,
        maybe_slippage_tolerance: Option<FixedPoint<U256>>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<()>;

    async fn close_long(
        &mut self,
        maturity_time: FixedPoint<U256>,
        bond_amount: FixedPoint<U256>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<()>;

    async fn open_short(
        &mut self,
        bond_amount: FixedPoint<U256>,
        maybe_slippage_tolerance: Option<FixedPoint<U256>>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<(FixedPoint<U256>, FixedPoint<U256>)>;

    async fn close_short(
        &mut self,
        maturity_time: FixedPoint<U256>,
        bond_amount: FixedPoint<U256>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<FixedPoint<U256>>;
}

impl HyperdriveMathAgent for Agent<ChainClient<LocalWallet>, ChaCha8Rng> {
    /// Gets the current state of the pool.
    async fn get_state(&self) -> Result<State> {
        let pool_config = self.get_config().clone();
        let pool_info = self.hyperdrive().get_pool_info().await?;
        Ok(State::new(pool_config, pool_info))
    }

    /// Gets the latest checkpoint.
    async fn latest_checkpoint(&self) -> Result<U256> {
        Ok(self.get_state().await?.to_checkpoint(self.now().await?))
    }
    /// Calculates the spot price.
    async fn calculate_spot_price(&self) -> Result<FixedPoint<U256>> {
        self.get_state().await?.calculate_spot_price()
    }

    /// Calculates the long amount that will be opened for a given base amount.
    async fn calculate_open_long(&self, base_amount: FixedPoint<U256>) -> Result<FixedPoint<U256>> {
        let state = self.get_state().await?;
        state.calculate_open_long(base_amount)
    }
    /// Calculates the deposit required to short a given amount of bonds with the
    /// current market state.
    async fn calculate_open_short(
        &self,
        bond_amount: FixedPoint<U256>,
    ) -> Result<FixedPoint<U256>> {
        let state = self.get_state().await?;
        let Checkpoint {
            vault_share_price: open_vault_share_price,
            ..
        } = self.get_checkpoint(self.latest_checkpoint().await?).await?;
        state.calculate_open_short(bond_amount, open_vault_share_price.into())
    }

    /// Calculates the max long that can be opened in the current checkpoint.
    async fn calculate_max_long(
        &self,
        maybe_max_iterations: Option<usize>,
    ) -> Result<FixedPoint<U256>> {
        let state = self.get_state().await?;
        let checkpoint_exposure = self
            .hyperdrive()
            .get_checkpoint_exposure(state.to_checkpoint(self.now().await?))
            .await?;
        Ok(
            state.calculate_max_long(
                self.wallet.base,
                checkpoint_exposure,
                maybe_max_iterations,
            )?,
        )
    }

    /// Gets the long that moves the fixed rate to a target value.
    async fn calculate_targeted_long(
        &self,
        target_rate: FixedPoint<U256>,
        maybe_max_iterations: Option<usize>,
        maybe_allowable_error: Option<FixedPoint<U256>>,
    ) -> Result<FixedPoint<U256>> {
        let state = self.get_state().await?;
        let checkpoint_exposure = self
            .hyperdrive()
            .get_checkpoint_exposure(state.to_checkpoint(self.now().await?))
            .await?;
        Ok(state
            .calculate_targeted_long_with_budget(
                self.wallet.base,
                target_rate,
                checkpoint_exposure,
                maybe_max_iterations,
                maybe_allowable_error,
            )
            .unwrap())
    }

    /// Calculates the max short that can be opened in the current checkpoint.
    ///
    /// Since interest can accrue between the time the calculation is made and
    /// the transaction is submitted, it's convenient to have a slippage
    /// tolerance to lower the revert rate.
    async fn calculate_max_short(
        &self,
        maybe_slippage_tolerance: Option<FixedPoint<U256>>,
    ) -> Result<FixedPoint<U256>> {
        let budget =
            self.wallet.base * (fixed!(1e18) - maybe_slippage_tolerance.unwrap_or(fixed!(0.01e18)));

        let latest_checkpoint = self.latest_checkpoint().await?;
        let Checkpoint {
            vault_share_price: open_vault_share_price,
            ..
        } = self.get_checkpoint(latest_checkpoint).await?;
        let checkpoint_exposure = self.get_checkpoint_exposure(latest_checkpoint).await?;
        let state = self.get_state().await?;

        // We linearly interpolate between the current spot price and the minimum
        // price that the pool can support. This is a conservative estimate of
        // the short's realized price.
        let conservative_price = {
            // We estimate the minimum price that short will pay by a
            // weighted average of the spot price and the minimum possible
            // spot price the pool can quote. We choose the weights so that this
            // is an underestimate of the worst case realized price.
            let spot_price = state.calculate_spot_price()?;
            let min_price = state.calculate_min_spot_price()?;

            // Calculate the linear interpolation.
            let base_reserves = FixedPoint::from(state.info.vault_share_price)
                * (FixedPoint::from(state.info.share_reserves));
            let weight = (min(self.wallet.base, base_reserves) / base_reserves)
                .pow(fixed!(1e18) - FixedPoint::from(self.get_config().time_stretch))?;
            spot_price * (fixed!(1e18) - weight) + min_price * weight
        };

        state.calculate_max_short(
            budget,
            open_vault_share_price,
            checkpoint_exposure,
            Some(conservative_price),
            None,
        )
    }

    #[instrument(skip(self))]
    async fn open_long(
        &mut self,
        base_paid: FixedPoint<U256>,
        maybe_slippage_tolerance: Option<FixedPoint<U256>>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<()> {
        // Ensure that the agent has a sufficient base balance to open the long.
        if self.wallet.base < base_paid {
            return Err(eyre::eyre!(
                "insufficient base balance to open long: {:?} < {:?}",
                self.wallet.base,
                base_paid
            ));
        }

        // Decrease the wallet's base balance.
        self.wallet.base -= base_paid;

        // Open the long and record the trade in the wallet.
        let log = {
            let min_output = {
                let slippage_tolerance = maybe_slippage_tolerance.unwrap_or(fixed!(0.01e18));
                self.calculate_open_long(base_paid).await? * (fixed!(1e18) - slippage_tolerance)
            };
            let tx = ContractCall(self.hyperdrive().open_long(
                base_paid.into(),
                min_output.into(),
                fixed!(0).into(), // TODO: This is fine for testing, but not prod.
                Options {
                    destination: self.client().address(),
                    as_base: true,
                    extra_data: [].into(),
                },
            ))
            .apply(self.pre_process_options(maybe_tx_options));
            let logs =
                tx.0.send()
                    .await?
                    .await?
                    .unwrap()
                    .logs
                    .into_iter()
                    .filter_map(|log| {
                        if let Ok(IHyperdriveEvents::OpenLongFilter(log)) =
                            IHyperdriveEvents::decode_log(&log.into())
                        {
                            Some(log)
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>();
            logs[0].clone()
        };
        *self
            .wallet
            .longs
            .entry(log.maturity_time.into())
            .or_default() += log.bond_amount.into();

        Ok(())
    }

    #[instrument(skip(self))]
    async fn close_long(
        &mut self,
        maturity_time: FixedPoint<U256>,
        bond_amount: FixedPoint<U256>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<()> {
        // TODO: It would probably be better for this part of the agent to just
        // be a dumb wrapper around Hyperdrive. It's going to be useful to test
        // with inputs that we'd consider invalid.
        //
        // If the wallet has a sufficient balance of longs, update the long
        // balance. Otherwise, return an error.
        match self.wallet.longs.entry(maturity_time) {
            Entry::Occupied(mut entry) => {
                let long_balance = entry.get();
                if *long_balance > bond_amount {
                    entry.insert(*long_balance - bond_amount);
                } else if *long_balance == bond_amount {
                    entry.remove();
                } else {
                    return Err(eyre::eyre!(
                        "insufficient long balance to close long: {:?} < {:?}",
                        long_balance,
                        bond_amount
                    ));
                }
            }
            Entry::Vacant(_) => {
                return Err(eyre::eyre!("no longs to close"));
            }
        }

        // Close the long and increase the wallet's base balance.
        let log = {
            let tx = ContractCall(self.hyperdrive().close_long(
                maturity_time.into(),
                bond_amount.into(),
                uint256!(0),
                Options {
                    destination: self.client().address(),
                    as_base: true,
                    extra_data: [].into(),
                },
            ))
            .apply(self.pre_process_options(maybe_tx_options));
            let logs =
                tx.0.send()
                    .await?
                    .await?
                    .unwrap()
                    .logs
                    .into_iter()
                    .filter_map(|log| {
                        if let Ok(IHyperdriveEvents::CloseLongFilter(log)) =
                            IHyperdriveEvents::decode_log(&log.into())
                        {
                            Some(log)
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>();
            logs[0].clone()
        };
        // We ensure trades here are executed as base
        // Panic here since we pass as_base=True in the call.
        if !log.as_base {
            panic!("Trades are expected to be executed as base.")
        }
        self.wallet.base += log.amount.into();

        Ok(())
    }

    /// Shorts ///

    #[instrument(skip(self))]
    async fn open_short(
        &mut self,
        bond_amount: FixedPoint<U256>,
        maybe_slippage_tolerance: Option<FixedPoint<U256>>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<(FixedPoint<U256>, FixedPoint<U256>)> {
        // Open the short and record the trade in the wallet.
        let log = {
            let max_deposit = {
                let slippage_tolerance = maybe_slippage_tolerance.unwrap_or(fixed!(0.01e18));
                self.calculate_open_short(bond_amount).await? * (fixed!(1e18) + slippage_tolerance)
            };
            let tx = ContractCall(self.hyperdrive().open_short(
                bond_amount.into(),
                max_deposit.into(),
                fixed!(0).into(), // TODO: This is fine for testing, but not prod.
                Options {
                    destination: self.client().address(),
                    as_base: true,
                    extra_data: [].into(),
                },
            ))
            .apply(self.pre_process_options(maybe_tx_options));
            let logs =
                tx.0.send()
                    .await?
                    .await?
                    .unwrap()
                    .logs
                    .into_iter()
                    .filter_map(|log| {
                        if let Ok(IHyperdriveEvents::OpenShortFilter(log)) =
                            IHyperdriveEvents::decode_log(&log.into())
                        {
                            Some(log)
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>();
            logs[0].clone()
        };
        *self
            .wallet
            .shorts
            .entry(log.maturity_time.into())
            .or_default() += log.bond_amount.into();

        // Decrease the wallet's base balance.
        // We ensure trades here are executed as base
        // Panic here since we pass as_base=True in the call.
        if !log.as_base {
            panic!("Trades are expected to be executed as base.")
        }
        self.wallet.base -= log.amount.into();

        Ok((log.maturity_time.into(), log.amount.into()))
    }

    #[instrument(skip(self))]
    async fn close_short(
        &mut self,
        maturity_time: FixedPoint<U256>,
        bond_amount: FixedPoint<U256>,
        maybe_tx_options: Option<TxOptions>,
    ) -> Result<FixedPoint<U256>> {
        // If the wallet has a sufficient balance of shorts, update the short
        // balance. Otherwise, return an error.
        match self.wallet.shorts.entry(maturity_time) {
            Entry::Occupied(mut entry) => {
                let short_balance = entry.get();
                if *short_balance > bond_amount {
                    entry.insert(*short_balance - bond_amount);
                } else if *short_balance == bond_amount {
                    entry.remove();
                } else {
                    return Err(eyre::eyre!(
                        "insufficient short balance to close short: {:?} < {:?}",
                        short_balance,
                        bond_amount
                    ));
                }
            }
            Entry::Vacant(_) => {
                return Err(eyre::eyre!("no shorts to close"));
            }
        }

        // Close the long and increase the wallet's base balance.
        let log = {
            let tx = ContractCall(self.hyperdrive().close_short(
                maturity_time.into(),
                bond_amount.into(),
                uint256!(0),
                Options {
                    destination: self.client().address(),
                    as_base: true,
                    extra_data: [].into(),
                },
            ))
            .apply(self.pre_process_options(maybe_tx_options));
            let logs =
                tx.0.send()
                    .await?
                    .await?
                    .unwrap()
                    .logs
                    .into_iter()
                    .filter_map(|log| {
                        if let Ok(IHyperdriveEvents::CloseShortFilter(log)) =
                            IHyperdriveEvents::decode_log(&log.into())
                        {
                            Some(log)
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>();
            logs[0].clone()
        };
        // We ensure trades here are executed as base
        // Panic here since we pass as_base=True in the call.
        if !log.as_base {
            panic!("Trades are expected to be executed as base.")
        }
        self.wallet.base += log.amount.into();

        Ok(log.amount.into())
    }
}