From 0615439849c09d0cf3c4a23aef136bcde1b7aca5 Mon Sep 17 00:00:00 2001 From: Sanjay Singh Date: Tue, 18 Jul 2023 17:10:48 +0530 Subject: [PATCH 1/5] re-formated --- token/cli/src/main.rs | 88 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index 29e583786ee..571996b5793 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -144,6 +144,7 @@ pub enum CommandName { EnableCpiGuard, DisableCpiGuard, UpdateDefaultAccountState, + UpdateMetadataPointerAddress, WithdrawWithheldTokens, SetTransferFee, WithdrawExcessLamports, @@ -2302,6 +2303,33 @@ async fn command_cpi_guard( }) } +async fn command_update_metadata_pointer_address( + config: &Config<'_>, + token_pubkey: Pubkey, + authority: Pubkey, + new_metadata_address: Option, + bulk_signers: BulkSigners, +) -> CommandResult { + if config.sign_only { + panic!("Config can not be sign-only for updating metadata pointer address."); + } + + let token = token_client_from_config(config, &token_pubkey, None)?; + let res = token + .update_metadata_address(&authority, Some(new_metadata_address), &bulk_signers) + .await?; + + let tx_return = finish_tx(config, &res, false).await?; + Ok(match tx_return { + TransactionReturnData::CliSignature(signature) => { + config.output_format.formatted_string(&signature) + } + TransactionReturnData::CliSignOnlyData(sign_only_data) => { + config.output_format.formatted_string(&sign_only_data) + } + }) +} + async fn command_update_default_account_state( config: &Config<'_>, token_pubkey: Pubkey, @@ -3624,6 +3652,44 @@ fn app<'a, 'b>( .nonce_args(true) .offline_args(), ) + .subcommand( + SubCommand::with_name(CommandName::UpdateMetadataPointerAddress.into()) + .about("Updates default account state for the mint. Requires the default account state extension.") + .arg( + Arg::with_name("token") + .validator(is_valid_pubkey) + .value_name("TOKEN_MINT_ADDRESS") + .takes_value(true) + .index(1) + .required(true) + .help("The address of the token mint to update default account state"), + ) + .arg( + Arg::with_name("metadata_address") + .long("metadata-address") + .value_name("ADDRESS") + .takes_value(true) + .help( + "Specify address that stores token metadata." + ), + ) + .arg( + Arg::with_name("authority") + .long("authority") + .value_name("KEYPAIR") + .validator(is_valid_signer) + .takes_value(true) + .help( + "Specify the token's authority. \ + This may be a keypair file or the ASK keyword. \ + Defaults to the client keypair.", + ), + ) + .arg(owner_address_arg()) + .arg(multisig_signer_arg()) + .nonce_args(true) + .offline_args(), + ) .subcommand( SubCommand::with_name(CommandName::WithdrawWithheldTokens.into()) .about("Withdraw withheld transfer fee tokens from mint and / or account(s)") @@ -4387,6 +4453,28 @@ async fn process_command<'a>( ) .await } + (CommandName::UpdateMetadataPointerAddress, arg_matches) => { + // Since account is required argument it will always be present + let token = pubkey_of_signer(arg_matches, "token", &mut wallet_manager) + .unwrap() + .unwrap(); + + let (authority_signer, authority) = + config.signer_or_default(arg_matches, "authority", &mut wallet_manager); + if config.multisigner_pubkeys.is_empty() { + push_signer_with_dedup(authority_signer, &mut bulk_signers); + } + let metadata_address = value_t!(arg_matches, "metadata_address", Pubkey).ok(); + + command_update_metadata_pointer_address( + config, + token, + authority, + metadata_address, + bulk_signers, + ) + .await + } (CommandName::WithdrawWithheldTokens, arg_matches) => { let (authority_signer, authority) = config.signer_or_default( arg_matches, From 3722b1d60e1ff764e91cc34cab9af4f5174f8121 Mon Sep 17 00:00:00 2001 From: Sanjay Singh Date: Wed, 19 Jul 2023 13:34:47 +0530 Subject: [PATCH 2/5] added testcase for updating metadata pointer address command --- token/cli/src/main.rs | 56 +++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index 571996b5793..e4968d990ce 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -144,7 +144,7 @@ pub enum CommandName { EnableCpiGuard, DisableCpiGuard, UpdateDefaultAccountState, - UpdateMetadataPointerAddress, + UpdateMetadataAddress, WithdrawWithheldTokens, SetTransferFee, WithdrawExcessLamports, @@ -2316,7 +2316,7 @@ async fn command_update_metadata_pointer_address( let token = token_client_from_config(config, &token_pubkey, None)?; let res = token - .update_metadata_address(&authority, Some(new_metadata_address), &bulk_signers) + .update_metadata_address(&authority, new_metadata_address, &bulk_signers) .await?; let tx_return = finish_tx(config, &res, false).await?; @@ -3653,8 +3653,8 @@ fn app<'a, 'b>( .offline_args(), ) .subcommand( - SubCommand::with_name(CommandName::UpdateMetadataPointerAddress.into()) - .about("Updates default account state for the mint. Requires the default account state extension.") + SubCommand::with_name(CommandName::UpdateMetadataAddress.into()) + .about("Updates metadata pointer address for the mint. Requires the metadata pointer extension.") .arg( Arg::with_name("token") .validator(is_valid_pubkey) @@ -3662,16 +3662,15 @@ fn app<'a, 'b>( .takes_value(true) .index(1) .required(true) - .help("The address of the token mint to update default account state"), + .help("The address of the token mint to update the metadata pointer address"), ) .arg( Arg::with_name("metadata_address") .long("metadata-address") - .value_name("ADDRESS") + .value_name("METADATA_ADDRESS") .takes_value(true) - .help( - "Specify address that stores token metadata." - ), + .required_unless("disable") + .help("Specify address that stores token's metadata-pointer"), ) .arg( Arg::with_name("authority") @@ -3680,15 +3679,13 @@ fn app<'a, 'b>( .validator(is_valid_signer) .takes_value(true) .help( - "Specify the token's authority. \ + "Specify the token's metadata-pointer authority. \ This may be a keypair file or the ASK keyword. \ Defaults to the client keypair.", ), ) - .arg(owner_address_arg()) .arg(multisig_signer_arg()) .nonce_args(true) - .offline_args(), ) .subcommand( SubCommand::with_name(CommandName::WithdrawWithheldTokens.into()) @@ -4453,7 +4450,7 @@ async fn process_command<'a>( ) .await } - (CommandName::UpdateMetadataPointerAddress, arg_matches) => { + (CommandName::UpdateMetadataAddress, arg_matches) => { // Since account is required argument it will always be present let token = pubkey_of_signer(arg_matches, "token", &mut wallet_manager) .unwrap() @@ -7551,6 +7548,8 @@ mod tests { let program_id = spl_token_2022::id(); let config = test_config_with_default_signer(&test_validator, &payer, &program_id); let metadata_address = Pubkey::new_unique(); + let token_keypair = Keypair::new(); + let token_pubkey = token_keypair.pubkey(); let result = process_test_command( &config, @@ -7577,5 +7576,36 @@ mod tests { extension.metadata_address, Some(metadata_address).try_into().unwrap() ); + + let new_metadata_address = Pubkey::new_unique(); + + let new_result = process_test_command( + &config, + &payer, + &[ + "spl-token", + CommandName::UpdateMetadataAddress.into(), + "--token", + &token_pubkey.to_string(), + "--metadata-address", + &new_metadata_address.to_string(), + "--authority", + &payer.pubkey().to_string(), + ], + ) + .await; + + let new_value: serde_json::Value = serde_json::from_str(&new_result.unwrap()).unwrap(); + let new_mint = Pubkey::from_str(new_value["commandOutput"]["address"].as_str().unwrap()).unwrap(); + let new_account = config.rpc_client.get_account(&new_mint).await.unwrap(); + let new_mint_state = StateWithExtensionsOwned::::unpack(new_account.data).unwrap(); + + let new_extension = new_mint_state.get_extension::().unwrap(); + + assert_eq!( + new_extension.metadata_address, + Some(new_metadata_address).try_into().unwrap() + ); + } } From edf575e18ca64f3ab51433a312fa3d6652cbc180 Mon Sep 17 00:00:00 2001 From: Sanjay Singh Date: Wed, 19 Jul 2023 14:40:10 +0530 Subject: [PATCH 3/5] updated and tested - testcase for updating metadata pointer address command --- token/cli/src/main.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index e4968d990ce..7f97c66448f 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -7548,8 +7548,6 @@ mod tests { let program_id = spl_token_2022::id(); let config = test_config_with_default_signer(&test_validator, &payer, &program_id); let metadata_address = Pubkey::new_unique(); - let token_keypair = Keypair::new(); - let token_pubkey = token_keypair.pubkey(); let result = process_test_command( &config, @@ -7579,25 +7577,20 @@ mod tests { let new_metadata_address = Pubkey::new_unique(); - let new_result = process_test_command( + let _new_result = process_test_command( &config, &payer, &[ "spl-token", CommandName::UpdateMetadataAddress.into(), - "--token", - &token_pubkey.to_string(), + &mint.to_string(), "--metadata-address", &new_metadata_address.to_string(), - "--authority", - &payer.pubkey().to_string(), ], ) .await; - - let new_value: serde_json::Value = serde_json::from_str(&new_result.unwrap()).unwrap(); - let new_mint = Pubkey::from_str(new_value["commandOutput"]["address"].as_str().unwrap()).unwrap(); - let new_account = config.rpc_client.get_account(&new_mint).await.unwrap(); + + let new_account = config.rpc_client.get_account(&mint).await.unwrap(); let new_mint_state = StateWithExtensionsOwned::::unpack(new_account.data).unwrap(); let new_extension = new_mint_state.get_extension::().unwrap(); @@ -7606,6 +7599,5 @@ mod tests { new_extension.metadata_address, Some(new_metadata_address).try_into().unwrap() ); - } } From 97e61b49c5434466b0fc8365f76b0d0790b5e40f Mon Sep 17 00:00:00 2001 From: Sanjay Singh Date: Fri, 21 Jul 2023 17:48:10 +0530 Subject: [PATCH 4/5] added disable support and testcase for the same --- token/cli/src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index 7f97c66448f..daeeab13068 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -3666,12 +3666,19 @@ fn app<'a, 'b>( ) .arg( Arg::with_name("metadata_address") - .long("metadata-address") + .index(2) .value_name("METADATA_ADDRESS") .takes_value(true) .required_unless("disable") .help("Specify address that stores token's metadata-pointer"), ) + .arg( + Arg::with_name("disable") + .long("disable") + .takes_value(false) + .conflicts_with("metadata_address") + .help("Unset metadata pointer address.") + ) .arg( Arg::with_name("authority") .long("authority") @@ -7584,7 +7591,6 @@ mod tests { "spl-token", CommandName::UpdateMetadataAddress.into(), &mint.to_string(), - "--metadata-address", &new_metadata_address.to_string(), ], ) @@ -7599,5 +7605,30 @@ mod tests { new_extension.metadata_address, Some(new_metadata_address).try_into().unwrap() ); + + let _result_with_disable = process_test_command( + &config, + &payer, + &[ + "spl-token", + CommandName::UpdateMetadataAddress.into(), + &mint.to_string(), + "--disable", + ], + ) + .await; + + let new_account_disbale = config.rpc_client.get_account(&mint).await.unwrap(); + let new_mint_state_disable = + StateWithExtensionsOwned::::unpack(new_account_disbale.data).unwrap(); + + let new_extension_disable = new_mint_state_disable + .get_extension::() + .unwrap(); + + assert_eq!( + new_extension_disable.metadata_address, + None.try_into().unwrap() + ); } } From 54d2fbe4f677d72be573c3b06354cf3bcd654fbd Mon Sep 17 00:00:00 2001 From: sanj singh <36148196+sanjsingh07@users.noreply.github.com> Date: Fri, 21 Jul 2023 19:21:52 +0530 Subject: [PATCH 5/5] added validation to update metadata address command --- token/cli/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index daeeab13068..58bf2d74019 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -3667,6 +3667,7 @@ fn app<'a, 'b>( .arg( Arg::with_name("metadata_address") .index(2) + .validator(is_valid_pubkey) .value_name("METADATA_ADDRESS") .takes_value(true) .required_unless("disable")