Skip to content

Commit 1f3125d

Browse files
committed
refactor(svm): extract convert_and_transfer_in shared helper
Extracted amount conversion + plugin transfer_in into `convert_and_transfer_in` on `HyperlaneSealevelToken<T>`. Both the remote dispatch path and CC's same-chain local path now delegate to it. Also fixed transfer_remote_to to return remote_amount so callers can restore the original log messages that were lost in the prior commit.
1 parent 7d97862 commit 1f3125d

2 files changed

Lines changed: 41 additions & 28 deletions

File tree

  • rust/sealevel

rust/sealevel/libraries/hyperlane-sealevel-token/src/processor.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ where
315315
.ok_or(ProgramError::InvalidArgument)?;
316316

317317
// Delegate to the shared remote-dispatch helper.
318-
Self::transfer_remote_to(
318+
let remote_amount = Self::transfer_remote_to(
319319
program_id,
320320
&token,
321321
system_program_account,
@@ -328,10 +328,10 @@ where
328328
)?;
329329

330330
msg!(
331-
"Warp route transfer completed to destination: {}, recipient: {}, router: {}",
331+
"Warp route transfer completed to destination: {}, recipient: {}, remote_amount: {}",
332332
xfer.destination_domain,
333333
xfer.recipient,
334-
router
334+
remote_amount
335335
);
336336

337337
Ok(())
@@ -362,7 +362,7 @@ where
362362
recipient: H256,
363363
router: H256,
364364
amount_or_id: U256,
365-
) -> ProgramResult {
365+
) -> Result<U256, ProgramError> {
366366
// Mailbox program
367367
let mailbox_info = next_account_info(accounts_iter)?;
368368
if mailbox_info.key != &token.mailbox {
@@ -468,21 +468,12 @@ where
468468
None
469469
};
470470

471-
// The amount denominated in the local decimals.
472-
let local_amount: u64 = amount_or_id
473-
.try_into()
474-
.map_err(|_| Error::IntegerOverflow)?;
475-
// Convert to the remote number of decimals, which is universally understood
476-
// by the remote routers as the number of decimals used by the message amount.
477-
let remote_amount = token.local_amount_to_remote_amount(local_amount)?;
478-
479-
// Transfer `local_amount` of tokens in...
480-
T::transfer_in(
471+
let remote_amount = Self::convert_and_transfer_in(
481472
program_id,
482473
token,
483474
sender_wallet,
484475
accounts_iter,
485-
local_amount,
476+
amount_or_id,
486477
)?;
487478

488479
if accounts_iter.next().is_some() {
@@ -561,7 +552,36 @@ where
561552
invoke(&igp_ixn, &igp_payment_account_infos)?;
562553
}
563554

564-
Ok(())
555+
Ok(remote_amount)
556+
}
557+
558+
/// Converts amount from local to remote decimals and calls plugin
559+
/// `transfer_in`. Returns `remote_amount` for the caller to build
560+
/// `TokenMessage`.
561+
pub fn convert_and_transfer_in<'a, 'b>(
562+
program_id: &Pubkey,
563+
token: &HyperlaneToken<T>,
564+
sender_wallet: &'a AccountInfo<'b>,
565+
accounts_iter: &mut std::slice::Iter<'a, AccountInfo<'b>>,
566+
amount_or_id: U256,
567+
) -> Result<U256, ProgramError> {
568+
// The amount denominated in the local decimals.
569+
let local_amount: u64 = amount_or_id
570+
.try_into()
571+
.map_err(|_| Error::IntegerOverflow)?;
572+
// Convert to the remote number of decimals, which is universally understood
573+
// by the remote routers as the number of decimals used by the message amount.
574+
let remote_amount = token.local_amount_to_remote_amount(local_amount)?;
575+
576+
T::transfer_in(
577+
program_id,
578+
token,
579+
sender_wallet,
580+
accounts_iter,
581+
local_amount,
582+
)?;
583+
584+
Ok(remote_amount)
565585
}
566586

567587
/// Accounts:

rust/sealevel/programs/hyperlane-sealevel-token-cross-collateral/src/processor.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ fn transfer_remote_to_remote<'account_info_slice, 'account_info>(
527527
}
528528

529529
// Delegate to the shared remote-dispatch helper in token-lib.
530-
HyperlaneSealevelToken::<CollateralPlugin>::transfer_remote_to(
530+
let remote_amount = HyperlaneSealevelToken::<CollateralPlugin>::transfer_remote_to(
531531
program_id,
532532
hyperlane_token,
533533
system_program_account,
@@ -540,9 +540,10 @@ fn transfer_remote_to_remote<'account_info_slice, 'account_info>(
540540
)?;
541541

542542
msg!(
543-
"CC transfer_remote_to completed to destination: {}, target_router: {:?}",
543+
"CC transfer_remote_to completed to destination: {}, target_router: {:?}, remote_amount: {}",
544544
xfer.destination_domain,
545545
xfer.target_router,
546+
remote_amount
546547
);
547548

548549
Ok(())
@@ -593,20 +594,12 @@ fn transfer_remote_to_local(
593594
return Err(ProgramError::InvalidAccountData);
594595
}
595596

596-
// Convert amount
597-
let local_amount: u64 = xfer
598-
.amount_or_id
599-
.try_into()
600-
.map_err(|_| ProgramError::InvalidArgument)?;
601-
let remote_amount = hyperlane_token.local_amount_to_remote_amount(local_amount)?;
602-
603-
// Transfer tokens into escrow via plugin
604-
CollateralPlugin::transfer_in(
597+
let remote_amount = HyperlaneSealevelToken::<CollateralPlugin>::convert_and_transfer_in(
605598
program_id,
606599
hyperlane_token,
607600
sender_wallet,
608601
accounts_iter,
609-
local_amount,
602+
xfer.amount_or_id,
610603
)?;
611604

612605
// Build HandleLocal instruction data

0 commit comments

Comments
 (0)