pcli/command/tx/auction/
dutch.rs

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
use std::path::Path;

use crate::command::tx::FeeTier;
use crate::App;
use anyhow::Result;
use anyhow::{anyhow, bail, Context};
use clap::Subcommand;
use comfy_table::presets;
use dialoguer::Confirm;
use penumbra_asset::{asset::Cache, Value};
use penumbra_auction::auction::{dutch::DutchAuction, dutch::DutchAuctionDescription, AuctionId};
use penumbra_keys::keys::AddressIndex;
use penumbra_num::Amount;
use penumbra_proto::{view::v1::GasPricesRequest, DomainType};
use penumbra_view::ViewClient;
use penumbra_wallet::plan::Planner;
use rand::RngCore;
use rand_core::OsRng;
use serde_json;

mod debug;
pub mod gda;

/// Commands related to Dutch auctions
#[derive(Debug, Subcommand)]
pub enum DutchCmd {
    /// Schedule a gradual dutch auction, a prototype for penumbra developers.
    #[clap(display_order = 1000, name = "gradual")]
    DutchAuctionGradualSchedule {
        /// Source account initiating the auction.
        #[clap(long, display_order = 100, default_value = "0")]
        source: u32,
        /// The value the seller wishes to auction.
        #[clap(long, display_order = 200)]
        input: String,
        /// The maximum output the seller can receive.
        ///
        /// This implicitly defines the starting price for the auction.
        #[clap(long, display_order = 400)]
        max_output: String,
        /// The minimum output the seller is willing to receive.
        ///
        /// This implicitly defines the ending price for the auction.
        #[clap(long, display_order = 500)]
        min_output: String,
        /// The duration for the auction
        #[clap(arg_enum, long, display_order = 600, name = "duration")]
        recipe: gda::GdaRecipe,
        /// Skip asking for confirmation, pay any fees, and execute the transaction.
        #[clap(long, display_order = 700)]
        yes: bool,
        /// The selected fee tier to multiply the fee amount by.
        #[clap(short, long, default_value_t, display_order = 1000)]
        fee_tier: FeeTier,
        #[clap(long, hide = true)]
        // Use to produce a debug file for numerical analysis.
        debug: bool,
    },
    /// Schedule a Dutch auction, a tool to help accomplish price discovery.
    #[clap(display_order = 100, name = "schedule")]
    DutchAuctionSchedule {
        /// Source account initiating the auction.
        #[clap(long, display_order = 100, default_value = "0")]
        source: u32,
        /// The value the seller wishes to auction.
        #[clap(long, display_order = 200)]
        input: String,
        /// The maximum output the seller can receive.
        ///
        /// This implicitly defines the starting price for the auction.
        #[clap(long, display_order = 400)]
        max_output: String,
        /// The minimum output the seller is willing to receive.
        ///
        /// This implicitly defines the ending price for the auction.
        #[clap(long, display_order = 500)]
        min_output: String,
        /// The block height at which the auction begins.
        ///
        /// This allows the seller to schedule an auction at a future time.
        #[clap(long, display_order = 600)]
        start_height: u64,
        /// The block height at which the auction ends.
        ///
        /// Together with `start_height`, `max_output`, and `min_output`,
        /// this implicitly defines the speed of the auction.
        #[clap(long, display_order = 700)]
        end_height: u64,
        /// The number of discrete price steps to use for the auction.
        ///
        /// `end_height - start_height` must be a multiple of `step_count`.
        #[clap(long, display_order = 800)]
        step_count: u64,
        /// The selected fee tier to multiply the fee amount by.
        #[clap(short, long, default_value_t, display_order = 1000)]
        fee_tier: FeeTier,
    },
    /// Terminate a Dutch auction.
    #[clap(display_order = 300, name = "end")]
    DutchAuctionEnd {
        /// Source account terminating the auction.
        #[clap(long, display_order = 100, default_value = "0")]
        source: u32,
        /// If set, ends all auctions owned by the specified account.
        #[clap(long, display_order = 150)]
        all: bool,
        /// Identifier of the auction to end, if `--all` is not set.
        #[clap(display_order = 200)]
        auction_id: Option<String>,
        /// The selected fee tier to multiply the fee amount by.
        #[clap(short, long, default_value_t, display_order = 300)]
        fee_tier: FeeTier,
    },
    /// Withdraw a Dutch auction, and claim its reserves.
    #[clap(display_order = 200, name = "withdraw")]
    DutchAuctionWithdraw {
        /// Source account withdrawing from the auction.
        #[clap(long, display_order = 100, default_value = "0")]
        source: u32,
        /// If set, withdraws all auctions owned by the specified account.
        #[clap(long, display_order = 150)]
        all: bool,
        /// Identifier of the auction to withdraw from, if `--all` is not set.
        #[clap(display_order = 200)]
        auction_id: Option<String>,
        /// The selected fee tier to multiply the fee amount by.
        #[clap(short, long, default_value_t, display_order = 600)]
        fee_tier: FeeTier,
    },
}

