2026-03-07 16:51:50 +01:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use clap::Subcommand;
|
|
|
|
|
use common::transaction::NSSATransaction;
|
|
|
|
|
use nssa::{Account, AccountId, program::Program};
|
|
|
|
|
use token_core::TokenHolding;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
AccDecodeData::Decode,
|
|
|
|
|
WalletCore,
|
2026-05-14 21:19:25 -04:00
|
|
|
account::AccountIdWithPrivacy,
|
|
|
|
|
cli::{CliAccountMention, SubcommandReturnValue, WalletSubcommand},
|
2026-03-07 16:51:50 +01:00
|
|
|
program_facades::ata::Ata,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Represents generic CLI subcommand for a wallet working with the ATA program.
|
|
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
|
pub enum AtaSubcommand {
|
|
|
|
|
/// Derive and print the Associated Token Account address (local only, no network).
|
|
|
|
|
Address {
|
|
|
|
|
/// Owner account - valid 32 byte base58 string (no privacy prefix).
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
owner: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
/// Token definition account - valid 32 byte base58 string (no privacy prefix).
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
token_definition: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
},
|
|
|
|
|
/// Create (or idempotently no-op) the Associated Token Account.
|
|
|
|
|
Create {
|
2026-05-14 21:19:25 -04:00
|
|
|
/// Owner account mention - account id with privacy prefix or label.
|
2026-03-07 16:51:50 +01:00
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
owner: CliAccountMention,
|
2026-03-07 16:51:50 +01:00
|
|
|
/// Token definition account - valid 32 byte base58 string WITHOUT privacy prefix.
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
token_definition: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
},
|
|
|
|
|
/// Send tokens from owner's ATA to a recipient token holding account.
|
|
|
|
|
Send {
|
2026-05-14 21:19:25 -04:00
|
|
|
/// Sender account mention - account id with privacy prefix or label.
|
2026-03-07 16:51:50 +01:00
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
from: CliAccountMention,
|
2026-03-07 16:51:50 +01:00
|
|
|
/// Token definition account - valid 32 byte base58 string WITHOUT privacy prefix.
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
token_definition: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
/// Recipient account - valid 32 byte base58 string WITHOUT privacy prefix.
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
to: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
#[arg(long)]
|
|
|
|
|
amount: u128,
|
|
|
|
|
},
|
|
|
|
|
/// Burn tokens from holder's ATA.
|
|
|
|
|
Burn {
|
2026-05-14 21:19:25 -04:00
|
|
|
/// Holder account mention - account id with privacy prefix or label.
|
2026-03-07 16:51:50 +01:00
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
holder: CliAccountMention,
|
2026-03-07 16:51:50 +01:00
|
|
|
/// Token definition account - valid 32 byte base58 string WITHOUT privacy prefix.
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
token_definition: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
#[arg(long)]
|
|
|
|
|
amount: u128,
|
|
|
|
|
},
|
|
|
|
|
/// List all ATAs for a given owner across multiple token definitions.
|
|
|
|
|
List {
|
|
|
|
|
/// Owner account - valid 32 byte base58 string (no privacy prefix).
|
|
|
|
|
#[arg(long)]
|
2026-05-14 21:19:25 -04:00
|
|
|
owner: AccountId,
|
2026-03-07 16:51:50 +01:00
|
|
|
/// Token definition accounts - valid 32 byte base58 strings (no privacy prefix).
|
|
|
|
|
#[arg(long, num_args = 1..)]
|
2026-05-14 21:19:25 -04:00
|
|
|
token_definition: Vec<AccountId>,
|
2026-03-07 16:51:50 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for AtaSubcommand {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Address {
|
|
|
|
|
owner,
|
|
|
|
|
token_definition,
|
|
|
|
|
} => {
|
|
|
|
|
let ata_program_id = Program::ata().id();
|
|
|
|
|
let ata_id = ata_core::get_associated_token_account_id(
|
|
|
|
|
&ata_program_id,
|
2026-05-14 21:19:25 -04:00
|
|
|
&ata_core::compute_ata_seed(owner, token_definition),
|
2026-03-07 16:51:50 +01:00
|
|
|
);
|
|
|
|
|
println!("{ata_id}");
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
|
|
|
|
Self::Create {
|
|
|
|
|
owner,
|
|
|
|
|
token_definition,
|
|
|
|
|
} => {
|
2026-05-15 18:15:54 -04:00
|
|
|
let owner_resolved = owner.resolve(wallet_core.storage())?;
|
2026-05-14 21:19:25 -04:00
|
|
|
let definition_id = token_definition;
|
2026-03-07 16:51:50 +01:00
|
|
|
|
2026-05-15 18:15:54 -04:00
|
|
|
match owner_resolved {
|
2026-05-14 21:19:25 -04:00
|
|
|
AccountIdWithPrivacy::Public(owner_id) => {
|
2026-03-07 16:51:50 +01:00
|
|
|
Ata(wallet_core)
|
2026-05-15 18:15:54 -04:00
|
|
|
.send_create(owner_id, definition_id, &owner)
|
2026-03-07 16:51:50 +01:00
|
|
|
.await?;
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
2026-05-14 21:19:25 -04:00
|
|
|
AccountIdWithPrivacy::Private(owner_id) => {
|
2026-03-07 16:51:50 +01:00
|
|
|
let (tx_hash, secret) = Ata(wallet_core)
|
|
|
|
|
.send_create_private_owner(owner_id, definition_id)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
println!("Transaction hash is {tx_hash}");
|
|
|
|
|
|
|
|
|
|
let tx = wallet_core.poll_native_token_transfer(tx_hash).await?;
|
|
|
|
|
if let NSSATransaction::PrivacyPreserving(tx) = tx {
|
|
|
|
|
wallet_core.decode_insert_privacy_preserving_transaction_results(
|
|
|
|
|
&tx,
|
|
|
|
|
&[Decode(secret, owner_id)],
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 21:19:25 -04:00
|
|
|
wallet_core.store_persistent_data()?;
|
2026-03-07 16:51:50 +01:00
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Self::Send {
|
|
|
|
|
from,
|
|
|
|
|
token_definition,
|
|
|
|
|
to,
|
|
|
|
|
amount,
|
|
|
|
|
} => {
|
2026-05-15 18:15:54 -04:00
|
|
|
let from_resolved = from.resolve(wallet_core.storage())?;
|
2026-05-14 21:19:25 -04:00
|
|
|
let definition_id = token_definition;
|
|
|
|
|
let to_id = to;
|
2026-03-07 16:51:50 +01:00
|
|
|
|
2026-05-15 18:15:54 -04:00
|
|
|
match from_resolved {
|
2026-05-14 21:19:25 -04:00
|
|
|
AccountIdWithPrivacy::Public(from_id) => {
|
2026-03-07 16:51:50 +01:00
|
|
|
Ata(wallet_core)
|
2026-05-15 18:15:54 -04:00
|
|
|
.send_transfer(from_id, definition_id, to_id, amount, &from)
|
2026-03-07 16:51:50 +01:00
|
|
|
.await?;
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
2026-05-14 21:19:25 -04:00
|
|
|
AccountIdWithPrivacy::Private(from_id) => {
|
2026-03-07 16:51:50 +01:00
|
|
|
let (tx_hash, secret) = Ata(wallet_core)
|
|
|
|
|
.send_transfer_private_owner(from_id, definition_id, to_id, amount)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
println!("Transaction hash is {tx_hash}");
|
|
|
|
|
|
|
|
|
|
let tx = wallet_core.poll_native_token_transfer(tx_hash).await?;
|
|
|
|
|
if let NSSATransaction::PrivacyPreserving(tx) = tx {
|
|
|
|
|
wallet_core.decode_insert_privacy_preserving_transaction_results(
|
|
|
|
|
&tx,
|
|
|
|
|
&[Decode(secret, from_id)],
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 21:19:25 -04:00
|
|
|
wallet_core.store_persistent_data()?;
|
2026-03-07 16:51:50 +01:00
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Self::Burn {
|
|
|
|
|
holder,
|
|
|
|
|
token_definition,
|
|
|
|
|
amount,
|
|
|
|
|
} => {
|
2026-05-15 18:15:54 -04:00
|
|
|
let holder_resolved = holder.resolve(wallet_core.storage())?;
|
2026-05-14 21:19:25 -04:00
|
|
|
let definition_id = token_definition;
|
2026-03-07 16:51:50 +01:00
|
|
|
|
2026-05-15 18:15:54 -04:00
|
|
|
match holder_resolved {
|
2026-05-14 21:19:25 -04:00
|
|
|
AccountIdWithPrivacy::Public(holder_id) => {
|
2026-03-07 16:51:50 +01:00
|
|
|
Ata(wallet_core)
|
2026-05-15 18:15:54 -04:00
|
|
|
.send_burn(holder_id, definition_id, amount, &holder)
|
2026-03-07 16:51:50 +01:00
|
|
|
.await?;
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
2026-05-14 21:19:25 -04:00
|
|
|
AccountIdWithPrivacy::Private(holder_id) => {
|
2026-03-07 16:51:50 +01:00
|
|
|
let (tx_hash, secret) = Ata(wallet_core)
|
|
|
|
|
.send_burn_private_owner(holder_id, definition_id, amount)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
println!("Transaction hash is {tx_hash}");
|
|
|
|
|
|
|
|
|
|
let tx = wallet_core.poll_native_token_transfer(tx_hash).await?;
|
|
|
|
|
if let NSSATransaction::PrivacyPreserving(tx) = tx {
|
|
|
|
|
wallet_core.decode_insert_privacy_preserving_transaction_results(
|
|
|
|
|
&tx,
|
|
|
|
|
&[Decode(secret, holder_id)],
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 21:19:25 -04:00
|
|
|
wallet_core.store_persistent_data()?;
|
2026-03-07 16:51:50 +01:00
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Self::List {
|
|
|
|
|
owner,
|
|
|
|
|
token_definition,
|
|
|
|
|
} => {
|
|
|
|
|
let ata_program_id = Program::ata().id();
|
|
|
|
|
|
|
|
|
|
for def in &token_definition {
|
|
|
|
|
let ata_id = ata_core::get_associated_token_account_id(
|
|
|
|
|
&ata_program_id,
|
2026-05-14 21:19:25 -04:00
|
|
|
&ata_core::compute_ata_seed(owner, *def),
|
2026-03-07 16:51:50 +01:00
|
|
|
);
|
|
|
|
|
let account = wallet_core.get_account_public(ata_id).await?;
|
|
|
|
|
|
|
|
|
|
if account == Account::default() {
|
2026-05-14 21:19:25 -04:00
|
|
|
println!("No ATA for definition {def}");
|
2026-03-07 16:51:50 +01:00
|
|
|
} else {
|
|
|
|
|
let holding = TokenHolding::try_from(&account.data)?;
|
|
|
|
|
match holding {
|
|
|
|
|
TokenHolding::Fungible { balance, .. } => {
|
2026-05-14 21:19:25 -04:00
|
|
|
println!("ATA {ata_id} (definition {def}): balance {balance}");
|
2026-03-07 16:51:50 +01:00
|
|
|
}
|
|
|
|
|
TokenHolding::NftMaster { .. }
|
|
|
|
|
| TokenHolding::NftPrintedCopy { .. } => {
|
2026-05-14 21:19:25 -04:00
|
|
|
println!("ATA {ata_id} (definition {def}): unsupported token type");
|
2026-03-07 16:51:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|