gmsol-cli 0.9.0

GMX-Solana is an extension of GMX on the Solana blockchain.
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
use std::{num::NonZeroUsize, ops::Deref};

use eyre::OptionExt;
use futures_util::StreamExt;
use gmsol_sdk::{
    programs::anchor_lang::{idl::IdlAccount, prelude::Pubkey, AccountDeserialize},
    solana_utils::solana_sdk::signer::Signer,
};

/// Inspects protocol data.
#[derive(Debug, clap::Args)]
pub struct Inspect {
    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, clap::Subcommand)]
enum Command {
    /// Print protocol addresses.
    Address {
        #[command(subcommand)]
        kind: AddressKind,
    },
    /// Inspect an account.
    Account { address: Pubkey },
    /// Inspect events that related to the given account.
    Events {
        address: Pubkey,
        #[arg(long)]
        limit: Option<NonZeroUsize>,
    },
    /// Inspect Timelocked instructions.
    Tld {
        #[clap(long)]
        raw: bool,
        #[arg(required = true)]
        addresses: Vec<Pubkey>,
    },
    #[cfg(feature = "squads")]
    Squads {
        vault_transaction_address: Pubkey,
        #[clap(long)]
        raw: bool,
    },
    /// Inspect Chainlink feed IDs.
    #[cfg(feature = "chainlink")]
    Chainlink {
        #[arg(long)]
        testnet: bool,
        #[arg(long, short)]
        decode: bool,
        #[arg(long, short)]
        watch: bool,
        #[arg(required = true)]
        feed_ids: Vec<String>,
    },
    /// Inspect Pyth feed IDs.
    #[cfg(feature = "pyth")]
    Pyth {
        #[arg(long, short)]
        watch: bool,
        #[arg(required = true)]
        feed_ids: Vec<String>,
    },
}

#[derive(Debug, clap::Subcommand)]
enum AddressKind {
    /// Event authority.
    EventAuthority,
    /// Idl account.
    IdlAccount {
        #[command(flatten)]
        select_program: SelectProgram,
    },
}

#[derive(Debug, clap::Args)]
#[group(required = true, multiple = false)]
struct SelectProgram {
    #[arg(long, group = "idl-program")]
    store_program: bool,
    #[arg(long, group = "idl-program")]
    treasury_program: bool,
    #[arg(long, group = "idl-program")]
    timelock_program: bool,
    #[arg(long, group = "idl-program")]
    custom_program: Option<Pubkey>,
}

impl SelectProgram {
    fn id<'a, C: Deref<Target = impl Signer> + Clone>(
        &'a self,
        client: &'a gmsol_sdk::Client<C>,
    ) -> &'a Pubkey {
        let Self {
            store_program,
            treasury_program,
            timelock_program,
            custom_program,
        } = self;
        if *store_program {
            client.store_program_id()
        } else if *treasury_program {
            client.treasury_program_id()
        } else if *timelock_program {
            client.timelock_program_id()
        } else if let Some(program_id) = custom_program {
            program_id
        } else {
            unreachable!()
        }
    }
}

impl super::Command for Inspect {
    fn is_client_required(&self) -> bool {
        true
    }

