Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

token-2022: add token 2022 argument to token cli for easy use #7006

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions token/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ After that, you can run the tests as any other Rust project:
```sh
cargo test
```

To run it locally you can do it like this:

```sh
cargo build --manifest-path token/cli/Cargo.toml
target/debug/spl-token <command>
```
10 changes: 10 additions & 0 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,15 @@ pub fn app<'a, 'b>(
.possible_values(&["json", "json-compact"])
.help("Return information in specified output format"),
)
.arg(
Arg::with_name("program_2022")
.short("2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is ready to go in except for this. We really don't want to use a number for a short arg like this, since it manifests as <command> -2 and that could easily be mistaken for a numerical negative two.

Can we just drop the .short here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 🫡
done

.long("program-2022")
.takes_value(false)
.global(true)
.conflicts_with("program_id")
.help("Use token extension program token 2022 with program id: TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"),
)
.arg(
Arg::with_name("program_id")
.short("p")
Expand All @@ -599,6 +608,7 @@ pub fn app<'a, 'b>(
.takes_value(true)
.global(true)
.validator(is_valid_token_program_id)
.conflicts_with("program_2022")
.help("SPL Token program id"),
)
.arg(
Expand Down
41 changes: 21 additions & 20 deletions token/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,28 +260,29 @@ impl<'a> Config<'a> {
let dump_transaction_message = matches.is_present(DUMP_TRANSACTION_MESSAGE.name);

let default_program_id = spl_token::id();
let (program_id, restrict_to_program_id) =
if let Some(program_id) = value_of(matches, "program_id") {
(program_id, true)
} else if !sign_only {
if let Some(address) = value_of(matches, "token")
.or_else(|| value_of(matches, "account"))
.or_else(|| value_of(matches, "address"))
{
(
rpc_client
.get_account(&address)
.await
.map(|account| account.owner)
.unwrap_or(default_program_id),
false,
)
} else {
(default_program_id, false)
}
let (program_id, restrict_to_program_id) = if matches.is_present("program_2022") {
(spl_token_2022::id(), true)
} else if let Some(program_id) = value_of(matches, "program_id") {
(program_id, true)
} else if !sign_only {
if let Some(address) = value_of(matches, "token")
.or_else(|| value_of(matches, "account"))
.or_else(|| value_of(matches, "address"))
{
(
rpc_client
.get_account(&address)
.await
.map(|account| account.owner)
.unwrap_or(default_program_id),
false,
)
} else {
(default_program_id, false)
};
}
} else {
(default_program_id, false)
};

// need to specify a compute limit if compute price and blockhash are specified
if matches.is_present(BLOCKHASH_ARG.name)
Expand Down
38 changes: 38 additions & 0 deletions token/cli/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async fn main() {
// maybe come up with a way to do this through a some macro tag on the function?
let tests = vec![
async_trial!(create_token_default, test_validator, payer),
async_trial!(create_token_2022, test_validator, payer),
async_trial!(create_token_interest_bearing, test_validator, payer),
async_trial!(set_interest_rate, test_validator, payer),
async_trial!(supply, test_validator, payer),
Expand Down Expand Up @@ -495,6 +496,43 @@ async fn create_token_default(test_validator: &TestValidator, payer: &Keypair) {
}
}

async fn create_token_2022(test_validator: &TestValidator, payer: &Keypair) {
let config = test_config_with_default_signer(test_validator, payer, &spl_token_2022::id());
let mut wallet_manager = None;
let mut bulk_signers: Vec<Arc<dyn Signer>> = Vec::new();
let mut multisigner_ids = Vec::new();

let args = &[
"spl-token",
CommandName::CreateToken.into(),
"--program-2022",
];

let default_decimals = format!("{}", spl_token_2022::native_mint::DECIMALS);
let minimum_signers_help = minimum_signers_help_string();
let multisig_member_help = multisig_member_help_string();

let app_matches = app(
&default_decimals,
&minimum_signers_help,
&multisig_member_help,
)
.get_matches_from(args);

let config = Config::new_with_clients_and_ws_url(
&app_matches,
&mut wallet_manager,
&mut bulk_signers,
&mut multisigner_ids,
config.rpc_client.clone(),
config.program_client.clone(),
config.websocket_url.clone(),
)
.await;

assert_eq!(config.program_id, spl_token_2022::ID);
}

async fn create_token_interest_bearing(test_validator: &TestValidator, payer: &Keypair) {
let config = test_config_with_default_signer(test_validator, payer, &spl_token_2022::id());
let rate_bps: i16 = 100;
Expand Down
Loading