Skip to content

Commit e43ee78

Browse files
committed
more corrections to portal transfer
1 parent fb57721 commit e43ee78

File tree

3 files changed

+40
-89
lines changed

3 files changed

+40
-89
lines changed

programs/portal/src/instructions/transfer.rs

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,25 @@ impl<'info> TransferBurn<'info> {
161161
return err!(ErrorCode::ConstraintAddress);
162162
}
163163

164-
let session_authority_bump = get_session_authority(
165-
self.common.payer.key,
166-
args.amount,
167-
args.recipient_chain,
168-
args.recipient_address,
169-
args.should_queue,
170-
self.session_authority.key,
171-
// We already validated the session in transfer_extension_burn.
172-
// Also, the amount argument is different after converting to $M.
173-
!self.common.from.owner.eq(self.token_authority.key),
174-
)?;
164+
// Owner of the $M token account depends on whether this function
165+
// was called directly or by tranfer_extension_burn.
166+
let session_owner_seed = if self.common.from.owner.eq(self.token_authority.key) {
167+
self.common.payer.key()
168+
} else {
169+
self.common.from.owner.key()
170+
};
171+
172+
let (session_authority, session_authority_bump) = Pubkey::find_program_address(
173+
&[
174+
crate::SESSION_AUTHORITY_SEED,
175+
session_owner_seed.as_ref(),
176+
args.keccak256().as_ref(),
177+
],
178+
&crate::ID,
179+
);
180+
if !self.session_authority.key().eq(&session_authority) {
181+
return err!(ErrorCode::ConstraintAddress);
182+
}
175183

176184
Ok(session_authority_bump)
177185
}
@@ -180,24 +188,34 @@ impl<'info> TransferBurn<'info> {
180188
pub fn transfer_burn<'info>(
181189
ctx: Context<'_, '_, '_, 'info, TransferBurn<'info>>,
182190
args: TransferArgs,
191+
) -> Result<()> {
192+
let amount = args.amount;
193+
transfer_burn_common(ctx, args, amount)
194+
}
195+
196+
pub fn transfer_burn_common<'info>(
197+
ctx: Context<'_, '_, '_, 'info, TransferBurn<'info>>,
198+
args: TransferArgs,
199+
principal_amount_m: u64,
183200
) -> Result<()> {
184201
let session_authority_bump = ctx.accounts.validate_accounts(&args)?;
185202

186203
let accs = ctx.accounts;
187204

188205
let TransferArgs {
189-
amount: principal,
190206
recipient_chain,
191207
recipient_address,
192208
should_queue,
209+
..
193210
} = args;
194211

195212
// The provided "amount" is the principal amount of M to bridge.
196213
// We scale this up to M units using the scaled UI config multiplier.
197214
let scaled_ui_config = earn::utils::conversion::get_scaled_ui_config(&accs.common.mint)?;
215+
198216
// Get the amount of M tokens to transfer using the multiplier
199217
let mut m_amount = earn::utils::conversion::principal_to_amount_down(
200-
principal,
218+
principal_amount_m,
201219
scaled_ui_config.new_multiplier.into(),
202220
)?;
203221

@@ -233,7 +251,7 @@ pub fn transfer_burn<'info>(
233251
accs.common.custody.to_account_info(),
234252
accs.session_authority.to_account_info(),
235253
ctx.remaining_accounts,
236-
principal,
254+
principal_amount_m,
237255
accs.common.mint.decimals,
238256
&[&[
239257
crate::SESSION_AUTHORITY_SEED,
@@ -254,7 +272,7 @@ pub fn transfer_burn<'info>(
254272
},
255273
&[&[crate::TOKEN_AUTHORITY_SEED, &[ctx.bumps.token_authority]]],
256274
),
257-
principal,
275+
principal_amount_m,
258276
)?;
259277