    async fn execute(&self, ctx: super::Context<'_>) -> eyre::Result<()> {
        let client = ctx.client()?;
        let store = ctx.store();

        match &self.command {
            Command::Address { kind } => {
                let address = match kind {
                    AddressKind::EventAuthority => client.store_event_authority(),
                    AddressKind::IdlAccount { select_program } => {
                        let program_id = select_program.id(client);
                        IdlAccount::address(program_id)
                    }
                };
                println!("{address}");
            }
            Command::Account { address } => {
                use gmsol_sdk::{
                    decode::{
                        decoder::AccountAccessDecoder, gmsol::programs::GMSOLAccountData, Decode,
                    },
                    programs::{
                        gmsol_competition::{
                            utils::Account as CompetitionAccount, ID as COMPETITION_PROGRAM_ID,
                        },
                        gmsol_timelock::utils::Account as TimelockAccount,
                        gmsol_treasury::utils::Account as TreausryAccount,
                    },
                    solana_utils::utils::WithSlot,
                    utils::decode::KeyedAccount,
                };

                let res = client
                    .raw_account_with_config(address, Default::default())
                    .await?;
                let slot = res.slot();
                let account = res.into_value().ok_or_eyre("account does not exist")?;
                if let Ok(idl_account) = IdlAccount::try_deserialize(&mut account.data.as_slice()) {
                    println!("{idl_account:#?}");
                    return Ok(());
                }
                if account.owner == *client.store_program_id() {
                    let account = KeyedAccount {
                        pubkey: *address,
                        account: WithSlot::new(slot, account),
                    };
                    let decoder = AccountAccessDecoder::new(account);
                    let decoded = GMSOLAccountData::decode(decoder)?;
                    println!("{decoded:#?}");
                } else if account.owner == *client.treasury_program_id() {
                    let account = TreausryAccount::try_from_bytes(&account.data)?;
                    match account {
                        TreausryAccount::Config(a) => println!("{a:#?}"),
                        TreausryAccount::GtBank(a) => println!("{a:#?}"),
                        TreausryAccount::GtExchange(a) => println!("{a:#?}"),
                        TreausryAccount::GtExchangeVault(a) => println!("{a:#?}"),
                        TreausryAccount::Oracle(a) => println!("{a:#?}"),
                        TreausryAccount::Store(a) => println!("{a:#?}"),
                        TreausryAccount::TreasuryVaultConfig(a) => println!("{a:#?}"),
                    }
                } else if account.owner == *client.timelock_program_id() {
                    let account = TimelockAccount::try_from_bytes(&account.data)?;
                    match account {
                        TimelockAccount::Executor(a) => println!("{a:#?}"),
                        TimelockAccount::InstructionHeader(a) => println!("{a:#?}"),
                        TimelockAccount::Store(a) => println!("{a:#?}"),
                        TimelockAccount::TimelockConfig(a) => println!("{a:#?}"),
                    }
                } else if account.owner == COMPETITION_PROGRAM_ID {
                    let account = CompetitionAccount::try_from_bytes(&account.data)?;
                    match account {
                        CompetitionAccount::Competition(a) => println!("{a:#?}"),
                        CompetitionAccount::Participant(a) => println!("{a:#?}"),
                        CompetitionAccount::TradeData(a) => println!("{a:#?}"),
                    }
                }
            }
            Command::Events { address, limit } => {
                let stream = match limit {
                    Some(limit) => client
                        .historical_store_cpi_events(address, None)
                        .await?
                        .take(limit.get())
                        .left_stream(),
                    None => client
                        .historical_store_cpi_events(address, None)
                        .await?
                        .right_stream(),
                };
                futures_util::pin_mut!(stream);
                while let Some(res) = stream.next().await {
                    match res {
                        Ok(events) => {
                            println!("{events:#?}");
                        }
                        Err(err) => {
                            tracing::error!(%err, "stream error");
                        }
                    }
                }
            }
            Command::Tld { raw, addresses } => {
                use gmsol_sdk::{
                    core::instruction::{InstructionAccess, InstructionFlag},
                    programs::gmsol_timelock::accounts::TimelockConfig,
                    solana_utils::{
                        solana_sdk::message::{Message, VersionedMessage},
                        utils::inspect_transaction,
                    },
                    utils::zero_copy::ZeroCopy,
                };

                let config = client.find_timelock_config_address(store);
                let delay = client
                    .account::<ZeroCopy<TimelockConfig>>(&config)
                    .await?
                    .ok_or(gmsol_sdk::Error::NotFound)?
                    .0
                    .delay;
                let delay = time::Duration::seconds(delay as i64);

                let mut instructions = Vec::with_capacity(addresses.len());

                for (idx, address) in addresses.iter().enumerate() {
                    let buffer = client
                        .instruction_buffer(address)
                        .await?
                        .ok_or(gmsol_sdk::Error::NotFound)?;

                    let status = if buffer.header.flags.get_flag(InstructionFlag::Approved) {
                        let approved_at =
                            time::OffsetDateTime::from_unix_timestamp(buffer.header.approved_at)
                                .map_err(gmsol_sdk::Error::custom)?;
                        let executable_at = approved_at.saturating_add(delay);
                        let now = time::OffsetDateTime::now_utc();
                        let delta = executable_at - now;
                        if delta.is_positive() {
                            format!("executable in {delta}")
                        } else {
                            "executable".to_string()
                        }
                    } else {
                        "is not approved".to_string()
                    };
                    println!("[{idx}] {address}: {status}");

                    instructions.push(
                        buffer
                            .to_instruction(true)
                            .map_err(gmsol_sdk::Error::custom)?,
                    );
                }

                let message = Message::new(&instructions, Some(&client.payer()));
                println!(
                    "Instructions: {}",
                    inspect_transaction(
                        &VersionedMessage::Legacy(message),
                        Some(client.cluster()),
                        *raw,
                    )
                );
            }
            #[cfg(feature = "squads")]
            Command::Squads {
                vault_transaction_address,
                raw,
            } => {
                use gmsol_sdk::{
                    client::squads::{SquadsProposal, SquadsVaultTransaction},
                    solana_utils::{
                        solana_sdk::message::VersionedMessage, utils::inspect_transaction,
                    },
                    squads::{get_proposal_pda, get_vault_pda},
                };

                let vault_transaction = client
                    .account::<SquadsVaultTransaction>(vault_transaction_address)
                    .await?
                    .ok_or(gmsol_sdk::Error::NotFound)?;

                let multisig = &vault_transaction.multisig;
                let proposal_pubkey = get_proposal_pda(multisig, vault_transaction.index, None).0;

                let proposal = client
                    .account::<SquadsProposal>(&proposal_pubkey)
                    .await?
                    .ok_or(gmsol_sdk::Error::NotFound)?;

                let message = vault_transaction.to_message();

                println!("Transaction Index: {}", vault_transaction.index);
                println!("Status: {:?}", proposal.status);
                println!(
                    "Results: approved {} - rejected {}",
                    proposal.approved.len(),
                    proposal.rejected.len()
                );
                println!("Multisig: {multisig}");
                println!(
                    "Vault: {}",
                    get_vault_pda(multisig, vault_transaction.vault_index, None).0
                );
                println!("Proposal: {proposal_pubkey}");
                println!("Creator: {}", vault_transaction.creator);
                println!(
                    "Inspector: {}",
                    inspect_transaction(
                        &VersionedMessage::V0(message),
                        Some(client.cluster()),
                        *raw
                    )
                );
            }
            #[cfg(feature = "chainlink")]
            Command::Chainlink {
                testnet,
                decode,
                watch,
                feed_ids,
            } => {
                use gmsol_sdk::client::chainlink::{
                    gmsol_chainlink_datastreams::report::Report, Client,
                };
                use time::OffsetDateTime;

                fn display_report(report: &Report) -> gmsol_sdk::Result<String> {
                    Ok(format!("{report:#?}"))
                }

                let client = if *testnet {
                    Client::from_testnet_defaults()?
                } else {
                    Client::from_defaults()?
                };

                let feed_ids = feed_ids.iter().map(|s| s.as_str());

                if *watch {
                    let stream = client.subscribe(feed_ids).await?;
                    futures_util::pin_mut!(stream);
                    while let Some(report) = stream.next().await {
                        match report {
                            Ok(report) => {
                                if *decode {
                                    println!("{}", display_report(&report.decode()?)?);
                                } else {
                                    println!("{report:#?}");
                                }
                            }
                            Err(err) => {
                                tracing::error!(%err, "receive error");
                            }
                        }
                    }
                } else {
                    let ts = OffsetDateTime::now_utc();
                    let reports = client.bulk_report(feed_ids, ts).await?;
                    for report in reports.iter() {
                        if *decode {
                            println!("{}", display_report(&report.decode()?)?);
                        } else {
                            println!("{report:#?}");
                        }
                    }
                }
            }
            #[cfg(feature = "pyth")]
            Command::Pyth { watch, feed_ids } => {
                use futures_util::TryStreamExt;
                use gmsol_sdk::client::pyth::{
                    pull_oracle::hermes::Identifier, EncodingType, Hermes,
                };

                fn parse_feed_ids(feed_ids: &[String]) -> gmsol_sdk::Result<Vec<Identifier>> {
                    let feed_ids = feed_ids
                        .iter()
                        .map(|id| {
                            let hex = id.strip_prefix("0x").unwrap_or(id);
                            Identifier::from_hex(hex).map_err(gmsol_sdk::Error::custom)
                        })
                        .collect::<gmsol_sdk::Result<Vec<_>>>()?;
                    Ok(feed_ids)
                }

                let hermes = Hermes::default();
                let feed_ids = parse_feed_ids(feed_ids)?;

                if *watch {
                    let stream = hermes
                        .price_updates(&feed_ids, Some(EncodingType::Base64))
                        .await?;
                    futures_util::pin_mut!(stream);
                    while let Some(update) = stream.try_next().await? {
                        println!("{:#?}", update.parsed());
                    }
                } else {
                    let update = hermes
                        .latest_price_updates(&feed_ids, Some(EncodingType::Base64))
                        .await?;
                    println!("{:#?}", update.parsed());
                }
            }
        }
        Ok(())
    }
}