Skip to content

Commit

Permalink
Extract common code into a separate function to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
10gic committed Sep 4, 2024
1 parent 023ffef commit 1e5bbfc
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions rust/wallet_core_rs/src/ffi/solana/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,7 @@ pub unsafe extern "C" fn tw_solana_address_default_token_address(
address: *const TWString,
token_mint_address: *const TWString,
) -> *mut TWString {
let main_address = try_or_else!(TWString::from_ptr_as_ref(address), std::ptr::null_mut);
let main_address = try_or_else!(main_address.as_str(), std::ptr::null_mut);

let token_mint_address = try_or_else!(
TWString::from_ptr_as_ref(token_mint_address),
std::ptr::null_mut
);
let token_mint_address = try_or_else!(token_mint_address.as_str(), std::ptr::null_mut);

let main_address = try_or_else!(SolanaAddress::from_str(main_address), std::ptr::null_mut);
let token_mint_address = try_or_else!(
SolanaAddress::from_str(token_mint_address),
std::ptr::null_mut
);

let token_address = try_or_else!(
StakeProgram::get_associated_token_address(
main_address,
*TOKEN_PROGRAM_ID_ADDRESS,
token_mint_address
),
std::ptr::null_mut
);

TWString::from(token_address.to_string()).into_ptr()
tw_solana_address_token_address_impl(address, token_mint_address, *TOKEN_PROGRAM_ID_ADDRESS)
}

/// Derive token 2022 address for token
Expand All @@ -58,6 +34,18 @@ pub unsafe extern "C" fn tw_solana_address_default_token_address(
pub unsafe extern "C" fn tw_solana_address_token_2022_address(
address: *const TWString,
token_mint_address: *const TWString,
) -> *mut TWString {
tw_solana_address_token_address_impl(
address,
token_mint_address,
*TOKEN_2022_PROGRAM_ID_ADDRESS,
)
}

unsafe fn tw_solana_address_token_address_impl(
address: *const TWString,
token_mint_address: *const TWString,
token_address: SolanaAddress,
) -> *mut TWString {
let main_address = try_or_else!(TWString::from_ptr_as_ref(address), std::ptr::null_mut);
let main_address = try_or_else!(main_address.as_str(), std::ptr::null_mut);
Expand All @@ -73,14 +61,10 @@ pub unsafe extern "C" fn tw_solana_address_token_2022_address(
std::ptr::null_mut
);

let token_address = try_or_else!(
StakeProgram::get_associated_token_address(
main_address,
*TOKEN_2022_PROGRAM_ID_ADDRESS,
token_mint_address
),
let associated_token_address = try_or_else!(
StakeProgram::get_associated_token_address(main_address, token_address, token_mint_address),
std::ptr::null_mut
);

TWString::from(token_address.to_string()).into_ptr()
TWString::from(associated_token_address.to_string()).into_ptr()
}

0 comments on commit 1e5bbfc

Please sign in to comment.