260278
accs.common.custody.reload()?;
@@ -339,35 +357,3 @@ fn insert_into_outbox(
339357

340358
Ok(())
341359
}
342-
343-
pub fn get_session_authority(
344-
payer: &Pubkey,
345-
amount: u64,
346-
recipient_chain: ChainId,
347-
recipient_address: [u8; 32],
348-
should_queue: bool,
349-
expected: &Pubkey,
350-
validate: bool,
351-
) -> Result<u8> {
352-
let (session_authority, bump) = Pubkey::find_program_address(
353-
&[
354-
crate::SESSION_AUTHORITY_SEED,
355-
payer.as_ref(),
356-
TransferArgs {
357-
amount,
358-
recipient_chain,
359-
recipient_address,
360-
should_queue,
361-
}
362-
.keccak256()
363-
.as_ref(),
364-
],
365-
&crate::ID,
366-
);
367-
368-
if validate && !session_authority.eq(expected) {
369-
return err!(ErrorCode::ConstraintAddress);
370-
}
371-
372-
Ok(bump)
373-
}

programs/portal/src/instructions/transfer_extension.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use ext_swap::{accounts::SwapGlobal, program::ExtSwap};
55

66
use crate::{
77
TransferBurnBumps, __client_accounts_transfer_burn, __cpi_client_accounts_transfer_burn,
8-
instructions::{ext_swap, get_session_authority, transfer_burn, TransferArgs, TransferBurn},
9-
ntt_messages::ChainId,
8+
instructions::{ext_swap, transfer_burn_common, TransferArgs, TransferBurn},
109
};
1110

1211
#[derive(Accounts)]
@@ -76,23 +75,9 @@ pub struct TransferExtensionBurn<'info> {
7675

7776
pub fn transfer_extension_burn<'info>(
7877
ctx: Context<'_, '_, '_, 'info, TransferExtensionBurn<'info>>,
79-
ext_principal: u64,
80-
recipient_chain: ChainId,
81-
recipient_address: [u8; 32],
78+
args: TransferArgs,
8279
destination_token: [u8; 32],
83-
should_queue: bool,
8480
) -> Result<()> {
85-
// Derive and validate session authority
86-
get_session_authority(
87-
ctx.accounts.common.common.payer.key,
88-
ext_principal,
89-
recipient_chain,
90-
recipient_address,
91-
should_queue,
92-
ctx.accounts.common.session_authority.key,
93-
true,
94-
)?;
95-
9681
// $M token account should be owned by token authority
9782
if !ctx
9883
.accounts
@@ -131,7 +116,7 @@ pub fn transfer_extension_burn<'info>(
131116
},
132117
&[&[crate::TOKEN_AUTHORITY_SEED, &[token_auth_bump]]],
133118
),
134-
ext_principal,
119+
args.amount,
135120
)?;
136121

137122
// Amount of $M we got from unwrap
@@ -163,15 +148,7 @@ pub fn transfer_extension_burn<'info>(
163148
);
164149

165150
// TransferBurn $M from unwrap
166-
transfer_burn(
167-
sub_ctx,
168-
TransferArgs {
169-
amount: m_amount,
170-
recipient_chain: recipient_chain,
171-
recipient_address: recipient_address,
172-
should_queue: should_queue,
173-
},
174-
)?;
151+
transfer_burn_common(sub_ctx, args, m_amount)?;
175152

176153
// Overwrite default destination token
177154
ctx.accounts.common.common.outbox_item.destination_token = destination_token;

programs/portal/src/lib.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ pub mod queue;
1717
pub mod registered_transceiver;
1818
pub mod transceivers;
1919

20-
use crate::ntt_messages::ChainId;
21-
use transceivers::wormhole::instructions::*;
22-
2320
use instructions::*;
21+
use transceivers::wormhole::instructions::*;
2422

2523
#[cfg(not(feature = "no-entrypoint"))]
2624
solana_security_txt::security_txt! {
@@ -90,20 +88,10 @@ pub mod portal {
9088

9189
pub fn transfer_extension_burn<'info>(
9290
ctx: Context<'_, '_, '_, 'info, TransferExtensionBurn<'info>>,
93-
ext_principal: u64,
94-
recipient_chain: ChainId,
95-
recipient_address: [u8; 32],
91+
args: TransferArgs,
9692
destination_token: [u8; 32],
97-
should_queue: bool,
9893
) -> Result<()> {
99-
instructions::transfer_extension_burn(
100-
ctx,
101-
ext_principal,
102-
recipient_chain,
103-
recipient_address,
104-
destination_token,
105-
should_queue,
106-
)
94+
instructions::transfer_extension_burn(ctx, args, destination_token)
10795
}
10896

10997
pub fn redeem(ctx: Context<Redeem>, args: RedeemArgs) -> Result<()> {

0 commit comments

Comments
 (0)