impl DutchCmd {
    /// Process the command by performing the appropriate action.
    pub async fn exec(&self, app: &mut App) -> anyhow::Result<()> {
        let gas_prices = app
            .view
            .as_mut()
            .context("view service must be initialized")?
            .gas_prices(GasPricesRequest {})
            .await?
            .into_inner()
            .gas_prices
            .expect("gas prices must be available")
            .try_into()?;

        match self {
            DutchCmd::DutchAuctionSchedule {
                source,
                input,
                max_output,
                min_output,
                start_height,
                end_height,
                step_count,
                fee_tier,
            } => {
                let mut nonce = [0u8; 32];
                OsRng.fill_bytes(&mut nonce);

                let input = input.parse::<Value>()?;
                let max_output = max_output.parse::<Value>()?;
                let min_output = min_output.parse::<Value>()?;
                let output_id = max_output.asset_id;

                let plan = Planner::new(OsRng)
                    .set_gas_prices(gas_prices)
                    .set_fee_tier((*fee_tier).into())
                    .dutch_auction_schedule(DutchAuctionDescription {
                        input,
                        output_id,
                        max_output: max_output.amount,
                        min_output: min_output.amount,
                        start_height: *start_height,
                        end_height: *end_height,
                        step_count: *step_count,
                        nonce,
                    })
                    .plan(
                        app.view
                            .as_mut()
                            .context("view service must be initialized")?,
                        AddressIndex::new(*source),
                    )
                    .await
                    .context("can't build auction schedule transaction")?;
                app.build_and_submit_transaction(plan).await?;
                Ok(())
            }
            DutchCmd::DutchAuctionEnd {
                all,
                auction_id,
                source,
                fee_tier,
            } => {
                let auction_ids = match (all, auction_id) {
                    (true, _) => auctions_to_end(app.view(), *source).await?,
                    (false, Some(auction_id)) => {
                        let auction_id = auction_id.parse::<AuctionId>()?;
                        vec![auction_id]
                    }
                    (false, None) => {
                        bail!("auction_id is required when --all is not set")
                    }
                };

                let mut planner = Planner::new(OsRng);

                planner
                    .set_gas_prices(gas_prices)
                    .set_fee_tier((*fee_tier).into());

                for auction_id in auction_ids {
                    planner.dutch_auction_end(auction_id);
                }

                let plan = planner
                    .plan(
                        app.view
                            .as_mut()
                            .context("view service must be initialized")?,
                        AddressIndex::new(*source),
                    )
                    .await
                    .context("can't build auction end transaction")?;
                app.build_and_submit_transaction(plan).await?;
                Ok(())
            }
            DutchCmd::DutchAuctionWithdraw {
                all,
                source,
                auction_id,
                fee_tier,
            } => {
                let auctions = match (all, auction_id) {
                    (true, _) => auctions_to_withdraw(app.view(), *source).await?,
                    (false, Some(auction_id)) => {
                        let auction_id = auction_id.parse::<AuctionId>()?;

                        let all = auctions_to_withdraw(app.view(), *source).await?;
                        vec![all
                            .into_iter()
                            .find(|a| a.description.id() == auction_id)
                            .ok_or_else(|| {
                                anyhow!("the auction id is unknown from the view service!")
                            })?]
                    }
                    (false, None) => {
                        bail!("auction_id is required when --all is not set")
                    }
                };

                let mut planner = Planner::new(OsRng);

                planner
                    .set_gas_prices(gas_prices)
                    .set_fee_tier((*fee_tier).into());

                for auction in &auctions {
                    planner.dutch_auction_withdraw(auction);
                }

                let plan = planner
                    .plan(
                        app.view
                            .as_mut()
                            .context("view service must be initialized")?,
                        AddressIndex::new(*source),
                    )
                    .await
                    .context("can't build auction withdrawal transaction")?;
                app.build_and_submit_transaction(plan).await?;
                Ok(())
            }
            DutchCmd::DutchAuctionGradualSchedule {
                source,
                input: input_str,
                max_output: max_output_str,
                min_output: min_output_str,
                recipe: duration,
                yes,
                fee_tier,
                debug,
            } => {
                println!("Gradual dutch auction prototype");

                let input = input_str.parse::<Value>()?;
                let max_output = max_output_str.parse::<Value>()?;
                let min_output = min_output_str.parse::<Value>()?;

                let asset_cache = app.view().assets().await?;
                let current_height = app.view().status().await?.full_sync_height;

                let gda = gda::GradualAuction::new(
                    input,
                    max_output,
                    min_output,
                    duration.clone(),
                    current_height,
                );

                let auction_descriptions = gda.generate_auctions();

                let input_fmt = input.format(&asset_cache);
                let max_output_fmt = max_output.format(&asset_cache);
                let min_output_fmt = min_output.format(&asset_cache);

                println!("total to auction: {input_fmt}");
                println!("start price: {max_output_fmt}");
                println!("end price: {min_output_fmt}");
                display_auction_description(&asset_cache, auction_descriptions.clone());

                let mut planner = Planner::new(OsRng);
                planner
                    .set_gas_prices(gas_prices)
                    .set_fee_tier((*fee_tier).into());

                for description in &auction_descriptions {
                    planner.dutch_auction_schedule(description.clone());
                }

                if *debug {
                    let debug_data_path = Path::new("gda-debug-definition-data.json");
                    let auction_data_path = Path::new("gda-debug-auction-data.json");

                    let gda_debug_data = serde_json::to_string(&gda)?;
                    std::fs::write(debug_data_path, gda_debug_data)?;

                    let gda_auction_data = serde_json::to_string(
                        &auction_descriptions
                            .clone()
                            .into_iter()
                            .map(Into::<debug::DebugDescription>::into)
                            .collect::<Vec<_>>(),
                    )?;
                    std::fs::write(auction_data_path, gda_auction_data)?;
                    tracing::debug!(?debug_data_path, ?auction_data_path, "wrote debug data");
                    return Ok(());
                }

                let plan = planner
                    .plan(
                        app.view
                            .as_mut()
                            .context("view service must be initialized")?,
                        AddressIndex::new(*source),
                    )
                    .await
                    .context("can't build send transaction")?;

                let tx = app.build_transaction(plan.clone()).await?;
                let fee_fmt = tx
                    .transaction_body
                    .transaction_parameters
                    .fee
                    .0
                    .format(&asset_cache);

                println!("Total fee: {fee_fmt}");

                if !yes {
                    Confirm::new()
                        .with_prompt("Do you wish to proceed")
                        .interact()?;
                }
                app.build_and_submit_transaction(plan).await?;

                Ok(())
            }
        }
    }
}

