lssa/wallet/src/cli/programs/pinata.rs

208 lines
6.9 KiB
Rust
Raw Normal View History

2025-10-14 15:29:18 +03:00
use anyhow::Result;
use clap::Subcommand;
2025-10-28 16:02:30 +02:00
use common::{PINATA_BASE58, transaction::NSSATransaction};
2025-10-14 15:29:18 +03:00
2025-10-28 16:02:30 +02:00
use crate::{
WalletCore,
cli::{SubcommandReturnValue, WalletSubcommand},
helperfunctions::{AccountPrivacyKind, parse_addr_with_privacy_prefix},
program_facades::pinata::Pinata,
2025-10-28 16:02:30 +02:00
};
2025-11-26 00:27:20 +03:00
/// Represents generic CLI subcommand for a wallet working with pinata program
2025-10-28 16:02:30 +02:00
#[derive(Subcommand, Debug, Clone)]
pub enum PinataProgramAgnosticSubcommand {
2025-11-26 00:27:20 +03:00
/// Claim pinata
2025-10-28 16:02:30 +02:00
Claim {
2025-11-26 00:27:20 +03:00
/// to_account_id - valid 32 byte base58 string with privacy prefix
2025-10-28 16:02:30 +02:00
#[arg(long)]
to_account_id: String,
2025-11-26 00:27:20 +03:00
/// solution - solution to pinata challenge
2025-10-28 16:02:30 +02:00
#[arg(long)]
solution: u128,
},
}
impl WalletSubcommand for PinataProgramAgnosticSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
let underlying_subcommand = match self {
PinataProgramAgnosticSubcommand::Claim {
to_account_id,
solution,
} => {
let (to_account_id, to_addr_privacy) =
parse_addr_with_privacy_prefix(&to_account_id)?;
2025-10-28 16:02:30 +02:00
match to_addr_privacy {
AccountPrivacyKind::Public => {
2025-10-28 16:02:30 +02:00
PinataProgramSubcommand::Public(PinataProgramSubcommandPublic::Claim {
pinata_account_id: PINATA_BASE58.to_string(),
winner_account_id: to_account_id,
2025-10-28 16:02:30 +02:00
solution,
})
}
AccountPrivacyKind::Private => PinataProgramSubcommand::Private(
2025-10-28 16:02:30 +02:00
PinataProgramSubcommandPrivate::ClaimPrivateOwned {
pinata_account_id: PINATA_BASE58.to_string(),
winner_account_id: to_account_id,
2025-10-28 16:02:30 +02:00
solution,
},
),
}
}
};
underlying_subcommand.handle_subcommand(wallet_core).await
}
}
2025-10-14 15:29:18 +03:00
2025-11-26 00:27:20 +03:00
/// Represents generic CLI subcommand for a wallet working with pinata program
2025-10-14 15:29:18 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum PinataProgramSubcommand {
2025-11-26 00:27:20 +03:00
/// Public execution
2025-10-14 15:29:18 +03:00
#[command(subcommand)]
Public(PinataProgramSubcommandPublic),
2025-11-26 00:27:20 +03:00
/// Private execution
2025-10-14 15:29:18 +03:00
#[command(subcommand)]
Private(PinataProgramSubcommandPrivate),
}
2025-11-26 00:27:20 +03:00
/// Represents generic public CLI subcommand for a wallet working with pinata program
2025-10-14 15:29:18 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum PinataProgramSubcommandPublic {
// TODO: Testnet only. Refactor to prevent compilation on mainnet.
// Claim piñata prize
2025-10-20 10:01:54 +03:00
Claim {
2025-11-26 00:27:20 +03:00
/// pinata_account_id - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
pinata_account_id: String,
2025-11-26 00:27:20 +03:00
/// winner_account_id - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
winner_account_id: String,
2025-11-26 00:27:20 +03:00
/// solution - solution to pinata challenge
2025-10-14 15:29:18 +03:00
#[arg(long)]
solution: u128,
},
}
2025-11-26 00:27:20 +03:00
/// Represents generic private CLI subcommand for a wallet working with pinata program
2025-10-14 15:29:18 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum PinataProgramSubcommandPrivate {
// TODO: Testnet only. Refactor to prevent compilation on mainnet.
// Claim piñata prize
2025-10-20 10:01:54 +03:00
ClaimPrivateOwned {
2025-11-26 00:27:20 +03:00
/// pinata_account_id - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
pinata_account_id: String,
2025-11-26 00:27:20 +03:00
/// winner_account_id - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
winner_account_id: String,
2025-11-26 00:27:20 +03:00
/// solution - solution to pinata challenge
2025-10-14 15:29:18 +03:00
#[arg(long)]
solution: u128,
},
}
impl WalletSubcommand for PinataProgramSubcommandPublic {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
2025-10-20 10:01:54 +03:00
PinataProgramSubcommandPublic::Claim {
pinata_account_id,
winner_account_id,
2025-10-14 15:29:18 +03:00
solution,
} => {
let res = Pinata(wallet_core)
.claim(
pinata_account_id.parse().unwrap(),
winner_account_id.parse().unwrap(),
2025-10-14 15:29:18 +03:00
solution,
)
.await?;
println!("Results of tx send is {res:#?}");
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
println!("Transaction data is {transfer_tx:?}");
let path = wallet_core.store_persistent_data().await?;
println!("Stored persistent accounts at {path:#?}");
2025-10-14 15:29:18 +03:00
Ok(SubcommandReturnValue::Empty)
}
}
}
}
impl WalletSubcommand for PinataProgramSubcommandPrivate {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
2025-10-20 10:01:54 +03:00
PinataProgramSubcommandPrivate::ClaimPrivateOwned {
pinata_account_id,
winner_account_id,
2025-10-14 15:29:18 +03:00
solution,
} => {
let pinata_account_id = pinata_account_id.parse().unwrap();
let winner_account_id = winner_account_id.parse().unwrap();
2025-10-14 15:29:18 +03:00
let (res, secret_winner) = Pinata(wallet_core)
.claim_private_owned_account(pinata_account_id, winner_account_id, solution)
.await?;
2025-10-14 15:29:18 +03:00
println!("Results of tx send is {res:#?}");
2025-10-14 15:29:18 +03:00
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
println!("Transaction data is {transfer_tx:?}");
2025-10-14 15:29:18 +03:00
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
let acc_decode_data = vec![(secret_winner, winner_account_id)];
2025-10-14 15:29:18 +03:00
wallet_core.decode_insert_privacy_preserving_transaction_results(
tx,
&acc_decode_data,
)?;
}
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-14 15:29:18 +03:00
println!("Stored persistent accounts at {path:#?}");
Ok(SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash })
}
}
}
}
impl WalletSubcommand for PinataProgramSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
PinataProgramSubcommand::Private(private_subcommand) => {
private_subcommand.handle_subcommand(wallet_core).await
}
PinataProgramSubcommand::Public(public_subcommand) => {
public_subcommand.handle_subcommand(wallet_core).await
}
}
}
}