0

I am trying to build a clap cli tool using the latest utils from solana_clap_v3_utils 1.18.2 and there are currently no good examples in the solana-program-library that use v3 in the builder context that don't also use deprecated functions.

I am specifically talking about the functions in solana_clap_v3_utils::input_validators like is_valid_pubkey. The suggested fix is:

use of deprecated function `solana_clap_v3_utils::input_validators::is_valid_pubkey`: 
please use `SignerSourceParserBuilder::default().allow_pubkey().allow_file_path().build()`

I am trying to use the parser instead like:

.arg(Arg::with_name("token")
.value_parser(SignerSourceParserBuilder::default().allow_pubkey().allow_file_path().build())
.value_name("TOKEN_MINT_ADDRESS")
.takes_value(true)
.index(1)
.required(true)
.help("Token to airdrop"),)

I assume this change was done to better match with regular clap functions so I could get the value with:

let token = arg_matches.get_one::<Pubkey>("token");

However I get this error.

Mismatch between definition and access of `token`. Could not downcast to solana_program::pubkey::Pubkey, need to downcast to solana_clap_v3_utils::keypair::SignerSource

Am I using this in the correct way? Or should I just stick to the validators?

2 Answers 2

2

To add to the answer above, there is currently two ways to parse pubkeys from user input.

The first way is to specify the argument with parser

Arg::new("token")
  .long("token")
  .takes_value(true)
  .value_parser(clap::value_parser!(Pubkey))

and parsing

let token = arg_matches.get_one::<Pubkey>("token");

See (https://github.com/solana-labs/solana/blob/master/clap-v3-utils/src/input_parsers/mod.rs#L351). This method is simple, but supports only base58-encoded pubkey strings.

The second way is to interpret a pubkey as a signer source (https://github.com/solana-labs/solana/blob/master/clap-v3-utils/src/input_parsers/signer.rs#L46), parse the signer source, and then extracting the pubkey from that source as specified in the answer above.

let source = arg_matches.get_one::<SignerSource>("token"); // parse `SignerSource` instead of `Pubkey`
let token_pubkey = solana_clap_v3_utils::keypair::pubkey_from_source(
    &arg_matches,
    &source,
    "token",
    &mut wallet_manager,
).unwrap(); // extract pubkey from source

The pubkey_from_source interface unfortunately does seem a little redundant as it requires arg_matches as input, but is needed given that pubkeys are parsed from multiple user arguments.

Since input validators are deprecated starting with clap-v3, you should use one of the two methods above. Generally, if you are extracting a pubkey from various sources (https://github.com/solana-labs/solana/blob/master/clap-v3-utils/src/input_parsers/signer.rs#L41), use the second approach. If you are simply parsing pubkeys directly from strings, then use the first approach, which is simpler.

1
  • pubkey_from_source is not in the crate version = 1.18.3, and SignerSource is private
    – tonton
    Commented Feb 28 at 3:43
1

One of the biggest changes between clap v2 and v3 is the deprecation of input validators. Clap v2 does redundant work by validating an input on start, and then validating it a second time when parsing it. To go with that, the Solana clap v3 utils deprecate all input validators.

You almost have the example right -- looking at the tests at https://github.com/solana-labs/solana/blob/8143fc3f4ae1fe89a7c362639c39ba7b68d92d5c/clap-v3-utils/src/input_parsers/signer.rs#L801 and the helper at https://github.com/solana-labs/solana/blob/8143fc3f4ae1fe89a7c362639c39ba7b68d92d5c/clap-v3-utils/src/keypair.rs#L750, you actually need to do:

let source = arg_matches.get_one::<SignerSource>("token");
let token_pubkey = solana_clap_v3_utils::keypair::pubkey_from_source(
    &arg_matches,
    &source,
    "token",
    &mut wallet_manager,
).unwrap();
1
  • I am not seeing pub_from_source in the crate and SignerSource is private.
    – tonton
    Commented Feb 28 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.