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

Commit 1210f98

Browse files
committed
clean up transfer hook account validator and parser
1 parent 8d27503 commit 1210f98

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
@@ -188,38 +188,37 @@ impl fmt::Display for AccountMetaRole {
188188
write!(f, "{:?}", self)
189189
}
190190
}
191-
pub fn parse_transfer_hook_account<T>(string: T) -> Result<AccountMeta, String>
192-
where
193-
T: AsRef<str> + fmt::Display,
194-
{
195-
match string.as_ref().split(':').collect::<Vec<_>>().as_slice() {
196-
[address, role] => {
197-
let address = Pubkey::from_str(address).map_err(|e| format!("{e}"))?;
198-
let meta = match AccountMetaRole::from_str(role).map_err(|e| format!("{e}"))? {
199-
AccountMetaRole::Readonly => AccountMeta::new_readonly(address, false),
200-
AccountMetaRole::Writable => AccountMeta::new(address, false),
201-
AccountMetaRole::ReadonlySigner => AccountMeta::new_readonly(address, true),
202-
AccountMetaRole::WritableSigner => AccountMeta::new(address, true),
203-
};
204-
Ok(meta)
191+
192+
#[derive(Clone, Copy)]
193+
pub(crate) struct TransferHookAccount {
194+
address: Pubkey,
195+
role: AccountMetaRole,
196+
}
197+
impl FromStr for TransferHookAccount {
198+
type Err = String;
199+
200+
fn from_str(s: &str) -> Result<Self, Self::Err> {
201+
match s.split(':').collect::<Vec<_>>().as_slice() {
202+
[address, role] => {
203+
let address = Pubkey::from_str(address).map_err(|e| format!("{e}"))?;
204+
let role = AccountMetaRole::from_str(role).map_err(|e| format!("{e}"))?;
205+
Ok(Self { address, role })
206+
}
207+
_ => Err("Transfer hook account must be present as <ADDRESS>:<ROLE>".to_string()),
205208
}
206-
_ => Err("Transfer hook account must be present as <ADDRESS>:<ROLE>".to_string()),
207209
}
208210
}
209-
fn validate_transfer_hook_account<T>(string: T) -> Result<(), String>
210-
where
211-
T: AsRef<str> + fmt::Display,
212-
{
213-
match string.as_ref().split(':').collect::<Vec<_>>().as_slice() {
214-
[address, role] => {
215-
is_valid_pubkey(address)?;
216-
AccountMetaRole::from_str(role)
217-
.map(|_| ())
218-
.map_err(|e| format!("{e}"))
211+
impl TransferHookAccount {
212+
pub(crate) fn create_account_meta(&self) -> AccountMeta {
213+
match self.role {
214+
AccountMetaRole::Readonly => AccountMeta::new_readonly(self.address, false),
215+
AccountMetaRole::Writable => AccountMeta::new(self.address, false),
216+
AccountMetaRole::ReadonlySigner => AccountMeta::new_readonly(self.address, true),
217+
AccountMetaRole::WritableSigner => AccountMeta::new(self.address, true),
219218
}
220-
_ => Err("Transfer hook account must be present as <ADDRESS>:<ROLE>".to_string()),
221219
}
222220
}
221+
223222
#[derive(Debug, Clone, PartialEq, EnumIter, EnumString, IntoStaticStr)]
224223
#[strum(serialize_all = "kebab-case")]
225224
pub enum CliAuthorityType {
@@ -1429,7 +1428,7 @@ pub fn app<'a>(
14291428
.arg(
14301429
Arg::with_name("transfer_hook_account")
14311430
.long("transfer-hook-account")
1432-
.validator(|s| validate_transfer_hook_account(s))
1431+
.value_parser(clap::value_parser!(TransferHookAccount))
14331432
.value_name("PUBKEY:ROLE")
14341433
.takes_value(true)
14351434
.multiple(true)

token/cli/src/command.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,11 +3937,13 @@ pub async fn process_command<'a>(
39373937
let use_unchecked_instruction = arg_matches.is_present("use_unchecked_instruction");
39383938
let expected_fee = arg_matches.get_one::<Amount>("expected_fee").copied();
39393939
let memo = value_t!(arg_matches, "memo", String).ok();
3940-
let transfer_hook_accounts = arg_matches.values_of("transfer_hook_account").map(|v| {
3941-
v.into_iter()
3942-
.map(|s| parse_transfer_hook_account(s).unwrap())
3943-
.collect::<Vec<_>>()
3944-
});
3940+
let transfer_hook_accounts = arg_matches
3941+
.get_many::<TransferHookAccount>("transfer_hook_account")
3942+
.map(|v| {
3943+
v.into_iter()
3944+
.map(|account| account.create_account_meta())
3945+
.collect::<Vec<_>>()
3946+
});
39453947

39463948
command_transfer(
39473949
config,

0 commit comments

Comments
 (0)