async fn all_dutch_auction_states(
    view_client: &mut impl ViewClient,
    source: impl Into<AddressIndex>,
) -> Result<Vec<(AuctionId, DutchAuction, u64)>> {
    fetch_dutch_auction_states(view_client, source, true).await
}

async fn active_dutch_auction_states(
    view_client: &mut impl ViewClient,
    source: impl Into<AddressIndex>,
) -> Result<Vec<(AuctionId, DutchAuction, u64)>> {
    fetch_dutch_auction_states(view_client, source, false).await
}

async fn fetch_dutch_auction_states(
    view_client: &mut impl ViewClient,
    source: impl Into<AddressIndex>,
    include_inactive: bool,
) -> Result<Vec<(AuctionId, DutchAuction, u64)>> {
    let auctions = view_client
        .auctions(Some(source.into()), include_inactive, true)
        .await?
        .into_iter()
        .filter_map(|(id, _, local_seq, state, _)| {
            if let Some(state) = state {
                if let Ok(da) = DutchAuction::decode(state.value) {
                    Some((id, da, local_seq))
                } else {
                    None
                }
            } else {
                None
            }
        })
        .collect();
    Ok(auctions)
}
/// Return all the auctions that need to be ended, based on our local view of the chain state.
async fn auctions_to_end(view_client: &mut impl ViewClient, source: u32) -> Result<Vec<AuctionId>> {
    let auctions = active_dutch_auction_states(view_client, source).await?;

    let auction_ids = auctions
        .into_iter()
        .filter_map(|(id, _auction, local_seq)| {
            // We want to end auctions that we track as "opened" (local_seq == 0)
            // so that we can close them, or catch-up with the chain state if they are already closed.
            if local_seq == 0 {
                Some(id)
            } else {
                None
            }
        })
        .collect();

    Ok(auction_ids)
}

