Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Add immutable get_extension, and clean up mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Feb 4, 2022
1 parent c2afeaf commit 91340df
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions token/program-2022/src/extension/memo_transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl Extension for MemoTransfer {
}

/// Determine if a memo is required for transfers into this account
pub fn memo_required(mut account_state: StateWithExtensionsMut<Account>) -> bool {
if let Ok(extension) = account_state.get_extension_mut::<MemoTransfer>() {
pub fn memo_required(account_state: &StateWithExtensionsMut<Account>) -> bool {
if let Ok(extension) = account_state.get_extension::<MemoTransfer>() {
return extension.require_incoming_transfer_memos.into();
}
false
Expand Down
21 changes: 20 additions & 1 deletion token/program-2022/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,30 @@ impl<'data, S: BaseState> StateWithExtensionsMut<'data, S> {
}
}

/// Unpack a portion of the TLV data as the desired type
/// Unpack a portion of the TLV data as the desired type that allows modifying the type
pub fn get_extension_mut<V: Extension>(&mut self) -> Result<&mut V, ProgramError> {
self.init_or_get_extension(false)
}

/// Unpack a portion of the TLV data as the desired type
pub fn get_extension<V: Extension>(&self) -> Result<&V, ProgramError> {
if V::TYPE.get_account_type() != S::ACCOUNT_TYPE {
return Err(ProgramError::InvalidAccountData);
}
let TlvIndices {
type_start,
length_start,
value_start,
} = get_extension_indices::<V>(self.tlv_data, false)?;

if self.tlv_data[type_start..].len() < V::TYPE.get_tlv_len() {
return Err(ProgramError::InvalidAccountData);
}
let length = pod_from_bytes::<Length>(&self.tlv_data[length_start..value_start])?;
let value_end = value_start.saturating_add(usize::from(*length));
pod_from_bytes::<V>(&self.tlv_data[value_start..value_end])
}

/// Packs base state data into the base data portion
pub fn pack_base(&mut self) {
S::pack_into_slice(&self.base, self.base_data);
Expand Down
8 changes: 3 additions & 5 deletions token/program-2022/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
confidential_transfer::{self, ConfidentialTransferAccount},
default_account_state::{self, DefaultAccountState},
immutable_owner::ImmutableOwner,
memo_transfer::{self, MemoTransfer},
memo_transfer::{self, memo_required},
mint_close_authority::MintCloseAuthority,
reallocate,
transfer_fee::{self, TransferFeeAmount, TransferFeeConfig},
Expand Down Expand Up @@ -373,10 +373,8 @@ impl Processor {
return Err(TokenError::MintMismatch.into());
}

if let Ok(extension) = dest_account.get_extension_mut::<MemoTransfer>() {
if extension.require_incoming_transfer_memos.into() {
// TODO: use get_processed_instructions syscall to check for memo
}
if memo_required(&dest_account) {
// TODO: use get_processed_instructions syscall to check for memo
}

source_account.base.amount = source_account
Expand Down

0 comments on commit 91340df

Please sign in to comment.