tensor_eigen/formatting/
amm.rs

1use std::fmt::Write;
2
3use {
4    console::Style,
5    tensor_amm::{
6        accounts::{NftDepositReceipt, Pool},
7        types::{PoolConfig, PoolStats},
8    },
9};
10
11use crate::formatting::{format_timestamp, pad_label};
12
13use super::CustomFormat;
14
15const POOL_LABEL_LENGTH: usize = 25;
16const POOL_SUB_LABEL_LENGTH: usize = POOL_LABEL_LENGTH - 2;
17const RECEIPT_LABEL_LENGTH: usize = 18;
18const LINE_BREAK: &str = "-------------------------";
19
20impl CustomFormat for Pool {
21    fn custom_format(&self) -> String {
22        // Use the default text color but set this up for future use.
23        let color = Style::new();
24
25        format!(
26            "{}
27{}: {}
28{}: {}
29{}: {}
30{}: {}
31{}: {}
32{}: {}
33{}: {}
34{}: {}
35{}: {}
36{}: {}
37{}: {}
38{}: {}
39{}: {}
40{}: {}
41{}
42{}: {}
43{}: {}
44{}: {}
45{}: {}
46{}
47{}: {}",
48            color.apply_to("Pool---------------------"),
49            pad_label("discriminator", POOL_LABEL_LENGTH),
50            color.apply_to(hex::encode(self.discriminator)),
51            pad_label("version", POOL_LABEL_LENGTH),
52            color.apply_to(self.version),
53            pad_label("bump", POOL_LABEL_LENGTH),
54            color.apply_to(self.bump[0]),
55            pad_label("pool_id", POOL_LABEL_LENGTH),
56            color.apply_to(self.pool_id.iter().fold(String::new(), |mut output, b| {
57                let _ = write!(output, "{b:02x}");
58                output
59            })),
60            pad_label("created_at", POOL_LABEL_LENGTH),
61            color.apply_to(format_timestamp(self.created_at)),
62            pad_label("updated_at", POOL_LABEL_LENGTH),
63            color.apply_to(format_timestamp(self.updated_at)),
64            pad_label("expiry", POOL_LABEL_LENGTH),
65            color.apply_to(format_timestamp(self.expiry)),
66            pad_label("owner", POOL_LABEL_LENGTH),
67            color.apply_to(self.owner),
68            pad_label("whitelist", POOL_LABEL_LENGTH),
69            color.apply_to(self.whitelist),
70            pad_label("rent_payer", POOL_LABEL_LENGTH),
71            color.apply_to(self.rent_payer),
72            pad_label("currency", POOL_LABEL_LENGTH),
73            color.apply_to(self.currency),
74            pad_label("amount", POOL_LABEL_LENGTH),
75            color.apply_to(self.amount),
76            pad_label("price_offset", POOL_LABEL_LENGTH),
77            color.apply_to(self.price_offset),
78            pad_label("nfts_held", POOL_LABEL_LENGTH),
79            color.apply_to(self.nfts_held),
80            self.stats.custom_format(),
81            pad_label("shared_escrow", POOL_LABEL_LENGTH),
82            color.apply_to(self.shared_escrow),
83            pad_label("cosigner", POOL_LABEL_LENGTH),
84            color.apply_to(self.cosigner),
85            pad_label("maker_broker", POOL_LABEL_LENGTH),
86            color.apply_to(self.maker_broker),
87            pad_label("max_taker_sell_count", POOL_LABEL_LENGTH),
88            color.apply_to(self.max_taker_sell_count),
89            self.config.custom_format(),
90            pad_label("reserved", POOL_LABEL_LENGTH),
91            if self.reserved.iter().all(|&x| x == 0) {
92                color.apply_to("[all zeros]".to_string())
93            } else {
94                color.apply_to(format!("{:?}", &self.reserved[..]))
95            }
96        )
97    }
98}
99
100impl CustomFormat for PoolConfig {
101    fn custom_format(&self) -> String {
102        // Use the default text color but set this up for future use.
103        let color = Style::new();
104        format!(
105            "{}
106  {}: {}
107  {}: {}
108  {}: {}
109  {}: {}
110  {}: {}
111  {}: {}
112{}",
113            color.apply_to("--PoolConfig--------------"),
114            pad_label("pool_type", POOL_SUB_LABEL_LENGTH),
115            color.apply_to(self.pool_type),
116            pad_label("curve_type", POOL_SUB_LABEL_LENGTH),
117            color.apply_to(self.curve_type),
118            pad_label("starting_price", POOL_SUB_LABEL_LENGTH),
119            color.apply_to(self.starting_price),
120            pad_label("delta", POOL_SUB_LABEL_LENGTH),
121            color.apply_to(self.delta),
122            pad_label("mm_compound_fees", POOL_SUB_LABEL_LENGTH),
123            color.apply_to(self.mm_compound_fees),
124            pad_label("mm_fee_bps", POOL_SUB_LABEL_LENGTH),
125            color.apply_to(
126                self.mm_fee_bps
127                    .to_option()
128                    .map_or("None".to_string(), |bps| bps.to_string())
129            ),
130            color.apply_to(LINE_BREAK)
131        )
132    }
133}
134
135impl CustomFormat for PoolStats {
136    fn custom_format(&self) -> String {
137        // Use the default text color but set this up for future use.
138        let color = Style::new();
139        format!(
140            "{}
141  {}: {}
142  {}: {}
143  {}: {}
144{}",
145            color.apply_to("--PoolStats-------------"),
146            pad_label("taker_sell_count", POOL_SUB_LABEL_LENGTH),
147            color.apply_to(self.taker_sell_count),
148            pad_label("taker_buy_count", POOL_SUB_LABEL_LENGTH),
149            color.apply_to(self.taker_buy_count),
150            pad_label("accumulated_mm_profit", POOL_SUB_LABEL_LENGTH),
151            color.apply_to(self.accumulated_mm_profit),
152            color.apply_to(LINE_BREAK)
153        )
154    }
155}
156
157impl CustomFormat for NftDepositReceipt {
158    fn custom_format(&self) -> String {
159        // Use the default text color but set this up for future use.
160        let color = Style::new();
161        format!(
162            "{}
163{}: {}
164{}: {}
165{}: {}",
166            color.apply_to("NftDepositReceipt"),
167            pad_label("bump", RECEIPT_LABEL_LENGTH),
168            color.apply_to(self.bump),
169            pad_label("mint", RECEIPT_LABEL_LENGTH),
170            color.apply_to(self.mint),
171            pad_label("pool", RECEIPT_LABEL_LENGTH),
172            color.apply_to(self.pool)
173        )
174    }
175}