async fn auctions_to_withdraw(
    view_client: &mut impl ViewClient,
    source: u32,
) -> Result<Vec<DutchAuction>> {
    let auctions = all_dutch_auction_states(view_client, source).await?;

    let auction_ids = auctions
        .into_iter()
        .filter_map(|(_, auction, local_seq)| {
            // We want to end auctions that we track as "closed" (local_seq == 1)
            // so that we can close them, or catch-up with the chain state if they are already closed.
            if local_seq == 1 {
                Some(auction)
            } else {
                None
            }
        })
        .collect();

    Ok(auction_ids)
}

fn display_auction_description(asset_cache: &Cache, auctions: Vec<DutchAuctionDescription>) {
    let mut tally_max_output = Amount::zero();
    let mut tally_min_output = Amount::zero();
    let mut tally_input = Amount::zero();
    let input_id = auctions[0].input.asset_id;
    let output_id = auctions[0].output_id;

    let mut table = comfy_table::Table::new();
    table.load_preset(presets::NOTHING);

    table.set_header(vec![
        "start",
        "",
        "end",
        "lot",
        "start price for the lot",
        "reserve price for the lot",
    ]);

    for auction in auctions {
        let start_height = auction.start_height;
        let end_height = auction.end_height;
        let input_chunk = Value {
            asset_id: auction.input.asset_id,
            amount: Amount::from(auction.input.amount.value()),
        };

        let max_price = Value {
            asset_id: auction.output_id,
            amount: Amount::from(auction.max_output.value()),
        };

        let min_price = Value {
            asset_id: auction.output_id,
            amount: Amount::from(auction.min_output.value()),
        };

        let max_price_fmt = max_price.format(&asset_cache);
        let min_price_fmt = min_price.format(&asset_cache);

        let input_chunk_fmt = input_chunk.format(&asset_cache);

        tally_input += input_chunk.amount;
        tally_max_output += max_price.amount;
        tally_min_output += min_price.amount;

        table.add_row(vec![
            format!("{start_height}"),
            "--------->".to_string(),
            format!("{end_height}"),
            input_chunk_fmt,
            max_price_fmt,
            min_price_fmt,
        ]);
    }

    println!("{}", table);

    let tally_input_fmt = Value {
        asset_id: input_id,
        amount: tally_input,
    }
    .format(&asset_cache);

    let tally_output_max_fmt = Value {
        asset_id: output_id,
        amount: tally_max_output,
    }
    .format(&asset_cache);

    let tally_output_min_fmt = Value {
        asset_id: output_id,
        amount: tally_min_output,
    }
    .format(&asset_cache);

    println!("Total auctioned: {tally_input_fmt}");
    println!("Total max output: {tally_output_max_fmt}");
    println!("Total min output: {tally_output_min_fmt}");
}