-
Notifications
You must be signed in to change notification settings - Fork 4
Add wallet utxos command #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| use anyhow::bail; | ||
| use clap::Parser; | ||
| use comfy_table::Table; | ||
| use serde_json::json; | ||
| use utxorpc::spec::query::{any_utxo_data::ParsedState, AnyUtxoData}; | ||
|
|
||
| use crate::output::{OutputFormat, OutputFormatter}; | ||
|
|
||
| #[derive(Parser)] | ||
| pub struct Args { | ||
| /// Name of the wallet to show the UTxOs of. If undefined, the default wallet is used. | ||
| name: Option<String>, | ||
|
|
||
| /// Name of the provider to use. If undefined, the default provider is used. | ||
| provider: Option<String>, | ||
| } | ||
|
|
||
| pub async fn run(args: Args, ctx: &crate::Context) -> anyhow::Result<()> { | ||
| let wallet = match args.name { | ||
| Some(name) => ctx.store.find_wallet(&name), | ||
| None => ctx.store.default_wallet(), | ||
| }; | ||
|
|
||
| let provider = match args.provider { | ||
| Some(name) => ctx.store.find_provider(&name), | ||
| None => ctx.store.default_provider(), | ||
| }; | ||
|
|
||
| match (wallet, provider) { | ||
| (Some(wallet), Some(provider)) => { | ||
| let address = wallet.address(provider.is_testnet()); | ||
| let utxos = provider.get_wallet_utxos(&address).await?; | ||
| let output = WalletUtxoOutput::new(utxos); | ||
|
|
||
| let format = if ctx.output_format_overridden { | ||
| ctx.output_format.clone() | ||
| } else { | ||
| OutputFormat::Json | ||
| }; | ||
|
|
||
| output.output(&format); | ||
|
|
||
| Ok(()) | ||
| } | ||
| (None, Some(_)) => bail!("Wallet not found."), | ||
| (Some(_), None) => bail!("Provider not found."), | ||
| (None, None) => bail!("Wallet and provider not found."), | ||
| } | ||
| } | ||
|
|
||
| struct WalletUtxoOutput { | ||
| utxos: Vec<AnyUtxoData>, | ||
| } | ||
|
|
||
| impl WalletUtxoOutput { | ||
| fn new(utxos: Vec<AnyUtxoData>) -> Self { | ||
| Self { utxos } | ||
| } | ||
| } | ||
|
|
||
| impl OutputFormatter for WalletUtxoOutput { | ||
| fn to_table(&self) { | ||
| let mut table = Table::new(); | ||
|
|
||
| table.set_header(vec!["Tx Hash", "Index", "Lovelace", "Assets", "Datum Hash"]); | ||
|
|
||
| for utxo in &self.utxos { | ||
| let (tx_hash, index) = utxo | ||
| .txo_ref | ||
| .as_ref() | ||
| .map(|reference| (hex::encode(&reference.hash), reference.index.to_string())) | ||
| .unwrap_or_else(|| ("-".to_string(), "-".to_string())); | ||
|
|
||
| let (coin, asset_count, datum_hash) = match &utxo.parsed_state { | ||
| Some(ParsedState::Cardano(output)) => { | ||
| let asset_count: usize = output | ||
| .assets | ||
| .iter() | ||
| .map(|multiasset| multiasset.assets.len()) | ||
| .sum(); | ||
|
|
||
| let datum_hash = output | ||
| .datum | ||
| .as_ref() | ||
| .map(|datum| hex::encode(&datum.hash)) | ||
| .unwrap_or_else(|| "-".to_string()); | ||
|
|
||
| (output.coin.to_string(), asset_count.to_string(), datum_hash) | ||
| } | ||
| None => ("-".to_string(), "0".to_string(), "-".to_string()), | ||
| }; | ||
|
|
||
| table.add_row(vec![tx_hash, index, coin, asset_count, datum_hash]); | ||
| } | ||
|
|
||
| println!("{table}"); | ||
| } | ||
|
|
||
| fn to_json(&self) { | ||
| let payload = json!({ "utxos": self.utxos }); | ||
| println!("{}", serde_json::to_string_pretty(&payload).unwrap()); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn parses_without_arguments() { | ||
| let args = Args::parse_from(["wallet-utxos"]); | ||
|
|
||
| assert!(args.name.is_none()); | ||
| assert!(args.provider.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parses_with_wallet_and_provider() { | ||
| let args = Args::parse_from(["wallet-utxos", "alice", "mainnet"]); | ||
|
|
||
| assert_eq!(args.name.as_deref(), Some("alice")); | ||
| assert_eq!(args.provider.as_deref(), Some("mainnet")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unbounded UTxO query could cause performance issues.
Using
u32::MAXas the limit allows fetching an unbounded number of UTxOs. Wallets with many UTxOs (e.g., exchange wallets, large DeFi contracts) could trigger slow queries, high memory usage, or timeouts.Consider either: