use gmsol_sdk::{
core::config::{ActionDisabledFlag, AddressKey, AmountKey, DomainDisabledFlag, FactorKey},
ops::config::ConfigOps,
programs::anchor_lang::prelude::Pubkey,
utils::Value,
};
#[derive(Debug, clap::Args)]
pub struct Configuration {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, clap::Subcommand)]
enum Command {
InsertAmount {
amount: u64,
#[arg(long)]
key: AmountKey,
},
InsertFactor {
factor: Value,
#[arg(long)]
key: FactorKey,
},
InsertAddress {
address: Pubkey,
#[arg(long)]
key: AddressKey,
},
ToggleFeature {
#[clap(requires = "toggle")]
domain: DomainDisabledFlag,
action: ActionDisabledFlag,
#[arg(long, group = "toggle")]
enable: bool,
#[arg(long, group = "toggle")]
disable: bool,
},
}
impl super::Command for Configuration {
fn is_client_required(&self) -> bool {
true
}
async fn execute(&self, ctx: super::Context<'_>) -> eyre::Result<()> {
let client = ctx.client()?;
let store = ctx.store();
let options = ctx.bundle_options();
let bundle = match &self.command {
Command::InsertAmount { amount, key } => {
let builder = client.insert_global_amount_by_key(store, *key, amount);
builder.into_bundle_with_options(options)?
}
Command::InsertFactor { factor, key } => {
let builder = client.insert_global_factor_by_key(store, *key, &factor.to_u128()?);
builder.into_bundle_with_options(options)?
}
Command::InsertAddress { address, key } => {
let builder = client.insert_global_address_by_key(store, *key, address);
builder.into_bundle_with_options(options)?
}
Command::ToggleFeature {
domain,
action,
enable,
disable,
} => {
if enable == disable {
return Err(eyre::eyre!("invalid toggle flags"));
}
let builder = client.toggle_feature(store, *domain, *action, *enable);
builder.into_bundle_with_options(options)?
}
};
client.send_or_serialize(bundle).await?;
Ok(())
}
}