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

Commit a47d5b8

Browse files
committed
clean up transfer hook account validator and parser
1 parent f26efd2 commit a47d5b8

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

token/cli/src/clap_app.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -186,38 +186,37 @@ impl fmt::Display for AccountMetaRole {
186186
write!(f, "{:?}", self)
187187
}
188188
}
189-
pub fn parse_transfer_hook_account<T>(string: T) -> Result<AccountMeta, String>
190-
where
191-
T: AsRef<str> + fmt::Display,
192-
{
193-
match string.as_ref().split(':').collect::<Vec<_>>().as_slice() {
194-
[address, role] => {
195-
let address = Pubkey::from_str(address).map_err(|e| format!("{e}"))?;
196-
let meta = match AccountMetaRole::from_str(role).map_err(|e| format!("{e}"))? {
197-
AccountMetaRole::Readonly => AccountMeta::new_readonly(address, false),
198-
AccountMetaRole::Writable => AccountMeta::new(address, false),
199-
AccountMetaRole::ReadonlySigner => AccountMeta::new_readonly(address, true),
200-
AccountMetaRole::WritableSigner => AccountMeta::new(address, true),
201-
};
202-
Ok(meta)
189+
190+
#[derive(Clone, Copy)]
191+
pub(crate) struct TransferHookAccount {
192+
address: Pubkey,
193+
role: AccountMetaRole,
194+
}
195+
impl FromStr for TransferHookAccount {
196+
type Err = String;
197+
198+
fn from_str(s: &str) -> Result<Self, Self::Err> {
199+
match s.split(':').collect::<Vec<_>>().as_slice() {
200+
[address, role] => {
201+
let address = Pubkey::from_str(address).map_err(|e| format!("{e}"))?;
202+
let role = AccountMetaRole::from_str(role).map_err(|e| format!("{e}"))?;
203+
Ok(Self { address, role })
204+
}
205+
_ => Err("Transfer hook account must be present as <ADDRESS>:<ROLE>".to_string()),
203206
}
204-
_ => Err("Transfer hook account must be present as <ADDRESS>:<ROLE>".to_string()),
205207
}
206208
}
207-
fn validate_transfer_hook_account<T>(string: T) -> Result<(), String>
208-
where
209-
T: AsRef<str> + fmt::Display,
210-
{
211-
match string.as_ref().split(':').collect::<Vec<_>>().as_slice() {
212-
[address, role] => {
213-
is_valid_pubkey(address)?;
214-
AccountMetaRole::from_str(role)
215-
.map(|_| ())
216-
.map_err(|e| format!("{e}"))
209+
impl TransferHookAccount {
210+
pub(crate) fn create_account_meta(&self) -> AccountMeta {
211+
match self.role {
212+
AccountMetaRole::Readonly => AccountMeta::new_readonly(self.address, false),
213+
AccountMetaRole::Writable => AccountMeta::new(self.address, false),
214+
AccountMetaRole::ReadonlySigner => AccountMeta::new_readonly(self.address, true),
215+
AccountMetaRole::WritableSigner => AccountMeta::new(self.address, true),
217216
}
218-
_ => Err("Transfer hook account must be present as <ADDRESS>:<ROLE>".to_string()),
219217
}
220218
}
219+
221220
#[derive(Debug, Clone, PartialEq, EnumIter, EnumString, IntoStaticStr)]
222221
#[strum(serialize_all = "kebab-case")]
223222
pub enum CliAuthorityType {
@@ -1427,7 +1426,7 @@ pub fn app<'a>(
14271426
.arg(
14281427
Arg::with_name("transfer_hook_account")
14291428
.long("transfer-hook-account")
1430-
.validator(|s| validate_transfer_hook_account(s))
1429+
.value_parser(clap::value_parser!(TransferHookAccount))
14311430
.value_name("PUBKEY:ROLE")
14321431
.takes_value(true)
14331432
.multiple(true)

token/cli/src/command.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,11 +3923,13 @@ pub async fn process_command<'a>(
39233923
let use_unchecked_instruction = arg_matches.is_present("use_unchecked_instruction");
39243924
let expected_fee = arg_matches.get_one::<Amount>("expected_fee").copied();
39253925
let memo = value_t!(arg_matches, "memo", String).ok();
3926-
let transfer_hook_accounts = arg_matches.values_of("transfer_hook_account").map(|v| {
3927-
v.into_iter()
3928-
.map(|s| parse_transfer_hook_account(s).unwrap())
3929-
.collect::<Vec<_>>()
3930-
});
3926+
let transfer_hook_accounts = arg_matches
3927+
.get_many::<TransferHookAccount>("transfer_hook_account")
3928+
.map(|v| {
3929+
v.into_iter()
3930+
.map(|account| account.create_account_meta())
3931+
.collect::<Vec<_>>()
3932+
});
39313933

39323934
command_transfer(
39333935
config,

0 commit comments

Comments
 (0)