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

Commit 1f988aa

Browse files
authored
token-2022: add token 2022 argument to token cli for easy use (#7006)
* token-2022: add token 2022 argument to token cli for easy use Instead of coping the token program id of the token22 program one can now just write: create-token -token22 * Added test and short version * Add conflicts_with and shortened command * Update README.md * Formatting * Drop the short value for program_2022
1 parent baef391 commit 1f988aa

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

token/cli/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ After that, you can run the tests as any other Rust project:
3333
```sh
3434
cargo test
3535
```
36+
37+
To run it locally you can do it like this:
38+
39+
```sh
40+
cargo build --manifest-path token/cli/Cargo.toml
41+
target/debug/spl-token <command>
42+
```

token/cli/src/clap_app.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,14 @@ pub fn app<'a, 'b>(
591591
.possible_values(&["json", "json-compact"])
592592
.help("Return information in specified output format"),
593593
)
594+
.arg(
595+
Arg::with_name("program_2022")
596+
.long("program-2022")
597+
.takes_value(false)
598+
.global(true)
599+
.conflicts_with("program_id")
600+
.help("Use token extension program token 2022 with program id: TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"),
601+
)
594602
.arg(
595603
Arg::with_name("program_id")
596604
.short("p")
@@ -599,6 +607,7 @@ pub fn app<'a, 'b>(
599607
.takes_value(true)
600608
.global(true)
601609
.validator(is_valid_token_program_id)
610+
.conflicts_with("program_2022")
602611
.help("SPL Token program id"),
603612
)
604613
.arg(

token/cli/src/config.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -260,28 +260,29 @@ impl<'a> Config<'a> {
260260
let dump_transaction_message = matches.is_present(DUMP_TRANSACTION_MESSAGE.name);
261261

262262
let default_program_id = spl_token::id();
263-
let (program_id, restrict_to_program_id) =
264-
if let Some(program_id) = value_of(matches, "program_id") {
265-
(program_id, true)
266-
} else if !sign_only {
267-
if let Some(address) = value_of(matches, "token")
268-
.or_else(|| value_of(matches, "account"))
269-
.or_else(|| value_of(matches, "address"))
270-
{
271-
(
272-
rpc_client
273-
.get_account(&address)
274-
.await
275-
.map(|account| account.owner)
276-
.unwrap_or(default_program_id),
277-
false,
278-
)
279-
} else {
280-
(default_program_id, false)
281-
}
263+
let (program_id, restrict_to_program_id) = if matches.is_present("program_2022") {
264+
(spl_token_2022::id(), true)
265+
} else if let Some(program_id) = value_of(matches, "program_id") {
266+
(program_id, true)
267+
} else if !sign_only {
268+
if let Some(address) = value_of(matches, "token")
269+
.or_else(|| value_of(matches, "account"))
270+
.or_else(|| value_of(matches, "address"))
271+
{
272+
(
273+
rpc_client
274+
.get_account(&address)
275+
.await
276+
.map(|account| account.owner)
277+
.unwrap_or(default_program_id),
278+
false,
279+
)
282280
} else {
283281
(default_program_id, false)
284-
};
282+
}
283+
} else {
284+
(default_program_id, false)
285+
};
285286

286287
// need to specify a compute limit if compute price and blockhash are specified
287288
if matches.is_present(BLOCKHASH_ARG.name)

token/cli/tests/command.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ async fn main() {
9292
// maybe come up with a way to do this through a some macro tag on the function?
9393
let tests = vec![
9494
async_trial!(create_token_default, test_validator, payer),
95+
async_trial!(create_token_2022, test_validator, payer),
9596
async_trial!(create_token_interest_bearing, test_validator, payer),
9697
async_trial!(set_interest_rate, test_validator, payer),
9798
async_trial!(supply, test_validator, payer),
@@ -496,6 +497,43 @@ async fn create_token_default(test_validator: &TestValidator, payer: &Keypair) {
496497
}
497498
}
498499

500+
async fn create_token_2022(test_validator: &TestValidator, payer: &Keypair) {
501+
let config = test_config_with_default_signer(test_validator, payer, &spl_token_2022::id());
502+
let mut wallet_manager = None;
503+
let mut bulk_signers: Vec<Arc<dyn Signer>> = Vec::new();
504+
let mut multisigner_ids = Vec::new();
505+
506+
let args = &[
507+
"spl-token",
508+
CommandName::CreateToken.into(),
509+
"--program-2022",
510+
];
511+
512+
let default_decimals = format!("{}", spl_token_2022::native_mint::DECIMALS);
513+
let minimum_signers_help = minimum_signers_help_string();
514+
let multisig_member_help = multisig_member_help_string();
515+
516+
let app_matches = app(
517+
&default_decimals,
518+
&minimum_signers_help,
519+
&multisig_member_help,
520+
)
521+
.get_matches_from(args);
522+
523+
let config = Config::new_with_clients_and_ws_url(
524+
&app_matches,
525+
&mut wallet_manager,
526+
&mut bulk_signers,
527+
&mut multisigner_ids,
528+
config.rpc_client.clone(),
529+
config.program_client.clone(),
530+
config.websocket_url.clone(),
531+
)
532+
.await;
533+
534+
assert_eq!(config.program_id, spl_token_2022::ID);
535+
}
536+
499537
async fn create_token_interest_bearing(test_validator: &TestValidator, payer: &Keypair) {
500538
let config = test_config_with_default_signer(test_validator, payer, &spl_token_2022::id());
501539
let rate_bps: i16 = 100;

0 commit comments

Comments
 (0)